Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] DDM: Add resource ddm v1 #2692

Merged
merged 16 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 168 additions & 0 deletions docs/resources/ddm_instance_v1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
---
subcategory: "Distributed Database Middleware (DDM)"
layout: "opentelekomcloud"
page_title: "OpenTelekomCloud: opentelekomcloud_ddm_instance_v1"
sidebar_current: "docs-opentelekomcloud-resource-ddm-instance-v1"
description: |-
Manages a DDM Instance resource within OpenTelekomCloud.
---

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

# opentelekomcloud_ddm_instance_v1

Manages DDM instance resource within OpenTelekomCloud

## Example Usage: Creating a basic DDM instance with 2 nodes
```hcl
variable "flavor_id" {}
variable "engine_id" {}
variable "vpc_id" {}
variable "subnet_id" {}
variable "security_group_id" {}

resource "opentelekomcloud_ddm_instance_v1" "instance_1" {
name = "ddm-instance"
availability_zones = ["eu-de-01", "eu-de-02", "eu-de-03"]
flavor_id = var.flavor_id
node_num = 2
engine_id = var.engine_id
vpc_id = var.vpc_id
subnet_id = var.subnet_id
security_group_id = var.security_group.id
purge_rds_on_delete = true
}
```

## Example Usage: Creating a DDM instance with custom credentials
```hcl
variable "flavor_id" {}
variable "engine_id" {}
variable "vpc_id" {}
variable "subnet_id" {}
variable "security_group_id" {}
variable "username" {}
variable "password" {}

resource "opentelekomcloud_ddm_instance_v1" "instance_1" {
name = "ddm-instance"
availability_zones = ["eu-de-01", "eu-de-02", "eu-de-03"]
flavor_id = var.flavor_id
node_num = 2
engine_id = var.engine_id
vpc_id = var.vpc_id
subnet_id = var.subnet_id
security_group_id = var.security_group.id
purge_rds_on_delete = true
username = var.username
password = var.password
}
```

## Example Usage: Creating a DDM instance with custom time zone
```hcl
variable "flavor_id" {}
variable "engine_id" {}
variable "vpc_id" {}
variable "subnet_id" {}
variable "security_group_id" {}

resource "opentelekomcloud_ddm_instance_v1" "instance_1" {
name = "ddm-instance"
availability_zones = ["eu-de-01", "eu-de-02", "eu-de-03"]
flavor_id = var.flavor_id
node_num = 2
engine_id = var.engine_id
vpc_id = var.vpc_id
subnet_id = var.subnet_id
security_group_id = var.security_group.id
purge_rds_on_delete = true
time_zone = "UTC+01:00"
}
```

## Argument Reference

The following arguments are supported:

* `region` - (Optional, String, ForceNew) Specifies the region of the DDM instance.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move this to attributes


* `name` - (Required, String) Specifies the DDM instance name. The DDM instance name of the same
type is unique in the same tenant. It can be 4 to 64 characters long. It must start with a letter and it can only contain etters, digits, and hyphens (-).

* `availability_zones` - (Required, List, ForceNew) Specifies the list of availability zones.

* `vpc_id` - (Required, String, ForceNew) Specifies the VPC ID.

* `subnet_id` - (Required, String, ForceNew) Specifies the subnet Network ID.

* `security_group_id` - (Required, String) Specifies the security group ID of the DDM instance.

* `node_num` - (Required, Integer) Specifies the disk encryption ID of the instance.

* `flavor_id` - (Required, String, ForceNew) Specifies the flavor ID of the instance nodes.

* `engine_id` - (Required, String, ForceNew) Specifies the Engine ID of the instance.

* `time_zone` - (Optional, String, ForceNew) Specifies the timezone. Valid formats are `UTC+12:00`, `UTC+11:00`, ... ,`UTC+01:00`, `UTC`, `UTC-01:00`, ... , `UTC-11:00`, `UTC-12:00`

* `username` - (Optional, String, ForceNew) Specifies the Administrator username of the DDM instance. It can be 1 to 32 characters long and can contain letters, digits, and underscores (_). It must start with a letter.

* `password` - (Optional, String) Specifies the Administrator password of the DDM instance. it can be 8 to 32 characters long. It must be a combination of uppercase letters, lowercase letters, digits, and the following special characters: `~ ! @ # % ^ * - _ = + ?`. It must be a strong password to improve security and prevent security risks such as brute force cracking.

* `param_group_id` - (Optional, String, ForceNew) Specifies the parameters group ID.

* `purge_rds_on_delete` - (Optional, Boolean) Specifies whether data stored on the associated DB instances is deleted. The value can be: `true` or `false` (default).


## Attributes Reference

The following attributes are exported:

* `region` - See Argument Reference above.
* `name` - See Argument Reference above.
* `availability_zones` - See Argument Reference above.
* `vpc_id` - See Argument Reference above.
* `subnet_id` - See Argument Reference above.
* `security_group_id` - See Argument Reference above.
* `node_num` - See Argument Reference above.
* `flavor_id` - See Argument Reference above.
* `engine_id` - See Argument Reference above.
* `time_zone` - See Argument Reference above.
* `username` - See Argument Reference above.
* `password` - See Argument Reference above.
* `param_group_id` - See Argument Reference above.
* `purge_rds_on_delete` - See Argument Reference above.
* `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.


## Import

DDMv1 Instance can be imported using the `id`, e.g.

```shell
terraform import opentelekomcloud_ddm_instance_v1.instance_1 c1851195-cdcb-4d23-96cb-032e6a3ee667
```

Following attributes are not properly imported.
* `availability_zones`
* `flavor_id`
* `engine_id`
* `time_zone`
* `password`
* `param_group_id`
* `purge_rds_on_delete`
Copy link
Member

@anton-sidelnikov anton-sidelnikov Oct 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change for:

Notes

But due to some attributes missing from the API response, it's required to ignore changes as below:

resource "opentelekomcloud_ddm_instance_v1" "instance_1" {
  # ...

  lifecycle {
    ignore_changes = [
      availability_zones,
      flavor_id,
      engine_id,
      time_zone,
      password,
    ]
  }
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
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/gophertelekomcloud/openstack/ddm/v1/instances"

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

const ddmInstanceResourceName = "opentelekomcloud_ddm_instance_v1.instance_1"

func getDDMInstanceResourceFunc(cfg *cfg.Config, state *terraform.ResourceState) (interface{}, error) {
client, err := cfg.DdmV1Client(env.OS_REGION_NAME)
if err != nil {
return nil, fmt.Errorf("error creating SDRS Client: %s", err)
}
return instances.QueryInstanceDetails(client, state.Primary.ID)
}

func TestAccDdmInstancesV1_basic(t *testing.T) {
var instance instances.QueryInstanceDetailsResponse
rc := common.InitResourceCheck(
ddmInstanceResourceName,
&instance,
getDDMInstanceResourceFunc,
)
resource.Test(t, resource.TestCase{
PreCheck: func() { common.TestAccPreCheck(t) },
ProviderFactories: common.TestAccProviderFactories,
CheckDestroy: rc.CheckResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testAccDdmInstanceV1Basic,
Check: resource.ComposeTestCheckFunc(
rc.CheckResourceExists(),
resource.TestCheckResourceAttr(ddmInstanceResourceName, "name", "ddm-instance"),
resource.TestCheckResourceAttr(ddmInstanceResourceName, "node_num", "2"),
resource.TestCheckResourceAttr(ddmInstanceResourceName, "username", "test_user"),
),
},
{
Config: testAccDdmInstanceV1ScaleUp,
Check: resource.ComposeTestCheckFunc(
rc.CheckResourceExists(),
resource.TestCheckResourceAttr(ddmInstanceResourceName, "name", "ddm-instance-scale-up"),
resource.TestCheckResourceAttr(ddmInstanceResourceName, "node_num", "3"),
),
},
{
Config: testAccDdmInstanceV1ScaleDown,
Check: resource.ComposeTestCheckFunc(
rc.CheckResourceExists(),
resource.TestCheckResourceAttr(ddmInstanceResourceName, "name", "ddm-instance-scale-down"),
resource.TestCheckResourceAttr(ddmInstanceResourceName, "node_num", "1"),
),
},
{
ResourceName: ddmInstanceResourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"availability_zones",
"flavor_id",
"engine_id",
"time_zone",
"password",
"param_group_id",
"purge_rds_on_delete",
},
},
},
})
}

var testAccDdmInstanceV1Basic = 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!"
}
`, common.DataSourceSubnet, common.DataSourceSecGroupDefault, env.OS_AVAILABILITY_ZONE)

var testAccDdmInstanceV1ScaleUp = fmt.Sprintf(`
%s

%s

resource "opentelekomcloud_ddm_instance_v1" "instance_1" {
name = "ddm-instance-scale-up"
availability_zones = ["%s"]
flavor_id = "941b5a6d-3485-329e-902c-ffd49d352f16"
node_num = 3
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!"
}
`, common.DataSourceSubnet, common.DataSourceSecGroupDefault, env.OS_AVAILABILITY_ZONE)

var testAccDdmInstanceV1ScaleDown = fmt.Sprintf(`
%s

%s

resource "opentelekomcloud_ddm_instance_v1" "instance_1" {
name = "ddm-instance-scale-down"
availability_zones = ["%s"]
flavor_id = "941b5a6d-3485-329e-902c-ffd49d352f16"
node_num = 1
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-V2!"
purge_rds_on_delete = true
}
`, common.DataSourceSubnet, common.DataSourceSecGroupDefault, env.OS_AVAILABILITY_ZONE)
21 changes: 21 additions & 0 deletions opentelekomcloud/common/cfg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,27 @@ func (c *Config) DCaaSV3Client(region string) (*golangsdk.ServiceClient, error)
})
}

func (c *Config) DdmV1Client(region string) (*golangsdk.ServiceClient, error) {
return openstack.NewDDMV1(c.HwClient, golangsdk.EndpointOpts{
Region: region,
Availability: c.getEndpointType(),
})
}

func (c *Config) DdmV2Client(region string) (*golangsdk.ServiceClient, error) {
return openstack.NewDDMV2(c.HwClient, golangsdk.EndpointOpts{
Region: region,
Availability: c.getEndpointType(),
})
}

func (c *Config) DdmV3Client(region string) (*golangsdk.ServiceClient, error) {
return openstack.NewDDMV3(c.HwClient, golangsdk.EndpointOpts{
Region: region,
Availability: c.getEndpointType(),
})
}

func (c *Config) DehV1Client(region string) (*golangsdk.ServiceClient, error) {
return openstack.NewDeHServiceV1(c.HwClient, golangsdk.EndpointOpts{
Region: region,
Expand Down
2 changes: 2 additions & 0 deletions opentelekomcloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/services/cts"
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/services/dcaas"
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/services/dcs"
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/services/ddm"
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/services/dds"
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/services/deh"
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/services/dis"
Expand Down Expand Up @@ -414,6 +415,7 @@ func Provider() *schema.Provider {
"opentelekomcloud_dc_virtual_gateway_v3": dcaas.ResourceVirtualGatewayV3(),
"opentelekomcloud_dcs_instance_v1": dcs.ResourceDcsInstanceV1(),
"opentelekomcloud_dcs_instance_v2": dcs.ResourceDcsInstanceV2(),
"opentelekomcloud_ddm_instance_v1": ddm.ResourceDdmInstanceV1(),
"opentelekomcloud_dds_backup_v3": dds.ResourceDdsBackupV3(),
"opentelekomcloud_dds_instance_v3": dds.ResourceDdsInstanceV3(),
"opentelekomcloud_deh_host_v1": deh.ResourceDeHHostV1(),
Expand Down
34 changes: 34 additions & 0 deletions opentelekomcloud/services/ddm/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package ddm

import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
ddmv1instances "github.com/opentelekomcloud/gophertelekomcloud/openstack/ddm/v1/instances"
)

const (
errCreationV1Client = "error creating OpenTelekomCloud DDMv1 client: %w"
errCreationV2Client = "error creating OpenTelekomCloud DDMv2 client: %w"
errCreationV3Client = "error creating OpenTelekomCloud DDMv3 client: %w"
keyClientV1 = "ddm-v1-client"
keyClientV2 = "ddm-v2-client"
keyClientV3 = "ddm-v3-client"
)

func instanceStateRefreshFunc(client *golangsdk.ServiceClient, instanceID string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
instanceList, err := ddmv1instances.QueryInstances(client, ddmv1instances.QueryInstancesOpts{})
if err != nil {
return nil, "Error retrieving DDM v1 Instances", err
}
if len(instanceList) == 0 {
return nil, "DELETED", nil
}
for _, instance := range instanceList {
if instance.Id == instanceID {
return instance, instance.Status, nil
}
}
return nil, "DELETED", nil
}
}
Loading