Skip to content

Commit

Permalink
feat: allow configuring auth token using UNLEASH_AUTH_TOKEN env (#106)
Browse files Browse the repository at this point in the history
* feat: allow configuring auth token using UNLEASH_AUTH_TOKEN env

* chore: add test for configValue function
  • Loading branch information
JonasBak authored Jan 3, 2024
1 parent c3f2cb5 commit 482f0bd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
10 changes: 7 additions & 3 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (p *UnleashProvider) Metadata(ctx context.Context, req provider.MetadataReq

func unleashClient(ctx context.Context, config *UnleashConfiguration, diagnostics *diag.Diagnostics) *unleash.APIClient {
base_url := strings.TrimSuffix(configValue(config.BaseUrl, "UNLEASH_URL"), "/")
authorization := configValue(config.Authorization, "AUTH_TOKEN")
authorization := configValue(config.Authorization, "AUTH_TOKEN", "UNLEASH_AUTH_TOKEN")
mustHave("base_url", base_url, diagnostics)
mustHave("authorization", authorization, diagnostics)

Expand Down Expand Up @@ -93,9 +93,13 @@ You can check a complete example [here](https://github.com/Unleash/terraform-pro
}
}

func configValue(configValue basetypes.StringValue, env string) string {
func configValue(configValue basetypes.StringValue, envs ...string) string {
if configValue.IsNull() {
return os.Getenv(env)
for _, env := range envs {
if val := os.Getenv(env); val != "" {
return val
}
}
}
return configValue.ValueString()
}
Expand Down
13 changes: 13 additions & 0 deletions internal/provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/providerserver"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -57,3 +58,15 @@ func Test_provider_checkIsSupportedVersion_560(t *testing.T) {
CheckIsSupportedVersion("5.6.0", &diags)
assert.False(t, diags.HasError())
}

func Test_provider_configValue(t *testing.T) {
t.Setenv("FOO", "foo")
t.Setenv("BAR", "bar")
t.Setenv("BAZ", "baz")
assert.Equal(t, "foo", configValue(types.StringValue("foo"), "BAR"))
assert.Equal(t, "", configValue(types.StringNull(), "SOME_ENV"))
assert.Equal(t, "bar", configValue(types.StringNull(), "BAR"))
assert.Equal(t, "bar", configValue(types.StringNull(), "BAR", "BAZ"))
assert.Equal(t, "baz", configValue(types.StringNull(), "QUX", "BAZ"))
assert.Equal(t, "bar", configValue(types.StringNull(), "QUX", "BAR", "BAZ"))
}

0 comments on commit 482f0bd

Please sign in to comment.