Skip to content

Commit

Permalink
add resource of cdm cluster (#128)
Browse files Browse the repository at this point in the history
* add service level to the sdkClient

* add resource of cdm cluster

* fix bugs
  • Loading branch information
zengchen1024 authored and niuzhenguo committed May 28, 2019
1 parent befa2d7 commit 7414b2b
Show file tree
Hide file tree
Showing 14 changed files with 2,033 additions and 13 deletions.
13 changes: 11 additions & 2 deletions huaweicloud/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ import (
"github.com/huaweicloud/golangsdk/openstack/objectstorage/v1/swauth"
)

const (
serviceProjectLevel string = "project"
serviceDomainLevel string = "domain"
)

type Config struct {
AccessKey string
SecretKey string
Expand Down Expand Up @@ -640,9 +645,13 @@ func (c *Config) dcsV1Client(region string) (*golangsdk.ServiceClient, error) {
})
}

func (c *Config) sdkClient(region, serviceType string) (*golangsdk.ServiceClient, error) {
func (c *Config) sdkClient(region, serviceType string, level string) (*golangsdk.ServiceClient, error) {
client := c.HwClient
if level == serviceDomainLevel {
client = c.DomainClient
}
return huaweisdk.NewSDKClient(
c.HwClient,
client,
golangsdk.EndpointOpts{
Region: c.determineRegion(region),
Availability: c.getHwEndpointType(),
Expand Down
21 changes: 21 additions & 0 deletions huaweicloud/custom_functions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package huaweicloud

func expandCdmClusterV1CreateAutoRemind(d interface{}, arrayIndex map[string]int) (interface{}, error) {
email, _ := navigateValue(d, []string{"email"}, nil)
e, eok := email.([]interface{})

phone, _ := navigateValue(d, []string{"phone_num"}, nil)
p, pok := phone.([]interface{})

return (eok && len(e) > 0) || (pok && len(p) > 0), nil
}

func expandCdmClusterV1CreateClusterIsScheduleBootOff(d interface{}, arrayIndex map[string]int) (interface{}, error) {
on, _ := navigateValue(d, []string{"schedule_boot_time"}, nil)
on1, ok1 := on.(string)

off, _ := navigateValue(d, []string{"schedule_off_time"}, nil)
off1, ok2 := off.(string)

return (ok1 && on1 != "") || (ok2 && off1 != ""), nil
}
163 changes: 163 additions & 0 deletions huaweicloud/data_source_huaweicloud_cdm_flavors_v1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package huaweicloud

import (
"fmt"

"github.com/hashicorp/terraform/helper/schema"
"github.com/huaweicloud/golangsdk"
)

func dataSourceCdmFlavorV1() *schema.Resource {
return &schema.Resource{
Read: dataSourceCdmFlavorV1Read,

Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"version": {
Type: schema.TypeString,
Computed: true,
},
"flavors": {
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,
},
},
},
},
},
}
}

func dataSourceCdmFlavorV1Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
client, err := config.sdkClient(GetRegion(d, config), "cdm", serviceProjectLevel)
if err != nil {
return fmt.Errorf("Error creating sdk client, err=%s", err)
}

dsid, err := getCdmDatastoreV1ID(client)
if err != nil {
return err
}

version, fs, err := getCdmFlavorV1(client, dsid)
if err != nil {
return err
}

d.SetId(version)
d.Set("version", version)
d.Set("flavors", fs)
return nil
}

func getCdmDatastoreV1ID(client *golangsdk.ServiceClient) (string, error) {
url := client.ServiceURL("datastores")
r := golangsdk.Result{}
_, r.Err = client.Get(url, &r.Body, &golangsdk.RequestOpts{
MoreHeaders: map[string]string{
"Content-Type": "application/json",
"X-Language": "en-us",
}})
if r.Err != nil {
return "", r.Err
}

v, err := navigateValue(r.Body, []string{"datastores"}, nil)
if err != nil {
return "", err
}

ds, ok := v.([]interface{})
if !ok {
return "", fmt.Errorf("can not find datastore")
}

for _, item := range ds {
name, err := navigateValue(item, []string{"name"}, nil)
if err != nil {
return "", err
}
if "cdm" == name.(string) {
dsid, err := navigateValue(item, []string{"id"}, nil)
if err != nil {
return "", err
}
return dsid.(string), nil
}
}

return "", fmt.Errorf("didn't find the datastore id")
}

func getCdmFlavorV1(client *golangsdk.ServiceClient, dsid string) (string, interface{}, error) {
url := client.ServiceURL("datastores", dsid, "flavors")
r := golangsdk.Result{}
_, r.Err = client.Get(url, &r.Body, &golangsdk.RequestOpts{
MoreHeaders: map[string]string{
"Content-Type": "application/json",
"X-Language": "en-us",
}})
if r.Err != nil {
return "", nil, r.Err
}

v, err := navigateValue(r.Body, []string{"versions"}, nil)
if err != nil {
return "", nil, err
}
vs, ok := v.([]interface{})
if !ok {
return "", nil, fmt.Errorf("can not find flavor")
}
for _, item := range vs {
version, err := navigateValue(item, []string{"name"}, nil)
if err != nil {
return "", nil, err
}
flavors, err := navigateValue(item, []string{"flavors"}, nil)
if err != nil {
return "", nil, err
}

fs, ok := flavors.([]interface{})
if !ok {
return "", nil, fmt.Errorf("can not find flavor")
}
num := len(fs)
r := make([]interface{}, num, num)
for i := 0; i < num; i++ {
item := fs[i]
name, err := navigateValue(item, []string{"name"}, nil)
if err != nil {
return "", nil, err
}
fid, err := navigateValue(item, []string{"str_id"}, nil)
if err != nil {
return "", nil, err
}

r[i] = map[string]interface{}{
"id": fid,
"name": name,
}
}
return version.(string), r, nil
}

return "", nil, fmt.Errorf("can not find flavor")
}
2 changes: 2 additions & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ func Provider() terraform.ResourceProvider {
"huaweicloud_dcs_maintainwindow_v1": dataSourceDcsMaintainWindowV1(),
"huaweicloud_dcs_product_v1": dataSourceDcsProductV1(),
"huaweicloud_identity_role_v3": dataSourceIdentityRoleV3(),
"huaweicloud_cdm_flavors_v1": dataSourceCdmFlavorV1(),
},

ResourcesMap: map[string]*schema.Resource{
Expand Down Expand Up @@ -306,6 +307,7 @@ func Provider() terraform.ResourceProvider {
"huaweicloud_identity_user_v3": resourceIdentityUserV3(),
"huaweicloud_identity_group_v3": resourceIdentityGroupV3(),
"huaweicloud_identity_group_membership_v3": resourceIdentityGroupMembershipV3(),
"huaweicloud_cdm_cluster_v1": resourceCdmClusterV1(),
},

ConfigureFunc: configureProvider,
Expand Down
Loading

0 comments on commit 7414b2b

Please sign in to comment.