Skip to content

Commit

Permalink
[Feat.] DDM: Add data_source_ddm_instance_v1 (#2709)
Browse files Browse the repository at this point in the history
[Feat.] DDM: Add data_source_ddm_instance_v1

Summary of the Pull Request
Add new data source: opentelekomcloud_ddm_instance_v1
PR Checklist

 Refers to: #2573
 Tests added/passed.
 Documentation updated.
 Schema updated.
 Release notes added.

Acceptance Steps Performed
=== RUN   TestAccDDMInstanceV1DataSource_basic
--- PASS: TestAccDDMInstanceV1DataSource_basic (343.55s)
PASS

Reviewed-by: Aloento
Reviewed-by: Artem Lifshits
Reviewed-by: Anton Sidelnikov
  • Loading branch information
muneeb-jan authored Nov 8, 2024
1 parent 015a465 commit d9bf9f4
Show file tree
Hide file tree
Showing 5 changed files with 289 additions and 0 deletions.
57 changes: 57 additions & 0 deletions docs/data-sources/ddm_instance_v1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
subcategory: "Distributed Database Middleware (DDM)"
layout: "opentelekomcloud"
page_title: "OpenTelekomCloud: opentelekomcloud_ddm_instance_v1"
sidebar_current: "docs-opentelekomcloud-resource-ddm-instance-v1"
description: |-
Get a DDM Instance resource from OpenTelekomCloud.
---

Up-to-date reference of API arguments for DDM instance you can get at
[documentation portal](https://docs.otc.t-systems.com/distributed-database-middleware/api-ref/apis_recommended/ddm_instances/querying_details_of_a_ddm_instance.html)

# opentelekomcloud_ddm_instance_v1

Use this data source to get info of the OpenTelekomCloud DDM instance.

## Example Usage
```hcl
variable "instance_id" {}
data "opentelekomcloud_ddm_instance_v1" "instance" {
instance_id = var.instance_id
}
```

## Argument Reference

The following arguments are supported:

* `instance_id` - (Required, String) Specifies the DDM instance ID.

## Attributes Reference

The following attributes are exported:

* `instance_id` - See Argument Reference above.
* `region` - Indicates the region of the DDM instance.
* `name` - Indicates the name of DDM instance.
* `vpc_id` - Indicates the VPC ID.
* `subnet_id` - Indicates the subnet Network ID.
* `security_group_id` - Indicates the security group ID of the DDM instance.
* `node_num` - Indicates the disk encryption ID of the instance.
* `username` - Indicates the Administrator username of the DDM instance.
* `status` - Indicates the DDM instance status.
* `access_IP` - Indicates the DDM access IP.
* `access_port` - Indicates the DDM access port.
* `created_at` - Indicates the creation time.
* `updated_at` - Indicates the update time.
* `availability_zone` - Indicates the availability zone of DDM instance.
* `node_status` - Indicates the DDM nodes status.
* `nodes` - Indicates the instance nodes information. Structure is documented below.

The `nodes` block contains:

- `ip` - Indicates the node IP.
- `port` - Indicates the node port.
- `status` - Indicates the node status.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package acceptance

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"

"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/acceptance/common"
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/acceptance/env"
)

const dataInstanceName = "data.opentelekomcloud_ddm_instance_v1.instance"

func TestAccDDMInstanceV1DataSource_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { common.TestAccPreCheck(t) },
ProviderFactories: common.TestAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccDDMInstanceV1DataSourceBasic,
Check: resource.ComposeTestCheckFunc(
testAccCheckDDMInstanceV1DataSourceID(dataInstanceName),
resource.TestCheckResourceAttr(dataInstanceName, "name", "ddm-instance"),
resource.TestCheckResourceAttrSet(dataInstanceName, "vpc_id"),
),
},
},
})
}

func testAccCheckDDMInstanceV1DataSourceID(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("DDM instance data source ID not set ")
}

return nil
}
}

var testAccDDMInstanceV1DataSourceBasic = fmt.Sprintf(`
%s
%s
resource "opentelekomcloud_ddm_instance_v1" "instance_1" {
name = "ddm-instance"
availability_zones = ["%s"]
flavor_id = "941b5a6d-3485-329e-902c-ffd49d352f16"
node_num = 2
engine_id = "367b68a3-b48b-3d8a-b3a1-4c463a75a4b4"
vpc_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.vpc_id
subnet_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.network_id
security_group_id = data.opentelekomcloud_networking_secgroup_v2.default_secgroup.id
time_zone = "UTC+01:00"
username = "test_user"
password = "test!-acc-Password-V1!"
}
data "opentelekomcloud_ddm_instance_v1" "instance" {
instance_id = opentelekomcloud_ddm_instance_v1.instance_1.id
}
`, common.DataSourceSubnet, common.DataSourceSecGroupDefault, env.OS_AVAILABILITY_ZONE)
1 change: 1 addition & 0 deletions opentelekomcloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ func Provider() *schema.Provider {
"opentelekomcloud_dcs_product_v1": dcs.DataSourceDcsProductV1(),
"opentelekomcloud_deh_host_v1": deh.DataSourceDEHHostV1(),
"opentelekomcloud_deh_server_v1": deh.DataSourceDEHServersV1(),
"opentelekomcloud_ddm_instance_v1": ddm.DataSourceDdmInstanceV1(),
"opentelekomcloud_dds_flavors_v3": dds.DataSourceDdsFlavorV3(),
"opentelekomcloud_dds_instance_v3": dds.DataSourceDdsInstanceV3(),
"opentelekomcloud_dms_az_v1": dms.DataSourceDmsAZV1(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package ddm

import (
"context"
"log"

"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
ddmv1instances "github.com/opentelekomcloud/gophertelekomcloud/openstack/ddm/v1/instances"

"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/common"
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/common/cfg"
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/common/fmterr"
)

func DataSourceDdmInstanceV1() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceDdmInstanceV1Read,

Schema: map[string]*schema.Schema{
"instance_id": {
Type: schema.TypeString,
Required: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"node_num": {
Type: schema.TypeInt,
Computed: true,
},
"vpc_id": {
Type: schema.TypeString,
Computed: true,
},
"subnet_id": {
Type: schema.TypeString,
Computed: true,
},
"security_group_id": {
Type: schema.TypeString,
Computed: true,
},
"username": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"access_ip": {
Type: schema.TypeString,
Computed: true,
},
"access_port": {
Type: schema.TypeString,
Computed: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
},
"node_status": {
Type: schema.TypeString,
Computed: true,
},
"nodes": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ip": {
Type: schema.TypeString,
Computed: true,
},
"port": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"availability_zone": {
Type: schema.TypeString,
Computed: true,
},
"region": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceDdmInstanceV1Read(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*cfg.Config)
client, err := common.ClientFromCtx(ctx, keyClientV1, func() (*golangsdk.ServiceClient, error) {
return config.DdmV1Client(config.GetRegion(d))
})
if err != nil {
return fmterr.Errorf(errCreationV1Client, err)
}

instance, err := ddmv1instances.QueryInstanceDetails(client, d.Get("instance_id").(string))
if err != nil {
return fmterr.Errorf("error fetching DDM instance: %w", err)
}

d.SetId(instance.Id)
log.Printf("[DEBUG] Retrieved instance %s: %#v", d.Id(), instance)

mErr := multierror.Append(nil,
d.Set("instance_id", d.Id()),
d.Set("region", config.GetRegion(d)),
d.Set("name", instance.Name),
d.Set("status", instance.Status),
d.Set("vpc_id", instance.VpcId),
d.Set("subnet_id", instance.SubnetId),
d.Set("security_group_id", instance.SecurityGroupId),
d.Set("username", instance.AdminUserName),
d.Set("availability_zone", instance.AvailableZone),
d.Set("node_num", instance.NodeCount),
d.Set("access_ip", instance.AccessIp),
d.Set("access_port", instance.AccessPort),
d.Set("node_status", instance.NodeStatus),
d.Set("created_at", instance.Created),
d.Set("updated_at", instance.Updated),
)

var nodesList []map[string]interface{}
for _, nodeObj := range instance.Nodes {
node := make(map[string]interface{})
node["ip"] = nodeObj.IP
node["port"] = nodeObj.Port
node["status"] = nodeObj.Status
nodesList = append(nodesList, node)
}

mErr = multierror.Append(
mErr,
d.Set("nodes", nodesList),
)

return diag.FromErr(mErr.ErrorOrNil())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
features:
- |
**[DDM]** New Data Source: ``opentelekomcloud_ddm_instance_v1`` (`#2709 <https://github.com/opentelekomcloud/terraform-provider-opentelekomcloud/pull/2709>`_)

0 comments on commit d9bf9f4

Please sign in to comment.