Skip to content

Commit b4d4f8d

Browse files
authored
Merge pull request #787 from timofurrer/feature/early-cred-fail
Support `early_auth_check` flag in provider config. Closes #777
2 parents 28c19f9 + c650fc4 commit b4d4f8d

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

docs/index.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,8 @@ The following arguments are supported in the `provider` block:
7474
* `client_cert` - (Optional) File path to client certificate when GitLab instance is behind company proxy. File must contain PEM encoded data.
7575

7676
* `client_key` - (Optional) File path to client key when GitLab instance is behind company proxy. File must contain PEM encoded data. Required when `client_cert` is set.
77+
78+
* `early_auth_check` - (Optional) (experimental) By default the provider does a dummy request to get the current user in order
79+
to verify that the provider configuration is correct and the GitLab API is reachable.
80+
Turn it off, to skip this check. This may be useful if the GitLab instance does not yet exist and is created within the same terraform module.
81+
This is an experimental feature and may change in the future. Please make sure to always keep backups of your state.

gitlab/config.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ import (
1212

1313
// Config is per-provider, specifies where to connect to gitlab
1414
type Config struct {
15-
Token string
16-
BaseURL string
17-
Insecure bool
18-
CACertFile string
19-
ClientCert string
20-
ClientKey string
15+
Token string
16+
BaseURL string
17+
Insecure bool
18+
CACertFile string
19+
ClientCert string
20+
ClientKey string
21+
EarlyAuthFail bool
2122
}
2223

2324
// Client returns a *gitlab.Client to interact with the configured gitlab instance
@@ -75,7 +76,9 @@ func (c *Config) Client() (*gitlab.Client, error) {
7576
}
7677

7778
// Test the credentials by checking we can get information about the authenticated user.
78-
_, _, err = client.Users.CurrentUser()
79+
if c.EarlyAuthFail {
80+
_, _, err = client.Users.CurrentUser()
81+
}
7982

8083
return client, err
8184
}

gitlab/provider.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ func Provider() *schema.Provider {
4949
Default: "",
5050
Description: descriptions["client_key"],
5151
},
52+
"early_auth_check": {
53+
Type: schema.TypeBool,
54+
Optional: true,
55+
Default: true,
56+
Description: descriptions["early_auth_check"],
57+
},
5258
},
5359

5460
DataSourcesMap: map[string]*schema.Resource{
@@ -127,17 +133,20 @@ func init() {
127133
"client_cert": "File path to client certificate when GitLab instance is behind company proxy. File must contain PEM encoded data.",
128134

129135
"client_key": "File path to client key when GitLab instance is behind company proxy. File must contain PEM encoded data.",
136+
137+
"early_auth_check": "Try to authenticate with the `CurrentUser` endpoint during the provider initialization. (experimental, see docs)",
130138
}
131139
}
132140

133141
func providerConfigure(ctx context.Context, p *schema.Provider, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
134142
config := Config{
135-
Token: d.Get("token").(string),
136-
BaseURL: d.Get("base_url").(string),
137-
CACertFile: d.Get("cacert_file").(string),
138-
Insecure: d.Get("insecure").(bool),
139-
ClientCert: d.Get("client_cert").(string),
140-
ClientKey: d.Get("client_key").(string),
143+
Token: d.Get("token").(string),
144+
BaseURL: d.Get("base_url").(string),
145+
CACertFile: d.Get("cacert_file").(string),
146+
Insecure: d.Get("insecure").(bool),
147+
ClientCert: d.Get("client_cert").(string),
148+
ClientKey: d.Get("client_key").(string),
149+
EarlyAuthFail: d.Get("early_auth_check").(bool),
141150
}
142151

143152
client, err := config.Client()

0 commit comments

Comments
 (0)