Skip to content

Commit

Permalink
New Resource: alicloud_esa_site; New Resource: alicloud_esa_rate_plan…
Browse files Browse the repository at this point in the history
…_instance.
  • Loading branch information
ChenHanZhang committed Nov 13, 2024
1 parent 74ea8eb commit 04dbe6d
Show file tree
Hide file tree
Showing 9 changed files with 1,865 additions and 0 deletions.
24 changes: 24 additions & 0 deletions alicloud/connectivity/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5721,3 +5721,27 @@ func (client *AliyunClient) NewGwlbClient() (*rpc.Client, error) {
}
return conn, nil
}
func (client *AliyunClient) NewEsaClient() (*rpc.Client, error) {
productCode := "dcdnservices"
endpoint := ""
if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" {
if err := client.loadEndpoint(productCode); err != nil {
endpoint = fmt.Sprintf("%s", client.config.RegionId)
client.config.Endpoints.Store(productCode, endpoint)
log.Printf("[ERROR] loading %s endpoint got an error: %#v. Using the endpoint %s instead.", productCode, err, endpoint)
}
}
if v, ok := client.config.Endpoints.Load(productCode); ok && v.(string) != "" {
endpoint = v.(string)
}
if endpoint == "" {
return nil, fmt.Errorf("[ERROR] missing the product %s endpoint.", productCode)
}
sdkConfig := client.teaSdkConfig
sdkConfig.SetEndpoint(endpoint)
conn, err := rpc.NewClient(&sdkConfig)
if err != nil {
return nil, fmt.Errorf("unable to initialize the %s client: %#v", productCode, err)
}
return conn, nil
}
2 changes: 2 additions & 0 deletions alicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,7 @@ func Provider() terraform.ResourceProvider {
"alicloud_cms_site_monitors": dataSourceAliCloudCloudMonitorServiceSiteMonitors(),
},
ResourcesMap: map[string]*schema.Resource{
"alicloud_esa_rate_plan_instance": resourceAliCloudEsaRatePlanInstance(),
"alicloud_vpc_ipam_ipam_pool_cidr": resourceAliCloudVpcIpamIpamPoolCidr(),
"alicloud_vpc_ipam_ipam_pool": resourceAliCloudVpcIpamIpamPool(),
"alicloud_vpc_ipam_ipam_scope": resourceAliCloudVpcIpamIpamScope(),
Expand All @@ -880,6 +881,7 @@ func Provider() terraform.ResourceProvider {
"alicloud_gwlb_load_balancer": resourceAliCloudGwlbLoadBalancer(),
"alicloud_oss_bucket_cname_token": resourceAliCloudOssBucketCnameToken(),
"alicloud_oss_bucket_cname": resourceAliCloudOssBucketCname(),
"alicloud_esa_site": resourceAliCloudEsaSite(),
"alicloud_pai_workspace_workspace": resourceAliCloudPaiWorkspaceWorkspace(),
"alicloud_gpdb_database": resourceAliCloudGpdbDatabase(),
"alicloud_sls_collection_policy": resourceAliCloudSlsCollectionPolicy(),
Expand Down
265 changes: 265 additions & 0 deletions alicloud/resource_alicloud_esa_rate_plan_instance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,265 @@
// 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 resourceAliCloudEsaRatePlanInstance() *schema.Resource {
return &schema.Resource{
Create: resourceAliCloudEsaRatePlanInstanceCreate,
Read: resourceAliCloudEsaRatePlanInstanceRead,
Update: resourceAliCloudEsaRatePlanInstanceUpdate,
Delete: resourceAliCloudEsaRatePlanInstanceDelete,
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{
"auto_pay": {
Type: schema.TypeBool,
Optional: true,
},
"auto_renew": {
Type: schema.TypeBool,
Optional: true,
},
"coverage": {
Type: schema.TypeString,
Optional: true,
},
"create_time": {
Type: schema.TypeString,
Computed: true,
},
"instance_status": {
Type: schema.TypeString,
Computed: true,
},
"order_id": {
Type: schema.TypeString,
Optional: true,
},
"payment_type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: StringInSlice([]string{"Subscription"}, false),
},
"period": {
Type: schema.TypeInt,
Optional: true,
},
"plan_name": {
Type: schema.TypeString,
Optional: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"type": {
Type: schema.TypeString,
Optional: true,
},
},
}
}

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

client := meta.(*connectivity.AliyunClient)

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

if v, ok := d.GetOk("plan_name"); ok {
request["PlanName"] = v
}
if v, ok := d.GetOk("coverage"); ok {
request["Coverage"] = v
}
if v, ok := d.GetOk("type"); ok {
request["Type"] = v
}
if v, ok := d.GetOkExists("auto_renew"); ok {
request["AutoRenew"] = v
}
if v, ok := d.GetOkExists("period"); ok {
request["Period"] = v
}
if v, ok := d.GetOk("payment_type"); ok {
request["ChargeType"] = convertEsaRatePlanInstanceChargeTypeRequest(v.(string))
}
if v, ok := d.GetOkExists("auto_pay"); ok {
request["AutoPay"] = 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("2024-09-10"), StringPointer("AK"), query, request, &runtime)
if err != nil {
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})
addDebug(action, response, request)

if err != nil {
return WrapErrorf(err, DefaultErrorMsg, "alicloud_esa_rate_plan_instance", action, AlibabaCloudSdkGoERROR)
}

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

esaServiceV2 := EsaServiceV2{client}
stateConf := BuildStateConf([]string{}, []string{"running"}, d.Timeout(schema.TimeoutCreate), 10*time.Second, esaServiceV2.EsaRatePlanInstanceStateRefreshFunc(d.Id(), "InstanceStatus", []string{}))
if _, err := stateConf.WaitForState(); err != nil {
return WrapErrorf(err, IdMsg, d.Id())
}

return resourceAliCloudEsaRatePlanInstanceRead(d, meta)
}

func resourceAliCloudEsaRatePlanInstanceRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
esaServiceV2 := EsaServiceV2{client}

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

if objectRaw["CreateTime"] != nil {
d.Set("create_time", objectRaw["CreateTime"])
}
if objectRaw["BillingMode"] != nil {
d.Set("payment_type", convertEsaRatePlanInstanceInstanceInfoBillingModeResponse(objectRaw["BillingMode"]))
}
if objectRaw["PlanName"] != nil {
d.Set("plan_name", objectRaw["PlanName"])
}
if objectRaw["Status"] != nil {
d.Set("status", objectRaw["Status"])
}

objectRaw, err = esaServiceV2.DescribeDescribeRatePlanInstanceStatus(d.Id())
if err != nil {
return WrapError(err)
}

if objectRaw["InstanceStatus"] != nil {
d.Set("instance_status", objectRaw["InstanceStatus"])
}

return nil
}

func resourceAliCloudEsaRatePlanInstanceUpdate(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 := "UpdateRatePlanSpec"
conn, err := client.NewEsaClient()
if err != nil {
return WrapError(err)
}
request = make(map[string]interface{})
query = make(map[string]interface{})
request["InstanceId"] = d.Id()

request["OrderType"] = "UPGRADE"
if d.HasChange("plan_name") {
update = true
request["TargetPlanName"] = d.Get("plan_name")
}

if d.HasChange("payment_type") {
update = true
}
request["ChargeType"] = convertEsaRatePlanInstanceChargeTypeRequest(d.Get("payment_type").(string))

request["AutoPay"] = true
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("2024-09-10"), StringPointer("AK"), query, request, &runtime)
if err != nil {
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})
addDebug(action, response, request)
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR)
}
esaServiceV2 := EsaServiceV2{client}
stateConf := BuildStateConf([]string{}, []string{"running"}, d.Timeout(schema.TimeoutUpdate), 10*time.Second, esaServiceV2.EsaRatePlanInstanceStateRefreshFunc(d.Id(), "InstanceStatus", []string{}))
if _, err := stateConf.WaitForState(); err != nil {
return WrapErrorf(err, IdMsg, d.Id())
}
}

return resourceAliCloudEsaRatePlanInstanceRead(d, meta)
}

func resourceAliCloudEsaRatePlanInstanceDelete(d *schema.ResourceData, meta interface{}) error {
log.Printf("[WARN] Cannot destroy resource AliCloud Resource Rate Plan Instance. Terraform will remove this resource from the state file, however resources may remain.")
return nil
}

func convertEsaRatePlanInstanceInstanceInfoBillingModeResponse(source interface{}) interface{} {
source = fmt.Sprint(source)
switch source {
case "PREPAY":
return "Subscription"
case "POSTPAY":
return "PayAsYouGo"
}
return source
}
func convertEsaRatePlanInstanceChargeTypeRequest(source interface{}) interface{} {
source = fmt.Sprint(source)
switch source {
case "PayAsYouGo":
return "POSTPAY"
case "Subscription":
return "PREPAY"
}
return source
}
Loading

0 comments on commit 04dbe6d

Please sign in to comment.