diff --git a/docs/data-sources/dds_flavors_v3.md b/docs/data-sources/dds_flavors_v3.md index 3c8454ac9..fc4311575 100644 --- a/docs/data-sources/dds_flavors_v3.md +++ b/docs/data-sources/dds_flavors_v3.md @@ -9,7 +9,7 @@ Use this data source to get info of available OpenTelekomCloud DDS flavors. ## Example Usage ```hcl -data "opentelekomcloud_dds_flavors" "flavor" { +data "opentelekomcloud_dds_flavors_v3" "flavor" { engine_name = "DDS-Community" vcpus = 8 } diff --git a/docs/data-sources/dds_instance_v3.md b/docs/data-sources/dds_instance_v3.md new file mode 100644 index 000000000..f366fc20e --- /dev/null +++ b/docs/data-sources/dds_instance_v3.md @@ -0,0 +1,91 @@ +--- +subcategory: "Document Database Service (DDS)" +--- + +# opentelekomcloud_dds_instance_v3 + +Use this data source to get info of the OpenTelekomCloud DDS instance. + +## Example Usage + +```hcl +variable "instance_id" { } + +data "opentelekomcloud_dds_instance_v3" "instance" { + instance_id = var.instance_id +} +``` + + +## Argument Reference + +The following arguments are supported: + +* `instance_id` - (Optional) Specifies the DB instance ID. + +* `name` - (Optional) Specifies the DB instance name. + +* `datastore_type` - (Optional) Specifies the database type. The value is `DDS-Community`. + +* `vpc_id` - (Optional) Specifies the VPC ID. You can log in to the VPC console and + obtain the ID of the VPC where the DDS instance is located. + +* `subnet_id` - (Optional) Specifies the network ID of the subnet. You can log in to + the VPC console and obtain the network ID of the subnet in the VPC where the DDS + instance is located. + + +## Attributes Reference + +The following attributes are exported: + +* `id` - Indicates the DB instance ID. + +* `region` - Indicates the region where the DB instance is deployed. + +* `name` - Indicates the DB instance name. + +* `availability_zone` - Indicates the AZ. + +* `vpc_id` - Indicates the VPC ID. + +* `subnet_id` - Indicates the subnet ID. + +* `security_group_id` - Indicates the security group ID. + +* `disk_encryption_id` - Indicates the disk encryption key ID. This parameter is returned + only when the instance disk is encrypted. + +* `mode` - Indicates the instance type, which is the same as the request parameter. + +* `db_username` - Indicates the default username. + +* `status` - Indicates the DB instance status. + +* `ssl` - Indicates that SSL is enabled or not. + +* `datastore/type` - Indicates the DB engine. + +* `datastore/version` - Indicates the database version. + +* `datastore/storage_engine` - Specifies the storage engine. + +* `backup_strategy/start_time` - Indicates the backup time window. Automated backups will + be triggered during the backup time window. The current time is the UTC time. + +* `backup_strategy/keep_days` - Indicates the number of days to retain the generated backup + files. The value range is from 0 to 732. + +* `nodes/id` - Indicates the node ID. + +* `nodes/name` - Indicates the node name. + +* `nodes/role` - Indicates the node role. + +* `nodes/type` - Indicates the node type. + +* `nodes/private_ip` - Indicates the private IP address of a node. + +* `nodes/public_ip` - Indicates the EIP that has been bound on a node. + +* `nodes/status` - Indicates the node status. diff --git a/opentelekomcloud/data_source_opentelekomcloud_dds_instance_v3.go b/opentelekomcloud/data_source_opentelekomcloud_dds_instance_v3.go new file mode 100644 index 000000000..c4cd2cf4a --- /dev/null +++ b/opentelekomcloud/data_source_opentelekomcloud_dds_instance_v3.go @@ -0,0 +1,233 @@ +package opentelekomcloud + +import ( + "fmt" + + "github.com/hashicorp/go-multierror" + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + "github.com/opentelekomcloud/gophertelekomcloud/openstack/dds/v3/instances" +) + +func dataSourceDdsInstanceV3() *schema.Resource { + return &schema.Resource{ + Read: dataSourceDdsInstanceV3Read, + + Schema: map[string]*schema.Schema{ + "instance_id": { + Type: schema.TypeString, + Optional: true, + }, + "region": { + Type: schema.TypeString, + Computed: true, + }, + "name": { + Type: schema.TypeString, + Optional: true, + }, + "datastore_type": { + Type: schema.TypeString, + Optional: true, + }, + "datastore": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "type": { + Type: schema.TypeString, + Computed: true, + }, + "version": { + Type: schema.TypeString, + Computed: true, + }, + "storage_engine": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + "vpc_id": { + Type: schema.TypeString, + Optional: true, + }, + "subnet_id": { + Type: schema.TypeString, + Optional: true, + }, + "security_group_id": { + Type: schema.TypeString, + Computed: true, + }, + "disk_encryption_id": { + Type: schema.TypeString, + Computed: true, + }, + "mode": { + Type: schema.TypeString, + Computed: true, + }, + "backup_strategy": { + Type: schema.TypeList, + Computed: true, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "start_time": { + Type: schema.TypeString, + Computed: true, + }, + "keep_days": { + Type: schema.TypeInt, + Computed: true, + }, + }, + }, + }, + "ssl": { + Type: schema.TypeBool, + Computed: true, + }, + "db_username": { + Type: schema.TypeString, + Computed: true, + }, + "status": { + Type: schema.TypeString, + Computed: true, + }, + "port": { + Type: schema.TypeInt, + Computed: true, + }, + "pay_mode": { + 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, + }, + "role": { + Type: schema.TypeString, + Computed: true, + }, + "private_ip": { + Type: schema.TypeString, + Computed: true, + }, + "public_ip": { + Type: schema.TypeString, + Computed: true, + }, + "status": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + }, + } +} + +func dataSourceDdsInstanceV3Read(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + ddsClient, err := config.ddsV3Client(GetRegion(d, config)) + if err != nil { + return fmt.Errorf("error creating OpenTelekomCloud DDS client: %s", err) + } + + listOpts := instances.ListInstanceOpts{ + Id: d.Get("instance_id").(string), + Name: d.Get("name").(string), + DataStoreType: d.Get("datastore_type").(string), + VpcId: d.Get("vpc_id").(string), + SubnetId: d.Get("subnet_id").(string), + } + + allPages, err := instances.List(ddsClient, listOpts).AllPages() + if err != nil { + return fmt.Errorf("error fetching DDS instance: %s", err) + } + instancesList, err := instances.ExtractInstances(allPages) + if err != nil { + return fmt.Errorf("error extracting DDS instance: %s", err) + } + if len(instancesList.Instances) < 0 { + return fmt.Errorf("your query returned no results. Please change your search criteria and try again") + } + + if len(instancesList.Instances) > 1 { + return fmt.Errorf("your query returned more than one result. Please try a more specific search criteria") + } + + ddsInstance := instancesList.Instances[0] + + d.SetId(ddsInstance.Id) + mErr := multierror.Append(nil, + d.Set("instance_id", ddsInstance.Id), + d.Set("name", ddsInstance.Name), + d.Set("region", GetRegion(d, config)), + d.Set("vpc_id", ddsInstance.VpcId), + d.Set("subnet_id", ddsInstance.SubnetId), + d.Set("security_group_id", ddsInstance.SecurityGroupId), + d.Set("disk_encryption_id", ddsInstance.DiskEncryptionId), + d.Set("mode", ddsInstance.Mode), + d.Set("db_username", ddsInstance.DbUserName), + d.Set("status", ddsInstance.Status), + d.Set("port", ddsInstance.Port), + d.Set("pay_mode", ddsInstance.PayMode), + d.Set("datastore_type", ddsInstance.DataStore.Type), + ) + sslEnable := true + if ddsInstance.Ssl == 0 { + sslEnable = false + } + mErr = multierror.Append( + mErr, + d.Set("ssl", sslEnable), + ) + datastoreList := make([]map[string]interface{}, 0, 1) + datastore := map[string]interface{}{ + "type": ddsInstance.DataStore.Type, + "version": ddsInstance.DataStore.Version, + "storage_engine": ddsInstance.Engine, + } + datastoreList = append(datastoreList, datastore) + if err = d.Set("datastore", datastoreList); err != nil { + return fmt.Errorf("error setting DDSv3 datastore opts: %s", err) + } + + backupStrategyList := make([]map[string]interface{}, 0, 1) + backupStrategy := map[string]interface{}{ + "start_time": ddsInstance.BackupStrategy.StartTime, + "keep_days": ddsInstance.BackupStrategy.KeepDays, + } + backupStrategyList = append(backupStrategyList, backupStrategy) + if err = d.Set("backup_strategy", backupStrategyList); err != nil { + return fmt.Errorf("error setting DDSv3 backup_strategy opts: %s", err) + } + + err = d.Set("nodes", flattenDdsInstanceV3Nodes(ddsInstance)) + if err != nil { + return fmt.Errorf("error setting nodes of DDSv3 instance: %s", err) + } + + return mErr.ErrorOrNil() +} diff --git a/opentelekomcloud/data_source_opentelekomcloud_dds_instance_v3_test.go b/opentelekomcloud/data_source_opentelekomcloud_dds_instance_v3_test.go new file mode 100644 index 000000000..af56774f5 --- /dev/null +++ b/opentelekomcloud/data_source_opentelekomcloud_dds_instance_v3_test.go @@ -0,0 +1,73 @@ +package opentelekomcloud + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/terraform" +) + +func TestAccDDSInstanceV3DataSource_basic(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDDSInstanceV3DataSource_basic, + Check: resource.ComposeTestCheckFunc( + testAccCheckDDSInstanceV3DataSourceID("data.opentelekomcloud_dds_instance_v3.instances"), + resource.TestCheckResourceAttr("data.opentelekomcloud_dds_instance_v3.instances", "name", "dds-instance"), + resource.TestCheckResourceAttr("data.opentelekomcloud_dds_instance_v3.instances", "vpc_id", OS_VPC_ID), + resource.TestCheckResourceAttr("data.opentelekomcloud_dds_instance_v3.instances", "mode", "ReplicaSet"), + resource.TestCheckResourceAttr("data.opentelekomcloud_dds_instance_v3.instances", "datastore.0.type", "DDS-Community"), + ), + }, + }, + }) +} + +func testAccCheckDDSInstanceV3DataSourceID(n string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Can't find instances data source: %s ", n) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("Node data source ID not set ") + } + + return nil + } +} + +var testAccDDSInstanceV3DataSource_basic = fmt.Sprintf(` +resource "opentelekomcloud_networking_secgroup_v2" "sg_acc" { + name = "secgroup_acc" +} +resource "opentelekomcloud_dds_instance_v3" "instance_1" { + name = "dds-instance" + availability_zone = "%s" + datastore { + type = "DDS-Community" + version = "3.4" + storage_engine = "wiredTiger" + } + vpc_id = "%s" + subnet_id = "%s" + security_group_id = opentelekomcloud_networking_secgroup_v2.sg_acc.id + password = "5ecuredPa55w0rd@" + mode = "ReplicaSet" + flavor { + type = "replica" + num = 1 + size = 20 + spec_code = "dds.mongodb.s2.medium.4.repset" + } +} + +data "opentelekomcloud_dds_instance_v3" "instances" { + instance_id = opentelekomcloud_dds_instance_v3.instance_1.id +} +`, OS_AVAILABILITY_ZONE, OS_VPC_ID, OS_NETWORK_ID) diff --git a/opentelekomcloud/provider.go b/opentelekomcloud/provider.go index b88671873..8fa9112dd 100644 --- a/opentelekomcloud/provider.go +++ b/opentelekomcloud/provider.go @@ -242,6 +242,7 @@ func Provider() terraform.ResourceProvider { "opentelekomcloud_dcs_maintainwindow_v1": dataSourceDcsMaintainWindowV1(), "opentelekomcloud_dcs_product_v1": dataSourceDcsProductV1(), "opentelekomcloud_dds_flavors_v3": dataSourceDdsFlavorV3(), + "opentelekomcloud_dds_instance_v3": dataSourceDdsInstanceV3(), "opentelekomcloud_dms_az_v1": dataSourceDmsAZV1(), "opentelekomcloud_dms_product_v1": dataSourceDmsProductV1(), "opentelekomcloud_dms_maintainwindow_v1": dataSourceDmsMaintainWindowV1(),