in internal/provider/provider.go [93:258]
func (p *GitLabProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
// Parse the provider configuration
var config GitLabProviderModel
resp.Diagnostics.Append(req.Config.Get(ctx, &config)...)
if resp.Diagnostics.HasError() {
return
}
// Prevent an unexpectedly misconfigured client, if Terraform configuration values are only known after another resource is applied.
if config.Token.IsUnknown() {
resp.Diagnostics.AddAttributeError(
path.Root("token"),
"Unknown GitLab Token",
"The provider cannot create the GitLab API client as there is an unknown configuration value for the GitLab Token. "+
"Either apply the source of the value first, set the token attribute value statically in the configuration, or use the GITLAB_TOKEN environment variable.",
)
}
if config.BaseUrl.IsUnknown() {
resp.Diagnostics.AddAttributeError(
path.Root("base_url"),
"Unknown GitLab Base URL for the API endpoint",
"The provider cannot create the GitLab API client as there is an unknown configuration value for the GitLab Base URL. "+
"Either apply the source of the value first, set the token attribute value statically in the configuration, or use the GITLAB_BASE_URL environment variable.",
)
}
if config.CACertFile.IsUnknown() {
resp.Diagnostics.AddAttributeError(
path.Root("cacert_file"),
"Unknown GitLab CA Certificate File",
"The provider cannot create the GitLab API client as there is an unknown configuration value for the GitLab CA Certificate File. "+
"Either apply the source of the value first, set the token attribute value statically in the configuration.",
)
}
if config.Insecure.IsUnknown() {
resp.Diagnostics.AddAttributeError(
path.Root("insecure"),
"Unknown GitLab Insecure Flag Value",
"The provider cannot create the GitLab API client as there is an unknown configuration value for the GitLab Insecure flag. "+
"Either apply the source of the value first, set the token attribute value statically in the configuration.",
)
}
if config.ClientCert.IsUnknown() {
resp.Diagnostics.AddAttributeError(
path.Root("client_cert"),
"Unknown GitLab Client Certificate",
"The provider cannot create the GitLab API client as there is an unknown configuration value for the GitLab Client Certificate. "+
"Either apply the source of the value first, set the token attribute value statically in the configuration.",
)
}
if config.ClientKey.IsUnknown() {
resp.Diagnostics.AddAttributeError(
path.Root("client_key"),
"Unknown GitLab Client Key",
"The provider cannot create the GitLab API client as there is an unknown configuration value for the GitLab Client Key. "+
"Either apply the source of the value first, set the token attribute value statically in the configuration.",
)
}
if config.EarlyAuthCheck.IsUnknown() {
resp.Diagnostics.AddAttributeError(
path.Root("early_auth_check"),
"Unknown GitLab Early Auth Check Flag Value",
"The provider cannot create the GitLab API client as there is an unknown configuration value for the GitLab Early Auth Check flag. "+
"Either apply the source of the value first, set the token attribute value statically in the configuration, or use the GITLAB_EARLY_AUTH_CHECK environment variable.",
)
}
if config.Headers.IsUnknown() {
resp.Diagnostics.AddAttributeError(
path.Root("headers"),
"Unknown Headers for the GitLab API calls",
"The provider cannot create the GitLab API client as there is an unknown configuration value for the GitLab Headers flag. "+
"Either apply the source of the value first or set the headers attribute value statically in the configuration.",
)
}
// If no value is explicitly provided for Retries, default to 10
if config.Retries.IsUnknown() || config.Retries.IsNull() {
config.Retries = types.Int64Value(10)
}
earlyAuthCheck, err := utils.ParseConfigBoolFromEnv("GITLAB_EARLY_AUTH_CHECK", true)
if err != nil {
resp.Diagnostics.AddAttributeError(
path.Root("early_auth_check"),
"Invalid GitLab Early Auth Check Flag Value from Env Variable",
err.Error(),
)
}
if resp.Diagnostics.HasError() {
return
}
// Provider Configuration containing the values after evaluation of defaults etc.
// Initialized with the defaults which get overridden later if config is set.
evaluatedConfig := api.Config{
Token: os.Getenv("GITLAB_TOKEN"),
BaseURL: os.Getenv("GITLAB_BASE_URL"),
CACertFile: "",
Insecure: false,
ClientCert: "",
ClientKey: "",
EarlyAuthFail: earlyAuthCheck,
Headers: nil,
}
// Evaluate Provider Attribute Default values now that they are all "known"
if !config.Token.IsNull() {
evaluatedConfig.Token = config.Token.ValueString()
}
if !config.BaseUrl.IsNull() {
evaluatedConfig.BaseURL = config.BaseUrl.ValueString()
}
if !config.CACertFile.IsNull() {
evaluatedConfig.CACertFile = config.CACertFile.ValueString()
}
if !config.Insecure.IsNull() {
evaluatedConfig.Insecure = config.Insecure.ValueBool()
}
if !config.ClientCert.IsNull() {
evaluatedConfig.ClientCert = config.ClientCert.ValueString()
}
if !config.ClientKey.IsNull() {
evaluatedConfig.ClientKey = config.ClientKey.ValueString()
}
if !config.EarlyAuthCheck.IsNull() {
evaluatedConfig.EarlyAuthFail = config.EarlyAuthCheck.ValueBool()
}
if !config.Retries.IsNull() {
evaluatedConfig.Retries = int(config.Retries.ValueInt64())
}
if !config.Headers.IsNull() {
// Convert the terraform types.Map (map[str]attr.Value) into one that have types.String as the key
stringMap := make(map[string]types.String, len(config.Headers.Elements()))
resp.Diagnostics.Append(config.Headers.ElementsAs(ctx, &stringMap, false)...)
// Further reduce that to ma[string]string
headersMap := map[string]any{}
for key, val := range stringMap {
headersMap[key] = val.ValueString()
}
evaluatedConfig.Headers = headersMap
}
// TODO(@timofurrer): validate configuration values
// Configure our logger masking
ctx = utils.ApplyLogMaskingToContext(ctx)
// Creating a new GitLab Client from the provider configuration
clientFactory := newGitLabClient(evaluatedConfig, req.TerraformVersion, p.version)
gitlabClient, err := clientFactory(ctx)
if err != nil {
resp.Diagnostics.AddError("Failed to create GitLab Client from provider configuration", err.Error())
return
}
// Attach the client to the response so that it will be available for the Data Sources and Resources
resp.DataSourceData = &GitLabDatasourceData{
Client: gitlabClient,
}
resp.ResourceData = &GitLabResourceData{
Client: gitlabClient,
NewGitLabClient: clientFactory,
}
}