Skip to content

Commit 6928e76

Browse files
authored
Merge pull request #12 from enthought/fix-unknown-values
Move provider validation from ValidateConfig to Configure
2 parents 1fc4822 + 6387e79 commit 6928e76

File tree

2 files changed

+76
-91
lines changed

2 files changed

+76
-91
lines changed

internal/provider/provider.go

Lines changed: 50 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -80,96 +80,6 @@ func (p *quayProvider) Configure(ctx context.Context, req provider.ConfigureRequ
8080
return
8181
}
8282

83-
url := os.Getenv("QUAY_URL")
84-
token := os.Getenv("QUAY_TOKEN")
85-
oauth2Username := os.Getenv("QUAY_OAUTH2_USERNAME")
86-
oauth2Password := os.Getenv("QUAY_OAUTH2_PASSWORD")
87-
oauth2ClientID := os.Getenv("QUAY_OAUTH2_CLIENT_ID")
88-
oauth2TokenURL := os.Getenv("QUAY_OAUTH2_TOKEN_URL")
89-
90-
if !config.Url.IsNull() {
91-
url = config.Url.ValueString()
92-
}
93-
94-
if !config.Token.IsNull() {
95-
token = config.Token.ValueString()
96-
}
97-
98-
if !config.OAuth2Username.IsNull() {
99-
oauth2Username = config.OAuth2Username.ValueString()
100-
}
101-
102-
if !config.OAuth2Password.IsNull() {
103-
oauth2Password = config.OAuth2Password.ValueString()
104-
}
105-
106-
if !config.OAuth2ClientID.IsNull() {
107-
oauth2ClientID = config.OAuth2ClientID.ValueString()
108-
}
109-
110-
if !config.OAuth2TokenURL.IsNull() {
111-
oauth2TokenURL = config.OAuth2TokenURL.ValueString()
112-
}
113-
114-
ctx = tflog.SetField(ctx, "url", url)
115-
ctx = tflog.SetField(ctx, "token", token)
116-
ctx = tflog.SetField(ctx, "oauth2_username", oauth2Username)
117-
ctx = tflog.SetField(ctx, "oauth2_password", oauth2Password)
118-
ctx = tflog.SetField(ctx, "oauth2_client_id", oauth2ClientID)
119-
ctx = tflog.SetField(ctx, "oauth2_token_url", oauth2TokenURL)
120-
ctx = tflog.MaskFieldValuesWithFieldKeys(ctx, "token")
121-
ctx = tflog.MaskFieldValuesWithFieldKeys(ctx, "oauth2_password")
122-
123-
tflog.Debug(ctx, "Creating Quay client")
124-
125-
configuration := &quay_api.Configuration{
126-
DefaultHeader: make(map[string]string),
127-
UserAgent: "OpenAPI-Generator/1.0.0/go",
128-
Debug: false,
129-
Servers: quay_api.ServerConfigurations{
130-
{
131-
URL: url,
132-
Description: "No description provided",
133-
},
134-
},
135-
OperationServers: map[string]quay_api.ServerConfigurations{},
136-
}
137-
138-
if token == "" {
139-
oauth2Config := &oauth2.Config{
140-
ClientID: oauth2ClientID,
141-
Endpoint: oauth2.Endpoint{
142-
TokenURL: oauth2TokenURL,
143-
},
144-
}
145-
146-
oauth2Token, err := oauth2Config.PasswordCredentialsToken(ctx, oauth2Username, oauth2Password)
147-
if err != nil {
148-
resp.Diagnostics.AddError("Error retrieving OAuth2 access token",
149-
"Error retrieving OAuth2 access token: "+err.Error())
150-
return
151-
}
152-
153-
token = oauth2Token.AccessToken
154-
}
155-
156-
configuration.AddDefaultHeader("Authorization", "Bearer "+token)
157-
client := quay_api.NewAPIClient(configuration)
158-
159-
resp.DataSourceData = client
160-
resp.ResourceData = client
161-
162-
tflog.Info(ctx, "Configured Quay client", map[string]any{"success": true})
163-
}
164-
165-
func (p *quayProvider) ValidateConfig(ctx context.Context, req provider.ValidateConfigRequest, resp *provider.ValidateConfigResponse) {
166-
var config quayProviderModel
167-
diags := req.Config.Get(ctx, &config)
168-
resp.Diagnostics.Append(diags...)
169-
if resp.Diagnostics.HasError() {
170-
return
171-
}
172-
17383
if config.Url.IsUnknown() || config.Token.IsUnknown() || config.OAuth2Username.IsUnknown() || config.OAuth2Password.IsUnknown() ||
17484
config.OAuth2ClientID.IsUnknown() || config.OAuth2TokenURL.IsUnknown() {
17585
resp.Diagnostics.AddError(
@@ -284,6 +194,56 @@ func (p *quayProvider) ValidateConfig(ctx context.Context, req provider.Validate
284194
)
285195
return
286196
}
197+
198+
ctx = tflog.SetField(ctx, "url", url)
199+
ctx = tflog.SetField(ctx, "token", token)
200+
ctx = tflog.SetField(ctx, "oauth2_username", oauth2Username)
201+
ctx = tflog.SetField(ctx, "oauth2_password", oauth2Password)
202+
ctx = tflog.SetField(ctx, "oauth2_client_id", oauth2ClientID)
203+
ctx = tflog.SetField(ctx, "oauth2_token_url", oauth2TokenURL)
204+
ctx = tflog.MaskFieldValuesWithFieldKeys(ctx, "token")
205+
ctx = tflog.MaskFieldValuesWithFieldKeys(ctx, "oauth2_password")
206+
207+
tflog.Debug(ctx, "Creating Quay client")
208+
209+
configuration := &quay_api.Configuration{
210+
DefaultHeader: make(map[string]string),
211+
UserAgent: "OpenAPI-Generator/1.0.0/go",
212+
Debug: false,
213+
Servers: quay_api.ServerConfigurations{
214+
{
215+
URL: url,
216+
Description: "No description provided",
217+
},
218+
},
219+
OperationServers: map[string]quay_api.ServerConfigurations{},
220+
}
221+
222+
if token == "" {
223+
oauth2Config := &oauth2.Config{
224+
ClientID: oauth2ClientID,
225+
Endpoint: oauth2.Endpoint{
226+
TokenURL: oauth2TokenURL,
227+
},
228+
}
229+
230+
oauth2Token, err := oauth2Config.PasswordCredentialsToken(ctx, oauth2Username, oauth2Password)
231+
if err != nil {
232+
resp.Diagnostics.AddError("Error retrieving OAuth2 access token",
233+
"Error retrieving OAuth2 access token: "+err.Error())
234+
return
235+
}
236+
237+
token = oauth2Token.AccessToken
238+
}
239+
240+
configuration.AddDefaultHeader("Authorization", "Bearer "+token)
241+
client := quay_api.NewAPIClient(configuration)
242+
243+
resp.DataSourceData = client
244+
resp.ResourceData = client
245+
246+
tflog.Info(ctx, "Configured Quay client", map[string]any{"success": true})
287247
}
288248

289249
func (p *quayProvider) Metadata(_ context.Context, _ provider.MetadataRequest, resp *provider.MetadataResponse) {

internal/provider/provider_test.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ func TestAccProviderValidationWithoutENV(t *testing.T) {
3636
Source: "hashicorp/random",
3737
VersionConstraint: "~> 3.6",
3838
},
39+
"null": {
40+
Source: "hashicorp/null",
41+
VersionConstraint: "~> 3.2",
42+
},
3943
},
4044
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
4145
Steps: []resource.TestStep{
@@ -48,7 +52,7 @@ resource "random_string" "this" {
4852
}
4953
5054
provider "quay" {
51-
url = "https://quay-${random_string.this}.example.com"
55+
url = "https://quay-${random_string.this.result}.example.com"
5256
}
5357
5458
data "quay_organization" "provider_test" {
@@ -57,6 +61,27 @@ data "quay_organization" "provider_test" {
5761
`,
5862
ExpectError: regexp.MustCompile(".*Error: Unknown configuration values"),
5963
},
64+
// Config value from data source
65+
{
66+
PlanOnly: true,
67+
Config: `
68+
data "null_data_source" "token" {
69+
inputs = {
70+
token = "test"
71+
}
72+
}
73+
74+
provider "quay" {
75+
url = "https://quay.example.com"
76+
token = data.null_data_source.token.outputs["token"]
77+
}
78+
79+
data "quay_organization" "provider_test" {
80+
name = "provider_test"
81+
}
82+
`,
83+
ExpectError: regexp.MustCompile(".*Error: Error reading Quay org"),
84+
},
6085
// Token and OAuth2 credentials
6186
{
6287
PlanOnly: true,

0 commit comments

Comments
 (0)