Skip to content

Commit

Permalink
[Provider]: implement reauth option (#2399)
Browse files Browse the repository at this point in the history
[Provider]: implement reauth option

Summary of the Pull Request
Implement reauth option for provider by default in case when token is expired.
PR Checklist

 Refers to: #2395
 Tests added/passed.
 Documentation updated.
 Schema updated.
 Release notes added.

Acceptance Steps Performed
=== RUN   TestAccProvider_reAuth
--- PASS: TestAccProvider_reAuth (1.26s)
PASS

Process finished with the exit code 0

Reviewed-by: Anton Sidelnikov
Reviewed-by: Aloento
  • Loading branch information
artem-lifshits authored Dec 28, 2023
1 parent 5a8d0d8 commit 36ef080
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,10 @@ The following arguments are supported:
* `max_retries` - (Optional) Maximum number of retries of HTTP requests failed
due to connection issues.

* `allow_reauth` - (Optional) If set to false, authorization won't be performed automatically if the
initial auth token get expired. It can be set using the `OS_ALLOW_REAUTH` environment variable.
Default: `true`.

* `max_backoff_retries` - (Optional) Maximum number of retries of HTTP requests failed
due to reaching the rate limit. It can be set using the `OS_MAX_BACKOFF_RETRIES` environment
variable. If not set, default value is used.
Expand Down
48 changes: 48 additions & 0 deletions opentelekomcloud/acceptance/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,51 @@ data "opentelekomcloud_networking_network_v2" "ext" {
},
})
}

func TestAccProvider_reAuth(t *testing.T) {
if os.Getenv("TF_ACC") == "" {
t.Skip("TF_ACC not set, skipping OpenTelekomCloud ReAuth test.")
}
type allCases struct {
Case map[string]interface{}
}
cases := map[string]allCases{
"True": {
Case: map[string]interface{}{
"allow_reauth": "true",
},
},
"False": {
Case: map[string]interface{}{
"allow_reauth": "false",
},
},
}

for name, auth := range cases {
p := opentelekomcloud.Provider()

if p.Configure(context.TODO(), terraform.NewResourceConfigRaw(auth.Case)).HasError() {
t.Fatalf("Unexpected err when specifying OpenTelekomCloud")
}
authOpts := p.Meta()

config := authOpts.(*cfg.Config)

switch name {
case "False":
if config.HwClient.ReauthFunc != nil {
t.Fatalf("ReauthFunc was set with disabled reauth")
}

case "True":
oldToken := config.HwClient.TokenID
if err := config.HwClient.ReauthFunc(); err != nil {
t.Fatalf("Error while getting new Token via ReauthFunc")
}
if oldToken == config.HwClient.TokenID {
t.Fatalf("Old token is same as new token")
}
}
}
}
2 changes: 2 additions & 0 deletions opentelekomcloud/common/cfg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Config struct {
AccessKey string
SecretKey string
CACertFile string
AllowReauth bool
ClientCertFile string
ClientKeyFile string
Cloud string
Expand Down Expand Up @@ -481,6 +482,7 @@ func buildClientByPassword(c *Config) error {
ao.Username = c.Username
ao.UserID = c.UserID
ao.Passcode = c.Passcode
ao.AllowReauth = c.AllowReauth
}
return c.genClients(pao, dao)
}
Expand Down
7 changes: 7 additions & 0 deletions opentelekomcloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,12 @@ func Provider() *schema.Provider {
DefaultFunc: schema.EnvDefaultFunc("OS_CLOUD", ""),
Description: common.Descriptions["cloud"],
},
"allow_reauth": {
Type: schema.TypeBool,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("OS_ALLOW_REAUTH", true),
Description: common.Descriptions["allow_reauth"],
},
"max_retries": {
Type: schema.TypeInt,
Optional: true,
Expand Down Expand Up @@ -561,6 +567,7 @@ func providerConfigure(_ context.Context, d *schema.ResourceData, p *schema.Prov
AgencyName: d.Get("agency_name").(string),
AgencyDomainName: d.Get("agency_domain_name").(string),
DelegatedProject: d.Get("delegated_project").(string),
AllowReauth: d.Get("allow_reauth").(bool),
MaxRetries: d.Get("max_retries").(int),
MaxBackoffRetries: d.Get("max_backoff_retries").(int),
BackoffRetryTimeout: d.Get("backoff_retry_timeout").(int),
Expand Down
4 changes: 4 additions & 0 deletions releasenotes/notes/provider_reauth-305df1d464bf6856.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
other:
- |
**[Provider]** Implement reauth feature for provider (`#2399 <https://github.com/opentelekomcloud/terraform-provider-opentelekomcloud/pull/2399>`_)

0 comments on commit 36ef080

Please sign in to comment.