Skip to content

Commit e1755e4

Browse files
committed
Add data sources gitlab_project_protected_branch(es)
Signed-off-by: Sune Keller <[email protected]>
1 parent 18e9201 commit e1755e4

File tree

3 files changed

+241
-6
lines changed

3 files changed

+241
-6
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package gitlab
2+
3+
import (
4+
"fmt"
5+
"log"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
8+
"github.com/xanzy/go-gitlab"
9+
)
10+
11+
func dataSourceGitlabProjectProtectedBranch() *schema.Resource {
12+
return &schema.Resource{
13+
Read: dataSourceGitlabProjectProtectedBranchRead,
14+
Schema: map[string]*schema.Schema{
15+
"project_id": {
16+
Type: schema.TypeString,
17+
Description: "ID or URL encoded name of project",
18+
Required: true,
19+
},
20+
"name": {
21+
Type: schema.TypeString,
22+
Description: "Name of the protected branch",
23+
Required: true,
24+
},
25+
"id": {
26+
Type: schema.TypeInt,
27+
Computed: true,
28+
},
29+
"push_access_levels": dataSourceGitlabProjectProtectedBranchSchemaAccessLevels(),
30+
"merge_access_levels": dataSourceGitlabProjectProtectedBranchSchemaAccessLevels(),
31+
"unprotect_access_levels": dataSourceGitlabProjectProtectedBranchSchemaAccessLevels(),
32+
"code_owner_approval_required": {
33+
Type: schema.TypeBool,
34+
Computed: true,
35+
},
36+
},
37+
}
38+
}
39+
40+
func dataSourceGitlabProjectProtectedBranchSchemaAccessLevels() *schema.Schema {
41+
return &schema.Schema{
42+
Type: schema.TypeList,
43+
Computed: true,
44+
Elem: &schema.Resource{
45+
Schema: map[string]*schema.Schema{
46+
"access_level": {
47+
Type: schema.TypeString,
48+
Computed: true,
49+
},
50+
"access_level_description": {
51+
Type: schema.TypeString,
52+
Computed: true,
53+
},
54+
"user_id": {
55+
Type: schema.TypeInt,
56+
Computed: true,
57+
},
58+
"group_id": {
59+
Type: schema.TypeInt,
60+
Computed: true,
61+
},
62+
},
63+
},
64+
}
65+
}
66+
67+
func dataSourceGitlabProjectProtectedBranchRead(d *schema.ResourceData, meta interface{}) error {
68+
client := meta.(*gitlab.Client)
69+
70+
log.Printf("[INFO] Reading Gitlab protected branch")
71+
72+
project := d.Get("project_id")
73+
name := d.Get("name").(string)
74+
75+
// Get protected branch by project ID/path and branch name
76+
pb, _, err := client.ProtectedBranches.GetProtectedBranch(project, name)
77+
if err != nil {
78+
return err
79+
}
80+
81+
if err := d.Set("push_access_levels", convertBranchAccessDescriptionsToStateBranchAccessDescriptions(pb.PushAccessLevels)); err != nil {
82+
return err
83+
}
84+
if err := d.Set("merge_access_levels", convertBranchAccessDescriptionsToStateBranchAccessDescriptions(pb.MergeAccessLevels)); err != nil {
85+
return err
86+
}
87+
if err := d.Set("unprotect_access_levels", convertBranchAccessDescriptionsToStateBranchAccessDescriptions(pb.UnprotectAccessLevels)); err != nil {
88+
return err
89+
}
90+
if err := d.Set("code_owner_approval_required", pb.CodeOwnerApprovalRequired); err != nil {
91+
return err
92+
}
93+
94+
d.SetId(fmt.Sprintf("%d", pb.ID))
95+
96+
return nil
97+
}
98+
99+
type stateBranchAccessDescription struct {
100+
AccessLevel string `json:"access_level" mapstructure:"access_level"`
101+
AccessLevelDescription string `json:"access_level_description" mapstructure:"access_level_description"`
102+
GroupID *int `json:"group_id,omitempty" mapstructure:"group_id,omitempty"`
103+
UserID *int `json:"user_id,omitempty" mapstructure:"user_id,omitempty"`
104+
}
105+
106+
func convertBranchAccessDescriptionsToStateBranchAccessDescriptions(descriptions []*gitlab.BranchAccessDescription) []stateBranchAccessDescription {
107+
result := make([]stateBranchAccessDescription, 0)
108+
109+
for _, description := range descriptions {
110+
result = append(result, convertBranchAccessDescriptionToStateBranchAccessDescription(description))
111+
}
112+
113+
return result
114+
}
115+
116+
func convertBranchAccessDescriptionToStateBranchAccessDescription(description *gitlab.BranchAccessDescription) stateBranchAccessDescription {
117+
stateDescription := stateBranchAccessDescription{
118+
AccessLevel: accessLevel[description.AccessLevel],
119+
AccessLevelDescription: description.AccessLevelDescription,
120+
}
121+
if description.UserID != 0 {
122+
stateDescription.UserID = &description.UserID
123+
}
124+
if description.GroupID != 0 {
125+
stateDescription.GroupID = &description.GroupID
126+
}
127+
return stateDescription
128+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package gitlab
2+
3+
import (
4+
"fmt"
5+
"log"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
8+
"github.com/mitchellh/hashstructure"
9+
"github.com/xanzy/go-gitlab"
10+
)
11+
12+
func dataSourceGitlabProjectProtectedBranches() *schema.Resource {
13+
return &schema.Resource{
14+
Read: dataSourceGitlabProjectProtectedBranchesRead,
15+
Schema: map[string]*schema.Schema{
16+
"project_id": {
17+
Type: schema.TypeString,
18+
Description: "ID or URL encoded name of project",
19+
Required: true,
20+
},
21+
"protected_branches": {
22+
Type: schema.TypeList,
23+
Computed: true,
24+
Elem: &schema.Resource{
25+
Schema: map[string]*schema.Schema{
26+
"name": {
27+
Type: schema.TypeString,
28+
Description: "Name of the protected branch",
29+
Computed: true,
30+
},
31+
"id": {
32+
Type: schema.TypeInt,
33+
Computed: true,
34+
},
35+
"push_access_levels": dataSourceGitlabProjectProtectedBranchSchemaAccessLevels(),
36+
"merge_access_levels": dataSourceGitlabProjectProtectedBranchSchemaAccessLevels(),
37+
"unprotect_access_levels": dataSourceGitlabProjectProtectedBranchSchemaAccessLevels(),
38+
"code_owner_approval_required": {
39+
Type: schema.TypeBool,
40+
Computed: true,
41+
},
42+
},
43+
},
44+
},
45+
},
46+
}
47+
}
48+
49+
type stateProtectedBranch struct {
50+
ID int `json:"id,omitempty" mapstructure:"id,omitempty"`
51+
Name string `json:"name,omitempty" mapstructure:"name,omitempty"`
52+
PushAccessLevels []stateBranchAccessDescription `json:"push_access_levels,omitempty" mapstructure:"push_access_levels,omitempty"`
53+
MergeAccessLevels []stateBranchAccessDescription `json:"merge_access_levels,omitempty" mapstructure:"merge_access_levels,omitempty"`
54+
UnprotectAccessLevels []stateBranchAccessDescription `json:"unprotect_access_levels,omitempty" mapstructure:"unprotect_access_levels,omitempty"`
55+
CodeOwnerApprovalRequired bool `json:"code_owner_approval_required,omitempty" mapstructure:"code_owner_approval_required,omitempty"`
56+
}
57+
58+
func dataSourceGitlabProjectProtectedBranchesRead(d *schema.ResourceData, meta interface{}) error {
59+
client := meta.(*gitlab.Client)
60+
61+
log.Printf("[INFO] Reading Gitlab protected branch")
62+
63+
project := d.Get("project_id")
64+
65+
projectObject, _, err := client.Projects.GetProject(project, &gitlab.GetProjectOptions{})
66+
if err != nil {
67+
return err
68+
}
69+
70+
allProtectedBranches := make([]stateProtectedBranch, 0)
71+
totalPages := -1
72+
for page := 0; page != totalPages; page++ {
73+
// Get protected branch by project ID/path and branch name
74+
pbs, resp, err := client.ProtectedBranches.ListProtectedBranches(project, &gitlab.ListProtectedBranchesOptions{
75+
Page: page + 1,
76+
})
77+
if err != nil {
78+
return err
79+
}
80+
totalPages = resp.TotalPages
81+
for _, pb := range pbs {
82+
allProtectedBranches = append(allProtectedBranches, stateProtectedBranch{
83+
ID: pb.ID,
84+
Name: pb.Name,
85+
PushAccessLevels: convertBranchAccessDescriptionsToStateBranchAccessDescriptions(pb.PushAccessLevels),
86+
MergeAccessLevels: convertBranchAccessDescriptionsToStateBranchAccessDescriptions(pb.MergeAccessLevels),
87+
UnprotectAccessLevels: convertBranchAccessDescriptionsToStateBranchAccessDescriptions(pb.UnprotectAccessLevels),
88+
CodeOwnerApprovalRequired: pb.CodeOwnerApprovalRequired,
89+
})
90+
}
91+
}
92+
93+
if err := d.Set("protected_branches", allProtectedBranches); err != nil {
94+
return err
95+
}
96+
97+
h, err := hashstructure.Hash(project, nil)
98+
if err != nil {
99+
return err
100+
}
101+
102+
d.SetId(fmt.Sprintf("%d-%d", projectObject.ID, h))
103+
104+
return nil
105+
}

gitlab/provider.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,14 @@ func Provider() terraform.ResourceProvider {
5555
},
5656

5757
DataSourcesMap: map[string]*schema.Resource{
58-
"gitlab_group": dataSourceGitlabGroup(),
59-
"gitlab_group_membership": dataSourceGitlabGroupMembership(),
60-
"gitlab_project": dataSourceGitlabProject(),
61-
"gitlab_projects": dataSourceGitlabProjects(),
62-
"gitlab_user": dataSourceGitlabUser(),
63-
"gitlab_users": dataSourceGitlabUsers(),
58+
"gitlab_group": dataSourceGitlabGroup(),
59+
"gitlab_group_membership": dataSourceGitlabGroupMembership(),
60+
"gitlab_project": dataSourceGitlabProject(),
61+
"gitlab_project_protected_branch": dataSourceGitlabProjectProtectedBranch(),
62+
"gitlab_project_protected_branches": dataSourceGitlabProjectProtectedBranches(),
63+
"gitlab_projects": dataSourceGitlabProjects(),
64+
"gitlab_user": dataSourceGitlabUser(),
65+
"gitlab_users": dataSourceGitlabUsers(),
6466
},
6567

6668
ResourcesMap: map[string]*schema.Resource{

0 commit comments

Comments
 (0)