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 data sources ddm_engines_v1and ddm_flavors_v1 #2719

Merged
merged 10 commits into from
Nov 12, 2024
42 changes: 42 additions & 0 deletions docs/data-sources/ddm_engines_v1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
subcategory: "Distributed Database Middleware (DDM)"
layout: "opentelekomcloud"
page_title: "OpenTelekomCloud: opentelekomcloud_ddm_engines_v1"
sidebar_current: "docs-opentelekomcloud-datasource-ddm-engines-v1"
description: |-
Get DDM engines from OpenTelekomCloud.
---

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

# opentelekomcloud_ddm_engines_v1

Use this data source to get info of OpenTelekomCloud DDM engines.

## Example Usage
```hcl
data "opentelekomcloud_ddm_engines_v1" "engine_list" {
}
```


## Attributes Reference

The following attributes are exported:

* `region` - Indicates the region of the DDM engines.
* `engines` - Indicates the DDM engines information. Structure is documented below.

The `engines` block contains:

- `id` - Indicates the DDM engine ID.
- `name` - Indicates the DDM engine name.
- `version` - Indicates the DDM engine version.
- `availability_zones` - Indicates the supported availability zones. Structure is documented below

The `availability_zones` block contains:

- `name` - Indicates the AZ name.
- `code` - Indicates the AZ code.
- `favored` - Indicates whether current AZ is favored.
57 changes: 57 additions & 0 deletions docs/data-sources/ddm_flavors_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_flavors_v1"
sidebar_current: "docs-opentelekomcloud-datasource-ddm-flavors-v1"
description: |-
Get DDM flavors from OpenTelekomCloud.
---

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

# opentelekomcloud_ddm_flavors_v1

Use this data source to get info of OpenTelekomCloud DDM compute flavors.

## Example Usage
```hcl
data "opentelekomcloud_ddm_engines_v1" "engine_list" {
}

data "opentelekomcloud_ddm_flavors_v1" "flavor_list" {
engine_id = data.opentelekomcloud_ddm_engines_v1.engine_list.engines.0.id
}
```

## Argument Reference

The following arguments are supported:

* `engine_id` - (Required, String) Specifies the DDM engine ID.

## Attributes Reference

The following attributes are exported:

* `region` - Indicates the region of the DDM compute flavors.
* `engine_id` - See Argument Reference above.
* `flavor_groups` - Indicates the DDM compute flvaor groups information. Structure is documented below.

The `flavor_groups` block contains:

- `type` - Indicates the DDM compute flavor group type. The value can be x86 or ARM.
- `flavors` - Indicates the available compute flavors in the flavor group. Structure is documented below

The `availability_zones` block contains:

- `id` - Indicates the compute flavor ID.
- `type_code` - Indicates the resource type code.
- `code` - Indicates the VM flavor types recorded in DDM.
- `iaas_code` - Indicates the VM flavor types recorded by the IaaS layer.
- `cpu` - Indicates the number of CPUs.
- `memory` - Indicates the memory size, in GB.
- `max_connections` - Indicates the maximum number of connections.
- `server_type` - Indicates the compute resource type.
- `architecture` - Indicates the coompute resource architecture type. The value can be x86 or ARM.
- `az_status` - Status of the AZ where node classes are available. The key is the AZ ID and the value is the AZ status. The value can be `normal`, `unsupported`, or `sellout`.
2 changes: 1 addition & 1 deletion docs/data-sources/ddm_instance_v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
subcategory: "Distributed Database Middleware (DDM)"
layout: "opentelekomcloud"
page_title: "OpenTelekomCloud: opentelekomcloud_ddm_instance_v1"
sidebar_current: "docs-opentelekomcloud-resource-ddm-instance-v1"
sidebar_current: "docs-opentelekomcloud-datasource-ddm-instance-v1"
description: |-
Get a DDM Instance resource from OpenTelekomCloud.
---
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package acceptance

import (
"testing"

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

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

const dataEnginesName = "data.opentelekomcloud_ddm_engines_v1.engine_list"

func TestAccDDMEnginesV1DataSource_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { common.TestAccPreCheck(t) },
ProviderFactories: common.TestAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccDDMEnginesV1DataSourceBasic,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(dataEnginesName, "engines.0.id"),
),
},
},
})
}

var testAccDDMEnginesV1DataSourceBasic = `
data "opentelekomcloud_ddm_engines_v1" "engine_list" {
}
`
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package acceptance

import (
"testing"

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

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

const dataFlavorsName = "data.opentelekomcloud_ddm_flavors_v1.flavor_list"

func TestAccDDMFlavorsV1DataSource_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { common.TestAccPreCheck(t) },
ProviderFactories: common.TestAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccDDMFlavorsV1DataSourceBasic,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(dataFlavorsName, "flavor_groups.0.type"),
),
},
},
})
}

var testAccDDMFlavorsV1DataSourceBasic = `
data "opentelekomcloud_ddm_engines_v1" "engine_list" {
}

data "opentelekomcloud_ddm_flavors_v1" "flavor_list" {
engine_id = data.opentelekomcloud_ddm_engines_v1.engine_list.engines.0.id
}
`
2 changes: 2 additions & 0 deletions opentelekomcloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,8 @@ func Provider() *schema.Provider {
"opentelekomcloud_dcs_product_v1": dcs.DataSourceDcsProductV1(),
"opentelekomcloud_deh_host_v1": deh.DataSourceDEHHostV1(),
"opentelekomcloud_deh_server_v1": deh.DataSourceDEHServersV1(),
"opentelekomcloud_ddm_engines_v1": ddm.DataSourceDdmEnginesV1(),
"opentelekomcloud_ddm_flavors_v1": ddm.DataSourceDdmFlavorsV1(),
"opentelekomcloud_ddm_instance_v1": ddm.DataSourceDdmInstanceV1(),
"opentelekomcloud_dds_flavors_v3": dds.DataSourceDdsFlavorV3(),
"opentelekomcloud_dds_instance_v3": dds.DataSourceDdsInstanceV3(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
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"
ddmv2instances "github.com/opentelekomcloud/gophertelekomcloud/openstack/ddm/v2/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 DataSourceDdmEnginesV1() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceDdmEnginesV1Read,

Schema: map[string]*schema.Schema{
"engines": {
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,
},
"version": {
Type: schema.TypeString,
Computed: true,
},
"availability_zones": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
"code": {
Type: schema.TypeString,
Computed: true,
},
"favored": {
Type: schema.TypeBool,
Computed: true,
},
},
},
},
},
},
},
"region": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

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

enginesRaw, err := ddmv2instances.QueryEngineInfo(client, ddmv2instances.QueryEngineOpts{})
if err != nil {
return fmterr.Errorf("error fetching DDM engines: %w", err)
}

log.Printf("[DEBUG] Retrieved DDM engines info: %#v", enginesRaw)

d.SetId(config.GetRegion(d))

mErr := multierror.Append(nil,
d.Set("region", d.Id()),
)

var enginesList []map[string]interface{}
for _, engineRaw := range enginesRaw {
engine := make(map[string]interface{})
engine["id"] = engineRaw.ID
engine["name"] = engineRaw.Name
engine["version"] = engineRaw.Version
var azList []map[string]interface{}
for _, azRaw := range engineRaw.SupportAzs {
az := make(map[string]interface{})
az["name"] = azRaw.Name
az["code"] = azRaw.Code
az["favored"] = azRaw.Favored
azList = append(azList, az)
}
engine["availability_zones"] = azList
enginesList = append(enginesList, engine)
}

mErr = multierror.Append(
mErr,
d.Set("engines", enginesList),
)

return diag.FromErr(mErr.ErrorOrNil())
}
Loading