diff --git a/huaweicloud/config.go b/huaweicloud/config.go index 90294c5bb4..fc304ed99e 100644 --- a/huaweicloud/config.go +++ b/huaweicloud/config.go @@ -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) diff --git a/huaweicloud/provider.go b/huaweicloud/provider.go index d3465b4dd8..4a3dff46d4 100644 --- a/huaweicloud/provider.go +++ b/huaweicloud/provider.go @@ -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": { @@ -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, @@ -381,7 +385,8 @@ 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) @@ -389,6 +394,13 @@ func configureProvider(d *schema.ResourceData, terraformVersion string) (interfa 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), @@ -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, }