Skip to content

Commit

Permalink
improvement on dds flavors data source (#355)
Browse files Browse the repository at this point in the history
collect the result into flavors list, and support filter by type,
vcpus and memory.
  • Loading branch information
ShiChangkuo authored Jul 2, 2020
1 parent 9ab8681 commit 8474b19
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 69 deletions.
113 changes: 72 additions & 41 deletions huaweicloud/data_source_huaweicloud_dds_flavors_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"log"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"

"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/huaweicloud/golangsdk/openstack/dds/v3/flavors"
)

Expand All @@ -18,29 +18,52 @@ func dataSourceDDSFlavorV3() *schema.Resource {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"engine_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"spec_code": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
"DDS-Community", "DDS-Enhanced",
}, true),
},
"type": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
"mongos", "shard", "config", "replica", "single",
}, true),
},
"vcpus": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
"ram": {
"memory": {
Type: schema.TypeString,
Optional: true,
},
"flavors": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"spec_code": {
Type: schema.TypeString,
Computed: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
},
"vcpus": {
Type: schema.TypeString,
Computed: true,
},
"memory": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
Expand All @@ -54,11 +77,8 @@ func dataSourceDDSFlavorV3Read(d *schema.ResourceData, meta interface{}) error {
}

listOpts := flavors.ListOpts{
Region: GetRegion(d, config),
}

if v, ok := d.GetOk("engine_name"); ok {
listOpts.EngineName = v.(string)
Region: GetRegion(d, config),
EngineName: d.Get("engine_name").(string),
}

pages, err := flavors.List(ddsClient, listOpts).AllPages()
Expand All @@ -71,37 +91,48 @@ func dataSourceDDSFlavorV3Read(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("Unable to extract flavors: %s", err)
}

if len(allFlavors) < 1 {
return fmt.Errorf("Your query returned no results. " +
"Please change your search criteria and try again.")
}
flavorList := make([]map[string]interface{}, 0)
filterType := d.Get("type").(string)
filterVcpus := d.Get("vcpus").(string)
filterMemory := d.Get("memory").(string)

var refinedFlavors []flavors.Flavor
var flavor flavors.Flavor
if v, ok := d.GetOk("spec_code"); ok {
for _, flavor = range allFlavors {
if flavor.SpecCode == v.(string) {
refinedFlavors = append(refinedFlavors, flavor)
}
for _, item := range allFlavors {
if filterFlavor(item, filterType, filterVcpus, filterMemory) {
continue
}
if len(refinedFlavors) < 1 {
return fmt.Errorf("Your query returned no results. " +
"Please change your search criteria and try again.")

flavor := map[string]interface{}{
"spec_code": item.SpecCode,
"type": item.Type,
"vcpus": item.Vcpus,
"memory": item.Ram,
}
flavor = refinedFlavors[0]
} else {
flavor = allFlavors[0]
flavorList = append(flavorList, flavor)
}

log.Printf("[DEBUG] Retrieved DDS Flavor: %+v ", flavor)
d.SetId(flavor.SpecCode)
log.Printf("Extract %d/%d flavors by filters.", len(flavorList), len(allFlavors))
if len(flavorList) < 1 {
return fmt.Errorf("Your query returned no results. " +
"Please change your search criteria and try again.")
}

d.Set("engine_name", flavor.EngineName)
d.Set("spec_code", flavor.SpecCode)
d.Set("type", flavor.Type)
d.Set("vcpus", flavor.Vcpus)
d.Set("ram", flavor.Ram)
d.SetId("dds flavors")
d.Set("flavors", flavorList)
d.Set("region", GetRegion(d, config))

return nil
}

func filterFlavor(item flavors.Flavor, flavorType, vcpus, memory string) bool {
if flavorType != "" && flavorType != item.Type {
return true
}
if vcpus != "" && vcpus != item.Vcpus {
return true
}
if memory != "" && memory != item.Ram {
return true
}

return false
}
1 change: 1 addition & 0 deletions huaweicloud/data_source_huaweicloud_dds_flavors_v3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,6 @@ func testAccCheckDDSFlavorV3DataSourceID(n string) resource.TestCheckFunc {
var testAccDDSFlavorV3DataSource_basic = `
data "huaweicloud_dds_flavors_v3" "flavor" {
engine_name = "DDS-Community"
vcpus = 8
}
`
43 changes: 15 additions & 28 deletions website/docs/d/dds_flavors_v3.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -14,46 +14,33 @@ Use this data source to get the ID of an available HuaweiCloud dds flavor.

```hcl
data "huaweicloud_dds_flavors_v3" "flavor" {
region = "cn-north-1"
engine_name = "DDS-Community"
vcpus = 8
}
```

## Argument Reference

* `region` - (Optional) The region in which to obtain the V3 dds client.
* `region` - (Optional) Specifies the region in which to obtain the V3 dds client.

* `engine_name` - (Optional) The engine name of the dds, now only DDS-Community is supported.
* `engine_name` - (Required) Specifies the engine name of the dds, "DDS-Community" and "DDS-Enhanced" are supported.

* `speccode` - (Optional) The spec code of a dds flavor.
* `type` - (Optional) Specifies the type of the dds falvor. "mongos", "shard", "config", "replica" and "single" are supported.

## Available value for attributes
* `vcpus` - (Optional) Specifies the vcpus of the dds flavor.

engine_name | type | vcpus | ram | speccode
---- | --- | --- | --- | ---
DDS-Community | mongos | 1 | 4 | dds.mongodb.c3.medium.4.mongos
DDS-Community | mongos | 2 | 8 | dds.mongodb.c3.large.4.mongos
DDS-Community | mongos | 4 | 16 | dds.mongodb.c3.xlarge.4.mongos
DDS-Community | mongos | 8 | 32 | dds.mongodb.c3.2xlarge.4.mongos
DDS-Community | mongos | 16 | 64 | dds.mongodb.c3.4xlarge.4.mongos
DDS-Community | shard | 1 | 4 | dds.mongodb.c3.medium.4.shard
DDS-Community | shard | 2 | 8 | dds.mongodb.c3.large.4.shard
DDS-Community | shard | 4 | 16 | dds.mongodb.c3.xlarge.4.shard
DDS-Community | shard | 8 | 32 | dds.mongodb.c3.2xlarge.4.shard
DDS-Community | shard | 16 | 64 | dds.mongodb.c3.4xlarge.4.shard
DDS-Community | config | 2 | 4 | dds.mongodb.c3.large.2.config
DDS-Community | replica | 1 | 4 | dds.mongodb.c3.medium.4.repset
DDS-Community | replica | 2 | 8 | dds.mongodb.c3.large.4.repset
DDS-Community | replica | 4 | 16 | dds.mongodb.c3.xlarge.4.repset
DDS-Community | replica | 8 | 32 | dds.mongodb.c3.2xlarge.4.repset
DDS-Community | replica | 16 | 64 | dds.mongodb.c3.4xlarge.4.repset
* `memory` - (Optional) Specifies the ram of the dds flavor in GB.


## Attributes Reference

* `region` - See Argument Reference above.
* `engine_name` - See Argument Reference above.
* `speccode` - See Argument Reference above.
* `type` - The type of the dds flavor.
* `vcpus` - The vcpus of the dds flavor.
* `ram` - The ram of the dds flavor.

* `flavors` - Indicates the flavors information. Structure is documented below.

The `flavors` block contains:

* `spec_code - The name of the rds flavor.
* `type` - See `type` above.
* `vcpus` - See `vcpus` above.
* `memory` - See 'memory' above.

0 comments on commit 8474b19

Please sign in to comment.