Skip to content

Commit

Permalink
Validate parameters of huaweicloud provider (#335)
Browse files Browse the repository at this point in the history
* Validate parameters of huaweicloud provider

add rules:
- `access_key` and `secret_key` must be set simultaneously;
- `agency_name` and `agency_domain_name` must be set simultaneously;
- one of `user_name`, `user_id` must be specified  together with `password`;

* Use region as delegated_project if not set
  • Loading branch information
ShiChangkuo authored Jun 5, 2020
1 parent 293a8f4 commit 26e52e0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
8 changes: 6 additions & 2 deletions huaweicloud/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,12 @@ func (c *Config) LoadAndValidate() error {
if c.Token != "" {
err = buildClientByToken(c)

} else if c.Password != "" && (c.Username != "" || c.UserID != "") {
err = buildClientByPassword(c)
} else if c.Password != "" {
if c.Username == "" && c.UserID == "" {
err = fmt.Errorf("\"password\": one of `user_name, user_id` must be specified")
} else {
err = buildClientByPassword(c)
}

} else if c.AccessKey != "" && c.SecretKey != "" {
err = buildClientByAKSK(c)
Expand Down
48 changes: 30 additions & 18 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,19 @@ func Provider() terraform.ResourceProvider {
provider := &schema.Provider{
Schema: map[string]*schema.Schema{
"access_key": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("OS_ACCESS_KEY", ""),
Description: descriptions["access_key"],
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("OS_ACCESS_KEY", nil),
Description: descriptions["access_key"],
RequiredWith: []string{"secret_key"},
},

"secret_key": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("OS_SECRET_KEY", ""),
Description: descriptions["secret_key"],
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("OS_SECRET_KEY", nil),
Description: descriptions["secret_key"],
RequiredWith: []string{"access_key"},
},

"auth_url": {
Expand Down Expand Up @@ -144,17 +146,19 @@ func Provider() terraform.ResourceProvider {
},

"agency_name": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("OS_AGENCY_NAME", ""),
Description: descriptions["agency_name"],
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("OS_AGENCY_NAME", nil),
Description: descriptions["agency_name"],
RequiredWith: []string{"agency_domain_name"},
},

"agency_domain_name": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("OS_AGENCY_DOMAIN_NAME", ""),
Description: descriptions["agency_domain_name"],
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("OS_AGENCY_DOMAIN_NAME", nil),
Description: descriptions["agency_domain_name"],
RequiredWith: []string{"agency_name"},
},
"delegated_project": {
Type: schema.TypeString,
Expand Down Expand Up @@ -381,14 +385,22 @@ func init() {
}

func configureProvider(d *schema.ResourceData, terraformVersion string) (interface{}, error) {
var tenant_name string
var tenant_name, delegated_project string

// Use region as tenant_name if it's not set
if v, ok := d.GetOk("tenant_name"); ok && v.(string) != "" {
tenant_name = v.(string)
} else {
tenant_name = d.Get("region").(string)
}

// Use region as delegated_project if it's not set
if v, ok := d.GetOk("delegated_project"); ok && v.(string) != "" {
delegated_project = v.(string)
} else {
delegated_project = d.Get("region").(string)
}

config := Config{
AccessKey: d.Get("access_key").(string),
SecretKey: d.Get("secret_key").(string),
Expand All @@ -408,7 +420,7 @@ func configureProvider(d *schema.ResourceData, terraformVersion string) (interfa
UserID: d.Get("user_id").(string),
AgencyName: d.Get("agency_name").(string),
AgencyDomainName: d.Get("agency_domain_name").(string),
DelegatedProject: d.Get("delegated_project").(string),
DelegatedProject: delegated_project,
terraformVersion: terraformVersion,
}

Expand Down

0 comments on commit 26e52e0

Please sign in to comment.