diff --git a/docs/data-sources/gaussdb_mysql_instance.md b/docs/data-sources/gaussdb_mysql_instance.md new file mode 100644 index 0000000000..a1efd02766 --- /dev/null +++ b/docs/data-sources/gaussdb_mysql_instance.md @@ -0,0 +1,82 @@ +--- +subcategory: "GaussDB" +--- + +# huaweicloud\_gaussdb\_mysql\_instance + +Use this data source to get available HuaweiCloud gaussdb mysql instance. + +## Example Usage + +```hcl +data "huaweicloud_gaussdb_mysql_instance" "this" { + name = "gaussdb-instance" +} +``` + +## Argument Reference + +* `region` - (Optional) The region in which to obtain the instance. If omitted, the provider-level region will be used. + +* `name` - (Optional) Specifies the name of the instance. + +* `vpc_id` - (Optional) Specifies the VPC ID. + +* `subnet_id` - (Optional) Specifies the network ID of a subnet. + + +## Attributes Reference + +* `id` - Indicates the ID of the instance. + +* `flavor` - Indicates the instance specifications. + +* `security_group_id` - Indicates the security group ID. Required if the selected subnet doesn't enable network ACL. + +* `configuration_id` - Indicates the configuration ID. + +* `enterprise_project_id` - Indicates the enterprise project id. + +* `read_replicas` - Indicates the count of read replicas. + +* `time_zone` - Indicates the time zone. + +* `availability_zone_mode` - Indicates the availability zone mode: "single" or "multi". + +* `master_availability_zone` - Indicates the availability zone where the master node resides. + +* `datastore` - Indicates the database information. Structure is documented below. + +* `backup_strategy` - Indicates the advanced backup policy. Structure is documented below. + +* `status` - Indicates the DB instance status. + +* `port` - Indicates the database port. + +* `mode` - Indicates the instance mode. + +* `db_user_name` - Indicates the default username. + +* `private_write_ip` - Indicates the private IP address of the DB instance. + +* `nodes` - Indicates the instance nodes information. Structure is documented below. + + +The `datastore` block supports: + +* `engine` - Indicates the database engine. +* `version` - Indicates the database version. + +The `backup_strategy` block supports: + +* `start_time` - Indicates the backup time window. +* `keep_days` - Indicates the number of days to retain the generated + +The `nodes` block contains: + +- `id` - Indicates the node ID. +- `name` - Indicates the node name. +- `type` - Indicates the node type: master or slave. +- `status` - Indicates the node status. +- `private_read_ip` - Indicates the private IP address of a node. +- `availability_zone` - Indicates the availability zone where the node resides. diff --git a/huaweicloud/data_source_huaweicloud_gaussdb_mysql_instance.go b/huaweicloud/data_source_huaweicloud_gaussdb_mysql_instance.go new file mode 100644 index 0000000000..0541dc574f --- /dev/null +++ b/huaweicloud/data_source_huaweicloud_gaussdb_mysql_instance.go @@ -0,0 +1,275 @@ +package huaweicloud + +import ( + "fmt" + "log" + "strconv" + + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + + "github.com/huaweicloud/golangsdk/openstack/taurusdb/v3/instances" +) + +func dataSourceGaussDBMysqlInstance() *schema.Resource { + return &schema.Resource{ + Read: dataSourceGaussDBMysqlInstanceRead, + + Schema: map[string]*schema.Schema{ + "region": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "name": { + Type: schema.TypeString, + Optional: true, + }, + "vpc_id": { + Type: schema.TypeString, + Optional: true, + }, + "subnet_id": { + Type: schema.TypeString, + Optional: true, + }, + "status": { + Type: schema.TypeString, + Computed: true, + }, + "mode": { + Type: schema.TypeString, + Computed: true, + }, + "security_group_id": { + Type: schema.TypeString, + Computed: true, + }, + "configuration_id": { + Type: schema.TypeString, + Computed: true, + }, + "enterprise_project_id": { + Type: schema.TypeString, + Computed: true, + }, + "db_user_name": { + Type: schema.TypeString, + Computed: true, + }, + "time_zone": { + Type: schema.TypeString, + Computed: true, + }, + "availability_zone_mode": { + Type: schema.TypeString, + Computed: true, + }, + "master_availability_zone": { + Type: schema.TypeString, + Computed: true, + }, + "port": { + Type: schema.TypeInt, + Computed: true, + }, + "private_write_ip": { + Type: schema.TypeString, + Computed: true, + }, + "datastore": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "engine": { + Type: schema.TypeString, + Computed: true, + }, + "version": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + "backup_strategy": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "start_time": { + Type: schema.TypeString, + Computed: true, + }, + "keep_days": { + Type: schema.TypeInt, + Computed: true, + }, + }, + }, + }, + "read_replicas": { + Type: schema.TypeInt, + Computed: true, + }, + "flavor": { + Type: schema.TypeString, + Computed: true, + }, + "nodes": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "id": { + Type: schema.TypeString, + Computed: true, + }, + "name": { + Type: schema.TypeString, + Computed: true, + }, + "type": { + Type: schema.TypeString, + Computed: true, + }, + "status": { + Type: schema.TypeString, + Computed: true, + }, + "private_read_ip": { + Type: schema.TypeString, + Computed: true, + }, + "availability_zone": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + }, + } +} + +func dataSourceGaussDBMysqlInstanceRead(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + region := GetRegion(d, config) + client, err := config.gaussdbV3Client(region) + if err != nil { + return fmt.Errorf("Error creating HuaweiCloud GaussDB client: %s", err) + } + + listOpts := instances.ListTaurusDBInstanceOpts{ + Name: d.Get("name").(string), + VpcId: d.Get("vpc_id").(string), + SubnetId: d.Get("subnet_id").(string), + } + + pages, err := instances.List(client, listOpts).AllPages() + if err != nil { + return err + } + + allInstances, err := instances.ExtractTaurusDBInstances(pages) + if err != nil { + return fmt.Errorf("Unable to retrieve instances: %s", err) + } + + if allInstances.TotalCount < 1 { + return fmt.Errorf("Your query returned no results. " + + "Please change your search criteria and try again.") + } + + if allInstances.TotalCount > 1 { + return fmt.Errorf("Your query returned more than one result." + + " Please try a more specific search criteria") + } + + instanceID := allInstances.Instances[0].Id + instance, err := instances.Get(client, instanceID).Extract() + if err != nil { + return err + } + + log.Printf("[DEBUG] Retrieved Instance %s: %+v", instance.Id, instance) + d.SetId(instance.Id) + + d.Set("region", region) + d.Set("name", instance.Name) + d.Set("status", instance.Status) + d.Set("mode", instance.Type) + d.Set("vpc_id", instance.VpcId) + d.Set("subnet_id", instance.SubnetId) + d.Set("security_group_id", instance.SecurityGroupId) + d.Set("configuration_id", instance.ConfigurationId) + d.Set("enterprise_project_id", instance.EnterpriseProjectId) + d.Set("db_user_name", instance.DbUserName) + d.Set("time_zone", instance.TimeZone) + d.Set("availability_zone_mode", instance.AZMode) + d.Set("master_availability_zone", instance.MasterAZ) + + if dbPort, err := strconv.Atoi(instance.Port); err == nil { + d.Set("port", dbPort) + } + if len(instance.PrivateIps) > 0 { + d.Set("private_write_ip", instance.PrivateIps[0]) + } + + // set data store + dbList := make([]map[string]interface{}, 1) + db := map[string]interface{}{ + "version": instance.DataStore.Version, + } + // normalize engine + engine := instance.DataStore.Type + if engine == "GaussDB(for MySQL)" { + engine = "gaussdb-mysql" + } + db["engine"] = engine + dbList[0] = db + d.Set("datastore", dbList) + + // set nodes + flavor := "" + slave_count := 0 + nodesList := make([]map[string]interface{}, 0, 1) + for _, raw := range instance.Nodes { + node := map[string]interface{}{ + "id": raw.Id, + "name": raw.Name, + "status": raw.Status, + "type": raw.Type, + "availability_zone": raw.AvailabilityZone, + } + if len(raw.PrivateIps) > 0 { + node["private_read_ip"] = raw.PrivateIps[0] + } + nodesList = append(nodesList, node) + if raw.Type == "slave" && raw.Status == "ACTIVE" { + slave_count += 1 + } + if flavor == "" { + flavor = raw.Flavor + } + } + d.Set("nodes", nodesList) + d.Set("read_replicas", slave_count) + if flavor != "" { + log.Printf("[DEBUG] Node Flavor: %s", flavor) + d.Set("flavor", flavor) + } + + // set backup_strategy + backupStrategyList := make([]map[string]interface{}, 1) + backupStrategy := map[string]interface{}{ + "start_time": instance.BackupStrategy.StartTime, + } + if days, err := strconv.Atoi(instance.BackupStrategy.KeepDays); err == nil { + backupStrategy["keep_days"] = days + } + backupStrategyList[0] = backupStrategy + d.Set("backup_strategy", backupStrategyList) + + return nil +} diff --git a/huaweicloud/data_source_huaweicloud_gaussdb_mysql_instance_test.go b/huaweicloud/data_source_huaweicloud_gaussdb_mysql_instance_test.go new file mode 100644 index 0000000000..2c288ea23f --- /dev/null +++ b/huaweicloud/data_source_huaweicloud_gaussdb_mysql_instance_test.go @@ -0,0 +1,68 @@ +package huaweicloud + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/terraform" +) + +func TestAccGaussdbMysqlInstanceDataSource_basic(t *testing.T) { + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(5)) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccGaussdbMysqlInstanceDataSource_basic(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckGaussdbMysqlInstanceDataSourceID("data.huaweicloud_gaussdb_mysql_instance.test"), + ), + }, + }, + }) +} + +func testAccCheckGaussdbMysqlInstanceDataSourceID(n string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Can't find GaussDB mysql instance data source: %s ", n) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("GaussDB mysql instance data source ID not set ") + } + + return nil + } +} + +func testAccGaussdbMysqlInstanceDataSource_basic(rName string) string { + return fmt.Sprintf(` +%s + +data "huaweicloud_availability_zones" "test" {} + +data "huaweicloud_networking_secgroup" "test" { + name = "default" +} + +resource "huaweicloud_gaussdb_mysql_instance" "test" { + name = "%s" + password = "Test@123" + flavor = "gaussdb.mysql.4xlarge.x86.4" + vpc_id = huaweicloud_vpc_v1.test.id + subnet_id = huaweicloud_vpc_subnet_v1.test.id + + security_group_id = data.huaweicloud_networking_secgroup.test.id +} + +data "huaweicloud_gaussdb_mysql_instance" "test" { + name = huaweicloud_gaussdb_mysql_instance.test.name +} +`, testAccVpcConfig_Base(rName), rName) +} diff --git a/huaweicloud/provider.go b/huaweicloud/provider.go index 0385b6469a..ce4dc5ca12 100644 --- a/huaweicloud/provider.go +++ b/huaweicloud/provider.go @@ -212,6 +212,7 @@ func Provider() terraform.ResourceProvider { "huaweicloud_enterprise_project": DataSourceEnterpriseProject(), "huaweicloud_gaussdb_mysql_configuration": dataSourceGaussdbMysqlConfigurations(), "huaweicloud_gaussdb_mysql_flavors": dataSourceGaussdbMysqlFlavors(), + "huaweicloud_gaussdb_mysql_instance": dataSourceGaussDBMysqlInstance(), "huaweicloud_iam_role": dataSourceIAMRoleV3(), "huaweicloud_identity_role": DataSourceIdentityRoleV3(), "huaweicloud_images_image": dataSourceImagesImageV2(), diff --git a/huaweicloud/resource_huaweicloud_gaussdb_mysql_instance_test.go b/huaweicloud/resource_huaweicloud_gaussdb_mysql_instance_test.go index 5624c41d65..ef34eeabea 100644 --- a/huaweicloud/resource_huaweicloud_gaussdb_mysql_instance_test.go +++ b/huaweicloud/resource_huaweicloud_gaussdb_mysql_instance_test.go @@ -89,7 +89,7 @@ func testAccGaussDBInstanceConfig_basic(rName string) string { data "huaweicloud_availability_zones" "test" {} -data "huaweicloud_networking_secgroup_v2" "test" { +data "huaweicloud_networking_secgroup" "test" { name = "default" } @@ -100,7 +100,7 @@ resource "huaweicloud_gaussdb_mysql_instance" "test" { vpc_id = huaweicloud_vpc_v1.test.id subnet_id = huaweicloud_vpc_subnet_v1.test.id - security_group_id = data.huaweicloud_networking_secgroup_v2.test.id + security_group_id = data.huaweicloud_networking_secgroup.test.id enterprise_project_id = "0" }