Skip to content

Commit 53c0a37

Browse files
committed
add support for can_push on gitlab_deploy_key_enable
1 parent 0852693 commit 53c0a37

File tree

2 files changed

+86
-6
lines changed

2 files changed

+86
-6
lines changed

gitlab/resource_gitlab_deploy_key_enable.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ func resourceGitlabDeployEnableKey() *schema.Resource {
5353
Description: "Can deploy key push to the project’s repository.",
5454
Type: schema.TypeBool,
5555
Optional: true,
56-
Computed: true,
56+
Default: false,
57+
ForceNew: true,
5758
},
5859
},
5960
}
@@ -71,6 +72,14 @@ func resourceGitlabDeployKeyEnableCreate(ctx context.Context, d *schema.Resource
7172
return diag.FromErr(err)
7273
}
7374

75+
options := &gitlab.UpdateDeployKeyOptions{
76+
CanPush: gitlab.Bool(d.Get("can_push").(bool)),
77+
}
78+
_, _, err = client.DeployKeys.UpdateDeployKey(project, key_id, options)
79+
if err != nil {
80+
return diag.FromErr(err)
81+
}
82+
7483
d.SetId(fmt.Sprintf("%s:%d", project, deployKey.ID))
7584

7685
return resourceGitlabDeployKeyEnableRead(ctx, d, meta)

gitlab/resource_gitlab_deploy_key_enable_test.go

Lines changed: 76 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,51 @@ func TestAccGitlabDeployKeyEnable_basic(t *testing.T) {
2424
Providers: testAccProviders,
2525
CheckDestroy: testAccCheckGitlabDeployKeyEnableDestroy,
2626
Steps: []resource.TestStep{
27-
// Create a project and deployKey with default options
27+
// Enable a deployKey on project with default options
2828
{
2929
Config: testAccGitlabDeployKeyEnableConfig(rInt, keyTitle, key),
3030
Check: resource.ComposeTestCheckFunc(
3131
testAccCheckGitlabDeployKeyEnableExists("gitlab_deploy_key_enable.foo", &deployKey),
3232
testAccCheckGitlabDeployKeyEnableAttributes(&deployKey, &testAccGitlabDeployKeyEnableExpectedAttributes{
33-
Title: keyTitle,
34-
Key: key,
33+
Title: keyTitle,
34+
Key: key,
35+
CanPush: false,
36+
}),
37+
),
38+
},
39+
// Define canPush to true
40+
{
41+
Config: testAccGitlabDeployKeyEnableConfigCanPush(rInt, keyTitle, key, true),
42+
Check: resource.ComposeTestCheckFunc(
43+
testAccCheckGitlabDeployKeyEnableExists("gitlab_deploy_key_enable.foo", &deployKey),
44+
testAccCheckGitlabDeployKeyEnableAttributes(&deployKey, &testAccGitlabDeployKeyEnableExpectedAttributes{
45+
Title: keyTitle,
46+
Key: key,
47+
CanPush: true,
48+
}),
49+
),
50+
},
51+
// Define canPush to false
52+
{
53+
Config: testAccGitlabDeployKeyEnableConfigCanPush(rInt, keyTitle, key, false),
54+
Check: resource.ComposeTestCheckFunc(
55+
testAccCheckGitlabDeployKeyEnableExists("gitlab_deploy_key_enable.foo", &deployKey),
56+
testAccCheckGitlabDeployKeyEnableAttributes(&deployKey, &testAccGitlabDeployKeyEnableExpectedAttributes{
57+
Title: keyTitle,
58+
Key: key,
59+
CanPush: false,
60+
}),
61+
),
62+
},
63+
// Get back to default options
64+
{
65+
Config: testAccGitlabDeployKeyEnableConfig(rInt, keyTitle, key),
66+
Check: resource.ComposeTestCheckFunc(
67+
testAccCheckGitlabDeployKeyEnableExists("gitlab_deploy_key_enable.foo", &deployKey),
68+
testAccCheckGitlabDeployKeyEnableAttributes(&deployKey, &testAccGitlabDeployKeyEnableExpectedAttributes{
69+
Title: keyTitle,
70+
Key: key,
71+
CanPush: false,
3572
}),
3673
),
3774
},
@@ -137,8 +174,8 @@ resource "gitlab_project" "foo" {
137174
138175
resource "gitlab_deploy_key" "parent" {
139176
project = "${gitlab_project.parent.id}"
140-
title = "%s"
141-
key = "%s"
177+
title = "%s"
178+
key = "%s"
142179
}
143180
144181
resource "gitlab_deploy_key_enable" "foo" {
@@ -147,3 +184,37 @@ resource "gitlab_deploy_key_enable" "foo" {
147184
}
148185
`, rInt, rInt, keyTitle, key)
149186
}
187+
188+
func testAccGitlabDeployKeyEnableConfigCanPush(rInt int, keyTitle string, key string, canPush bool) string {
189+
return fmt.Sprintf(`
190+
resource "gitlab_project" "parent" {
191+
name = "parent-%d"
192+
description = "Terraform acceptance tests - Parent project"
193+
194+
# So that acceptance tests can be run in a gitlab organization
195+
# with no billing
196+
visibility_level = "public"
197+
}
198+
199+
resource "gitlab_project" "foo" {
200+
name = "foo-%d"
201+
description = "Terraform acceptance tests - Test Project"
202+
203+
# So that acceptance tests can be run in a gitlab organization
204+
# with no billing
205+
visibility_level = "public"
206+
}
207+
208+
resource "gitlab_deploy_key" "parent" {
209+
project = "${gitlab_project.parent.id}"
210+
title = "%s"
211+
key = "%s"
212+
}
213+
214+
resource "gitlab_deploy_key_enable" "foo" {
215+
project = "${gitlab_project.foo.id}"
216+
key_id = "${gitlab_deploy_key.parent.id}"
217+
can_push = %t
218+
}
219+
`, rInt, rInt, keyTitle, key, canPush)
220+
}

0 commit comments

Comments
 (0)