diff --git a/ibm/service/vpc/resource_ibm_is_bare_metal_server.go b/ibm/service/vpc/resource_ibm_is_bare_metal_server.go index f6b6607f33..282d30811c 100644 --- a/ibm/service/vpc/resource_ibm_is_bare_metal_server.go +++ b/ibm/service/vpc/resource_ibm_is_bare_metal_server.go @@ -303,7 +303,7 @@ func ResourceIBMIsBareMetalServer() *schema.Resource { Optional: true, Computed: true, ValidateFunc: validate.InvokeValidator("ibm_is_bare_metal_server", isBareMetalServerNicInterfaceType), - Description: "The network interface type: [ pci, hipersocket ]", + Description: "The network interface type: [ pci, hipersocket, vlan ]", }, isBareMetalServerNicPrimaryIP: { Type: schema.TypeList, @@ -1168,7 +1168,7 @@ func ResourceIBMIsBareMetalServer() *schema.Resource { func ResourceIBMIsBareMetalServerValidator() *validate.ResourceValidator { bareMetalServerActions := "start, restart, stop" tpmModes := "disabled, tpm_2" - interface_types := "pci, hipersocket" + interface_types := "pci, vlan, hipersocket" validateSchema := make([]validate.ValidateSchema, 1) validateSchema = append(validateSchema, @@ -1407,26 +1407,194 @@ func resourceIBMISBareMetalServerCreate(context context.Context, d *schema.Resou } if nicsintf, ok := d.GetOk(isBareMetalServerNetworkInterfaces); ok { - nics := nicsintf.(*schema.Set).List() inlinenicobj := make([]vpcv1.BareMetalServerNetworkInterfacePrototypeIntf, 0) for _, resource := range nics { nic := resource.(map[string]interface{}) interfaceType := "" + if vlan, ok := nic[isBareMetalServerNicVlan]; ok && vlan.(int) != 0 { + interfaceType = "vlan" + var nicobj = &vpcv1.BareMetalServerNetworkInterfacePrototypeBareMetalServerNetworkInterfaceByVlanPrototype{} + nicobj.InterfaceType = &interfaceType + + if aitf, ok := nic[isBareMetalServerNicAllowInterfaceToFloat]; ok { + allowInterfaceToFloat := aitf.(bool) + nicobj.AllowInterfaceToFloat = &allowInterfaceToFloat + } + if vlan, ok := nic[isBareMetalServerNicVlan]; ok { + vlanInt := int64(vlan.(int)) + nicobj.Vlan = &vlanInt + } + + subnetintf, _ := nic[isBareMetalServerNicSubnet] + subnetintfstr := subnetintf.(string) + nicobj.Subnet = &vpcv1.SubnetIdentity{ + ID: &subnetintfstr, + } + name, _ := nic[isBareMetalServerNicName] + namestr := name.(string) + if namestr != "" { + nicobj.Name = &namestr + } + + enableInfraNAT, ok := nic[isBareMetalServerNicEnableInfraNAT] + enableInfraNATbool := enableInfraNAT.(bool) + if ok { + nicobj.EnableInfrastructureNat = &enableInfraNATbool + } + + if primaryIpIntf, ok := nic[isBareMetalServerNicPrimaryIP]; ok && len(primaryIpIntf.([]interface{})) > 0 { + primaryIp := primaryIpIntf.([]interface{})[0].(map[string]interface{}) + reservedIpIdOk, ok := primaryIp[isBareMetalServerNicIpID] + if ok && reservedIpIdOk.(string) != "" { + ipid := reservedIpIdOk.(string) + nicobj.PrimaryIP = &vpcv1.NetworkInterfaceIPPrototypeReservedIPIdentity{ + ID: &ipid, + } + } else { + primaryip := &vpcv1.NetworkInterfaceIPPrototypeReservedIPPrototypeNetworkInterfaceContext{} + + reservedIpAddressOk, okAdd := primaryIp[isBareMetalServerNicIpAddress] + if okAdd && reservedIpAddressOk.(string) != "" { + reservedIpAddress := reservedIpAddressOk.(string) + primaryip.Address = &reservedIpAddress + } + + reservedIpNameOk, okName := primaryIp[isBareMetalServerNicIpName] + if okName && reservedIpNameOk.(string) != "" { + reservedIpName := reservedIpNameOk.(string) + primaryip.Name = &reservedIpName + } + + reservedIpAutoOk, okAuto := primaryIp[isBareMetalServerNicIpAutoDelete] + if okAuto { + reservedIpAuto := reservedIpAutoOk.(bool) + primaryip.AutoDelete = &reservedIpAuto + } + if okAdd || okName || okAuto { + nicobj.PrimaryIP = primaryip + } + } + } + + allowIPSpoofing, ok := nic[isBareMetalServerNicAllowIPSpoofing] + allowIPSpoofingbool := allowIPSpoofing.(bool) + if ok && allowIPSpoofingbool { + nicobj.AllowIPSpoofing = &allowIPSpoofingbool + } + secgrpintf, ok := nic[isBareMetalServerNicSecurityGroups] + if ok { + secgrpSet := secgrpintf.(*schema.Set) + if secgrpSet.Len() != 0 { + var secgrpobjs = make([]vpcv1.SecurityGroupIdentityIntf, secgrpSet.Len()) + for i, secgrpIntf := range secgrpSet.List() { + secgrpIntfstr := secgrpIntf.(string) + secgrpobjs[i] = &vpcv1.SecurityGroupIdentity{ + ID: &secgrpIntfstr, + } + } + nicobj.SecurityGroups = secgrpobjs + } + } + inlinenicobj = append(inlinenicobj, nicobj) + } else if interfaceTypeOk, ok := nic[isBareMetalServerNicInterfaceType]; ok && interfaceTypeOk.(string) == "hipersocket" { + interfaceType = interfaceTypeOk.(string) + var nicobj = &vpcv1.BareMetalServerNetworkInterfacePrototypeBareMetalServerNetworkInterfaceByHiperSocketPrototype{} + nicobj.InterfaceType = &interfaceType + + subnetintf, _ := nic[isBareMetalServerNicSubnet] + subnetintfstr := subnetintf.(string) + nicobj.Subnet = &vpcv1.SubnetIdentity{ + ID: &subnetintfstr, + } + name, _ := nic[isBareMetalServerNicName] + namestr := name.(string) + if namestr != "" { + nicobj.Name = &namestr + } + + enableInfraNAT, ok := nic[isBareMetalServerNicEnableInfraNAT] + enableInfraNATbool := enableInfraNAT.(bool) + if ok { + nicobj.EnableInfrastructureNat = &enableInfraNATbool + } + + if primaryIpIntf, ok := nic[isBareMetalServerNicPrimaryIP]; ok && len(primaryIpIntf.([]interface{})) > 0 { + // primaryIp := primaryIpIntf.([]interface{})[0].(map[string]interface{}) + // reservedIpAddressOk, ok := primaryIp[isBareMetalServerNicIpAddress] + // if ok && reservedIpAddressOk.(string) != "" { + // reservedIpAddress := reservedIpAddressOk.(string) + // nicobj.PrimaryIpv4Address = &reservedIpAddress + // } + + primaryIp := primaryIpIntf.([]interface{})[0].(map[string]interface{}) + reservedIpIdOk, ok := primaryIp[isBareMetalServerNicIpID] + if ok && reservedIpIdOk.(string) != "" { + ipid := reservedIpIdOk.(string) + nicobj.PrimaryIP = &vpcv1.NetworkInterfaceIPPrototypeReservedIPIdentity{ + ID: &ipid, + } + } else { + primaryip := &vpcv1.NetworkInterfaceIPPrototypeReservedIPPrototypeNetworkInterfaceContext{} + + reservedIpAddressOk, okAdd := primaryIp[isBareMetalServerNicIpAddress] + if okAdd && reservedIpAddressOk.(string) != "" { + reservedIpAddress := reservedIpAddressOk.(string) + primaryip.Address = &reservedIpAddress + } + + reservedIpNameOk, okName := primaryIp[isBareMetalServerNicIpName] + if okName && reservedIpNameOk.(string) != "" { + reservedIpName := reservedIpNameOk.(string) + primaryip.Name = &reservedIpName + } + + reservedIpAutoOk, okAuto := primaryIp[isBareMetalServerNicIpAutoDelete] + if okAuto { + reservedIpAuto := reservedIpAutoOk.(bool) + primaryip.AutoDelete = &reservedIpAuto + } + if okAdd || okName || okAuto { + nicobj.PrimaryIP = primaryip + } + } + } - if allowedVlansOk, ok := nic[isBareMetalServerNicAllowedVlans]; ok { + allowIPSpoofing, ok := nic[isBareMetalServerNicAllowIPSpoofing] + allowIPSpoofingbool := allowIPSpoofing.(bool) + if ok && allowIPSpoofingbool { + nicobj.AllowIPSpoofing = &allowIPSpoofingbool + } + secgrpintf, ok := nic[isBareMetalServerNicSecurityGroups] + if ok { + secgrpSet := secgrpintf.(*schema.Set) + if secgrpSet.Len() != 0 { + var secgrpobjs = make([]vpcv1.SecurityGroupIdentityIntf, secgrpSet.Len()) + for i, secgrpIntf := range secgrpSet.List() { + secgrpIntfstr := secgrpIntf.(string) + secgrpobjs[i] = &vpcv1.SecurityGroupIdentity{ + ID: &secgrpIntfstr, + } + } + nicobj.SecurityGroups = secgrpobjs + } + } + inlinenicobj = append(inlinenicobj, nicobj) + } else { + allowedVlansOk, _ := nic[isBareMetalServerNicAllowedVlans] interfaceType = "pci" var nicobj = &vpcv1.BareMetalServerNetworkInterfacePrototypeBareMetalServerNetworkInterfaceByPciPrototype{} nicobj.InterfaceType = &interfaceType - allowedVlansList := allowedVlansOk.(*schema.Set).List() - - if len(allowedVlansList) > 0 { - allowedVlans := make([]int64, 0, len(allowedVlansList)) - for _, k := range allowedVlansList { - allowedVlans = append(allowedVlans, int64(k.(int))) + if allowedVlansOk != nil || vlan.(int) == 0 { + if allowedVlansOk != nil { + allowedVlansList := allowedVlansOk.(*schema.Set).List() + allowedVlans := make([]int64, 0, len(allowedVlansList)) + for _, k := range allowedVlansList { + allowedVlans = append(allowedVlans, int64(k.(int))) + } + nicobj.AllowedVlans = allowedVlans } - nicobj.AllowedVlans = allowedVlans subnetintf, _ := nic[isBareMetalServerNicSubnet] subnetintfstr := subnetintf.(string) @@ -1667,174 +1835,6 @@ func resourceIBMISBareMetalServerCreate(context context.Context, d *schema.Resou } inlinenicobj = append(inlinenicobj, nicobj) } - } else if interfaceTypeOk, ok := nic[isBareMetalServerNicInterfaceType]; ok && interfaceTypeOk.(string) == "hipersocket" { - interfaceType = interfaceTypeOk.(string) - var nicobj = &vpcv1.BareMetalServerNetworkInterfacePrototypeBareMetalServerNetworkInterfaceByHiperSocketPrototype{} - nicobj.InterfaceType = &interfaceType - - subnetintf, _ := nic[isBareMetalServerNicSubnet] - subnetintfstr := subnetintf.(string) - nicobj.Subnet = &vpcv1.SubnetIdentity{ - ID: &subnetintfstr, - } - name, _ := nic[isBareMetalServerNicName] - namestr := name.(string) - if namestr != "" { - nicobj.Name = &namestr - } - - enableInfraNAT, ok := nic[isBareMetalServerNicEnableInfraNAT] - enableInfraNATbool := enableInfraNAT.(bool) - if ok { - nicobj.EnableInfrastructureNat = &enableInfraNATbool - } - - if primaryIpIntf, ok := nic[isBareMetalServerNicPrimaryIP]; ok && len(primaryIpIntf.([]interface{})) > 0 { - // primaryIp := primaryIpIntf.([]interface{})[0].(map[string]interface{}) - // reservedIpAddressOk, ok := primaryIp[isBareMetalServerNicIpAddress] - // if ok && reservedIpAddressOk.(string) != "" { - // reservedIpAddress := reservedIpAddressOk.(string) - // nicobj.PrimaryIpv4Address = &reservedIpAddress - // } - - primaryIp := primaryIpIntf.([]interface{})[0].(map[string]interface{}) - reservedIpIdOk, ok := primaryIp[isBareMetalServerNicIpID] - if ok && reservedIpIdOk.(string) != "" { - ipid := reservedIpIdOk.(string) - nicobj.PrimaryIP = &vpcv1.NetworkInterfaceIPPrototypeReservedIPIdentity{ - ID: &ipid, - } - } else { - primaryip := &vpcv1.NetworkInterfaceIPPrototypeReservedIPPrototypeNetworkInterfaceContext{} - - reservedIpAddressOk, okAdd := primaryIp[isBareMetalServerNicIpAddress] - if okAdd && reservedIpAddressOk.(string) != "" { - reservedIpAddress := reservedIpAddressOk.(string) - primaryip.Address = &reservedIpAddress - } - - reservedIpNameOk, okName := primaryIp[isBareMetalServerNicIpName] - if okName && reservedIpNameOk.(string) != "" { - reservedIpName := reservedIpNameOk.(string) - primaryip.Name = &reservedIpName - } - - reservedIpAutoOk, okAuto := primaryIp[isBareMetalServerNicIpAutoDelete] - if okAuto { - reservedIpAuto := reservedIpAutoOk.(bool) - primaryip.AutoDelete = &reservedIpAuto - } - if okAdd || okName || okAuto { - nicobj.PrimaryIP = primaryip - } - } - } - - allowIPSpoofing, ok := nic[isBareMetalServerNicAllowIPSpoofing] - allowIPSpoofingbool := allowIPSpoofing.(bool) - if ok && allowIPSpoofingbool { - nicobj.AllowIPSpoofing = &allowIPSpoofingbool - } - secgrpintf, ok := nic[isBareMetalServerNicSecurityGroups] - if ok { - secgrpSet := secgrpintf.(*schema.Set) - if secgrpSet.Len() != 0 { - var secgrpobjs = make([]vpcv1.SecurityGroupIdentityIntf, secgrpSet.Len()) - for i, secgrpIntf := range secgrpSet.List() { - secgrpIntfstr := secgrpIntf.(string) - secgrpobjs[i] = &vpcv1.SecurityGroupIdentity{ - ID: &secgrpIntfstr, - } - } - nicobj.SecurityGroups = secgrpobjs - } - } - inlinenicobj = append(inlinenicobj, nicobj) - } else { - interfaceType = "vlan" - var nicobj = &vpcv1.BareMetalServerNetworkInterfacePrototypeBareMetalServerNetworkInterfaceByVlanPrototype{} - nicobj.InterfaceType = &interfaceType - - if aitf, ok := nic[isBareMetalServerNicAllowInterfaceToFloat]; ok { - allowInterfaceToFloat := aitf.(bool) - nicobj.AllowInterfaceToFloat = &allowInterfaceToFloat - } - if vlan, ok := nic[isBareMetalServerNicVlan]; ok { - vlanInt := int64(vlan.(int)) - nicobj.Vlan = &vlanInt - } - - subnetintf, _ := nic[isBareMetalServerNicSubnet] - subnetintfstr := subnetintf.(string) - nicobj.Subnet = &vpcv1.SubnetIdentity{ - ID: &subnetintfstr, - } - name, _ := nic[isBareMetalServerNicName] - namestr := name.(string) - if namestr != "" { - nicobj.Name = &namestr - } - - enableInfraNAT, ok := nic[isBareMetalServerNicEnableInfraNAT] - enableInfraNATbool := enableInfraNAT.(bool) - if ok { - nicobj.EnableInfrastructureNat = &enableInfraNATbool - } - - if primaryIpIntf, ok := nic[isBareMetalServerNicPrimaryIP]; ok && len(primaryIpIntf.([]interface{})) > 0 { - primaryIp := primaryIpIntf.([]interface{})[0].(map[string]interface{}) - reservedIpIdOk, ok := primaryIp[isBareMetalServerNicIpID] - if ok && reservedIpIdOk.(string) != "" { - ipid := reservedIpIdOk.(string) - nicobj.PrimaryIP = &vpcv1.NetworkInterfaceIPPrototypeReservedIPIdentity{ - ID: &ipid, - } - } else { - primaryip := &vpcv1.NetworkInterfaceIPPrototypeReservedIPPrototypeNetworkInterfaceContext{} - - reservedIpAddressOk, okAdd := primaryIp[isBareMetalServerNicIpAddress] - if okAdd && reservedIpAddressOk.(string) != "" { - reservedIpAddress := reservedIpAddressOk.(string) - primaryip.Address = &reservedIpAddress - } - - reservedIpNameOk, okName := primaryIp[isBareMetalServerNicIpName] - if okName && reservedIpNameOk.(string) != "" { - reservedIpName := reservedIpNameOk.(string) - primaryip.Name = &reservedIpName - } - - reservedIpAutoOk, okAuto := primaryIp[isBareMetalServerNicIpAutoDelete] - if okAuto { - reservedIpAuto := reservedIpAutoOk.(bool) - primaryip.AutoDelete = &reservedIpAuto - } - if okAdd || okName || okAuto { - nicobj.PrimaryIP = primaryip - } - } - } - - allowIPSpoofing, ok := nic[isBareMetalServerNicAllowIPSpoofing] - allowIPSpoofingbool := allowIPSpoofing.(bool) - if ok && allowIPSpoofingbool { - nicobj.AllowIPSpoofing = &allowIPSpoofingbool - } - secgrpintf, ok := nic[isBareMetalServerNicSecurityGroups] - if ok { - secgrpSet := secgrpintf.(*schema.Set) - if secgrpSet.Len() != 0 { - var secgrpobjs = make([]vpcv1.SecurityGroupIdentityIntf, secgrpSet.Len()) - for i, secgrpIntf := range secgrpSet.List() { - secgrpIntfstr := secgrpIntf.(string) - secgrpobjs[i] = &vpcv1.SecurityGroupIdentity{ - ID: &secgrpIntfstr, - } - } - nicobj.SecurityGroups = secgrpobjs - } - } - inlinenicobj = append(inlinenicobj, nicobj) } } options.NetworkInterfaces = inlinenicobj @@ -3899,10 +3899,10 @@ func isBareMetalServerRestart(bmsC *vpcv1.VpcV1, id string, d *schema.ResourceDa func resourceIBMBMSNicSet(v interface{}) int { var buf bytes.Buffer a := v.(map[string]interface{}) - buf.WriteString(fmt.Sprintf("%s-", a["subnet"].(string))) - // buf.WriteString(fmt.Sprintf("%s-", a["name"].(string))) - buf.WriteString(fmt.Sprintf("%d-", a["vlan"].(int))) - buf.WriteString(fmt.Sprintf("%v-", a["allowed_vlans"].(*schema.Set))) + // buf.WriteString(fmt.Sprintf("%s-", a["subnet"].(string))) + buf.WriteString(fmt.Sprintf("%s-", a["name"].(string))) + // buf.WriteString(fmt.Sprintf("%d-", a["vlan"].(int))) + // buf.WriteString(fmt.Sprintf("%v-", a["allowed_vlans"].(*schema.Set))) return conns.String(buf.String()) } diff --git a/ibm/service/vpc/resource_ibm_is_bare_metal_server_test.go b/ibm/service/vpc/resource_ibm_is_bare_metal_server_test.go index 545ff26719..70d8b2b381 100644 --- a/ibm/service/vpc/resource_ibm_is_bare_metal_server_test.go +++ b/ibm/service/vpc/resource_ibm_is_bare_metal_server_test.go @@ -338,7 +338,37 @@ ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCKVmnMOlHKcZK8tpt3MP1lqOLAcqcJzhsvJcjscgVE resource.TestCheckResourceAttr( "ibm_is_bare_metal_server.testacc_bms", "zone", acc.ISZoneName), resource.TestCheckResourceAttr( - "ibm_is_bare_metal_server.testacc_bms", "network_interfaces.0.vlan", "102"), + "ibm_is_bare_metal_server.testacc_bms", "network_interfaces.0.vlan", "3"), + ), + }, + }, + }) +} +func TestAccIBMISBareMetalServer_mix_multi_nic(t *testing.T) { + var server string + vpcname := fmt.Sprintf("tf-vpc-%d", acctest.RandIntRange(10, 100)) + name := fmt.Sprintf("tf-server-%d", acctest.RandIntRange(10, 100)) + subnetname := fmt.Sprintf("tfip-subnet-%d", acctest.RandIntRange(10, 100)) + publicKey := strings.TrimSpace(` +ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCKVmnMOlHKcZK8tpt3MP1lqOLAcqcJzhsvJcjscgVERRN7/9484SOBJ3HSKxxNG5JN8owAjy5f9yYwcUg+JaUVuytn5Pv3aeYROHGGg+5G346xaq3DAwX6Y5ykr2fvjObgncQBnuU5KHWCECO/4h8uWuwh/kfniXPVjFToc+gnkqA+3RKpAecZhFXwfalQ9mMuYGFxn+fwn8cYEApsJbsEmb0iJwPiZ5hjFC8wREuiTlhPHDgkBLOiycd20op2nXzDbHfCHInquEe/gYxEitALONxm0swBOwJZwlTDOB7C6y2dzlrtxr1L59m7pCkWI4EtTRLvleehBoj3u7jB4usR +`) + sshname := fmt.Sprintf("tf-sshname-%d", acctest.RandIntRange(10, 100)) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { acc.TestAccPreCheck(t) }, + Providers: acc.TestAccProviders, + CheckDestroy: testAccCheckIBMISBareMetalServerDestroy, + Steps: []resource.TestStep{ + { + Config: testAccCheckIBMISBareMetalServerMixMultiNicConfig(vpcname, subnetname, sshname, publicKey, name), + Check: resource.ComposeTestCheckFunc( + testAccCheckIBMISBareMetalServerExists("ibm_is_bare_metal_server.testacc_bms", server), + resource.TestCheckResourceAttr( + "ibm_is_bare_metal_server.testacc_bms", "name", name), + resource.TestCheckResourceAttr( + "ibm_is_bare_metal_server.testacc_bms", "zone", acc.ISZoneName), + resource.TestCheckResourceAttr( + "ibm_is_bare_metal_server.testacc_bms", "network_interfaces.#", "3"), ), }, }, @@ -771,6 +801,54 @@ func testAccCheckIBMISBareMetalServerMultiNicConfig(vpcname, subnetname, sshname } `, vpcname, subnetname, acc.ISZoneName, sshname, publicKey, acc.IsBareMetalServerProfileName, name, acc.IsBareMetalServerImage, acc.ISZoneName) } +func testAccCheckIBMISBareMetalServerMixMultiNicConfig(vpcname, subnetname, sshname, publicKey, name string) string { + return fmt.Sprintf(` + resource "ibm_is_vpc" "testacc_vpc" { + name = "%s" + } + + resource "ibm_is_subnet" "testacc_subnet" { + name = "%s" + vpc = ibm_is_vpc.testacc_vpc.id + zone = "%s" + total_ipv4_address_count = 16 + } + + resource "ibm_is_ssh_key" "testacc_sshkey" { + name = "%s" + public_key = "%s" + } + + resource "ibm_is_bare_metal_server" "testacc_bms" { + profile = "%s" + name = "%s" + image = "%s" + zone = "%s" + keys = [ibm_is_ssh_key.testacc_sshkey.id] + primary_network_interface { + name = "primary-nic" + subnet = ibm_is_subnet.testacc_subnet.id + allowed_vlans = [102,103] + } + network_interfaces { + name = "sec-nic-vlan112" + subnet = ibm_is_subnet.testacc_subnet.id + vlan = 102 + } + network_interfaces { + name = "sec-nic-pci" + subnet = ibm_is_subnet.testacc_subnet.id + } + network_interfaces { + name = "sec-nic-pci108-09" + subnet = ibm_is_subnet.testacc_subnet.id + allowed_vlans = [108,109] + + } + vpc = ibm_is_vpc.testacc_vpc.id + } +`, vpcname, subnetname, acc.ISZoneName, sshname, publicKey, acc.IsBareMetalServerProfileName, name, acc.IsBareMetalServerImage, acc.ISZoneName) +} func testAccCheckIBMISBareMetalServerMultiNicWithAllowFloatConfig(vpcname, subnetname, sshname, publicKey, name string) string { return fmt.Sprintf(` resource "ibm_is_vpc" "testacc_vpc" {