Skip to content

Commit c650fc4

Browse files
committed
Support early_auth_check flag in provider config. Closes #777
This change adds a new `early_auth_check` flag to the provider configuration. It defaults to `true` which causes the provider to perform a dummy request to the GitLab API to verify the configuration. If that is not desired it can be set to `false` which avoids this call. This can be in useful if the GitLab instance is created within the same terraform module as this provider is being used.
1 parent 03fcf11 commit c650fc4

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{
@@ -124,17 +130,20 @@ func init() {
124130
"client_cert": "File path to client certificate when GitLab instance is behind company proxy. File must contain PEM encoded data.",
125131

126132
"client_key": "File path to client key when GitLab instance is behind company proxy. File must contain PEM encoded data.",
133+
134+
"early_auth_check": "Try to authenticate with the `CurrentUser` endpoint during the provider initialization. (experimental, see docs)",
127135
}
128136
}
129137

130138
func providerConfigure(ctx context.Context, p *schema.Provider, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
131139
config := Config{
132-
Token: d.Get("token").(string),
133-
BaseURL: d.Get("base_url").(string),
134-
CACertFile: d.Get("cacert_file").(string),
135-
Insecure: d.Get("insecure").(bool),
136-
ClientCert: d.Get("client_cert").(string),
137-
ClientKey: d.Get("client_key").(string),
140+
Token: d.Get("token").(string),
141+
BaseURL: d.Get("base_url").(string),
142+
CACertFile: d.Get("cacert_file").(string),
143+
Insecure: d.Get("insecure").(bool),
144+
ClientCert: d.Get("client_cert").(string),
145+
ClientKey: d.Get("client_key").(string),
146+
EarlyAuthFail: d.Get("early_auth_check").(bool),
138147
}
139148

140149
client, err := config.Client()

0 commit comments

Comments
 (0)