From f54ebb3a0acbe456a9e70257fb2aa838ce7f9eea Mon Sep 17 00:00:00 2001 From: chenhanzhang Date: Thu, 4 Jul 2024 18:44:51 +0800 Subject: [PATCH] New Resource: alicloud_ens_nat_gateway; New Resource: alicloud_ens_eip_instance_attachment. --- alicloud/provider.go | 2 + ...ce_alicloud_ens_eip_instance_attachment.go | 190 +++++++++++ ...icloud_ens_eip_instance_attachment_test.go | 297 ++++++++++++++++++ alicloud/resource_alicloud_ens_nat_gateway.go | 226 +++++++++++++ .../resource_alicloud_ens_nat_gateway_test.go | 236 ++++++++++++++ alicloud/service_alicloud_ens_v2.go | 236 ++++++++++++++ .../ens_eip_instance_attachment.html.markdown | 106 +++++++ website/docs/r/ens_nat_gateway.html.markdown | 87 +++++ 8 files changed, 1380 insertions(+) create mode 100644 alicloud/resource_alicloud_ens_eip_instance_attachment.go create mode 100644 alicloud/resource_alicloud_ens_eip_instance_attachment_test.go create mode 100644 alicloud/resource_alicloud_ens_nat_gateway.go create mode 100644 alicloud/resource_alicloud_ens_nat_gateway_test.go create mode 100644 website/docs/r/ens_eip_instance_attachment.html.markdown create mode 100644 website/docs/r/ens_nat_gateway.html.markdown diff --git a/alicloud/provider.go b/alicloud/provider.go index 664ecd4fc73e..b708c90b9985 100644 --- a/alicloud/provider.go +++ b/alicloud/provider.go @@ -863,6 +863,8 @@ func Provider() terraform.ResourceProvider { "alicloud_cms_site_monitors": dataSourceAliCloudCloudMonitorServiceSiteMonitors(), }, ResourcesMap: map[string]*schema.Resource{ + "alicloud_ens_nat_gateway": resourceAliCloudEnsNatGateway(), + "alicloud_ens_eip_instance_attachment": resourceAliCloudEnsEipInstanceAttachment(), "alicloud_ddos_bgp_policy": resourceAliCloudDdosBgpPolicy(), "alicloud_cen_transit_router_ecr_attachment": resourceAliCloudCenTransitRouterEcrAttachment(), "alicloud_alb_load_balancer_security_group_attachment": resourceAliCloudAlbLoadBalancerSecurityGroupAttachment(), diff --git a/alicloud/resource_alicloud_ens_eip_instance_attachment.go b/alicloud/resource_alicloud_ens_eip_instance_attachment.go new file mode 100644 index 000000000000..2bc326374a38 --- /dev/null +++ b/alicloud/resource_alicloud_ens_eip_instance_attachment.go @@ -0,0 +1,190 @@ +// Package alicloud. This file is generated automatically. Please do not modify it manually, thank you! +package alicloud + +import ( + "fmt" + "log" + "strings" + "time" + + util "github.com/alibabacloud-go/tea-utils/service" + "github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity" + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" +) + +func resourceAliCloudEnsEipInstanceAttachment() *schema.Resource { + return &schema.Resource{ + Create: resourceAliCloudEnsEipInstanceAttachmentCreate, + Read: resourceAliCloudEnsEipInstanceAttachmentRead, + Delete: resourceAliCloudEnsEipInstanceAttachmentDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + Timeouts: &schema.ResourceTimeout{ + Create: schema.DefaultTimeout(5 * time.Minute), + Delete: schema.DefaultTimeout(5 * time.Minute), + }, + Schema: map[string]*schema.Schema{ + "allocation_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "instance_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "instance_type": { + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + ValidateFunc: StringInSlice([]string{"Nat", "SlbInstance", "NetworkInterface", "EnsInstance"}, false), + }, + "standby": { + Type: schema.TypeBool, + Optional: true, + ForceNew: true, + }, + "status": { + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func resourceAliCloudEnsEipInstanceAttachmentCreate(d *schema.ResourceData, meta interface{}) error { + + client := meta.(*connectivity.AliyunClient) + + action := "AssociateEnsEipAddress" + var request map[string]interface{} + var response map[string]interface{} + query := make(map[string]interface{}) + conn, err := client.NewEnsClient() + if err != nil { + return WrapError(err) + } + request = make(map[string]interface{}) + query["AllocationId"] = d.Get("allocation_id") + query["InstanceId"] = d.Get("instance_id") + query["InstanceType"] = d.Get("instance_type") + + if v, ok := d.GetOkExists("standby"); ok { + request["Standby"] = v + } + runtime := util.RuntimeOptions{} + runtime.SetAutoretry(true) + wait := incrementalWait(3*time.Second, 5*time.Second) + err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError { + response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2017-11-10"), StringPointer("AK"), query, request, &runtime) + if err != nil { + if NeedRetry(err) { + wait() + return resource.RetryableError(err) + } + return resource.NonRetryableError(err) + } + addDebug(action, response, request) + return nil + }) + + if err != nil { + return WrapErrorf(err, DefaultErrorMsg, "alicloud_ens_eip_instance_attachment", action, AlibabaCloudSdkGoERROR) + } + + d.SetId(fmt.Sprintf("%v:%v:%v", query["AllocationId"], query["InstanceId"], query["InstanceType"])) + + ensServiceV2 := EnsServiceV2{client} + stateConf := BuildStateConf([]string{}, []string{"InUse"}, d.Timeout(schema.TimeoutCreate), 5*time.Second, ensServiceV2.EnsEipInstanceAttachmentStateRefreshFunc(d.Id(), "Status", []string{})) + if _, err := stateConf.WaitForState(); err != nil { + return WrapErrorf(err, IdMsg, d.Id()) + } + + return resourceAliCloudEnsEipInstanceAttachmentRead(d, meta) +} + +func resourceAliCloudEnsEipInstanceAttachmentRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*connectivity.AliyunClient) + ensServiceV2 := EnsServiceV2{client} + + objectRaw, err := ensServiceV2.DescribeEnsEipInstanceAttachment(d.Id()) + if err != nil { + if !d.IsNewResource() && NotFoundError(err) { + log.Printf("[DEBUG] Resource alicloud_ens_eip_instance_attachment DescribeEnsEipInstanceAttachment Failed!!! %s", err) + d.SetId("") + return nil + } + return WrapError(err) + } + + if objectRaw["Standby"] != nil { + d.Set("standby", objectRaw["Standby"]) + } + if objectRaw["Status"] != nil { + d.Set("status", objectRaw["Status"]) + } + if objectRaw["AllocationId"] != nil { + d.Set("allocation_id", objectRaw["AllocationId"]) + } + if objectRaw["InstanceId"] != nil { + d.Set("instance_id", objectRaw["InstanceId"]) + } + if objectRaw["InstanceType"] != nil { + d.Set("instance_type", objectRaw["InstanceType"]) + } + + parts := strings.Split(d.Id(), ":") + d.Set("allocation_id", parts[0]) + d.Set("instance_id", parts[1]) + d.Set("instance_type", parts[2]) + + return nil +} + +func resourceAliCloudEnsEipInstanceAttachmentDelete(d *schema.ResourceData, meta interface{}) error { + + client := meta.(*connectivity.AliyunClient) + parts := strings.Split(d.Id(), ":") + action := "UnAssociateEnsEipAddress" + var request map[string]interface{} + var response map[string]interface{} + query := make(map[string]interface{}) + conn, err := client.NewEnsClient() + if err != nil { + return WrapError(err) + } + request = make(map[string]interface{}) + query["AllocationId"] = parts[0] + + runtime := util.RuntimeOptions{} + runtime.SetAutoretry(true) + wait := incrementalWait(3*time.Second, 5*time.Second) + err = resource.Retry(d.Timeout(schema.TimeoutDelete), func() *resource.RetryError { + response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2017-11-10"), StringPointer("AK"), query, request, &runtime) + + if err != nil { + if NeedRetry(err) { + wait() + return resource.RetryableError(err) + } + return resource.NonRetryableError(err) + } + addDebug(action, response, request) + return nil + }) + + if err != nil { + return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR) + } + + ensServiceV2 := EnsServiceV2{client} + stateConf := BuildStateConf([]string{}, []string{}, d.Timeout(schema.TimeoutDelete), 5*time.Second, ensServiceV2.EnsEipInstanceAttachmentStateRefreshFunc(d.Id(), "InstanceId", []string{})) + if _, err := stateConf.WaitForState(); err != nil { + return WrapErrorf(err, IdMsg, d.Id()) + } + return nil +} diff --git a/alicloud/resource_alicloud_ens_eip_instance_attachment_test.go b/alicloud/resource_alicloud_ens_eip_instance_attachment_test.go new file mode 100644 index 000000000000..3a394b3e1abd --- /dev/null +++ b/alicloud/resource_alicloud_ens_eip_instance_attachment_test.go @@ -0,0 +1,297 @@ +package alicloud + +import ( + "fmt" + "testing" + + "github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity" + "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" +) + +// Test Ens EipInstanceAttachment. >>> Resource test cases, automatically generated. +// Case LB绑定EIP_20240426 6608 +func TestAccAliCloudEnsEipInstanceAttachment_basic6608(t *testing.T) { + var v map[string]interface{} + resourceId := "alicloud_ens_eip_instance_attachment.default" + ra := resourceAttrInit(resourceId, AlicloudEnsEipInstanceAttachmentMap6608) + rc := resourceCheckInitWithDescribeMethod(resourceId, &v, func() interface{} { + return &EnsServiceV2{testAccProvider.Meta().(*connectivity.AliyunClient)} + }, "DescribeEnsEipInstanceAttachment") + rac := resourceAttrCheckInit(rc, ra) + testAccCheck := rac.resourceAttrMapUpdateSet() + rand := acctest.RandIntRange(10000, 99999) + name := fmt.Sprintf("tf-testacc%senseipinstanceattachment%d", defaultRegionToTest, rand) + testAccConfig := resourceTestAccConfigFunc(resourceId, name, AlicloudEnsEipInstanceAttachmentBasicDependence6608) + resource.Test(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheck(t) + }, + IDRefreshName: resourceId, + Providers: testAccProviders, + CheckDestroy: rac.checkResourceDestroy(), + Steps: []resource.TestStep{ + { + Config: testAccConfig(map[string]interface{}{ + "instance_id": "${alicloud_ens_load_balancer.defaultj8Egvj.id}", + "allocation_id": "${alicloud_ens_eip.defaultsGsN4e.id}", + "instance_type": "SlbInstance", + "standby": "false", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "instance_id": CHECKSET, + "allocation_id": CHECKSET, + "instance_type": "SlbInstance", + "standby": "false", + }), + ), + }, + { + ResourceName: resourceId, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{}, + }, + }, + }) +} + +var AlicloudEnsEipInstanceAttachmentMap6608 = map[string]string{ + "status": CHECKSET, + "instance_type": CHECKSET, +} + +func AlicloudEnsEipInstanceAttachmentBasicDependence6608(name string) string { + return fmt.Sprintf(` +variable "name" { + default = "%s" +} + +variable "ens_region_id" { + default = "cn-chenzhou-telecom_unicom_cmcc" +} + +resource "alicloud_ens_network" "defaultmIE0nO" { + network_name = var.name + cidr_block = "10.0.0.0/8" + ens_region_id = var.ens_region_id +} + +resource "alicloud_ens_vswitch" "defaultDiOqwH" { + cidr_block = "10.0.0.0/24" + vswitch_name = var.name + ens_region_id = var.ens_region_id + network_id = alicloud_ens_network.defaultmIE0nO.id +} + +resource "alicloud_ens_load_balancer" "defaultj8Egvj" { + vswitch_id = alicloud_ens_vswitch.defaultDiOqwH.id + ens_region_id = var.ens_region_id + network_id = alicloud_ens_vswitch.defaultDiOqwH.network_id + load_balancer_spec = "elb.s1.small" + payment_type = "PayAsYouGo" +} + +resource "alicloud_ens_eip" "defaultsGsN4e" { + bandwidth = "5" + eip_name = var.name + ens_region_id = var.ens_region_id + internet_charge_type = "95BandwidthByMonth" + payment_type = "PayAsYouGo" +} + + +`, name) +} + +// Case 实例绑定EIP_20240423 6589 +func TestAccAliCloudEnsEipInstanceAttachment_basic6589(t *testing.T) { + var v map[string]interface{} + resourceId := "alicloud_ens_eip_instance_attachment.default" + ra := resourceAttrInit(resourceId, AlicloudEnsEipInstanceAttachmentMap6589) + rc := resourceCheckInitWithDescribeMethod(resourceId, &v, func() interface{} { + return &EnsServiceV2{testAccProvider.Meta().(*connectivity.AliyunClient)} + }, "DescribeEnsEipInstanceAttachment") + rac := resourceAttrCheckInit(rc, ra) + testAccCheck := rac.resourceAttrMapUpdateSet() + rand := acctest.RandIntRange(10000, 99999) + name := fmt.Sprintf("tf-testacc%senseipinstanceattachment%d", defaultRegionToTest, rand) + testAccConfig := resourceTestAccConfigFunc(resourceId, name, AlicloudEnsEipInstanceAttachmentBasicDependence6589) + resource.Test(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheck(t) + }, + IDRefreshName: resourceId, + Providers: testAccProviders, + CheckDestroy: rac.checkResourceDestroy(), + Steps: []resource.TestStep{ + { + Config: testAccConfig(map[string]interface{}{ + "instance_id": "${alicloud_ens_instance.defaultXKjq1W.id}", + "allocation_id": "${alicloud_ens_eip.defaultsGsN4e.id}", + "instance_type": "EnsInstance", + "standby": "false", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "instance_id": CHECKSET, + "allocation_id": CHECKSET, + "instance_type": "EnsInstance", + "standby": "false", + }), + ), + }, + { + ResourceName: resourceId, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{}, + }, + }, + }) +} + +var AlicloudEnsEipInstanceAttachmentMap6589 = map[string]string{ + "status": CHECKSET, + "instance_type": CHECKSET, +} + +func AlicloudEnsEipInstanceAttachmentBasicDependence6589(name string) string { + return fmt.Sprintf(` +variable "name" { + default = "%s" +} + +variable "ens_region_id" { + default = "cn-chenzhou-telecom_unicom_cmcc" +} + +resource "alicloud_ens_instance" "defaultXKjq1W" { + system_disk { + size = "20" + category = "cloud_efficiency" + } + scheduling_strategy = "Concentrate" + schedule_area_level = "Region" + image_id = "centos_6_08_64_20G_alibase_20171208" + payment_type = "Subscription" + instance_type = "ens.sn1.stiny" + password = "12345678abcABC" + status = "Running" + amount = "1" + internet_charge_type = "95BandwidthByMonth" + instance_name = var.name + auto_use_coupon = "true" + instance_charge_strategy = "PriceHighPriority" + ens_region_id = var.ens_region_id + period_unit = "Month" +} + +resource "alicloud_ens_eip" "defaultsGsN4e" { + bandwidth = "5" + eip_name = var.name + ens_region_id = var.ens_region_id + internet_charge_type = "95BandwidthByMonth" + payment_type = "PayAsYouGo" +} + + +`, name) +} + +// Case LB绑定EIP_20240426 6608 raw +func TestAccAliCloudEnsEipInstanceAttachment_basic6608_raw(t *testing.T) { + var v map[string]interface{} + resourceId := "alicloud_ens_eip_instance_attachment.default" + ra := resourceAttrInit(resourceId, AlicloudEnsEipInstanceAttachmentMap6608) + rc := resourceCheckInitWithDescribeMethod(resourceId, &v, func() interface{} { + return &EnsServiceV2{testAccProvider.Meta().(*connectivity.AliyunClient)} + }, "DescribeEnsEipInstanceAttachment") + rac := resourceAttrCheckInit(rc, ra) + testAccCheck := rac.resourceAttrMapUpdateSet() + rand := acctest.RandIntRange(10000, 99999) + name := fmt.Sprintf("tf-testacc%senseipinstanceattachment%d", defaultRegionToTest, rand) + testAccConfig := resourceTestAccConfigFunc(resourceId, name, AlicloudEnsEipInstanceAttachmentBasicDependence6608) + resource.Test(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheck(t) + }, + IDRefreshName: resourceId, + Providers: testAccProviders, + CheckDestroy: rac.checkResourceDestroy(), + Steps: []resource.TestStep{ + { + Config: testAccConfig(map[string]interface{}{ + "instance_id": "${alicloud_ens_load_balancer.defaultj8Egvj.id}", + "allocation_id": "${alicloud_ens_eip.defaultsGsN4e.id}", + "instance_type": "SlbInstance", + "standby": "false", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "instance_id": CHECKSET, + "allocation_id": CHECKSET, + "instance_type": "SlbInstance", + "standby": "false", + }), + ), + }, + { + ResourceName: resourceId, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{}, + }, + }, + }) +} + +// Case 实例绑定EIP_20240423 6589 raw +func TestAccAliCloudEnsEipInstanceAttachment_basic6589_raw(t *testing.T) { + var v map[string]interface{} + resourceId := "alicloud_ens_eip_instance_attachment.default" + ra := resourceAttrInit(resourceId, AlicloudEnsEipInstanceAttachmentMap6589) + rc := resourceCheckInitWithDescribeMethod(resourceId, &v, func() interface{} { + return &EnsServiceV2{testAccProvider.Meta().(*connectivity.AliyunClient)} + }, "DescribeEnsEipInstanceAttachment") + rac := resourceAttrCheckInit(rc, ra) + testAccCheck := rac.resourceAttrMapUpdateSet() + rand := acctest.RandIntRange(10000, 99999) + name := fmt.Sprintf("tf-testacc%senseipinstanceattachment%d", defaultRegionToTest, rand) + testAccConfig := resourceTestAccConfigFunc(resourceId, name, AlicloudEnsEipInstanceAttachmentBasicDependence6589) + resource.Test(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheck(t) + }, + IDRefreshName: resourceId, + Providers: testAccProviders, + CheckDestroy: rac.checkResourceDestroy(), + Steps: []resource.TestStep{ + { + Config: testAccConfig(map[string]interface{}{ + "instance_id": "${alicloud_ens_instance.defaultXKjq1W.id}", + "allocation_id": "${alicloud_ens_eip.defaultsGsN4e.id}", + "instance_type": "EnsInstance", + "standby": "false", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "instance_id": CHECKSET, + "allocation_id": CHECKSET, + "instance_type": "EnsInstance", + "standby": "false", + }), + ), + }, + { + ResourceName: resourceId, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{}, + }, + }, + }) +} + +// Test Ens EipInstanceAttachment. <<< Resource test cases, automatically generated. diff --git a/alicloud/resource_alicloud_ens_nat_gateway.go b/alicloud/resource_alicloud_ens_nat_gateway.go new file mode 100644 index 000000000000..52780a7ad55f --- /dev/null +++ b/alicloud/resource_alicloud_ens_nat_gateway.go @@ -0,0 +1,226 @@ +// Package alicloud. This file is generated automatically. Please do not modify it manually, thank you! +package alicloud + +import ( + "fmt" + "log" + "time" + + util "github.com/alibabacloud-go/tea-utils/service" + "github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity" + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" +) + +func resourceAliCloudEnsNatGateway() *schema.Resource { + return &schema.Resource{ + Create: resourceAliCloudEnsNatGatewayCreate, + Read: resourceAliCloudEnsNatGatewayRead, + Update: resourceAliCloudEnsNatGatewayUpdate, + Delete: resourceAliCloudEnsNatGatewayDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + Timeouts: &schema.ResourceTimeout{ + Create: schema.DefaultTimeout(5 * time.Minute), + Update: schema.DefaultTimeout(5 * time.Minute), + Delete: schema.DefaultTimeout(5 * time.Minute), + }, + Schema: map[string]*schema.Schema{ + "create_time": { + Type: schema.TypeString, + Computed: true, + }, + "ens_region_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "instance_type": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + Computed: true, + }, + "nat_name": { + Type: schema.TypeString, + Optional: true, + }, + "network_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "vswitch_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + }, + } +} + +func resourceAliCloudEnsNatGatewayCreate(d *schema.ResourceData, meta interface{}) error { + + client := meta.(*connectivity.AliyunClient) + + action := "CreateNatGateway" + var request map[string]interface{} + var response map[string]interface{} + query := make(map[string]interface{}) + conn, err := client.NewEnsClient() + if err != nil { + return WrapError(err) + } + request = make(map[string]interface{}) + + request["EnsRegionId"] = d.Get("ens_region_id") + request["NetworkId"] = d.Get("network_id") + request["VSwitchId"] = d.Get("vswitch_id") + if v, ok := d.GetOk("instance_type"); ok { + request["InstanceType"] = v + } + if v, ok := d.GetOk("nat_name"); ok { + request["Name"] = v + } + runtime := util.RuntimeOptions{} + runtime.SetAutoretry(true) + wait := incrementalWait(3*time.Second, 5*time.Second) + err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError { + response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2017-11-10"), StringPointer("AK"), query, request, &runtime) + if err != nil { + if NeedRetry(err) { + wait() + return resource.RetryableError(err) + } + return resource.NonRetryableError(err) + } + addDebug(action, response, request) + return nil + }) + + if err != nil { + return WrapErrorf(err, DefaultErrorMsg, "alicloud_ens_nat_gateway", action, AlibabaCloudSdkGoERROR) + } + + d.SetId(fmt.Sprint(response["NatGatewayId"])) + + return resourceAliCloudEnsNatGatewayRead(d, meta) +} + +func resourceAliCloudEnsNatGatewayRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*connectivity.AliyunClient) + ensServiceV2 := EnsServiceV2{client} + + objectRaw, err := ensServiceV2.DescribeEnsNatGateway(d.Id()) + if err != nil { + if !d.IsNewResource() && NotFoundError(err) { + log.Printf("[DEBUG] Resource alicloud_ens_nat_gateway DescribeEnsNatGateway Failed!!! %s", err) + d.SetId("") + return nil + } + return WrapError(err) + } + + if objectRaw["CreationTime"] != nil { + d.Set("create_time", objectRaw["CreationTime"]) + } + if objectRaw["EnsRegionId"] != nil { + d.Set("ens_region_id", objectRaw["EnsRegionId"]) + } + if objectRaw["Spec"] != nil { + d.Set("instance_type", objectRaw["Spec"]) + } + if objectRaw["Name"] != nil { + d.Set("nat_name", objectRaw["Name"]) + } + if objectRaw["NetworkId"] != nil { + d.Set("network_id", objectRaw["NetworkId"]) + } + if objectRaw["VSwitchId"] != nil { + d.Set("vswitch_id", objectRaw["VSwitchId"]) + } + + return nil +} + +func resourceAliCloudEnsNatGatewayUpdate(d *schema.ResourceData, meta interface{}) error { + client := meta.(*connectivity.AliyunClient) + var request map[string]interface{} + var response map[string]interface{} + var query map[string]interface{} + update := false + action := "ModifyNatGatewayAttribute" + conn, err := client.NewEnsClient() + if err != nil { + return WrapError(err) + } + request = make(map[string]interface{}) + query = make(map[string]interface{}) + query["NatGatewayId"] = d.Id() + if d.HasChange("nat_name") { + update = true + request["Name"] = d.Get("nat_name") + } + + if update { + runtime := util.RuntimeOptions{} + runtime.SetAutoretry(true) + wait := incrementalWait(3*time.Second, 5*time.Second) + err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError { + response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2017-11-10"), StringPointer("AK"), query, request, &runtime) + if err != nil { + if NeedRetry(err) { + wait() + return resource.RetryableError(err) + } + return resource.NonRetryableError(err) + } + addDebug(action, response, request) + return nil + }) + if err != nil { + return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR) + } + } + + return resourceAliCloudEnsNatGatewayRead(d, meta) +} + +func resourceAliCloudEnsNatGatewayDelete(d *schema.ResourceData, meta interface{}) error { + + client := meta.(*connectivity.AliyunClient) + action := "DeleteNatGateway" + var request map[string]interface{} + var response map[string]interface{} + query := make(map[string]interface{}) + conn, err := client.NewEnsClient() + if err != nil { + return WrapError(err) + } + request = make(map[string]interface{}) + query["NatGatewayId"] = d.Id() + + runtime := util.RuntimeOptions{} + runtime.SetAutoretry(true) + wait := incrementalWait(3*time.Second, 5*time.Second) + err = resource.Retry(d.Timeout(schema.TimeoutDelete), func() *resource.RetryError { + response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2017-11-10"), StringPointer("AK"), query, request, &runtime) + + if err != nil { + if NeedRetry(err) { + wait() + return resource.RetryableError(err) + } + return resource.NonRetryableError(err) + } + addDebug(action, response, request) + return nil + }) + + if err != nil { + return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR) + } + + return nil +} diff --git a/alicloud/resource_alicloud_ens_nat_gateway_test.go b/alicloud/resource_alicloud_ens_nat_gateway_test.go new file mode 100644 index 000000000000..379d91923eb0 --- /dev/null +++ b/alicloud/resource_alicloud_ens_nat_gateway_test.go @@ -0,0 +1,236 @@ +package alicloud + +import ( + "fmt" + "testing" + + "github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity" + "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" +) + +// Test Ens NatGateway. >>> Resource test cases, automatically generated. +// Case Nat网关测试_20240507 6657 +func TestAccAliCloudEnsNatGateway_basic6657(t *testing.T) { + var v map[string]interface{} + resourceId := "alicloud_ens_nat_gateway.default" + ra := resourceAttrInit(resourceId, AlicloudEnsNatGatewayMap6657) + rc := resourceCheckInitWithDescribeMethod(resourceId, &v, func() interface{} { + return &EnsServiceV2{testAccProvider.Meta().(*connectivity.AliyunClient)} + }, "DescribeEnsNatGateway") + rac := resourceAttrCheckInit(rc, ra) + testAccCheck := rac.resourceAttrMapUpdateSet() + rand := acctest.RandIntRange(10000, 99999) + name := fmt.Sprintf("tf-testacc%sensnatgateway%d", defaultRegionToTest, rand) + testAccConfig := resourceTestAccConfigFunc(resourceId, name, AlicloudEnsNatGatewayBasicDependence6657) + resource.Test(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheck(t) + }, + IDRefreshName: resourceId, + Providers: testAccProviders, + CheckDestroy: rac.checkResourceDestroy(), + Steps: []resource.TestStep{ + { + Config: testAccConfig(map[string]interface{}{ + "vswitch_id": "${alicloud_ens_vswitch.defaulteFw783.id}", + "ens_region_id": "${alicloud_ens_vswitch.defaulteFw783.ens_region_id}", + "network_id": "${alicloud_ens_vswitch.defaulteFw783.network_id}", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "vswitch_id": CHECKSET, + "ens_region_id": CHECKSET, + "network_id": CHECKSET, + }), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "nat_name": "test1", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "nat_name": "test1", + }), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "nat_name": "test2", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "nat_name": "test2", + }), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "vswitch_id": "${alicloud_ens_vswitch.defaulteFw783.id}", + "ens_region_id": "${alicloud_ens_vswitch.defaulteFw783.ens_region_id}", + "network_id": "${alicloud_ens_vswitch.defaulteFw783.network_id}", + "instance_type": "enat.default", + "nat_name": "test1", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "vswitch_id": CHECKSET, + "ens_region_id": CHECKSET, + "network_id": CHECKSET, + "instance_type": "enat.default", + "nat_name": "test1", + }), + ), + }, + { + ResourceName: resourceId, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{}, + }, + }, + }) +} + +var AlicloudEnsNatGatewayMap6657 = map[string]string{ + "create_time": CHECKSET, +} + +func AlicloudEnsNatGatewayBasicDependence6657(name string) string { + return fmt.Sprintf(` +variable "name" { + default = "%s" +} + +variable "ens_region_id" { + default = "cn-chenzhou-telecom_unicom_cmcc" +} + +resource "alicloud_ens_network" "defaultObbrL7" { + network_name = var.name + description = "测试用例-测试NAT使用" + cidr_block = "10.0.0.0/8" + ens_region_id = var.ens_region_id +} + +resource "alicloud_ens_vswitch" "defaulteFw783" { + cidr_block = "10.0.8.0/24" + vswitch_name = var.name + ens_region_id = alicloud_ens_network.defaultObbrL7.ens_region_id + network_id = alicloud_ens_network.defaultObbrL7.id +} + + +`, name) +} + +// Case Nat网关测试_20240507 6657 twin +func TestAccAliCloudEnsNatGateway_basic6657_twin(t *testing.T) { + var v map[string]interface{} + resourceId := "alicloud_ens_nat_gateway.default" + ra := resourceAttrInit(resourceId, AlicloudEnsNatGatewayMap6657) + rc := resourceCheckInitWithDescribeMethod(resourceId, &v, func() interface{} { + return &EnsServiceV2{testAccProvider.Meta().(*connectivity.AliyunClient)} + }, "DescribeEnsNatGateway") + rac := resourceAttrCheckInit(rc, ra) + testAccCheck := rac.resourceAttrMapUpdateSet() + rand := acctest.RandIntRange(10000, 99999) + name := fmt.Sprintf("tf-testacc%sensnatgateway%d", defaultRegionToTest, rand) + testAccConfig := resourceTestAccConfigFunc(resourceId, name, AlicloudEnsNatGatewayBasicDependence6657) + resource.Test(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheck(t) + }, + IDRefreshName: resourceId, + Providers: testAccProviders, + CheckDestroy: rac.checkResourceDestroy(), + Steps: []resource.TestStep{ + { + Config: testAccConfig(map[string]interface{}{ + "vswitch_id": "${alicloud_ens_vswitch.defaulteFw783.id}", + "ens_region_id": "${alicloud_ens_vswitch.defaulteFw783.ens_region_id}", + "network_id": "${alicloud_ens_vswitch.defaulteFw783.network_id}", + "instance_type": "enat.default", + "nat_name": "test1", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "vswitch_id": CHECKSET, + "ens_region_id": CHECKSET, + "network_id": CHECKSET, + "instance_type": "enat.default", + "nat_name": "test1", + }), + ), + }, + { + ResourceName: resourceId, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{}, + }, + }, + }) +} + +// Case Nat网关测试_20240507 6657 raw +func TestAccAliCloudEnsNatGateway_basic6657_raw(t *testing.T) { + var v map[string]interface{} + resourceId := "alicloud_ens_nat_gateway.default" + ra := resourceAttrInit(resourceId, AlicloudEnsNatGatewayMap6657) + rc := resourceCheckInitWithDescribeMethod(resourceId, &v, func() interface{} { + return &EnsServiceV2{testAccProvider.Meta().(*connectivity.AliyunClient)} + }, "DescribeEnsNatGateway") + rac := resourceAttrCheckInit(rc, ra) + testAccCheck := rac.resourceAttrMapUpdateSet() + rand := acctest.RandIntRange(10000, 99999) + name := fmt.Sprintf("tf-testacc%sensnatgateway%d", defaultRegionToTest, rand) + testAccConfig := resourceTestAccConfigFunc(resourceId, name, AlicloudEnsNatGatewayBasicDependence6657) + resource.Test(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheck(t) + }, + IDRefreshName: resourceId, + Providers: testAccProviders, + CheckDestroy: rac.checkResourceDestroy(), + Steps: []resource.TestStep{ + { + Config: testAccConfig(map[string]interface{}{ + "vswitch_id": "${alicloud_ens_vswitch.defaulteFw783.id}", + "ens_region_id": "${alicloud_ens_vswitch.defaulteFw783.ens_region_id}", + "network_id": "${alicloud_ens_vswitch.defaulteFw783.network_id}", + "instance_type": "enat.default", + "nat_name": "test1", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "vswitch_id": CHECKSET, + "ens_region_id": CHECKSET, + "network_id": CHECKSET, + "instance_type": "enat.default", + "nat_name": "test1", + }), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "nat_name": "test2", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "nat_name": "test2", + }), + ), + }, + { + ResourceName: resourceId, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{}, + }, + }, + }) +} + +// Test Ens NatGateway. <<< Resource test cases, automatically generated. diff --git a/alicloud/service_alicloud_ens_v2.go b/alicloud/service_alicloud_ens_v2.go index c484fc6c43cd..3f19dc840f4e 100644 --- a/alicloud/service_alicloud_ens_v2.go +++ b/alicloud/service_alicloud_ens_v2.go @@ -829,3 +829,239 @@ func (s *EnsServiceV2) EnsInstanceSecurityGroupAttachmentStateRefreshFunc(id str } // DescribeEnsInstanceSecurityGroupAttachment >>> Encapsulated. +// DescribeEnsEipInstanceAttachment <<< Encapsulated get interface for Ens EipInstanceAttachment. + +func (s *EnsServiceV2) DescribeEnsEipInstanceAttachment(id string) (object map[string]interface{}, err error) { + client := s.client + var request map[string]interface{} + var response map[string]interface{} + var query map[string]interface{} + parts := strings.Split(id, ":") + if len(parts) != 3 { + err = WrapError(fmt.Errorf("invalid Resource Id %s. Expected parts' length %d, got %d", id, 3, len(parts))) + } + action := "DescribeEnsEipAddresses" + conn, err := client.NewEnsClient() + if err != nil { + return object, WrapError(err) + } + request = make(map[string]interface{}) + query = make(map[string]interface{}) + query["AllocationId"] = parts[0] + query["AssociatedInstanceId"] = parts[1] + + runtime := util.RuntimeOptions{} + runtime.SetAutoretry(true) + wait := incrementalWait(3*time.Second, 5*time.Second) + err = resource.Retry(1*time.Minute, func() *resource.RetryError { + response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2017-11-10"), StringPointer("AK"), query, request, &runtime) + + if err != nil { + if NeedRetry(err) { + wait() + return resource.RetryableError(err) + } + return resource.NonRetryableError(err) + } + addDebug(action, response, request) + return nil + }) + if err != nil { + addDebug(action, response, request) + return object, WrapErrorf(err, DefaultErrorMsg, id, action, AlibabaCloudSdkGoERROR) + } + + v, err := jsonpath.Get("$.EipAddresses.EipAddress[*]", response) + if err != nil { + return object, WrapErrorf(err, FailedGetAttributeMsg, id, "$.EipAddresses.EipAddress[*]", response) + } + + if len(v.([]interface{})) == 0 { + return object, WrapErrorf(Error(GetNotFoundMessage("EipInstanceAttachment", id)), NotFoundMsg, response) + } + + result, _ := v.([]interface{}) + for _, v := range result { + item := v.(map[string]interface{}) + if item["AllocationId"] != parts[0] { + continue + } + if item["InstanceId"] != parts[1] { + continue + } + if item["InstanceType"] != parts[2] { + continue + } + return item, nil + } + return object, WrapErrorf(Error(GetNotFoundMessage("EipInstanceAttachment", id)), NotFoundMsg, response) +} + +func (s *EnsServiceV2) EnsEipInstanceAttachmentStateRefreshFunc(id string, field string, failStates []string) resource.StateRefreshFunc { + return func() (interface{}, string, error) { + object, err := s.DescribeEnsEipInstanceAttachment(id) + if err != nil { + if NotFoundError(err) { + return nil, "", nil + } + return nil, "", WrapError(err) + } + + v, err := jsonpath.Get(field, object) + currentStatus := fmt.Sprint(v) + + for _, failState := range failStates { + if currentStatus == failState { + return object, currentStatus, WrapError(Error(FailedToReachTargetStatus, currentStatus)) + } + } + return object, currentStatus, nil + } +} + +// DescribeEnsEipInstanceAttachment >>> Encapsulated. + +// DescribeEnsNatGateway <<< Encapsulated get interface for Ens NatGateway. + +func (s *EnsServiceV2) DescribeEnsNatGateway(id string) (object map[string]interface{}, err error) { + client := s.client + var request map[string]interface{} + var response map[string]interface{} + var query map[string]interface{} + action := "DescribeNatGateways" + conn, err := client.NewEnsClient() + if err != nil { + return object, WrapError(err) + } + request = make(map[string]interface{}) + query = make(map[string]interface{}) + query["NatGatewayId"] = id + + runtime := util.RuntimeOptions{} + runtime.SetAutoretry(true) + wait := incrementalWait(3*time.Second, 5*time.Second) + err = resource.Retry(1*time.Minute, func() *resource.RetryError { + response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("GET"), StringPointer("2017-11-10"), StringPointer("AK"), query, request, &runtime) + + if err != nil { + if NeedRetry(err) { + wait() + return resource.RetryableError(err) + } + return resource.NonRetryableError(err) + } + addDebug(action, response, request) + return nil + }) + if err != nil { + addDebug(action, response, request) + return object, WrapErrorf(err, DefaultErrorMsg, id, action, AlibabaCloudSdkGoERROR) + } + + v, err := jsonpath.Get("$.NatGateways[*]", response) + if err != nil { + return object, WrapErrorf(err, FailedGetAttributeMsg, id, "$.NatGateways[*]", response) + } + + if len(v.([]interface{})) == 0 { + return object, WrapErrorf(Error(GetNotFoundMessage("NatGateway", id)), NotFoundMsg, response) + } + + return v.([]interface{})[0].(map[string]interface{}), nil +} + +func (s *EnsServiceV2) EnsNatGatewayStateRefreshFunc(id string, field string, failStates []string) resource.StateRefreshFunc { + return func() (interface{}, string, error) { + object, err := s.DescribeEnsNatGateway(id) + if err != nil { + if NotFoundError(err) { + return object, "", nil + } + return nil, "", WrapError(err) + } + + v, err := jsonpath.Get(field, object) + currentStatus := fmt.Sprint(v) + + for _, failState := range failStates { + if currentStatus == failState { + return object, currentStatus, WrapError(Error(FailedToReachTargetStatus, currentStatus)) + } + } + return object, currentStatus, nil + } +} + +// DescribeEnsNatGateway >>> Encapsulated. +// DescribeEnsKeyPair <<< Encapsulated get interface for Ens KeyPair. + +func (s *EnsServiceV2) DescribeEnsKeyPair(id string) (object map[string]interface{}, err error) { + client := s.client + var request map[string]interface{} + var response map[string]interface{} + var query map[string]interface{} + action := "DescribeKeyPairs" + conn, err := client.NewEnsClient() + if err != nil { + return object, WrapError(err) + } + request = make(map[string]interface{}) + query = make(map[string]interface{}) + query["KeyPairId"] = id + + runtime := util.RuntimeOptions{} + runtime.SetAutoretry(true) + wait := incrementalWait(3*time.Second, 5*time.Second) + err = resource.Retry(1*time.Minute, func() *resource.RetryError { + response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2017-11-10"), StringPointer("AK"), query, request, &runtime) + + if err != nil { + if NeedRetry(err) { + wait() + return resource.RetryableError(err) + } + return resource.NonRetryableError(err) + } + addDebug(action, response, request) + return nil + }) + if err != nil { + addDebug(action, response, request) + return object, WrapErrorf(err, DefaultErrorMsg, id, action, AlibabaCloudSdkGoERROR) + } + + v, err := jsonpath.Get("$.KeyPairs.KeyPair[*]", response) + if err != nil { + return object, WrapErrorf(err, FailedGetAttributeMsg, id, "$.KeyPairs.KeyPair[*]", response) + } + + if len(v.([]interface{})) == 0 { + return object, WrapErrorf(Error(GetNotFoundMessage("KeyPair", id)), NotFoundMsg, response) + } + + return v.([]interface{})[0].(map[string]interface{}), nil +} + +func (s *EnsServiceV2) EnsKeyPairStateRefreshFunc(id string, field string, failStates []string) resource.StateRefreshFunc { + return func() (interface{}, string, error) { + object, err := s.DescribeEnsKeyPair(id) + if err != nil { + if NotFoundError(err) { + return object, "", nil + } + return nil, "", WrapError(err) + } + + v, err := jsonpath.Get(field, object) + currentStatus := fmt.Sprint(v) + + for _, failState := range failStates { + if currentStatus == failState { + return object, currentStatus, WrapError(Error(FailedToReachTargetStatus, currentStatus)) + } + } + return object, currentStatus, nil + } +} + +// DescribeEnsKeyPair >>> Encapsulated. diff --git a/website/docs/r/ens_eip_instance_attachment.html.markdown b/website/docs/r/ens_eip_instance_attachment.html.markdown new file mode 100644 index 000000000000..2645651f05de --- /dev/null +++ b/website/docs/r/ens_eip_instance_attachment.html.markdown @@ -0,0 +1,106 @@ +--- +subcategory: "Ens" +layout: "alicloud" +page_title: "Alicloud: alicloud_ens_eip_instance_attachment" +description: |- + Provides a Alicloud Ens Eip Instance Attachment resource. +--- + +# alicloud_ens_eip_instance_attachment + +Provides a Ens Eip Instance Attachment resource. + +Bind an EIP to an instance. + +For information about Ens Eip Instance Attachment and how to use it, see [What is Eip Instance Attachment](https://www.alibabacloud.com/help/en/). + +-> **NOTE:** Available since v1.227.0. + +## Example Usage + +Basic Usage + +```terraform +variable "name" { + default = "terraform-example" +} + +provider "alicloud" { + region = "cn-hangzhou" +} + +variable "ens_region_id" { + default = "cn-chenzhou-telecom_unicom_cmcc" +} + +resource "alicloud_ens_instance" "defaultXKjq1W" { + system_disk { + size = "20" + category = "cloud_efficiency" + } + scheduling_strategy = "Concentrate" + schedule_area_level = "Region" + image_id = "centos_6_08_64_20G_alibase_20171208" + payment_type = "Subscription" + instance_type = "ens.sn1.stiny" + password = "12345678abcABC" + status = "Running" + amount = "1" + internet_charge_type = "95BandwidthByMonth" + instance_name = var.name + auto_use_coupon = "true" + instance_charge_strategy = "PriceHighPriority" + ens_region_id = var.ens_region_id + period_unit = "Month" +} + +resource "alicloud_ens_eip" "defaultsGsN4e" { + bandwidth = "5" + eip_name = var.name + ens_region_id = var.ens_region_id + internet_charge_type = "95BandwidthByMonth" + payment_type = "PayAsYouGo" +} + +resource "alicloud_ens_eip_instance_attachment" "default" { + instance_id = alicloud_ens_instance.defaultXKjq1W.id + allocation_id = alicloud_ens_eip.defaultsGsN4e.id + instance_type = "EnsInstance" + standby = "false" +} +``` + +## Argument Reference + +The following arguments are supported: +* `allocation_id` - (Required, ForceNew) The first ID of the resource +* `instance_id` - (Required, ForceNew) Instance ID +* `instance_type` - (Optional, ForceNew, Computed) The type of the EIP instance. Value: + - `Nat`:NAT gateway. + - `SlbInstance`: Server Load Balancer (ELB). + - `NetworkInterface`: Secondary ENI. + - `EnsInstance` (default): The ENS instance. +* `standby` - (Optional, ForceNew) Indicates whether the EIP is a backup EIP. Value: + - true: Spare. + - false: not standby. + +## Attributes Reference + +The following attributes are exported: +* `id` - The ID of the resource supplied above.The value is formulated as `::`. +* `status` - The status of the EIP. + + +## Timeouts + +The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration-0-11/resources.html#timeouts) for certain actions: +* `create` - (Defaults to 5 mins) Used when create the Eip Instance Attachment. +* `delete` - (Defaults to 5 mins) Used when delete the Eip Instance Attachment. + +## Import + +Ens Eip Instance Attachment can be imported using the id, e.g. + +```shell +$ terraform import alicloud_ens_eip_instance_attachment.example :: +``` \ No newline at end of file diff --git a/website/docs/r/ens_nat_gateway.html.markdown b/website/docs/r/ens_nat_gateway.html.markdown new file mode 100644 index 000000000000..a510a09d3a17 --- /dev/null +++ b/website/docs/r/ens_nat_gateway.html.markdown @@ -0,0 +1,87 @@ +--- +subcategory: "Ens" +layout: "alicloud" +page_title: "Alicloud: alicloud_ens_nat_gateway" +description: |- + Provides a Alicloud Ens Nat Gateway resource. +--- + +# alicloud_ens_nat_gateway + +Provides a Ens Nat Gateway resource. + +Nat gateway of ENS. + +For information about Ens Nat Gateway and how to use it, see [What is Nat Gateway](https://www.alibabacloud.com/help/en/). + +-> **NOTE:** Available since v1.227.0. + +## Example Usage + +Basic Usage + +```terraform +variable "name" { + default = "terraform-example" +} + +provider "alicloud" { + region = "cn-hangzhou" +} + +variable "ens_region_id" { + default = "cn-chenzhou-telecom_unicom_cmcc" +} + +resource "alicloud_ens_network" "defaultObbrL7" { + network_name = var.name + description = var.name + cidr_block = "10.0.0.0/8" + ens_region_id = var.ens_region_id +} + +resource "alicloud_ens_vswitch" "defaulteFw783" { + cidr_block = "10.0.8.0/24" + vswitch_name = var.name + ens_region_id = alicloud_ens_network.defaultObbrL7.ens_region_id + network_id = alicloud_ens_network.defaultObbrL7.id +} + +resource "alicloud_ens_nat_gateway" "default" { + vswitch_id = alicloud_ens_vswitch.defaulteFw783.id + ens_region_id = alicloud_ens_vswitch.defaulteFw783.ens_region_id + network_id = alicloud_ens_vswitch.defaulteFw783.network_id + instance_type = "enat.default" + nat_name = var.name +} +``` + +## Argument Reference + +The following arguments are supported: +* `ens_region_id` - (Required, ForceNew) The ID of the ENS node. +* `instance_type` - (Optional, ForceNew) NAT specifications. Value: `enat.default`. +* `nat_name` - (Optional) The name of the NAT gateway. The length is 1 to 128 characters, but it cannot start with 'http:// 'or 'https. +* `network_id` - (Required, ForceNew) The network ID. +* `vswitch_id` - (Required, ForceNew) The vSwitch ID. + +## Attributes Reference + +The following attributes are exported: +* `id` - The ID of the resource supplied above. +* `create_time` - Creation time. UTC time, in the format of YYYY-MM-DDThh:mm:ssZ. + +## Timeouts + +The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration-0-11/resources.html#timeouts) for certain actions: +* `create` - (Defaults to 5 mins) Used when create the Nat Gateway. +* `delete` - (Defaults to 5 mins) Used when delete the Nat Gateway. +* `update` - (Defaults to 5 mins) Used when update the Nat Gateway. + +## Import + +Ens Nat Gateway can be imported using the id, e.g. + +```shell +$ terraform import alicloud_ens_nat_gateway.example +``` \ No newline at end of file