Skip to content

Commit

Permalink
TokenURL goes into provider config
Browse files Browse the repository at this point in the history
  • Loading branch information
1riatsila1 committed Sep 3, 2024
1 parent 75709ce commit 11a2137
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
34 changes: 27 additions & 7 deletions pkg/config-api-provider/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ var (
_ provider.Provider = &uxiConfigurationProvider{}
)

var tokenURLDefault = "https://sso.common.cloud.hpe.com/as/token.oauth2"

// New is a helper function to simplify provider server and testing implementation.
func New(version string) func() provider.Provider {
return func() provider.Provider {
Expand All @@ -38,6 +40,7 @@ type uxiProviderModel struct {
Host types.String `tfsdk:"host"`
ClientID types.String `tfsdk:"client_id"`
ClientSecret types.String `tfsdk:"client_secret"`
TokenURL types.String `tfsdk:"token_url"`
}

type uxiConfigurationProvider struct {
Expand All @@ -56,9 +59,10 @@ func (p *uxiConfigurationProvider) Metadata(_ context.Context, _ provider.Metada
// Schema defines the provider-level schema for configuration data.
func (p *uxiConfigurationProvider) Schema(_ context.Context, _ provider.SchemaRequest, resp *provider.SchemaResponse) {
resp.Schema = schema.Schema{Attributes: map[string]schema.Attribute{
"host": schema.StringAttribute{Required: true},
"client_id": schema.StringAttribute{Required: true},
"client_secret": schema.StringAttribute{Required: true, Sensitive: true},
"host": schema.StringAttribute{Optional: true},
"client_id": schema.StringAttribute{Optional: true},
"client_secret": schema.StringAttribute{Optional: true, Sensitive: true},
"token_url": schema.StringAttribute{Optional: true},
}}
}

Expand Down Expand Up @@ -103,13 +107,23 @@ func (p *uxiConfigurationProvider) Configure(ctx context.Context, req provider.C
)

Check warning on line 107 in pkg/config-api-provider/provider/provider.go

View check run for this annotation

Codecov / codecov/patch

pkg/config-api-provider/provider/provider.go#L102-L107

Added lines #L102 - L107 were not covered by tests
}

if config.TokenURL.IsUnknown() {
resp.Diagnostics.AddAttributeError(
path.Root("token_url"),
"Unknown Token URL",
"The provider cannot create the UXI API client as there is an unknown configuration value for the Token URL. "+
"Either target apply the source of the value first, set the value statically in the configuration, or use the TOKEN_URL environment variable.",
)

Check warning on line 116 in pkg/config-api-provider/provider/provider.go

View check run for this annotation

Codecov / codecov/patch

pkg/config-api-provider/provider/provider.go#L111-L116

Added lines #L111 - L116 were not covered by tests
}

if resp.Diagnostics.HasError() {
return

Check warning on line 120 in pkg/config-api-provider/provider/provider.go

View check run for this annotation

Codecov / codecov/patch

pkg/config-api-provider/provider/provider.go#L120

Added line #L120 was not covered by tests
}

host := os.Getenv("UXI_HOST")
clientID := os.Getenv("CLIENT_ID")
clientSecret := os.Getenv("CLIENT_SECRET")
tokenURL := os.Getenv("TOKEN_URL")

if !config.Host.IsNull() {
host = config.Host.ValueString()
Expand All @@ -123,6 +137,10 @@ func (p *uxiConfigurationProvider) Configure(ctx context.Context, req provider.C
clientSecret = config.ClientSecret.ValueString()
}

if !config.TokenURL.IsNull() {
tokenURL = config.TokenURL.ValueString()
}

// If any of the expected configurations are missing, return
// errors with provider-specific guidance.

Expand Down Expand Up @@ -156,6 +174,10 @@ func (p *uxiConfigurationProvider) Configure(ctx context.Context, req provider.C
)

Check warning on line 174 in pkg/config-api-provider/provider/provider.go

View check run for this annotation

Codecov / codecov/patch

pkg/config-api-provider/provider/provider.go#L168-L174

Added lines #L168 - L174 were not covered by tests
}

if tokenURL == "" {
tokenURL = tokenURLDefault

Check warning on line 178 in pkg/config-api-provider/provider/provider.go

View check run for this annotation

Codecov / codecov/patch

pkg/config-api-provider/provider/provider.go#L178

Added line #L178 was not covered by tests
}

if resp.Diagnostics.HasError() {
return

Check warning on line 182 in pkg/config-api-provider/provider/provider.go

View check run for this annotation

Codecov / codecov/patch

pkg/config-api-provider/provider/provider.go#L182

Added line #L182 was not covered by tests
}
Expand All @@ -164,7 +186,7 @@ func (p *uxiConfigurationProvider) Configure(ctx context.Context, req provider.C
uxiConfiguration := config_api_client.NewConfiguration()
uxiConfiguration.Host = host
uxiConfiguration.Scheme = "https"
uxiConfiguration.HTTPClient = getHttpClient(clientID, clientSecret)
uxiConfiguration.HTTPClient = getHttpClient(clientID, clientSecret, tokenURL)
uxiClient := config_api_client.NewAPIClient(uxiConfiguration)

resp.DataSourceData = uxiClient
Expand Down Expand Up @@ -192,9 +214,7 @@ func (p *uxiConfigurationProvider) Resources(_ context.Context) []func() resourc
}
}

func getHttpClient(clientID string, clientSecret string) *http.Client {
tokenURL := "https://sso.common.cloud.hpe.com/as/token.oauth2"

func getHttpClient(clientID string, clientSecret string, tokenURL string) *http.Client {
// Set up the client credentials config
config := &clientcredentials.Config{
ClientID: clientID,
Expand Down
1 change: 1 addition & 0 deletions pkg/config-api-provider/test/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const (
host = "test.api.capenetworks.com"
client_id = "client_id"
client_secret = "client_secret"
token_url = "https://test.sso.common.cloud.hpe.com/as/token.oauth2"
}`
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/config-api-provider/test/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func StructToMap(obj interface{}) map[string]interface{} {
}

func MockOAuth() {
gock.New("https://sso.common.cloud.hpe.com").
gock.New("https://test.sso.common.cloud.hpe.com").
Post("/as/token.oauth2").
MatchHeader("Content-Type", "application/x-www-form-urlencoded").
Reply(200).
Expand Down

0 comments on commit 11a2137

Please sign in to comment.