Skip to content

Commit

Permalink
feat: new resource alicloud_ack_one_membership_attaching
Browse files Browse the repository at this point in the history
doc: add doc for alicloud_ack_one_membership_attaching

test: new resource alicloud_ack_one_membership_attaching
Signed-off-by: 宜松 <[email protected]>
  • Loading branch information
vie-serendipity committed Jan 16, 2025
1 parent 3b76ac7 commit 7378ccc
Show file tree
Hide file tree
Showing 5 changed files with 520 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 @@ -1049,6 +1049,7 @@ func Provider() terraform.ResourceProvider {
"alicloud_arms_environment": resourceAliCloudArmsEnvironment(),
"alicloud_hologram_instance": resourceAliCloudHologramInstance(),
"alicloud_ack_one_cluster": resourceAliCloudAckOneCluster(),
"alicloud_ack_one_membership_attachment": resourceAliCloudAckOneMembershipAttachment(),
"alicloud_drds_polardbx_instance": resourceAliCloudDrdsPolardbxInstance(),
"alicloud_gpdb_backup_policy": resourceAliCloudGpdbBackupPolicy(),
"alicloud_threat_detection_file_upload_limit": resourceAliCloudThreatDetectionFileUploadLimit(),
Expand Down
211 changes: 211 additions & 0 deletions alicloud/resource_alicloud_ack_one_membership_attaching.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
package alicloud

import (
"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 resourceAliCloudAckOneMembershipAttachment() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"cluster_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "ID of the ACK One fleet cluster",
},
"sub_cluster_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "ID of the ACK cluster that needs to be managed by ACK One fleet",
},
"managed_cluster_id": {
Type: schema.TypeString,
Computed: true,
Description: "ID of the managed ACK cluster, if null, the managed cluster is not attached",
},
"attach_to_mesh": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Description: "Whether to attach the managed cluster to the service mesh",
},
"detach_from_mesh": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Description: "Whether to detach the managed cluster from the service mesh",
},
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(25 * time.Minute),
Delete: schema.DefaultTimeout(25 * time.Minute),
},
Create: resourceAliCloudAckOneMembershipAttachmentCreate,
Read: resourceAliCloudAckOneMembershipAttachmentRead,
Delete: resourceAliCloudAckOneMembershipAttachmentDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
}
}

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

action := "AttachClusterToHub"
var request map[string]interface{}
var response map[string]interface{}
conn, err := client.NewAckoneClient()
if err != nil {
return WrapError(err)
}
request = make(map[string]interface{})
request["ClusterId"] = d.Get("cluster_id")
request["ClusterIds"] = "[\"" + d.Get("sub_cluster_id").(string) + "\"]"

if v, ok := d.GetOk("attach_to_mesh"); ok {
request["AttachToMesh"] = 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("2022-01-01"), StringPointer("AK"), nil, 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_ack_one_membership_attachment", action, AlibabaCloudSdkGoERROR)
}

managedClusterIds := response["ManagedClusterIds"].([]interface{})
if len(managedClusterIds) != 1 {
return WrapErrorf(err, DefaultErrorMsg, "alicloud_ack_one_membership_attachment", action, AlibabaCloudSdkGoERROR)
}

managedClusterId := managedClusterIds[0].(string)
d.SetId(response["ClusterId"].(string) + ":" + managedClusterId)
d.Set("managed_cluster_id", managedClusterId)

return nil
}

func resourceAliCloudAckOneMembershipAttachmentRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
action := "DescribeManagedClusters"
var request map[string]interface{}
var response map[string]interface{}
conn, err := client.NewAckoneClient()
if err != nil {
return WrapError(err)
}
request = make(map[string]interface{})
clusterId := strings.Split(d.Id(), ":")[0]
subClusterId := strings.Split(d.Id(), ":")[1]
d.Set("cluster_id", clusterId)
d.Set("sub_cluster_id", subClusterId)
request["ClusterId"] = clusterId

runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
err = resource.Retry(d.Timeout(schema.TimeoutRead), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2022-01-01"), StringPointer("AK"), nil, request, &runtime)

if err != nil {
if NeedRetry(err) {
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
addDebug(action, response, request)
return nil
})

if err != nil {
if NotFoundError(err) {
d.SetId("")
return nil
}
return WrapErrorf(err, DefaultErrorMsg, "alicloud_ack_one_membership_attachment", action, AlibabaCloudSdkGoERROR)
}

clusters := response["Clusters"].([]interface{})

found := false
for _, cluster := range clusters {
managedCluster := cluster.(map[string]interface{})
clusterId := managedCluster["Cluster"].(map[string]interface{})["ClusterID"].(string)
if clusterId == subClusterId {
found = true
break
}
}
if !found {
d.SetId("")
return nil
}

d.Set("managed_cluster_id", subClusterId)

return nil
}

func resourceAliCloudAckOneMembershipAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
action := "DetachClusterFromHub"
var request map[string]interface{}
var response map[string]interface{}
conn, err := client.NewAckoneClient()
if err != nil {
return WrapError(err)
}
request = make(map[string]interface{})
request["ClusterId"] = d.Get("cluster_id")
request["ClusterIds"] = "[\"" + d.Get("sub_cluster_id").(string) + "\"]"
if v, ok := d.GetOk("detach_from_mesh"); ok {
request["DetachFromMesh"] = v
}

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("2022-01-01"), StringPointer("AK"), nil, 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 {
if NotFoundError(err) {
return nil
}
return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR)
}

return nil
}
Loading

0 comments on commit 7378ccc

Please sign in to comment.