Skip to content

Commit 43f9631

Browse files
armsnydertimofurrer
authored andcommitted
Refactor: Decentralized resources
1 parent dad475f commit 43f9631

File tree

52 files changed

+148
-156
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+148
-156
lines changed

internal/provider/data_source_gitlab_group.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/xanzy/go-gitlab"
1111
)
1212

13-
func dataSourceGitlabGroup() *schema.Resource {
13+
var _ = registerDataSource("gitlab_group", func() *schema.Resource {
1414
return &schema.Resource{
1515
Description: "Provide details about a specific group in the gitlab provider.\n\n" +
1616
"> **Note**: exactly one of group_id or full_path must be provided.",
@@ -93,7 +93,7 @@ func dataSourceGitlabGroup() *schema.Resource {
9393
},
9494
},
9595
}
96-
}
96+
})
9797

9898
func dataSourceGitlabGroupRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
9999
client := meta.(*gitlab.Client)

internal/provider/data_source_gitlab_group_membership.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"github.com/xanzy/go-gitlab"
1414
)
1515

16-
func dataSourceGitlabGroupMembership() *schema.Resource {
16+
var _ = registerDataSource("gitlab_group_membership", func() *schema.Resource {
1717
return &schema.Resource{
1818
Description: "Provide details about a list of group members in the gitlab provider. The results include id, username, name and more about the requested members.\n\n" +
1919
"> **Note**: exactly one of group_id or full_path must be provided.",
@@ -96,7 +96,7 @@ func dataSourceGitlabGroupMembership() *schema.Resource {
9696
},
9797
},
9898
}
99-
}
99+
})
100100

101101
func dataSourceGitlabGroupMembershipRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
102102
client := meta.(*gitlab.Client)

internal/provider/data_source_gitlab_project.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/xanzy/go-gitlab"
1313
)
1414

15-
func dataSourceGitlabProject() *schema.Resource {
15+
var _ = registerDataSource("gitlab_project", func() *schema.Resource {
1616
return &schema.Resource{
1717
Description: "Provide details about a specific project in the gitlab provider. The results include the name of the project, path, description, default branch, etc.",
1818

@@ -201,7 +201,7 @@ func dataSourceGitlabProject() *schema.Resource {
201201
},
202202
},
203203
}
204-
}
204+
})
205205

206206
func dataSourceGitlabProjectRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
207207
client := meta.(*gitlab.Client)

internal/provider/data_source_gitlab_project_protected_branch.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"github.com/xanzy/go-gitlab"
1212
)
1313

14-
func dataSourceGitlabProjectProtectedBranch() *schema.Resource {
14+
var _ = registerDataSource("gitlab_project_protected_branch", func() *schema.Resource {
1515
return &schema.Resource{
1616
Description: "Provides details about a specific protected branch in a given project.",
1717

@@ -48,7 +48,7 @@ func dataSourceGitlabProjectProtectedBranch() *schema.Resource {
4848
},
4949
},
5050
}
51-
}
51+
})
5252

5353
func dataSourceGitlabProjectProtectedBranchSchemaAccessLevels() *schema.Schema {
5454
return &schema.Schema{

internal/provider/data_source_gitlab_project_protected_branches.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"github.com/xanzy/go-gitlab"
1212
)
1313

14-
func dataSourceGitlabProjectProtectedBranches() *schema.Resource {
14+
var _ = registerDataSource("gitlab_project_protected_branches", func() *schema.Resource {
1515
return &schema.Resource{
1616
Description: "Provides details about all protected branches in a given project.",
1717

@@ -55,7 +55,7 @@ func dataSourceGitlabProjectProtectedBranches() *schema.Resource {
5555
},
5656
},
5757
}
58-
}
58+
})
5959

6060
type stateProtectedBranch struct {
6161
ID int `json:"id,omitempty" mapstructure:"id,omitempty"`

internal/provider/data_source_gitlab_projects.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func flattenProjects(projects []*gitlab.Project) (values []map[string]interface{
171171
return values
172172
}
173173

174-
func dataSourceGitlabProjects() *schema.Resource {
174+
var _ = registerDataSource("gitlab_projects", func() *schema.Resource {
175175
// lintignore: S024 // TODO: Resolve this tfproviderlint issue
176176
return &schema.Resource{
177177
Description: "Provide details about a list of projects in the Gitlab provider. Listing all projects and group projects with [project filtering](https://docs.gitlab.com/ee/api/projects.html#list-user-projects) or [group project filtering](https://docs.gitlab.com/ee/api/groups.html#list-a-groups-projects) is supported.\n\n" +
@@ -776,7 +776,7 @@ func dataSourceGitlabProjects() *schema.Resource {
776776
},
777777
},
778778
}
779-
}
779+
})
780780

781781
// CRUD methods
782782

internal/provider/data_source_gitlab_user.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
gitlab "github.com/xanzy/go-gitlab"
1212
)
1313

14-
func dataSourceGitlabUser() *schema.Resource {
14+
var _ = registerDataSource("gitlab_user", func() *schema.Resource {
1515
return &schema.Resource{
1616
Description: "Provide details about a specific user in the gitlab provider. Especially the ability to lookup the id for linking to other resources.\n\n" +
1717
"-> Some attributes might not be returned depending on if you're an admin or not. Please refer to [Gitlab documentation](https://docs.gitlab.com/ce/api/users.html#single-user) for more details.",
@@ -170,7 +170,7 @@ func dataSourceGitlabUser() *schema.Resource {
170170
},
171171
},
172172
}
173-
}
173+
})
174174

175175
func dataSourceGitlabUserRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
176176
client := meta.(*gitlab.Client)

internal/provider/data_source_gitlab_users.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
gitlab "github.com/xanzy/go-gitlab"
1414
)
1515

16-
func dataSourceGitlabUsers() *schema.Resource {
16+
var _ = registerDataSource("gitlab_users", func() *schema.Resource {
1717
return &schema.Resource{
1818
Description: "Provide details about a list of users in the gitlab provider. The results include id, username, email, name and more about the requested users. Users can also be sorted and filtered using several options.\n\n" +
1919
"**NOTE**: Some available options require administrator privileges. Please visit [Gitlab API documentation][users_for_admins] for more information.",
@@ -212,7 +212,7 @@ func dataSourceGitlabUsers() *schema.Resource {
212212
},
213213
},
214214
}
215-
}
215+
})
216216

217217
func dataSourceGitlabUsersRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
218218
client := meta.(*gitlab.Client)

internal/provider/provider.go

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

12+
var (
13+
allDataSources = make(map[string]func() *schema.Resource)
14+
allResources = make(map[string]func() *schema.Resource)
15+
)
16+
17+
// registerDataSource may be called during package initialization to register a new data source with
18+
// the provider.
19+
var registerDataSource = makeRegisterResourceFunc(allDataSources, "data source")
20+
21+
// registerResource may be called during package initialization to register a new resource with the
22+
// provider.
23+
var registerResource = makeRegisterResourceFunc(allResources, "resource")
24+
1225
func init() {
1326
// Set descriptions to support markdown syntax, this will be used in document generation
1427
// and the language server.
@@ -70,61 +83,8 @@ func New(version string) func() *schema.Provider {
7083
},
7184
},
7285

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-
},
83-
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-
},
86+
DataSourcesMap: resourceFactoriesToMap(allDataSources),
87+
ResourcesMap: resourceFactoriesToMap(allResources),
12888
}
12989

13090
provider.ConfigureContextFunc = configure(version, provider)
@@ -156,3 +116,35 @@ func configure(version string, p *schema.Provider) func(context.Context, *schema
156116
return client, nil
157117
}
158118
}
119+
120+
func makeRegisterResourceFunc(factories map[string]func() *schema.Resource, resourceType string) func(name string, fn func() *schema.Resource) interface{} {
121+
// lintignore: R009 // panic() during package initialization is ok
122+
return func(name string, fn func() *schema.Resource) interface{} {
123+
if strings.ToLower(name) != name {
124+
panic(fmt.Sprintf("cannot register %s %q: name must be lowercase", resourceType, name))
125+
}
126+
127+
const wantPrefix = "gitlab_"
128+
if !strings.HasPrefix(name, wantPrefix) {
129+
panic(fmt.Sprintf("cannot register %s %q: name must begin with %q", resourceType, name, wantPrefix))
130+
}
131+
132+
if _, exists := factories[name]; exists {
133+
panic(fmt.Sprintf("cannot register %s %q: a %s with the same name already exists", resourceType, name, resourceType))
134+
}
135+
136+
factories[name] = fn
137+
138+
return nil
139+
}
140+
}
141+
142+
func resourceFactoriesToMap(factories map[string]func() *schema.Resource) map[string]*schema.Resource {
143+
resourcesMap := make(map[string]*schema.Resource)
144+
145+
for name, fn := range factories {
146+
resourcesMap[name] = fn()
147+
}
148+
149+
return resourcesMap
150+
}

internal/provider/resource_gitlab_branch_protection.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ var (
3939
}
4040
)
4141

42-
func resourceGitlabBranchProtection() *schema.Resource {
42+
var _ = registerResource("gitlab_branch_protection", func() *schema.Resource {
4343
return &schema.Resource{
4444
Description: "This resource allows you to protect a specific branch by an access level so that the user with less access level cannot Merge/Push to the branch.\n\n" +
4545
"-> The `allowed_to_push`, `allowed_to_merge` and `code_owner_approval_required` arguments require a GitLab Premium account or above. Please refer to [Gitlab API documentation](https://docs.gitlab.com/ee/api/protected_branches.html) for further information.",
@@ -93,7 +93,7 @@ func resourceGitlabBranchProtection() *schema.Resource {
9393
},
9494
},
9595
}
96-
}
96+
})
9797

9898
func resourceGitlabBranchProtectionCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
9999
client := meta.(*gitlab.Client)

0 commit comments

Comments
 (0)