Skip to content

Commit

Permalink
feat: modify resource ackonecluster to support update
Browse files Browse the repository at this point in the history
Signed-off-by: 宜松 <zzy405810@alibaba-inc.com>
  • Loading branch information
vie-serendipity committed Jan 22, 2025
1 parent d3543a5 commit de08df2
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 4 deletions.
100 changes: 99 additions & 1 deletion alicloud/resource_alicloud_ack_one_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ func resourceAliCloudAckOneCluster() *schema.Resource {
return &schema.Resource{
Create: resourceAliCloudAckOneClusterCreate,
Read: resourceAliCloudAckOneClusterRead,
Update: resourceAliCloudAckOneClusterUpdate,
Delete: resourceAliCloudAckOneClusterDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(25 * time.Minute),
Delete: schema.DefaultTimeout(25 * time.Minute),
Update: schema.DefaultTimeout(25 * time.Minute),
},
Schema: map[string]*schema.Schema{
"cluster_name": {
Expand All @@ -36,6 +38,11 @@ func resourceAliCloudAckOneCluster() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"argocd_enabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"network": {
Type: schema.TypeList,
Required: true,
Expand Down Expand Up @@ -78,6 +85,10 @@ func resourceAliCloudAckOneCluster() *schema.Resource {
}

func resourceAliCloudAckOneClusterCreate(d *schema.ResourceData, meta interface{}) error {
// Set argocd_enabled to nil when profile is XFlow
if profile, ok := d.GetOk("profile"); ok && profile.(string) == "XFlow" {
d.Set("argocd_enabled", nil)
}

client := meta.(*connectivity.AliyunClient)

Expand All @@ -101,8 +112,10 @@ func resourceAliCloudAckOneClusterCreate(d *schema.ResourceData, meta interface{
request["VSwitches"] = convertListToJsonString(jsonPathResult1.([]interface{}))
}

profile := "Default"
if v, ok := d.GetOk("profile"); ok {
request["Profile"] = v
profile = v.(string)
}
if v, ok := d.GetOk("cluster_name"); ok {
request["Name"] = v
Expand Down Expand Up @@ -136,10 +149,25 @@ func resourceAliCloudAckOneClusterCreate(d *schema.ResourceData, meta interface{
return WrapErrorf(err, IdMsg, d.Id())
}

// Wait until argocd_enabled attribute is available
if profile == "Default" {
stateConf = BuildStateConf([]string{"<nil>"}, []string{"true", "false"}, d.Timeout(schema.TimeoutCreate), 5*time.Second, ackOneServiceV2.AckOneClusterStateRefreshFunc(d.Id(), "$.ClusterInfo.MetaData.ACKOne.GitOps.Enabled", []string{}))
if _, err := stateConf.WaitForState(); err != nil {
return WrapErrorf(err, "Failed to get argocd_enabled attribute after cluster creation")
}

if v, ok := d.GetOkExists("argocd_enabled"); ok && !v.(bool) {
if err := resourceAliCloudAckOneClusterUpdate(d, meta); err != nil {
return WrapErrorf(err, "Failed to set argocd_enabled correctly when creating cluster")
}
}
}

return resourceAliCloudAckOneClusterRead(d, meta)
}

func resourceAliCloudAckOneClusterRead(d *schema.ResourceData, meta interface{}) error {
// Set argocd_enabled to nil when profile is XFlow
client := meta.(*connectivity.AliyunClient)
ackOneServiceV2 := AckOneServiceV2{client}

Expand Down Expand Up @@ -187,9 +215,79 @@ func resourceAliCloudAckOneClusterRead(d *schema.ResourceData, meta interface{})
}
d.Set("network", networkMaps)

if d.Get("profile") == "Default" {
argocdEnabled, err := jsonpath.Get("$.ClusterInfo.MetaData.ACKOne.GitOps.Enabled", objectRaw)
if err != nil {
d.Set("argocd_enabled", false)
} else {
if v, ok := argocdEnabled.(bool); ok {
d.Set("argocd_enabled", v)
} else {
d.Set("argocd_enabled", false)
}
}
} else {
d.Set("argocd_enabled", nil)
}
return nil
}

func resourceAliCloudAckOneClusterUpdate(d *schema.ResourceData, meta interface{}) error {
// Set argocd_enabled to nil when profile is XFlow
if profile, ok := d.GetOk("profile"); ok && profile.(string) == "XFlow" {
d.Set("argocd_enabled", nil)
}

client := meta.(*connectivity.AliyunClient)
action := "UpdateHubClusterFeature"
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.Id()
if d.Get("profile") == "Default" {
if v, ok := d.GetOkExists("argocd_enabled"); ok {
request["ArgoCDEnabled"] = v.(bool)
} else {
request["ArgoCDEnabled"] = true
}
} else {
d.Set("argocd_enabled", nil)
}

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("2022-01-01"), StringPointer("AK"), nil, 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_ack_one_cluster", action, AlibabaCloudSdkGoERROR)
}

ackOneServiceV2 := AckOneServiceV2{client}
stateConf := BuildStateConf([]string{}, []string{"running"}, d.Timeout(schema.TimeoutUpdate), 5*time.Second, ackOneServiceV2.AckOneClusterStateRefreshFunc(d.Id(), "$.ClusterInfo.State", []string{}))
if _, err := stateConf.WaitForState(); err != nil {
return WrapErrorf(err, IdMsg, d.Id())
}

return resourceAliCloudAckOneClusterRead(d, meta)
}

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

client := meta.(*connectivity.AliyunClient)
Expand All @@ -216,9 +314,9 @@ func resourceAliCloudAckOneClusterDelete(d *schema.ResourceData, meta interface{
}
return resource.NonRetryableError(err)
}
addDebug(action, response, request)
return nil
})
addDebug(action, response, request)

if err != nil {
if NotFoundError(err) {
Expand Down
76 changes: 73 additions & 3 deletions alicloud/resource_alicloud_ack_one_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,79 @@ func TestAccAliCloudAckOneCluster_basic4593_twin(t *testing.T) {
})
}

func TestAccAliCloudAckOneCluster_updateArgocdEnabled(t *testing.T) {
var v map[string]interface{}
resourceId := "alicloud_ack_one_cluster.default"
ra := resourceAttrInit(resourceId, AlicloudAckOneClusterMap4593)
rc := resourceCheckInitWithDescribeMethod(resourceId, &v, func() interface{} {
return &AckOneServiceV2{testAccProvider.Meta().(*connectivity.AliyunClient)}
}, "DescribeAckOneCluster")
rac := resourceAttrCheckInit(rc, ra)
testAccCheck := rac.resourceAttrMapUpdateSet()
rand := acctest.RandIntRange(10000, 99999)
name := fmt.Sprintf("tf-testacc%sackonecluster%d", defaultRegionToTest, rand)
testAccConfig := resourceTestAccConfigFunc(resourceId, name, AlicloudAckOneClusterBasicDependence4593)
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckWithRegions(t, true, connectivity.AckOneSupportRegions)
},
IDRefreshName: resourceId,
Providers: testAccProviders,
CheckDestroy: rac.checkResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testAccConfig(map[string]interface{}{
"cluster_name": name,
"network": []map[string]interface{}{
{
"vpc_id": "${alicloud_vpc.defaultVpc.id}",
"vswitches": []string{
"${alicloud_vswitch.defaultyVSwitch.id}"},
},
},
"profile": "Default",
"argocd_enabled": true,
}),
Check: resource.ComposeTestCheckFunc(
testAccCheck(map[string]string{
"cluster_name": CHECKSET,
"profile": "Default",
"argocd_enabled": "true",
}),
),
},
{
Config: testAccConfig(map[string]interface{}{
"cluster_name": name,
"network": []map[string]interface{}{
{
"vpc_id": "${alicloud_vpc.defaultVpc.id}",
"vswitches": []string{
"${alicloud_vswitch.defaultyVSwitch.id}"},
},
},
"profile": "Default",
"argocd_enabled": false,
}),
Check: resource.ComposeTestCheckFunc(
testAccCheck(map[string]string{
"cluster_name": CHECKSET,
"profile": "Default",
"argocd_enabled": "false",
}),
),
},
{
ResourceName: resourceId,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{},
},
},
})
}

var AlicloudAckOneClusterMap4593 = map[string]string{
"status": CHECKSET,
"create_time": CHECKSET,
Expand All @@ -176,18 +249,15 @@ data "alicloud_zones" "default" {
resource "alicloud_vpc" "defaultVpc" {
cidr_block = "172.16.0.0/12"
vpc_name = var.name
}
resource "alicloud_vswitch" "defaultyVSwitch" {
vpc_id = alicloud_vpc.defaultVpc.id
cidr_block = "172.16.2.0/24"
zone_id = data.alicloud_zones.default.zones.0.id
vswitch_name = var.name
}
`, name)
}

Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/ack_one_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ The following arguments are supported:
* `cluster_name` - (Optional, ForceNew, Computed) Cluster name.
* `network` - (Required, ForceNew) Cluster network information. See [`network`](#network) below.
* `profile` - (Optional, ForceNew, Computed) Cluster attributes. Valid values: 'Default', 'XFlow'.
* `argocd_enabled` - (Optional) Whether to enable ArgoCD. Default to true. Only valid when `profile` is 'Default'.

### `network`

Expand All @@ -81,6 +82,7 @@ The following attributes are exported:
* `network` - Cluster network information.
* `security_group_ids` - Security group to which the cluster belongs.
* `status` - The status of the resource.
* `argocd_enabled` - Whether to enable ArgoCD.

## Timeouts

Expand Down

0 comments on commit de08df2

Please sign in to comment.