Skip to content

Commit

Permalink
New Resource: alicloud_api_gateway_instance.
Browse files Browse the repository at this point in the history
  • Loading branch information
ChenHanZhang committed Mar 4, 2024
1 parent 1e3b4cf commit 383176c
Show file tree
Hide file tree
Showing 5 changed files with 845 additions and 0 deletions.
1 change: 1 addition & 0 deletions alicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,7 @@ func Provider() terraform.ResourceProvider {
},
ResourcesMap: map[string]*schema.Resource{
"alicloud_vpc_ipv6_address": resourceAliCloudVpcIpv6Address(),
"alicloud_api_gateway_instance": resourceAliCloudApiGatewayInstance(),
"alicloud_ens_instance_security_group_attachment": resourceAliCloudEnsInstanceSecurityGroupAttachment(),
"alicloud_ens_disk_instance_attachment": resourceAliCloudEnsDiskInstanceAttachment(),
"alicloud_ens_image": resourceAliCloudEnsImage(),
Expand Down
314 changes: 314 additions & 0 deletions alicloud/resource_alicloud_api_gateway_instance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,314 @@
// Package alicloud. This file is generated automatically. Please do not modify it manually, thank you!
package alicloud

import (
"fmt"
"log"
"regexp"
"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 resourceAliCloudApiGatewayInstance() *schema.Resource {
return &schema.Resource{
Create: resourceAliCloudApiGatewayInstanceCreate,
Read: resourceAliCloudApiGatewayInstanceRead,
Update: resourceAliCloudApiGatewayInstanceUpdate,
Delete: resourceAliCloudApiGatewayInstanceDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(25 * time.Minute),
Update: schema.DefaultTimeout(25 * time.Minute),
Delete: schema.DefaultTimeout(25 * time.Minute),
},
Schema: map[string]*schema.Schema{
"create_time": {
Type: schema.TypeString,
Computed: true,
},
"duration": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: IntBetween(0, 100),
},
"egress_ipv6_enable": {
Type: schema.TypeBool,
Optional: true,
},
"https_policy": {
Type: schema.TypeString,
Required: true,
},
"instance_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: StringMatch(regexp.MustCompile("^[A-Za-z0-9_-]+$"), "Instance name"),
},
"instance_spec": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"instance_type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: StringInSlice([]string{"normal"}, true),
},
"payment_type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: StringInSlice([]string{"PayAsYouGo", "Subscription"}, true),
},
"pricing_cycle": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: StringInSlice([]string{"year", "month"}, true),
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"support_ipv6": {
Type: schema.TypeBool,
Optional: true,
},
"user_vpc_id": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: StringMatch(regexp.MustCompile("^[A-Za-z0-9_-]+$"), "User's VpcID"),
},
"vpc_slb_intranet_enable": {
Type: schema.TypeBool,
Optional: true,
},
"zone_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
},
},
}
}

func resourceAliCloudApiGatewayInstanceCreate(d *schema.ResourceData, meta interface{}) error {

client := meta.(*connectivity.AliyunClient)

action := "CreateInstance"
var request map[string]interface{}
var response map[string]interface{}
query := make(map[string]interface{})
conn, err := client.NewApigatewayClient()
if err != nil {
return WrapError(err)
}
request = make(map[string]interface{})

request["Token"] = buildClientToken(action)

request["InstanceName"] = d.Get("instance_name")
request["InstanceSpec"] = d.Get("instance_spec")
request["ChargeType"] = convertApiGatewayInstanceChargeTypeRequest(d.Get("payment_type").(string))
request["HttpsPolicy"] = d.Get("https_policy")
if v, ok := d.GetOk("instance_type"); ok {
request["InstanceType"] = v
}
if v, ok := d.GetOk("user_vpc_id"); ok {
request["UserVpcId"] = v
}
if v, ok := d.GetOk("zone_id"); ok {
request["ZoneId"] = v
}
if v, ok := d.GetOk("pricing_cycle"); ok {
request["PricingCycle"] = v
}
if v, ok := d.GetOk("duration"); ok {
request["Duration"] = 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("2016-07-14"), StringPointer("AK"), query, request, &runtime)
request["Token"] = buildClientToken(action)

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_api_gateway_instance", action, AlibabaCloudSdkGoERROR)
}

d.SetId(fmt.Sprint(response["InstanceId"]))

apiGatewayServiceV2 := ApiGatewayServiceV2{client}
stateConf := BuildStateConf([]string{}, []string{"RUNNING"}, d.Timeout(schema.TimeoutCreate), 15*time.Second, apiGatewayServiceV2.ApiGatewayInstanceStateRefreshFunc(d.Id(), "Status", []string{}))
if _, err := stateConf.WaitForState(); err != nil {
return WrapErrorf(err, IdMsg, d.Id())
}

return resourceAliCloudApiGatewayInstanceRead(d, meta)
}

func resourceAliCloudApiGatewayInstanceRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
apiGatewayServiceV2 := ApiGatewayServiceV2{client}

objectRaw, err := apiGatewayServiceV2.DescribeApiGatewayInstance(d.Id())
if err != nil {
if !d.IsNewResource() && NotFoundError(err) {
log.Printf("[DEBUG] Resource alicloud_api_gateway_instance DescribeApiGatewayInstance Failed!!! %s", err)
d.SetId("")
return nil
}
return WrapError(err)
}

d.Set("create_time", objectRaw["CreatedTime"])
d.Set("https_policy", objectRaw["HttpsPolicies"])
d.Set("instance_name", objectRaw["InstanceName"])
d.Set("instance_spec", objectRaw["InstanceSpec"])
d.Set("instance_type", objectRaw["DedicatedInstanceType"])
d.Set("payment_type", convertApiGatewayInstanceInstancesInstanceAttributeInstanceChargeTypeResponse(objectRaw["InstanceChargeType"]))
d.Set("status", objectRaw["Status"])
d.Set("zone_id", objectRaw["ZoneId"])

return nil
}

func resourceAliCloudApiGatewayInstanceUpdate(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 := "ModifyInstanceAttribute"
conn, err := client.NewApigatewayClient()
if err != nil {
return WrapError(err)
}
request = make(map[string]interface{})
query = make(map[string]interface{})
query["InstanceId"] = d.Id()
if d.HasChange("instance_name") {
update = true
}
request["InstanceName"] = d.Get("instance_name")
if d.HasChange("https_policy") {
update = true
}
request["HttpsPolicy"] = d.Get("https_policy")
if v, ok := d.GetOkExists("egress_ipv6_enable"); ok {
request["EgressIpv6Enable"] = v
}
if v, ok := d.GetOkExists("vpc_slb_intranet_enable"); ok {
request["VpcSlbIntranetEnable"] = v
}
if v, ok := d.GetOkExists("support_ipv6"); ok {
request["IPV6Enabled"] = v
}
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("2016-07-14"), 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)
}
apiGatewayServiceV2 := ApiGatewayServiceV2{client}
stateConf := BuildStateConf([]string{}, []string{"RUNNING"}, d.Timeout(schema.TimeoutUpdate), 30*time.Second, apiGatewayServiceV2.ApiGatewayInstanceStateRefreshFunc(d.Id(), "Status", []string{}))
if _, err := stateConf.WaitForState(); err != nil {
return WrapErrorf(err, IdMsg, d.Id())
}
}

return resourceAliCloudApiGatewayInstanceRead(d, meta)
}

func resourceAliCloudApiGatewayInstanceDelete(d *schema.ResourceData, meta interface{}) error {

client := meta.(*connectivity.AliyunClient)
action := "DeleteInstance"
var request map[string]interface{}
var response map[string]interface{}
query := make(map[string]interface{})
conn, err := client.NewApigatewayClient()
if err != nil {
return WrapError(err)
}
request = make(map[string]interface{})
query["InstanceId"] = 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("2016-07-14"), 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)
}

apiGatewayServiceV2 := ApiGatewayServiceV2{client}
stateConf := BuildStateConf([]string{}, []string{""}, d.Timeout(schema.TimeoutDelete), 5*time.Second, apiGatewayServiceV2.ApiGatewayInstanceStateRefreshFunc(d.Id(), "Status", []string{}))
if _, err := stateConf.WaitForState(); err != nil {
return WrapErrorf(err, IdMsg, d.Id())
}
return nil
}

func convertApiGatewayInstanceInstancesInstanceAttributeInstanceChargeTypeResponse(source interface{}) interface{} {
switch source {
case "PrePaid":
return "Subscription"
}
return source
}
func convertApiGatewayInstanceChargeTypeRequest(source interface{}) interface{} {
switch source {
case "Subscription":
return "PREPAY"
}
return source
}

0 comments on commit 383176c

Please sign in to comment.