Skip to content

Commit

Permalink
Fix region handling for the provider (#697)
Browse files Browse the repository at this point in the history
Fix region handling for the provider

Reviewed-by: Anton Kachurin <katchuring@gmail.com>
             https://github.com/outcatcher
  • Loading branch information
outcatcher authored Nov 9, 2020
1 parent c81642b commit 677d88a
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 21 deletions.
12 changes: 7 additions & 5 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,6 @@ See [OpenStack configuration documentation](https://docs.openstack.org/python-op

## Configuration Reference

-> **Note:** The `region`, `tenant_id`, `domain_id`, `user_id` arguments has been deprecated and `tenant_name`, `domain_name` changed to be `required`. Please update your configurations as it might be removed in the future releases.

The following arguments are supported:

* `access_key` - (Optional) The access key of the OpenTelekomCloud cloud to use.
Expand All @@ -166,10 +164,14 @@ The following arguments are supported:
* `user_name` - (Optional) The Username to login with. If omitted, the
`OS_USERNAME` environment variable is used.

* `tenant_name` - (Required) The Name of the Tenant (Identity v2) or Project
* `tenant_name` - (Optional) The Name of the Tenant (Identity v2) or Project
(Identity v3) to login with. If omitted, the `OS_TENANT_NAME` or
`OS_PROJECT_NAME` environment variable are used.

* `region` - (Optional) The name of the region to be used. Required for some resources
(e.g. `s3_bucket`) in case no tenant name provided and no region is defined in the
resource. If omitted, the `OS_REGION` or `OS_REGION_NAME` environment variables are used.

* `password` - (Optional) The Password to login with. If omitted, the
`OS_PASSWORD` environment variable is used.

Expand All @@ -181,7 +183,7 @@ The following arguments are supported:

* `security_token` - (Optional) Security token to use for OBS federated authentication.

* `domain_name` - (Required) The Name of the Domain to scope to (Identity v3).
* `domain_name` - (Optional) The Name of the Domain to scope to (Identity v3).
If omitted, the following environment variables are checked (in this order):
`OS_USER_DOMAIN_NAME`, `OS_PROJECT_DOMAIN_NAME`, `OS_DOMAIN_NAME`,
`DEFAULT_DOMAIN`.
Expand Down Expand Up @@ -209,7 +211,7 @@ The following arguments are supported:
Swift-native authentication system. If omitted, the `OS_SWAUTH` environment
variable is used. You must also set `username` to the Swauth/Swift username
such as `username:project`. Set the `password` to the Swauth/Swift key.
Finally, set `auth_url` as the location of the Swift service.
Finally, set `auth_url` as the location of the Swift service.
-> **Note:** This will only work when used with the OpenTelekomCloud Object Storage resources.

* `agency_name` - (Optional) if authorized by assume role, it must be set. The
Expand Down
5 changes: 4 additions & 1 deletion opentelekomcloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ func Provider() terraform.ResourceProvider {
Type: schema.TypeString,
Optional: true,
Description: descriptions["region"],
DefaultFunc: schema.EnvDefaultFunc("OS_REGION_NAME", ""),
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"OS_REGION_NAME",
"OS_REGION",
}, ""),
},

"user_name": {
Expand Down
9 changes: 4 additions & 5 deletions opentelekomcloud/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,11 @@ func init() {
testAccProviders = map[string]terraform.ResourceProvider{
"opentelekomcloud": testAccProvider,
}
tn := os.Getenv("OS_TENANT_NAME")
if tn == "" {
tn = os.Getenv("OS_PROJECT_NAME")
}
OS_REGION_NAME = GetRegion(nil, &Config{TenantName: tn})

err := testAccProvider.Configure(terraform.NewResourceConfigRaw(nil))
if err == nil {
OS_REGION_NAME = GetRegion(nil, testAccProvider.Meta().(*Config))
}
}

func getTenantName() ProjectName {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1299,7 +1299,7 @@ func securityGroupsByIDs(diff *schema.ResourceDiff, meta interface{}) error {

// resolve IDs
config := meta.(*Config)
computeClient, err := config.computeV2HWClient(GetRegion(nil, config))
computeClient, err := config.computeV2HWClient(GetRegion(diff, config))
if err != nil {
return fmt.Errorf("error creating OpenTelekomCloud compute client: %s", err)
}
Expand Down
28 changes: 19 additions & 9 deletions opentelekomcloud/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,34 @@ func CheckDeleted(d *schema.ResourceData, err error, msg string) error {
return fmt.Errorf("%s: %s", msg, err)
}

type schemaOrDiff interface {
GetOk(key string) (interface{}, bool)
}

// GetRegion returns the region that was specified in the resource. If a
// region was not set, the provider-level region is checked. The provider-level
// region can either be set by the region argument or by OS_REGION_NAME.
func GetRegion(_ *schema.ResourceData, config *Config) string {
n := config.TenantName
if n == "" {
n = config.DelegatedProject
func GetRegion(d schemaOrDiff, config *Config) string {
if d != nil {
if v, ok := d.GetOk("region"); ok {
return v.(string)
}
}
return strings.Split(n, "_")[0]
if v := config.Region; v != "" {
return v
}
tenantName := string(GetProjectName(d, config))
return strings.Split(tenantName, "_")[0]
}

type ProjectName string

// GetProjectName returns the project name that was specified in the resource.
func GetProjectName(d *schema.ResourceData, config *Config) ProjectName {
projectName := d.Get("project_name").(string)
if projectName != "" {
return ProjectName(projectName)
func GetProjectName(d schemaOrDiff, config *Config) ProjectName {
if d != nil {
if v, ok := d.GetOk("project_name"); ok {
return ProjectName(v.(string))
}
}
tenantName := config.TenantName
if tenantName == "" {
Expand Down

0 comments on commit 677d88a

Please sign in to comment.