-
Notifications
You must be signed in to change notification settings - Fork 561
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new resource alicloud_ack_one_membership_attaching
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
1 parent
3b76ac7
commit 7378ccc
Showing
5 changed files
with
520 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
211 changes: 211 additions & 0 deletions
211
alicloud/resource_alicloud_ack_one_membership_attaching.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.