Skip to content

Commit

Permalink
resource/alicloud_instance: Added the field network_interfaces.vswitc…
Browse files Browse the repository at this point in the history
…h_id, network_interfaces.network_interface_traffic_mode, network_interfaces.security_group_ids, enable_jumbo_frame
  • Loading branch information
MrWolong committed May 21, 2024
1 parent bef37a9 commit 0b9310b
Show file tree
Hide file tree
Showing 3 changed files with 232 additions and 113 deletions.
84 changes: 80 additions & 4 deletions alicloud/resource_alicloud_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,25 @@ func resourceAliCloudInstance() *schema.Resource {
ForceNew: true,
Computed: true,
},
"vswitch_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
},
"network_interface_traffic_mode": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
},
"security_group_ids": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
Expand Down Expand Up @@ -610,6 +629,11 @@ func resourceAliCloudInstance() *schema.Resource {
Optional: true,
ForceNew: true,
},
"enable_jumbo_frame": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -884,16 +908,42 @@ func resourceAliCloudInstanceCreate(d *schema.ResourceData, meta interface{}) er

if v, ok := d.GetOk("network_interfaces"); ok {
networkInterfacesMaps := make([]map[string]interface{}, 0)
interfaces := v.([]interface{})
for _, rew := range interfaces {
for _, networkInterfaces := range v.([]interface{}) {
networkInterfacesMap := make(map[string]interface{})
item := rew.(map[string]interface{})
networkInterfacesMap["NetworkInterfaceId"] = item["network_interface_id"].(string)
networkInterfacesArg := networkInterfaces.(map[string]interface{})

if networkInterfaceId, ok := networkInterfacesArg["network_interface_id"]; ok && fmt.Sprint(networkInterfaceId) != "" {
networkInterfacesMap["NetworkInterfaceId"] = networkInterfaceId
}

if vSwitchId, ok := networkInterfacesArg["vswitch_id"]; ok {
networkInterfacesMap["VSwitchId"] = vSwitchId
}

if networkInterfaceTrafficMode, ok := networkInterfacesArg["network_interface_traffic_mode"]; ok {
networkInterfacesMap["NetworkInterfaceTrafficMode"] = networkInterfaceTrafficMode
}

if securityGroupIds, ok := networkInterfacesArg["security_group_ids"]; ok {
networkInterfacesMap["SecurityGroupIds"] = securityGroupIds
}

networkInterfacesMaps = append(networkInterfacesMaps, networkInterfacesMap)
}

request["NetworkInterface"] = networkInterfacesMaps
}

networkOptionsMap := make(map[string]interface{})

if v, ok := d.GetOkExists("enable_jumbo_frame"); ok {
networkOptionsMap["EnableJumboFrame"] = v
}

if len(networkOptionsMap) > 0 {
request["NetworkOptions"] = networkOptionsMap
}

if v, ok := d.GetOk("hpc_cluster_id"); ok {
request["HpcClusterId"] = v
}
Expand Down Expand Up @@ -1151,6 +1201,15 @@ func resourceAliCloudInstanceRead(d *schema.ResourceData, meta interface{}) erro
} else {
networkInterfaceMap := make(map[string]interface{})
networkInterfaceMap["network_interface_id"] = obj.NetworkInterfaceId

object, err := ecsService.DescribeEcsNetworkInterface(obj.NetworkInterfaceId)
if err != nil {
return WrapError(err)
}

networkInterfaceMap["vswitch_id"] = object["VSwitchId"]
networkInterfaceMap["network_interface_traffic_mode"] = object["NetworkInterfaceTrafficMode"]
networkInterfaceMap["security_group_ids"] = object["SecurityGroupIds"].(map[string]interface{})["SecurityGroupId"]
networkInterfaceMaps = append(networkInterfaceMaps, networkInterfaceMap)
}
}
Expand Down Expand Up @@ -1204,6 +1263,13 @@ func resourceAliCloudInstanceRead(d *schema.ResourceData, meta interface{}) erro

d.Set("maintenance_notify", maintenanceAttribute["NotifyOnMaintenance"])

instanceAttribute, err := ecsService.DescribeInstanceAttribute(d.Id())
if err != nil {
return WrapError(err)
}

d.Set("enable_jumbo_frame", instanceAttribute.EnableJumboFrame)

return nil
}

Expand Down Expand Up @@ -2091,6 +2157,16 @@ func modifyInstanceAttribute(d *schema.ResourceData, meta interface{}) (bool, er
update = true
}

if d.HasChange("enable_jumbo_frame") {
d.SetPartial("enable_jumbo_frame")

if v, ok := d.GetOkExists("enable_jumbo_frame"); ok {
request["EnableJumboFrame"] = v
}

update = true
}

if update {
wait := incrementalWait(1*time.Minute, 1*time.Minute)
err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
Expand Down
53 changes: 43 additions & 10 deletions alicloud/resource_alicloud_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1354,6 +1354,7 @@ func TestAccAliCloudECSInstancePrepaidAll(t *testing.T) {
},
})
}

func TestAccAliCloudECSInstanceDataDisks(t *testing.T) {
var v ecs.Instance

Expand Down Expand Up @@ -2399,20 +2400,17 @@ func TestAccAliCloudECSInstanceIpv6AddressesCount(t *testing.T) {

func TestAccAliCloudECSInstanceIpv6Addresses(t *testing.T) {
var v ecs.Instance

resourceId := "alicloud_instance.default"
ra := resourceAttrInit(resourceId, testAccInstanceCheckMap)
serviceFunc := func() interface{} {
return &EcsService{testAccProvider.Meta().(*connectivity.AliyunClient)}
}
rc := resourceCheckInit(resourceId, &v, serviceFunc)
rac := resourceAttrCheckInit(rc, ra)

rand := acctest.RandIntRange(1000, 9999)
testAccCheck := rac.resourceAttrMapUpdateSet()
name := fmt.Sprintf("tf-testAcc%sEcsInstance%d", defaultRegionToTest, rand)
testAccConfig := resourceTestAccConfigFunc(resourceId, name, resourceInstanceIpv6Dependence)

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
Expand All @@ -2435,11 +2433,19 @@ func TestAccAliCloudECSInstanceIpv6Addresses(t *testing.T) {
"user_data": "I_am_user_data",
"vswitch_id": "${alicloud_vswitch.vswitch.id}",
"ipv6_addresses": []string{"${cidrhost(alicloud_vswitch.vswitch.ipv6_cidr_block, 64)}"},
"network_interfaces": []map[string]interface{}{
{
"vswitch_id": "${alicloud_vswitch.networkInterface.id}",
"network_interface_traffic_mode": "Standard",
"security_group_ids": []string{"${alicloud_security_group.networkInterface.id}"},
},
},
}),
Check: resource.ComposeTestCheckFunc(
testAccCheck(map[string]string{
"instance_name": name,
"ipv6_addresses.#": "1",
"network_interfaces.#": "1",
"system_disk_category": "cloud_efficiency",
}),
),
Expand Down Expand Up @@ -2480,18 +2486,26 @@ data "alicloud_instance_types" "default" {
resource "alicloud_vpc" "vpc" {
vpc_name = var.name
cidr_block = "172.16.0.0/16"
cidr_block = "172.16.0.0/12"
enable_ipv6 = "true"
}
resource "alicloud_vswitch" "vswitch" {
vswitch_name = var.name
cidr_block = "172.16.0.0/24"
cidr_block = "172.16.0.0/21"
zone_id = data.alicloud_zones.default.zones.0.id
vpc_id = alicloud_vpc.vpc.id
ipv6_cidr_block_mask = "22"
}
resource "alicloud_vswitch" "networkInterface" {
vswitch_name = var.name
cidr_block = "172.18.0.0/24"
zone_id = data.alicloud_zones.default.zones.0.id
vpc_id = alicloud_vpc.vpc.id
ipv6_cidr_block_mask = "25"
}
data "alicloud_images" "default" {
name_regex = "^ubuntu_[0-9]+_[0-9]+_x64*"
owners = "system"
Expand All @@ -2502,6 +2516,11 @@ resource "alicloud_security_group" "default" {
vpc_id = alicloud_vpc.vpc.id
}
resource "alicloud_security_group" "networkInterface" {
name = "${var.name}"
vpc_id = alicloud_vpc.vpc.id
}
variable "name" {
default = "%s"
}
Expand Down Expand Up @@ -3106,7 +3125,7 @@ func TestAccAliCloudECSInstance_OperatorType(t *testing.T) {
{
Config: testAccConfig(map[string]interface{}{
"operator_type": "downgrade",
"instance_type": "${data.alicloud_instance_types.default.instance_types.1.id}",
"instance_type": "${data.alicloud_instance_types.default.instance_types.0.id}",
},
),
Check: resource.ComposeTestCheckFunc(
Expand Down Expand Up @@ -3348,6 +3367,7 @@ func TestAccAliCloudECSInstanceSystemDisk(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckWithRegions(t, true, connectivity.TestSalveRegions)
},
IDRefreshName: resourceId,
Providers: testAccProviders,
Expand Down Expand Up @@ -3432,6 +3452,7 @@ func TestAccAliCloudECSInstanceMaintenance(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckWithRegions(t, true, connectivity.TestSalveRegions)
},
IDRefreshName: resourceId,
Providers: testAccProviders,
Expand Down Expand Up @@ -3461,6 +3482,7 @@ func TestAccAliCloudECSInstanceMaintenance(t *testing.T) {
},
"maintenance_action": "Stop",
"maintenance_notify": "true",
"enable_jumbo_frame": "false",
}),
Check: resource.ComposeTestCheckFunc(
testAccCheck(map[string]string{
Expand All @@ -3470,6 +3492,7 @@ func TestAccAliCloudECSInstanceMaintenance(t *testing.T) {
"maintenance_time.#": "1",
"maintenance_action": "Stop",
"maintenance_notify": "true",
"enable_jumbo_frame": "false",
"cpu": CHECKSET,
"memory": CHECKSET,
"os_name": CHECKSET,
Expand Down Expand Up @@ -3523,6 +3546,16 @@ func TestAccAliCloudECSInstanceMaintenance(t *testing.T) {
}),
),
},
{
Config: testAccConfig(map[string]interface{}{
"enable_jumbo_frame": "true",
}),
Check: resource.ComposeTestCheckFunc(
testAccCheck(map[string]string{
"enable_jumbo_frame": "true",
}),
),
},
{
ResourceName: resourceId,
ImportState: true,
Expand Down Expand Up @@ -3683,7 +3716,7 @@ func TestAccAliCloudECSInstance_LaunchTemplate(t *testing.T) {
func resourceInstanceDedicatedHostIdConfigDependence(name string) string {
return fmt.Sprintf(`
data "alicloud_instance_types" "default" {
instance_type_family = "ecs.g6"
instance_type_family = "ecs.i2"
availability_zone = alicloud_vswitch.default.zone_id
}
Expand All @@ -3704,7 +3737,7 @@ resource "alicloud_vpc" "default" {
resource "alicloud_vswitch" "default" {
vpc_id = alicloud_vpc.default.id
cidr_block = cidrsubnet(alicloud_vpc.default.cidr_block, 8, 2)
zone_id = data.alicloud_zones.default.zones.1.id
zone_id = data.alicloud_zones.default.zones.0.id
vswitch_name = var.name
}
Expand Down Expand Up @@ -3753,7 +3786,7 @@ resource "alicloud_key_pair" "default" {
}
resource "alicloud_ecs_dedicated_host" "default" {
dedicated_host_type = "ddh.g6"
dedicated_host_type = "ddh.i2"
description = "From_Terraform"
dedicated_host_name = var.name
action_on_maintenance = "Migrate"
Expand All @@ -3766,7 +3799,7 @@ resource "alicloud_ecs_dedicated_host" "default" {
func resourceInstanceSystemDiskDependence(name string) string {
return fmt.Sprintf(`
data "alicloud_instance_types" "default" {
instance_type_family = "ecs.g6"
instance_type_family = "ecs.g8i"
system_disk_category = "cloud_essd"
availability_zone = alicloud_vswitch.default.zone_id
}
Expand Down
Loading

0 comments on commit 0b9310b

Please sign in to comment.