Skip to content

Commit 8b11b75

Browse files
author
Brad Downey
committed
Create management_id option for project and group Cluster
1 parent a5ebd54 commit 8b11b75

6 files changed

+157
-0
lines changed

gitlab/resource_gitlab_group_cluster.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ func resourceGitlabGroupCluster() *schema.Resource {
8787
Default: "rbac",
8888
ValidateFunc: validation.StringInSlice([]string{"rbac", "abac", "unknown_authorization"}, false),
8989
},
90+
"management_project_id": {
91+
Type: schema.TypeString,
92+
Optional: true,
93+
},
9094
},
9195
}
9296
}
@@ -123,6 +127,10 @@ func resourceGitlabGroupClusterCreate(d *schema.ResourceData, meta interface{})
123127
options.EnvironmentScope = gitlab.String(v.(string))
124128
}
125129

130+
if v, ok := d.GetOk("management_project_id"); ok {
131+
options.ManagementProjectID = gitlab.String(v.(string))
132+
}
133+
126134
log.Printf("[DEBUG] create gitlab group cluster %q/%q", group, *options.Name)
127135

128136
cluster, _, err := client.GroupCluster.AddCluster(group, options)
@@ -165,6 +173,12 @@ func resourceGitlabGroupClusterRead(d *schema.ResourceData, meta interface{}) er
165173
d.Set("kubernetes_ca_cert", cluster.PlatformKubernetes.CaCert)
166174
d.Set("kubernetes_authorization_type", cluster.PlatformKubernetes.AuthorizationType)
167175

176+
if cluster.ManagementProject == nil {
177+
d.Set("management_project_id", "")
178+
} else {
179+
d.Set("management_project_id", strconv.Itoa(cluster.ManagementProject.ID))
180+
}
181+
168182
return nil
169183
}
170184

@@ -208,6 +222,10 @@ func resourceGitlabGroupClusterUpdate(d *schema.ResourceData, meta interface{})
208222
options.PlatformKubernetes = pk
209223
}
210224

225+
if d.HasChange("management_project_id") {
226+
options.ManagementProjectID = gitlab.String(d.Get("management_project_id").(string))
227+
}
228+
211229
if *options != (gitlab.EditGroupClusterOptions{}) {
212230
log.Printf("[DEBUG] update gitlab group cluster %q/%d", group, clusterId)
213231
_, _, err := client.GroupCluster.EditCluster(group, clusterId, options)

gitlab/resource_gitlab_group_cluster_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,21 @@ func TestAccGitlabGroupCluster_basic(t *testing.T) {
7979
}),
8080
),
8181
},
82+
// Create cluster with management_project_id
83+
{
84+
Config: testAccGitlabGroupClusterManagement(rInt, true),
85+
Check: resource.ComposeTestCheckFunc(
86+
testAccCheckGitlabGroupClusterExists("gitlab_group_cluster.foo", &cluster),
87+
testAccCheckGitlabGroupClusterAttributes(&cluster, &testAccGitlabGroupClusterExpectedAttributes{
88+
Name: fmt.Sprintf("foo-cluster-%d", rInt),
89+
Domain: "example.com",
90+
EnvironmentScope: "*",
91+
KubernetesApiURL: "https://123.123.123",
92+
KubernetesCACert: groupClusterFakeCert,
93+
KubernetesAuthorizationType: "abac",
94+
}),
95+
),
96+
},
8297
},
8398
})
8499
}
@@ -111,6 +126,7 @@ type testAccGitlabGroupClusterExpectedAttributes struct {
111126
KubernetesApiURL string
112127
KubernetesCACert string
113128
KubernetesAuthorizationType string
129+
ManagementProjectID string
114130
}
115131

116132
func testAccCheckGitlabGroupClusterExists(n string, cluster *gitlab.GroupCluster) resource.TestCheckFunc {
@@ -261,6 +277,48 @@ resource gitlab_group_cluster "foo" {
261277
`, groupClusterFakeCert, rInt, rInt, rInt, authType)
262278
}
263279

280+
func testAccGitlabGroupClusterManagement(rInt int, managed bool) string {
281+
m := "false"
282+
if managed {
283+
m = "true"
284+
}
285+
286+
return fmt.Sprintf(`
287+
variable "cert" {
288+
default = <<EOF
289+
%s
290+
EOF
291+
}
292+
293+
resource "gitlab_group" "foo" {
294+
name = "foo-group-%d"
295+
path = "foo-group-%d"
296+
description = "Terraform acceptance tests"
297+
298+
# So that acceptance tests can be run in a gitlab organization
299+
# with no billing
300+
visibility_level = "public"
301+
}
302+
303+
resource "gitlab_project" "cluster-management" {
304+
name = "cluster-management"
305+
namespace_id = gitlab_group.foo.id
306+
}
307+
308+
resource gitlab_group_cluster "foo" {
309+
group = "${gitlab_group.foo.id}"
310+
name = "foo-cluster-%d"
311+
domain = "example.com"
312+
managed = "%s"
313+
kubernetes_api_url = "https://123.123.123"
314+
kubernetes_token = "some-token"
315+
kubernetes_ca_cert = "${trimspace(var.cert)}"
316+
kubernetes_authorization_type = "abac"
317+
management_project_id = "${gitlab_project.cluster-management.id}"
318+
}
319+
`, groupClusterFakeCert, rInt, rInt, rInt, m)
320+
}
321+
264322
var groupClusterFakeCert = `-----BEGIN CERTIFICATE-----
265323
MIICljCCAX4CCQDV7q2baHBlJjANBgkqhkiG9w0BAQsFADANMQswCQYDVQQGEwJV
266324
SzAeFw0xOTAzMjUxMTUxNTZaFw0xOTA0MjQxMTUxNTZaMA0xCzAJBgNVBAYTAlVL

gitlab/resource_gitlab_project_cluster.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ func resourceGitlabProjectCluster() *schema.Resource {
9191
Default: "rbac",
9292
ValidateFunc: validation.StringInSlice([]string{"rbac", "abac", "unknown_authorization"}, false),
9393
},
94+
"management_project_id": {
95+
Type: schema.TypeString,
96+
Optional: true,
97+
},
9498
},
9599
}
96100
}
@@ -131,6 +135,10 @@ func resourceGitlabProjectClusterCreate(d *schema.ResourceData, meta interface{}
131135
options.EnvironmentScope = gitlab.String(v.(string))
132136
}
133137

138+
if v, ok := d.GetOk("management_project_id"); ok {
139+
options.ManagementProjectID = gitlab.String(v.(string))
140+
}
141+
134142
log.Printf("[DEBUG] create gitlab project cluster %q/%q", project, *options.Name)
135143

136144
cluster, _, err := client.ProjectCluster.AddCluster(project, options)
@@ -174,6 +182,12 @@ func resourceGitlabProjectClusterRead(d *schema.ResourceData, meta interface{})
174182
d.Set("kubernetes_namespace", cluster.PlatformKubernetes.Namespace)
175183
d.Set("kubernetes_authorization_type", cluster.PlatformKubernetes.AuthorizationType)
176184

185+
if cluster.ManagementProject == nil {
186+
d.Set("management_project_id", "")
187+
} else {
188+
d.Set("management_project_id", strconv.Itoa(cluster.ManagementProject.ID))
189+
}
190+
177191
return nil
178192
}
179193

@@ -221,6 +235,10 @@ func resourceGitlabProjectClusterUpdate(d *schema.ResourceData, meta interface{}
221235
options.PlatformKubernetes = pk
222236
}
223237

238+
if d.HasChange("management_project_id") {
239+
options.ManagementProjectID = gitlab.String(d.Get("management_project_id").(string))
240+
}
241+
224242
if *options != (gitlab.EditClusterOptions{}) {
225243
log.Printf("[DEBUG] update gitlab project cluster %q/%d", project, clusterId)
226244
_, _, err := client.ProjectCluster.EditCluster(project, clusterId, options)

gitlab/resource_gitlab_project_cluster_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,21 @@ func TestAccGitlabProjectCluster_basic(t *testing.T) {
8181
}),
8282
),
8383
},
84+
// Create cluster with management_project_id
85+
{
86+
Config: testAccGitlabProjectClusterManagement(rInt, true),
87+
Check: resource.ComposeTestCheckFunc(
88+
testAccCheckGitlabProjectClusterExists("gitlab_project_cluster.foo", &cluster),
89+
testAccCheckGitlabProjectClusterAttributes(&cluster, &testAccGitlabProjectClusterExpectedAttributes{
90+
Name: fmt.Sprintf("foo-cluster-%d", rInt),
91+
Domain: "example.com",
92+
EnvironmentScope: "*",
93+
KubernetesApiURL: "https://123.123.123",
94+
KubernetesCACert: projectClusterFakeCert,
95+
KubernetesAuthorizationType: "abac",
96+
}),
97+
),
98+
},
8499
},
85100
})
86101
}
@@ -114,6 +129,7 @@ type testAccGitlabProjectClusterExpectedAttributes struct {
114129
KubernetesCACert string
115130
KubernetesNamespace string
116131
KubernetesAuthorizationType string
132+
ManagementProjectID string
117133
}
118134

119135
func testAccCheckGitlabProjectClusterExists(n string, cluster *gitlab.ProjectCluster) resource.TestCheckFunc {
@@ -267,6 +283,47 @@ resource gitlab_project_cluster "foo" {
267283
`, projectClusterFakeCert, rInt, rInt, authType)
268284
}
269285

286+
func testAccGitlabProjectClusterManagement(rInt int, managed bool) string {
287+
m := "false"
288+
if managed {
289+
m = "true"
290+
}
291+
292+
return fmt.Sprintf(`
293+
variable "cert" {
294+
default = <<EOF
295+
%s
296+
EOF
297+
}
298+
299+
resource "gitlab_project" "foo" {
300+
name = "foo-project-%d"
301+
description = "Terraform acceptance tests"
302+
303+
# So that acceptance tests can be run in a gitlab organization
304+
# with no billing
305+
visibility_level = "public"
306+
}
307+
308+
resource "gitlab_project" "cluster-management" {
309+
name = "cluster-management-%d"
310+
visibility_level = "public"
311+
}
312+
313+
resource gitlab_project_cluster "foo" {
314+
project = "${gitlab_project.foo.id}"
315+
name = "foo-cluster-%d"
316+
domain = "example.com"
317+
managed = "%s"
318+
kubernetes_api_url = "https://123.123.123"
319+
kubernetes_token = "some-token"
320+
kubernetes_ca_cert = "${trimspace(var.cert)}"
321+
kubernetes_authorization_type = "abac"
322+
management_project_id = "${gitlab_project.cluster-management.id}"
323+
}
324+
`, projectClusterFakeCert, rInt, rInt, rInt, m)
325+
}
326+
270327
var projectClusterFakeCert = `-----BEGIN CERTIFICATE-----
271328
MIICljCCAX4CCQDV7q2baHBlJjANBgkqhkiG9w0BAQsFADANMQswCQYDVQQGEwJV
272329
SzAeFw0xOTAzMjUxMTUxNTZaFw0xOTA0MjQxMTUxNTZaMA0xCzAJBgNVBAYTAlVL

website/docs/r/group_cluster.html.markdown

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ resource gitlab_group_cluster "bar" {
3131
kubernetes_ca_cert = "some-cert"
3232
kubernetes_authorization_type = "rbac"
3333
environment_scope = "*"
34+
management_cluster_id = "123456"
3435
}
3536
```
3637

@@ -58,6 +59,8 @@ The following arguments are supported:
5859

5960
* `environment_scope` - (Optional, string) The associated environment to the cluster. Defaults to `*`.
6061

62+
* `management_cluster_id` - (Optional, string) The ID of the management project for the cluster.
63+
6164
## Import
6265

6366
GitLab group clusters can be imported using an id made up of `groupid:clusterid`, e.g.

website/docs/r/project_cluster.html.markdown

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ resource gitlab_project_cluster "bar" {
3131
kubernetes_namespace = "namespace"
3232
kubernetes_authorization_type = "rbac"
3333
environment_scope = "*"
34+
management_cluster_id = "123456"
3435
}
3536
```
3637

@@ -60,6 +61,8 @@ The following arguments are supported:
6061

6162
* `environment_scope` - (Optional, string) The associated environment to the cluster. Defaults to `*`.
6263

64+
* `management_cluster_id` - (Optional, string) The ID of the management project for the cluster.
65+
6366
## Import
6467

6568
GitLab project clusters can be imported using an id made up of `projectid:clusterid`, e.g.

0 commit comments

Comments
 (0)