Skip to content

Commit de3dc9a

Browse files
committed
Use scaffold provider skeleton
1 parent 24699cf commit de3dc9a

File tree

3 files changed

+166
-133
lines changed

3 files changed

+166
-133
lines changed

GNUmakefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ default: reviewable
33
reviewable: build fmt generate test ## Run before committing.
44

55
GOBIN = $(shell pwd)/bin
6+
PROVIDER_SRC_DIR := ./internal/provider
67

78
build: ## Build the provider binary.
89
go mod tidy
@@ -17,7 +18,7 @@ TESTARGS += -test.run $(RUN)
1718
endif
1819

1920
test: ## Run unit tests.
20-
go test $(TESTARGS) ./gitlab
21+
go test $(TESTARGS) $(PROVIDER_SRC_DIR)
2122

2223
TFPROVIDERLINTX_CHECKS = -XAT001=false -XR003=false -XS002=false
2324

@@ -68,7 +69,7 @@ testacc-down: ## Teardown a GitLab instance.
6869
docker-compose down
6970

7071
testacc: ## Run acceptance tests against a GitLab instance.
71-
TF_ACC=1 GITLAB_TOKEN=$(GITLAB_TOKEN) GITLAB_BASE_URL=$(GITLAB_BASE_URL) go test -v ./gitlab $(TESTARGS) -timeout 40m
72+
TF_ACC=1 GITLAB_TOKEN=$(GITLAB_TOKEN) GITLAB_BASE_URL=$(GITLAB_BASE_URL) go test -v $(PROVIDER_SRC_DIR) $(TESTARGS) -timeout 40m
7273

7374
# TOOLS
7475
# Tool dependencies are installed into a project-local /bin folder.

internal/provider/provider.go

Lines changed: 134 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -9,144 +9,150 @@ import (
99
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1010
)
1111

12-
func Provider() *schema.Provider {
13-
provider := &schema.Provider{
14-
Schema: map[string]*schema.Schema{
15-
"token": {
16-
Type: schema.TypeString,
17-
Required: true,
18-
DefaultFunc: schema.EnvDefaultFunc("GITLAB_TOKEN", nil),
19-
Description: "The OAuth2 Token, Project, Group, Personal Access Token or CI Job Token used to connect to GitLab. The OAuth method is used in this provider for authentication (using Bearer authorization token). See https://docs.gitlab.com/ee/api/#authentication for details. It may be sourced from the `GITLAB_TOKEN` environment variable.",
20-
},
21-
"base_url": {
22-
Type: schema.TypeString,
23-
Optional: true,
24-
DefaultFunc: schema.EnvDefaultFunc("GITLAB_BASE_URL", ""),
25-
Description: "This is the target GitLab base API endpoint. Providing a value is a requirement when working with GitLab CE or GitLab Enterprise e.g. `https://my.gitlab.server/api/v4/`. It is optional to provide this value and it can also be sourced from the `GITLAB_BASE_URL` environment variable. The value must end with a slash.",
26-
ValidateFunc: validateApiURLVersion,
27-
},
28-
"cacert_file": {
29-
Type: schema.TypeString,
30-
Optional: true,
31-
Default: "",
32-
Description: "This is a file containing the ca cert to verify the gitlab instance. This is available for use when working with GitLab CE or Gitlab Enterprise with a locally-issued or self-signed certificate chain.",
33-
},
34-
"insecure": {
35-
Type: schema.TypeBool,
36-
Optional: true,
37-
Default: false,
38-
Description: "When set to true this disables SSL verification of the connection to the GitLab instance.",
39-
},
40-
"client_cert": {
41-
Type: schema.TypeString,
42-
Optional: true,
43-
Default: "",
44-
Description: "File path to client certificate when GitLab instance is behind company proxy. File must contain PEM encoded data.",
45-
},
46-
"client_key": {
47-
Type: schema.TypeString,
48-
Optional: true,
49-
Default: "",
50-
Description: "File path to client key when GitLab instance is behind company proxy. File must contain PEM encoded data. Required when `client_cert` is set.",
51-
},
52-
"early_auth_check": {
53-
Type: schema.TypeBool,
54-
Optional: true,
55-
Default: true,
56-
Description: "(Experimental) By default the provider does a dummy request to get the current user in order to verify that the provider configuration is correct and the GitLab API is reachable. 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. This is an experimental feature and may change in the future. Please make sure to always keep backups of your state.",
12+
func init() {
13+
// Set descriptions to support markdown syntax, this will be used in document generation
14+
// and the language server.
15+
schema.DescriptionKind = schema.StringMarkdown
16+
}
17+
18+
func New(version string) func() *schema.Provider {
19+
return func() *schema.Provider {
20+
provider := &schema.Provider{
21+
Schema: map[string]*schema.Schema{
22+
"token": {
23+
Type: schema.TypeString,
24+
Required: true,
25+
DefaultFunc: schema.EnvDefaultFunc("GITLAB_TOKEN", nil),
26+
Description: "The OAuth2 Token, Project, Group, Personal Access Token or CI Job Token used to connect to GitLab. The OAuth method is used in this provider for authentication (using Bearer authorization token). See https://docs.gitlab.com/ee/api/#authentication for details. It may be sourced from the `GITLAB_TOKEN` environment variable.",
27+
},
28+
"base_url": {
29+
Type: schema.TypeString,
30+
Optional: true,
31+
DefaultFunc: schema.EnvDefaultFunc("GITLAB_BASE_URL", ""),
32+
Description: "This is the target GitLab base API endpoint. Providing a value is a requirement when working with GitLab CE or GitLab Enterprise e.g. `https://my.gitlab.server/api/v4/`. It is optional to provide this value and it can also be sourced from the `GITLAB_BASE_URL` environment variable. The value must end with a slash.",
33+
ValidateFunc: func(value interface{}, key string) (ws []string, es []error) {
34+
v := value.(string)
35+
if strings.HasSuffix(v, "/api/v3") || strings.HasSuffix(v, "/api/v3/") {
36+
es = append(es, fmt.Errorf("terraform-provider-gitlab does not support v3 api; please upgrade to /api/v4 in %s", v))
37+
}
38+
return
39+
},
40+
},
41+
"cacert_file": {
42+
Type: schema.TypeString,
43+
Optional: true,
44+
Default: "",
45+
Description: "This is a file containing the ca cert to verify the gitlab instance. This is available for use when working with GitLab CE or Gitlab Enterprise with a locally-issued or self-signed certificate chain.",
46+
},
47+
"insecure": {
48+
Type: schema.TypeBool,
49+
Optional: true,
50+
Default: false,
51+
Description: "When set to true this disables SSL verification of the connection to the GitLab instance.",
52+
},
53+
"client_cert": {
54+
Type: schema.TypeString,
55+
Optional: true,
56+
Default: "",
57+
Description: "File path to client certificate when GitLab instance is behind company proxy. File must contain PEM encoded data.",
58+
},
59+
"client_key": {
60+
Type: schema.TypeString,
61+
Optional: true,
62+
Default: "",
63+
Description: "File path to client key when GitLab instance is behind company proxy. File must contain PEM encoded data. Required when `client_cert` is set.",
64+
},
65+
"early_auth_check": {
66+
Type: schema.TypeBool,
67+
Optional: true,
68+
Default: true,
69+
Description: "(Experimental) By default the provider does a dummy request to get the current user in order to verify that the provider configuration is correct and the GitLab API is reachable. 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. This is an experimental feature and may change in the future. Please make sure to always keep backups of your state.",
70+
},
5771
},
58-
},
5972

60-
DataSourcesMap: map[string]*schema.Resource{
61-
"gitlab_group": dataSourceGitlabGroup(),
62-
"gitlab_group_membership": dataSourceGitlabGroupMembership(),
63-
"gitlab_project": dataSourceGitlabProject(),
64-
"gitlab_project_protected_branch": dataSourceGitlabProjectProtectedBranch(),
65-
"gitlab_project_protected_branches": dataSourceGitlabProjectProtectedBranches(),
66-
"gitlab_projects": dataSourceGitlabProjects(),
67-
"gitlab_user": dataSourceGitlabUser(),
68-
"gitlab_users": dataSourceGitlabUsers(),
69-
},
73+
DataSourcesMap: map[string]*schema.Resource{
74+
"gitlab_group": dataSourceGitlabGroup(),
75+
"gitlab_group_membership": dataSourceGitlabGroupMembership(),
76+
"gitlab_project": dataSourceGitlabProject(),
77+
"gitlab_project_protected_branch": dataSourceGitlabProjectProtectedBranch(),
78+
"gitlab_project_protected_branches": dataSourceGitlabProjectProtectedBranches(),
79+
"gitlab_projects": dataSourceGitlabProjects(),
80+
"gitlab_user": dataSourceGitlabUser(),
81+
"gitlab_users": dataSourceGitlabUsers(),
82+
},
7083

71-
ResourcesMap: map[string]*schema.Resource{
72-
"gitlab_branch_protection": resourceGitlabBranchProtection(),
73-
"gitlab_tag_protection": resourceGitlabTagProtection(),
74-
"gitlab_group": resourceGitlabGroup(),
75-
"gitlab_group_custom_attribute": resourceGitlabGroupCustomAttribute(),
76-
"gitlab_project": resourceGitlabProject(),
77-
"gitlab_project_custom_attribute": resourceGitlabProjectCustomAttribute(),
78-
"gitlab_label": resourceGitlabLabel(),
79-
"gitlab_managed_license": resourceGitlabManagedLicense(),
80-
"gitlab_group_label": resourceGitlabGroupLabel(),
81-
"gitlab_pipeline_schedule": resourceGitlabPipelineSchedule(),
82-
"gitlab_pipeline_schedule_variable": resourceGitlabPipelineScheduleVariable(),
83-
"gitlab_pipeline_trigger": resourceGitlabPipelineTrigger(),
84-
"gitlab_project_hook": resourceGitlabProjectHook(),
85-
"gitlab_deploy_key": resourceGitlabDeployKey(),
86-
"gitlab_deploy_key_enable": resourceGitlabDeployEnableKey(),
87-
"gitlab_deploy_token": resourceGitlabDeployToken(),
88-
"gitlab_user": resourceGitlabUser(),
89-
"gitlab_user_custom_attribute": resourceGitlabUserCustomAttribute(),
90-
"gitlab_project_membership": resourceGitlabProjectMembership(),
91-
"gitlab_group_membership": resourceGitlabGroupMembership(),
92-
"gitlab_project_variable": resourceGitlabProjectVariable(),
93-
"gitlab_group_variable": resourceGitlabGroupVariable(),
94-
"gitlab_project_access_token": resourceGitlabProjectAccessToken(),
95-
"gitlab_project_cluster": resourceGitlabProjectCluster(),
96-
"gitlab_service_slack": resourceGitlabServiceSlack(),
97-
"gitlab_service_jira": resourceGitlabServiceJira(),
98-
"gitlab_service_microsoft_teams": resourceGitlabServiceMicrosoftTeams(),
99-
"gitlab_service_github": resourceGitlabServiceGithub(),
100-
"gitlab_service_pipelines_email": resourceGitlabServicePipelinesEmail(),
101-
"gitlab_project_share_group": resourceGitlabProjectShareGroup(),
102-
"gitlab_group_cluster": resourceGitlabGroupCluster(),
103-
"gitlab_group_ldap_link": resourceGitlabGroupLdapLink(),
104-
"gitlab_instance_cluster": resourceGitlabInstanceCluster(),
105-
"gitlab_project_mirror": resourceGitlabProjectMirror(),
106-
"gitlab_project_level_mr_approvals": resourceGitlabProjectLevelMRApprovals(),
107-
"gitlab_project_approval_rule": resourceGitlabProjectApprovalRule(),
108-
"gitlab_instance_variable": resourceGitlabInstanceVariable(),
109-
"gitlab_project_freeze_period": resourceGitlabProjectFreezePeriod(),
110-
"gitlab_group_share_group": resourceGitlabGroupShareGroup(),
111-
"gitlab_project_badge": resourceGitlabProjectBadge(),
112-
"gitlab_group_badge": resourceGitlabGroupBadge(),
113-
"gitlab_repository_file": resourceGitLabRepositoryFile(),
114-
},
115-
}
84+
ResourcesMap: map[string]*schema.Resource{
85+
"gitlab_branch_protection": resourceGitlabBranchProtection(),
86+
"gitlab_tag_protection": resourceGitlabTagProtection(),
87+
"gitlab_group": resourceGitlabGroup(),
88+
"gitlab_group_custom_attribute": resourceGitlabGroupCustomAttribute(),
89+
"gitlab_project": resourceGitlabProject(),
90+
"gitlab_project_custom_attribute": resourceGitlabProjectCustomAttribute(),
91+
"gitlab_label": resourceGitlabLabel(),
92+
"gitlab_managed_license": resourceGitlabManagedLicense(),
93+
"gitlab_group_label": resourceGitlabGroupLabel(),
94+
"gitlab_pipeline_schedule": resourceGitlabPipelineSchedule(),
95+
"gitlab_pipeline_schedule_variable": resourceGitlabPipelineScheduleVariable(),
96+
"gitlab_pipeline_trigger": resourceGitlabPipelineTrigger(),
97+
"gitlab_project_hook": resourceGitlabProjectHook(),
98+
"gitlab_deploy_key": resourceGitlabDeployKey(),
99+
"gitlab_deploy_key_enable": resourceGitlabDeployEnableKey(),
100+
"gitlab_deploy_token": resourceGitlabDeployToken(),
101+
"gitlab_user": resourceGitlabUser(),
102+
"gitlab_user_custom_attribute": resourceGitlabUserCustomAttribute(),
103+
"gitlab_project_membership": resourceGitlabProjectMembership(),
104+
"gitlab_group_membership": resourceGitlabGroupMembership(),
105+
"gitlab_project_variable": resourceGitlabProjectVariable(),
106+
"gitlab_group_variable": resourceGitlabGroupVariable(),
107+
"gitlab_project_access_token": resourceGitlabProjectAccessToken(),
108+
"gitlab_project_cluster": resourceGitlabProjectCluster(),
109+
"gitlab_service_slack": resourceGitlabServiceSlack(),
110+
"gitlab_service_jira": resourceGitlabServiceJira(),
111+
"gitlab_service_microsoft_teams": resourceGitlabServiceMicrosoftTeams(),
112+
"gitlab_service_github": resourceGitlabServiceGithub(),
113+
"gitlab_service_pipelines_email": resourceGitlabServicePipelinesEmail(),
114+
"gitlab_project_share_group": resourceGitlabProjectShareGroup(),
115+
"gitlab_group_cluster": resourceGitlabGroupCluster(),
116+
"gitlab_group_ldap_link": resourceGitlabGroupLdapLink(),
117+
"gitlab_instance_cluster": resourceGitlabInstanceCluster(),
118+
"gitlab_project_mirror": resourceGitlabProjectMirror(),
119+
"gitlab_project_level_mr_approvals": resourceGitlabProjectLevelMRApprovals(),
120+
"gitlab_project_approval_rule": resourceGitlabProjectApprovalRule(),
121+
"gitlab_instance_variable": resourceGitlabInstanceVariable(),
122+
"gitlab_project_freeze_period": resourceGitlabProjectFreezePeriod(),
123+
"gitlab_group_share_group": resourceGitlabGroupShareGroup(),
124+
"gitlab_project_badge": resourceGitlabProjectBadge(),
125+
"gitlab_group_badge": resourceGitlabGroupBadge(),
126+
"gitlab_repository_file": resourceGitLabRepositoryFile(),
127+
},
128+
}
116129

117-
provider.ConfigureContextFunc = func(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
118-
return providerConfigure(ctx, provider, d)
130+
provider.ConfigureContextFunc = configure(version, provider)
131+
return provider
119132
}
120133

121-
return provider
122134
}
123135

124-
func providerConfigure(ctx context.Context, p *schema.Provider, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
125-
config := Config{
126-
Token: d.Get("token").(string),
127-
BaseURL: d.Get("base_url").(string),
128-
CACertFile: d.Get("cacert_file").(string),
129-
Insecure: d.Get("insecure").(bool),
130-
ClientCert: d.Get("client_cert").(string),
131-
ClientKey: d.Get("client_key").(string),
132-
EarlyAuthFail: d.Get("early_auth_check").(bool),
133-
}
136+
func configure(version string, p *schema.Provider) func(context.Context, *schema.ResourceData) (interface{}, diag.Diagnostics) {
137+
return func(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
138+
config := Config{
139+
Token: d.Get("token").(string),
140+
BaseURL: d.Get("base_url").(string),
141+
CACertFile: d.Get("cacert_file").(string),
142+
Insecure: d.Get("insecure").(bool),
143+
ClientCert: d.Get("client_cert").(string),
144+
ClientKey: d.Get("client_key").(string),
145+
EarlyAuthFail: d.Get("early_auth_check").(bool),
146+
}
134147

135-
client, err := config.Client()
136-
if err != nil {
137-
return nil, diag.FromErr(err)
138-
}
139-
140-
userAgent := p.UserAgent("terraform-provider-gitlab", "")
141-
client.UserAgent = userAgent
148+
client, err := config.Client()
149+
if err != nil {
150+
return nil, diag.FromErr(err)
151+
}
142152

143-
return client, nil
144-
}
153+
userAgent := p.UserAgent("terraform-provider-gitlab", version)
154+
client.UserAgent = userAgent
145155

146-
func validateApiURLVersion(value interface{}, key string) (ws []string, es []error) {
147-
v := value.(string)
148-
if strings.HasSuffix(v, "/api/v3") || strings.HasSuffix(v, "/api/v3/") {
149-
es = append(es, fmt.Errorf("terraform-provider-gitlab does not support v3 api; please upgrade to /api/v4 in %s", v))
156+
return client, nil
150157
}
151-
return
152158
}

main.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,37 @@
11
package main
22

33
import (
4-
"github.com/gitlabhq/terraform-provider-gitlab/gitlab"
4+
"context"
5+
"flag"
6+
"log"
7+
8+
"github.com/gitlabhq/terraform-provider-gitlab/internal/provider"
59
"github.com/hashicorp/terraform-plugin-sdk/v2/plugin"
610
)
711

12+
var (
13+
// these will be set by the goreleaser configuration
14+
// to appropriate values for the compiled binary
15+
version string = "dev"
16+
)
17+
818
func main() {
9-
plugin.Serve(&plugin.ServeOpts{
10-
ProviderFunc: gitlab.Provider})
19+
20+
var debugMode bool
21+
22+
flag.BoolVar(&debugMode, "debug", false, "set to true to run the provider with support for debuggers like delve")
23+
flag.Parse()
24+
25+
opts := &plugin.ServeOpts{ProviderFunc: provider.New(version)}
26+
27+
if debugMode {
28+
// TODO: update this string with the full name of your provider as used in your configs
29+
err := plugin.Debug(context.Background(), "registry.terraform.io/providers/gitlabhq/gitlab", opts)
30+
if err != nil {
31+
log.Fatal(err.Error())
32+
}
33+
return
34+
}
35+
36+
plugin.Serve(opts)
1137
}

0 commit comments

Comments
 (0)