forked from hashicorp/terraform
/
resource_gitlab_group.go
153 lines (123 loc) · 3.74 KB
/
resource_gitlab_group.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package gitlab
import (
"fmt"
"log"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
gitlab "github.com/xanzy/go-gitlab"
)
func resourceGitlabGroup() *schema.Resource {
return &schema.Resource{
Create: resourceGitlabGroupCreate,
Read: resourceGitlabGroupRead,
Update: resourceGitlabGroupUpdate,
Delete: resourceGitlabGroupDelete,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"path": {
Type: schema.TypeString,
Required: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"lfs_enabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"request_access_enabled": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"visibility_level": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"private", "internal", "public"}, true),
Default: "private",
},
},
}
}
func resourceGitlabGroupCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*gitlab.Client)
options := &gitlab.CreateGroupOptions{
Name: gitlab.String(d.Get("name").(string)),
LFSEnabled: gitlab.Bool(d.Get("lfs_enabled").(bool)),
RequestAccessEnabled: gitlab.Bool(d.Get("request_access_enabled").(bool)),
}
if v, ok := d.GetOk("path"); ok {
options.Path = gitlab.String(v.(string))
}
if v, ok := d.GetOk("description"); ok {
options.Description = gitlab.String(v.(string))
}
if v, ok := d.GetOk("visibility_level"); ok {
options.VisibilityLevel = stringToVisibilityLevel(v.(string))
}
log.Printf("[DEBUG] create gitlab group %q", options.Name)
group, _, err := client.Groups.CreateGroup(options)
if err != nil {
return err
}
d.SetId(fmt.Sprintf("%d", group.ID))
return resourceGitlabGroupRead(d, meta)
}
func resourceGitlabGroupRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*gitlab.Client)
log.Printf("[DEBUG] read gitlab group %s", d.Id())
group, response, err := client.Groups.GetGroup(d.Id())
if err != nil {
if response.StatusCode == 404 {
log.Printf("[WARN] removing group %s from state because it no longer exists in gitlab", d.Id())
d.SetId("")
return nil
}
return err
}
d.Set("name", group.Name)
d.Set("path", group.Path)
d.Set("description", group.Description)
d.Set("lfs_enabled", group.LFSEnabled)
d.Set("request_access_enabled", group.RequestAccessEnabled)
return nil
}
func resourceGitlabGroupUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*gitlab.Client)
options := &gitlab.UpdateGroupOptions{}
if d.HasChange("name") {
options.Name = gitlab.String(d.Get("name").(string))
}
if d.HasChange("path") {
options.Path = gitlab.String(d.Get("path").(string))
}
if d.HasChange("description") {
options.Description = gitlab.String(d.Get("description").(string))
}
if d.HasChange("lfs_enabled") {
options.LFSEnabled = gitlab.Bool(d.Get("lfs_enabled").(bool))
}
if d.HasChange("request_access_enabled") {
options.RequestAccessEnabled = gitlab.Bool(d.Get("request_access_enabled").(bool))
}
if d.HasChange("visibility_level") {
options.VisibilityLevel = stringToVisibilityLevel(d.Get("visibility_level").(string))
}
log.Printf("[DEBUG] update gitlab group %s", d.Id())
_, _, err := client.Groups.UpdateGroup(d.Id(), options)
if err != nil {
return err
}
return resourceGitlabGroupRead(d, meta)
}
func resourceGitlabGroupDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*gitlab.Client)
log.Printf("[DEBUG] Delete gitlab group %s", d.Id())
_, err := client.Groups.DeleteGroup(d.Id())
return err
}