|
| 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 | +} |
0 commit comments