diff --git a/alicloud/resource_alicloud_click_house_db_cluster.go b/alicloud/resource_alicloud_click_house_db_cluster.go index eb153134fd41..b267994c8afb 100644 --- a/alicloud/resource_alicloud_click_house_db_cluster.go +++ b/alicloud/resource_alicloud_click_house_db_cluster.go @@ -185,6 +185,14 @@ func resourceAlicloudClickHouseDbCluster() *schema.Resource { Type: schema.TypeString, Computed: true, }, + "open_public_connection": { + Type: schema.TypeBool, + Optional: true, + }, + "public_connection_string": { + Type: schema.TypeString, + Computed: true, + }, }, } } @@ -351,6 +359,12 @@ func resourceAlicloudClickHouseDbClusterRead(d *schema.ResourceData, meta interf d.Set("vpc_id", object["VpcId"]) d.Set("connection_string", object["ConnectionString"]) d.Set("port", object["Port"]) + d.Set("public_connection_string", object["PublicConnectionString"]) + if object["PublicConnectionString"].(string) == "" { + d.Set("open_public_connection", false) + } else { + d.Set("open_public_connection", true) + } if ZoneIdVswitchMap, ok := object["ZoneIdVswitchMap"]; ok { vMap := ZoneIdVswitchMap.(map[string]interface{}) @@ -669,6 +683,67 @@ func resourceAlicloudClickHouseDbClusterUpdate(d *schema.ResourceData, meta inte d.SetPartial("renewal_status") } } + if d.HasChange("open_public_connection") && !d.IsNewResource() { + openPublicConnection := d.Get("open_public_connection").(bool) + if openPublicConnection { + action := "AllocateClusterPublicConnection" + request := map[string]interface{}{ + "DBClusterId": d.Id(), + "RegionId": client.RegionId, + } + wait := incrementalWait(3*time.Second, 3*time.Second) + err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError { + response, err = client.RpcPost("clickhouse", "2019-11-11", action, nil, request, false) + if err != nil { + if IsExpectedErrors(err, []string{"IncorrectDBInstanceState"}) || 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) + } + clickhouseService := ClickhouseService{client} + stateConf := BuildStateConf([]string{"NetAddressCreating"}, []string{"Running"}, d.Timeout(schema.TimeoutCreate), 5*time.Second, clickhouseService.ClickHouseDbClusterStateRefreshFunc(d.Id(), []string{})) + if _, err := stateConf.WaitForState(); err != nil { + return WrapErrorf(err, IdMsg, d.Id()) + } + d.SetPartial("open_public_connection") + } else { + action := "ReleaseClusterPublicConnection" + //NetAddressDeleting + request := map[string]interface{}{ + "DBClusterId": d.Id(), + "RegionId": client.RegionId, + } + wait := incrementalWait(3*time.Second, 3*time.Second) + err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError { + response, err = client.RpcPost("clickhouse", "2019-11-11", action, nil, request, false) + if err != nil { + if IsExpectedErrors(err, []string{"IncorrectDBInstanceState"}) || 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) + } + clickhouseService := ClickhouseService{client} + stateConf := BuildStateConf([]string{"NetAddressDeleting"}, []string{"Running"}, d.Timeout(schema.TimeoutCreate), 5*time.Second, clickhouseService.ClickHouseDbClusterStateRefreshFunc(d.Id(), []string{})) + if _, err := stateConf.WaitForState(); err != nil { + return WrapErrorf(err, IdMsg, d.Id()) + } + d.SetPartial("open_public_connection") + } + } d.Partial(false) return resourceAlicloudClickHouseDbClusterRead(d, meta) diff --git a/alicloud/resource_alicloud_click_house_db_cluster_test.go b/alicloud/resource_alicloud_click_house_db_cluster_test.go index 46776d8c27d6..e7fa89552902 100644 --- a/alicloud/resource_alicloud_click_house_db_cluster_test.go +++ b/alicloud/resource_alicloud_click_house_db_cluster_test.go @@ -798,6 +798,84 @@ func TestAccAliCloudClickHouseDBCluster_multiZone(t *testing.T) { }) } +func TestAccAliCloudClickHouseDBCluster_OpenPublicConnection(t *testing.T) { + checkoutSupportedRegions(t, true, connectivity.VbrSupportRegions) + var v map[string]interface{} + resourceId := "alicloud_click_house_db_cluster.default" + ra := resourceAttrInit(resourceId, AliCloudClickHouseDBClusterMap0) + rc := resourceCheckInitWithDescribeMethod(resourceId, &v, func() interface{} { + return &ClickhouseService{testAccProvider.Meta().(*connectivity.AliyunClient)} + }, "DescribeClickHouseDbCluster") + rac := resourceAttrCheckInit(rc, ra) + testAccCheck := rac.resourceAttrMapUpdateSet() + rand := acctest.RandIntRange(10000, 99999) + name := fmt.Sprintf("tf-testacc%sclickhousedbcluster%d", defaultRegionToTest, rand) + testAccConfig := resourceTestAccConfigFunc(resourceId, name, AliCloudClickHouseDBClusterBasicDependence0) + resource.Test(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheck(t) + }, + IDRefreshName: resourceId, + Providers: testAccProviders, + CheckDestroy: rac.checkResourceDestroy(), + Steps: []resource.TestStep{ + { + Config: testAccConfig(map[string]interface{}{ + "db_cluster_version": "23.8", + "category": "Basic", + "db_cluster_class": "S8", + "db_cluster_network_type": "vpc", + "db_node_group_count": "1", + "payment_type": "PayAsYouGo", + "db_node_storage": "100", + "storage_type": "cloud_essd", + "vpc_id": "${alicloud_vswitch.default.vpc_id}", + "vswitch_id": "${alicloud_vswitch.default.id}", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "db_cluster_version": "23.8", + "category": "Basic", + "db_cluster_class": "S8", + "db_cluster_network_type": "vpc", + "db_node_group_count": "1", + "payment_type": "PayAsYouGo", + "db_node_storage": "100", + "storage_type": "cloud_essd", + "vswitch_id": CHECKSET, + "vpc_id": CHECKSET, + }), + ), + }, { + Config: testAccConfig(map[string]interface{}{ + "open_public_connection": true, + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "open_public_connection": "true", + "public_connection_string": CHECKSET, + }), + ), + }, { + Config: testAccConfig(map[string]interface{}{ + "open_public_connection": false, + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "open_public_connection": "false", + "public_connection_string": "", + }), + ), + }, + { + ResourceName: resourceId, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + var AliCloudClickHouseDBClusterMap0 = map[string]string{ "db_cluster_version": CHECKSET, "category": CHECKSET, diff --git a/website/docs/r/click_house_db_cluster.html.markdown b/website/docs/r/click_house_db_cluster.html.markdown index e5e5ad9a415b..5095ebcf11e6 100644 --- a/website/docs/r/click_house_db_cluster.html.markdown +++ b/website/docs/r/click_house_db_cluster.html.markdown @@ -96,6 +96,9 @@ The following arguments are supported: * `zone_id` - (Optional, ForceNew, Available since v1.185.0) The zone ID of the instance. * `multi_zone_vswitch_list` - (Optional, ForceNew, Available since v1.228.0) The zone IDs and corresponding vswitch IDs and zone IDs of multi-zone setup. if set, a multi-zone DBCluster will be created. Currently only support 2 available zones, primary zone not included. See [`multi_zone_vswitch_list`](#multi_zone_vswitch_list) below. +* `open_public_connection` - (Optional, Available since v1.244.0) Whether to enable public connection. Value options: `true`, `false`. +* `public_connection_string` - (Available since v1.244.0) The public connection string of the cluster. Only valid when `open_public_connection` is `true`. + ### `db_cluster_access_white_list`