Skip to content

Commit 4bd90e0

Browse files
sfang97ringods
andauthored
parent 1cbab81 (#367)
author Ringo De Smet <[email protected]> 1592760067 +0200 committer sfang97 <[email protected]> 1600137187 -0500 parent 1cbab81 author Ringo De Smet <[email protected]> 1592760067 +0200 committer sfang97 <[email protected]> 1600137111 -0500 parent 1cbab81 author Ringo De Smet <[email protected]> 1592760067 +0200 committer sfang97 <[email protected]> 1600136946 -0500 Fix required Go version Add instance clusters to Terraform provider Add support for instance level cluster management via the Gitlab Terraform provider. Add instance cluster to provider Add instance cluster go file Add instance clusters to website Only test read instance cluster for now Add functionality to CRUD Tests mostly passing now, except a weird invalid URL escape thing, will investigate this Remove instance_clusters.go from this PR Contributed the instance API file to the upstream repo github.com/xanzy/go-gitlab, so removing from this PR Readding instance_clusters for failed pipeline Replace required libary in go mod Replace vendor with upstream in go mod Remove vendor files Remove changes to gitlab go Remove instance clusters from README Add management project param back in Rerun CI build Removing line for CI build Rerun CI pipeline Undo change to xanzy README Undo another change to README Change the commit to replace go-gitlab requirement Add go mod vendor to travis Move go mod vendors command to lint stage Change function names based on vendor change Remove replace package feat: Add support to create projects from templates Undo cherry pick This reverts commit 5191839. Update go mod to latest go-gitlab Remove changes to travis Remove newline for new pipeline Update docs, no instance ID Add backlashes so no italics Remove instance from example usage Remove white space, change instance link Address PR comments Add forcenew back to auth type run make fmt Prepare changelog for release v2.11.0 Cleanup after v2.11.0 release Fix Jira Service error handling (#363) Fix nil pointer dereferences during error handling (#364) * Fix error handling when deleting deploy key There was the possibility of a panic if response was nil, and the GitLab client already treats the 204 code as a success, so the extra check was not needed. * Fix nil pointer dereference issues in error messages correct management_project_id argument in documentation (#368) Fix incorrect description of valid access levels (#342) This minor change corrects a mistake showing `master` as a valid level of `group_access_level`. Import group labels (#339) * add support for importing gitlab group labels * fix resource name in the acceptance test * convert group ID to string Co-authored-by: David Townshend <[email protected]> Add importer to resource_gitlab_project_push_rules (#360) * Add importer to resource_gitlab_project_push_rules * Fix apparent copy-paste problem and inconsistency in push rules debug logs Bump go-gitlab to v.0.34.1 (#378) * Fix variable passed to ListLabelsOptions Needed to fix nested struct format * Don't need go mod changes * Add back go mod * Bump to 0.34.1 * Add 0.34.1 to modules.txt * Revert "Add 0.34.1 to modules.txt" This reverts commit 2ac4e3e. * Add vendor files for bump Co-authored-by: sfang97 <[email protected]> Renamed the Github organization after the migration to https://github.com/gitlabhq Change isRunningInEE test Fix import packages Call configure in provider test Only initialize provider in acceptance tests Remove undefined resource reference Add undefined testenvvar Had to run make fmt Remove unused variable Reference testenvvar resource correctly Add group_ldap_link to index Signed-off-by: Sune Keller <[email protected]> Adding project miorror and tests responding to PR comments, removing import state verify for acceptance test Setting all properties in SetToState removed commented out ignore adding docs page adding to index Store more attributes into the state Fix missing test checks in the resource user Allow to change user email address without creating a new resource Fix the external flag being mapped to admin instead Switching the external flag makes tests fail Add new data source gitlab_group_membership Allow to filter members by their access_level Add new doc page to gitlab.erb Update website/gitlab.erb Co-authored-by: Adam Snyder <[email protected]> Added Quotes in the example for group cluster Prepare changelog for release Cleanup after v2.11.0 release Fix Jira Service error handling (#363) Fix nil pointer dereferences during error handling (#364) * Fix error handling when deleting deploy key There was the possibility of a panic if response was nil, and the GitLab client already treats the 204 code as a success, so the extra check was not needed. * Fix nil pointer dereference issues in error messages correct management_project_id argument in documentation (#368) Fix incorrect description of valid access levels (#342) This minor change corrects a mistake showing `master` as a valid level of `group_access_level`. Import group labels (#339) * add support for importing gitlab group labels * fix resource name in the acceptance test * convert group ID to string Co-authored-by: David Townshend <[email protected]> Add importer to resource_gitlab_project_push_rules (#360) * Add importer to resource_gitlab_project_push_rules * Fix apparent copy-paste problem and inconsistency in push rules debug logs Bump go-gitlab to v.0.34.1 (#378) * Fix variable passed to ListLabelsOptions Needed to fix nested struct format * Don't need go mod changes * Add back go mod * Bump to 0.34.1 * Add 0.34.1 to modules.txt * Revert "Add 0.34.1 to modules.txt" This reverts commit 2ac4e3e. * Add vendor files for bump Co-authored-by: sfang97 <[email protected]> Change isRunningInEE test Fix import packages Call configure in provider test Only initialize provider in acceptance tests Remove undefined resource reference Add undefined testenvvar Had to run make fmt Remove unused variable Reference testenvvar resource correctly Add group_ldap_link to index Signed-off-by: Sune Keller <[email protected]> Adding project miorror and tests responding to PR comments, removing import state verify for acceptance test Setting all properties in SetToState removed commented out ignore adding docs page adding to index Store more attributes into the state Fix missing test checks in the resource user Allow to change user email address without creating a new resource Fix the external flag being mapped to admin instead Switching the external flag makes tests fail Allow to filter members by their access_level Update website/gitlab.erb Co-authored-by: Adam Snyder <[email protected]> Added Quotes in the example for group cluster Remove redundant test func Add managed test Address PR comments Revert "Address PR comments" This reverts commit a963eaf. Remove managed and enabled for now Co-authored-by: Ringo De Smet <[email protected]>
1 parent bd16b81 commit 4bd90e0

6 files changed

+598
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ ENHANCEMENTS:
9797
## 2.5.1 (April 06, 2020)
9898

9999
BUGFIXES:
100+
100101
* Support for soft-delete of groups and projects in Gitlab Enterprise Edition
101102
([#282](https://github.com/gitlabhq/terraform-provider-gitlab/issues/282),
102103
[#283](https://github.com/gitlabhq/terraform-provider-gitlab/issues/283),

gitlab/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ func Provider() terraform.ResourceProvider {
9090
"gitlab_project_share_group": resourceGitlabProjectShareGroup(),
9191
"gitlab_group_cluster": resourceGitlabGroupCluster(),
9292
"gitlab_group_ldap_link": resourceGitlabGroupLdapLink(),
93+
"gitlab_instance_cluster": resourceGitlabInstanceCluster(),
9394
"gitlab_project_mirror": resourceGitlabProjectMirror(),
9495
"gitlab_project_level_mr_approvals": resourceGitlabProjectLevelMRApprovals(),
9596
},
+254
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
package gitlab
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"strconv"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
9+
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
10+
"github.com/xanzy/go-gitlab"
11+
)
12+
13+
func resourceGitlabInstanceCluster() *schema.Resource {
14+
return &schema.Resource{
15+
Create: resourceGitlabInstanceClusterCreate,
16+
Read: resourceGitlabInstanceClusterRead,
17+
Update: resourceGitlabInstanceClusterUpdate,
18+
Delete: resourceGitlabInstanceClusterDelete,
19+
Importer: &schema.ResourceImporter{
20+
State: schema.ImportStatePassthrough,
21+
},
22+
23+
Schema: map[string]*schema.Schema{
24+
"name": {
25+
Type: schema.TypeString,
26+
Required: true,
27+
},
28+
"domain": {
29+
Type: schema.TypeString,
30+
Optional: true,
31+
},
32+
"enabled": {
33+
Type: schema.TypeBool,
34+
Optional: true,
35+
Default: true,
36+
ForceNew: true,
37+
},
38+
"managed": {
39+
Type: schema.TypeBool,
40+
Optional: true,
41+
Default: true,
42+
ForceNew: true,
43+
},
44+
"created_at": {
45+
Type: schema.TypeString,
46+
Computed: true,
47+
},
48+
"provider_type": {
49+
Type: schema.TypeString,
50+
Computed: true,
51+
},
52+
"platform_type": {
53+
Type: schema.TypeString,
54+
Computed: true,
55+
},
56+
"environment_scope": {
57+
Type: schema.TypeString,
58+
Optional: true,
59+
Default: "*",
60+
},
61+
"cluster_type": {
62+
Type: schema.TypeString,
63+
Computed: true,
64+
},
65+
"kubernetes_api_url": {
66+
Type: schema.TypeString,
67+
Required: true,
68+
},
69+
"kubernetes_token": {
70+
Type: schema.TypeString,
71+
Required: true,
72+
Sensitive: true,
73+
},
74+
"kubernetes_ca_cert": {
75+
Type: schema.TypeString,
76+
Optional: true,
77+
},
78+
"kubernetes_namespace": {
79+
Type: schema.TypeString,
80+
Optional: true,
81+
},
82+
"kubernetes_authorization_type": {
83+
Type: schema.TypeString,
84+
Optional: true,
85+
ForceNew: true,
86+
Default: "rbac",
87+
ValidateFunc: validation.StringInSlice([]string{"rbac", "abac", "unknown_authorization"}, false),
88+
},
89+
"management_project_id": {
90+
Type: schema.TypeString,
91+
Optional: true,
92+
},
93+
},
94+
}
95+
}
96+
97+
func resourceGitlabInstanceClusterCreate(d *schema.ResourceData, meta interface{}) error {
98+
client := meta.(*gitlab.Client)
99+
100+
pk := gitlab.AddPlatformKubernetesOptions{
101+
APIURL: gitlab.String(d.Get("kubernetes_api_url").(string)),
102+
Token: gitlab.String(d.Get("kubernetes_token").(string)),
103+
}
104+
105+
if v, ok := d.GetOk("kubernetes_ca_cert"); ok {
106+
pk.CaCert = gitlab.String(v.(string))
107+
}
108+
109+
if v, ok := d.GetOk("kubernetes_authorization_type"); ok {
110+
pk.AuthorizationType = gitlab.String(v.(string))
111+
}
112+
113+
options := &gitlab.AddClusterOptions{
114+
Name: gitlab.String(d.Get("name").(string)),
115+
Enabled: gitlab.Bool(d.Get("enabled").(bool)),
116+
Managed: gitlab.Bool(d.Get("managed").(bool)),
117+
PlatformKubernetes: &pk,
118+
}
119+
120+
if v, ok := d.GetOk("domain"); ok {
121+
options.Domain = gitlab.String(v.(string))
122+
}
123+
124+
if v, ok := d.GetOk("environment_scope"); ok {
125+
options.EnvironmentScope = gitlab.String(v.(string))
126+
}
127+
128+
if v, ok := d.GetOk("management_project_id"); ok {
129+
options.ManagementProjectID = gitlab.String(v.(string))
130+
}
131+
132+
log.Printf("[DEBUG] create gitlab instance cluster %q", *options.Name)
133+
134+
cluster, _, err := client.InstanceCluster.AddCluster(options)
135+
136+
if err != nil {
137+
return err
138+
}
139+
140+
clusterIdString := fmt.Sprintf("%d", cluster.ID)
141+
d.SetId(clusterIdString)
142+
143+
return resourceGitlabInstanceClusterRead(d, meta)
144+
}
145+
146+
func resourceGitlabInstanceClusterRead(d *schema.ResourceData, meta interface{}) error {
147+
client := meta.(*gitlab.Client)
148+
149+
clusterId, err := strconv.Atoi(d.Id())
150+
if err != nil {
151+
return err
152+
}
153+
154+
log.Printf("[DEBUG] read gitlab instance cluster %d", clusterId)
155+
156+
cluster, _, err := client.InstanceCluster.GetCluster(clusterId)
157+
if err != nil {
158+
return err
159+
}
160+
161+
d.Set("name", cluster.Name)
162+
d.Set("domain", cluster.Domain)
163+
d.Set("created_at", cluster.CreatedAt.String())
164+
d.Set("provider_type", cluster.ProviderType)
165+
d.Set("platform_type", cluster.PlatformType)
166+
d.Set("environment_scope", cluster.EnvironmentScope)
167+
d.Set("cluster_type", cluster.ClusterType)
168+
169+
d.Set("kubernetes_api_url", cluster.PlatformKubernetes.APIURL)
170+
d.Set("kubernetes_ca_cert", cluster.PlatformKubernetes.CaCert)
171+
d.Set("kubernetes_namespace", cluster.PlatformKubernetes.Namespace)
172+
d.Set("kubernetes_authorization_type", cluster.PlatformKubernetes.AuthorizationType)
173+
174+
if cluster.ManagementProject == nil {
175+
d.Set("management_project_id", "")
176+
} else {
177+
d.Set("management_project_id", strconv.Itoa(cluster.ManagementProject.ID))
178+
}
179+
180+
return nil
181+
}
182+
183+
func resourceGitlabInstanceClusterUpdate(d *schema.ResourceData, meta interface{}) error {
184+
client := meta.(*gitlab.Client)
185+
186+
clusterId, err := strconv.Atoi(d.Id())
187+
if err != nil {
188+
return err
189+
}
190+
191+
options := &gitlab.EditClusterOptions{}
192+
193+
if d.HasChange("name") {
194+
options.Name = gitlab.String(d.Get("name").(string))
195+
}
196+
197+
if d.HasChange("domain") {
198+
options.Domain = gitlab.String(d.Get("domain").(string))
199+
}
200+
201+
if d.HasChange("environment_scope") {
202+
options.EnvironmentScope = gitlab.String(d.Get("environment_scope").(string))
203+
}
204+
205+
pk := &gitlab.EditPlatformKubernetesOptions{}
206+
207+
if d.HasChange("kubernetes_api_url") {
208+
pk.APIURL = gitlab.String(d.Get("kubernetes_api_url").(string))
209+
}
210+
211+
if d.HasChange("kubernetes_token") {
212+
pk.Token = gitlab.String(d.Get("kubernetes_token").(string))
213+
}
214+
215+
if d.HasChange("kubernetes_ca_cert") {
216+
pk.CaCert = gitlab.String(d.Get("kubernetes_ca_cert").(string))
217+
}
218+
219+
if d.HasChange("namespace") {
220+
pk.Namespace = gitlab.String(d.Get("namespace").(string))
221+
}
222+
223+
if *pk != (gitlab.EditPlatformKubernetesOptions{}) {
224+
options.PlatformKubernetes = pk
225+
}
226+
227+
if d.HasChange("management_project_id") {
228+
options.ManagementProjectID = gitlab.String(d.Get("management_project_id").(string))
229+
}
230+
231+
if *options != (gitlab.EditClusterOptions{}) {
232+
log.Printf("[DEBUG] update gitlab instance cluster %d", clusterId)
233+
_, _, err := client.InstanceCluster.EditCluster(clusterId, options)
234+
if err != nil {
235+
return err
236+
}
237+
}
238+
239+
return resourceGitlabInstanceClusterRead(d, meta)
240+
}
241+
242+
func resourceGitlabInstanceClusterDelete(d *schema.ResourceData, meta interface{}) error {
243+
client := meta.(*gitlab.Client)
244+
clusterId, err := strconv.Atoi(d.Id())
245+
if err != nil {
246+
return err
247+
}
248+
249+
log.Printf("[DEBUG] delete gitlab instance cluster %d", clusterId)
250+
251+
_, err = client.InstanceCluster.DeleteCluster(clusterId)
252+
253+
return err
254+
}

0 commit comments

Comments
 (0)