Skip to content

Commit c0df60c

Browse files
author
Julien Pivotto
committed
Make deploy keys indempotent when newlines are added
Close #25 Signed-off-by: Julien Pivotto <[email protected]>
1 parent ac68dd1 commit c0df60c

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

gitlab/resource_gitlab_deploy_key.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"log"
66
"strconv"
7+
"strings"
78

89
"github.com/hashicorp/terraform/helper/schema"
910
gitlab "github.com/xanzy/go-gitlab"
@@ -30,6 +31,9 @@ func resourceGitlabDeployKey() *schema.Resource {
3031
Type: schema.TypeString,
3132
Required: true,
3233
ForceNew: true,
34+
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
35+
return old == strings.TrimSpace(new)
36+
},
3337
},
3438
"can_push": {
3539
Type: schema.TypeBool,
@@ -46,7 +50,7 @@ func resourceGitlabDeployKeyCreate(d *schema.ResourceData, meta interface{}) err
4650
project := d.Get("project").(string)
4751
options := &gitlab.AddDeployKeyOptions{
4852
Title: gitlab.String(d.Get("title").(string)),
49-
Key: gitlab.String(d.Get("key").(string)),
53+
Key: gitlab.String(strings.TrimSpace(d.Get("key").(string))),
5054
CanPush: gitlab.Bool(d.Get("can_push").(bool)),
5155
}
5256

gitlab/resource_gitlab_deploy_key_test.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestAccGitlabDeployKey_basic(t *testing.T) {
2222
Steps: []resource.TestStep{
2323
// Create a project and deployKey with default options
2424
{
25-
Config: testAccGitlabDeployKeyConfig(rInt),
25+
Config: testAccGitlabDeployKeyConfig(rInt, ""),
2626
Check: resource.ComposeTestCheckFunc(
2727
testAccCheckGitlabDeployKeyExists("gitlab_deploy_key.foo", &deployKey),
2828
testAccCheckGitlabDeployKeyAttributes(&deployKey, &testAccGitlabDeployKeyExpectedAttributes{
@@ -44,7 +44,7 @@ func TestAccGitlabDeployKey_basic(t *testing.T) {
4444
},
4545
// Update the project deployKey to toggle the options back
4646
{
47-
Config: testAccGitlabDeployKeyConfig(rInt),
47+
Config: testAccGitlabDeployKeyConfig(rInt, ""),
4848
Check: resource.ComposeTestCheckFunc(
4949
testAccCheckGitlabDeployKeyExists("gitlab_deploy_key.foo", &deployKey),
5050
testAccCheckGitlabDeployKeyAttributes(&deployKey, &testAccGitlabDeployKeyExpectedAttributes{
@@ -57,6 +57,22 @@ func TestAccGitlabDeployKey_basic(t *testing.T) {
5757
})
5858
}
5959

60+
func TestAccGitlabDeployKey_suppressfunc(t *testing.T) {
61+
rInt := acctest.RandInt()
62+
63+
resource.Test(t, resource.TestCase{
64+
PreCheck: func() { testAccPreCheck(t) },
65+
Providers: testAccProviders,
66+
CheckDestroy: testAccCheckGitlabDeployKeyDestroy,
67+
Steps: []resource.TestStep{
68+
// Create a project and deployKey with newline as suffix
69+
{
70+
Config: testAccGitlabDeployKeyConfig(rInt, "\\n"),
71+
},
72+
},
73+
})
74+
}
75+
6076
func testAccCheckGitlabDeployKeyExists(n string, deployKey *gitlab.DeployKey) resource.TestCheckFunc {
6177
return func(s *terraform.State) error {
6278
rs, ok := s.RootModule().Resources[n]
@@ -131,7 +147,7 @@ func testAccCheckGitlabDeployKeyDestroy(s *terraform.State) error {
131147
return nil
132148
}
133149

134-
func testAccGitlabDeployKeyConfig(rInt int) string {
150+
func testAccGitlabDeployKeyConfig(rInt int, suffix string) string {
135151
return fmt.Sprintf(`
136152
resource "gitlab_project" "foo" {
137153
name = "foo-%d"
@@ -145,9 +161,9 @@ resource "gitlab_project" "foo" {
145161
resource "gitlab_deploy_key" "foo" {
146162
project = "${gitlab_project.foo.id}"
147163
title = "deployKey-%d"
148-
key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCj13ozEBZ0s4el4k6mYqoyIKKKMh9hHY0sAYqSPXs2zGuVFZss1P8TPuwmdXVjHR7TiRXwC49zDrkyWJgiufggYJ1VilOohcMOODwZEJz+E5q4GCfHuh90UEh0nl8B2R0Uoy0LPeg93uZzy0hlHApsxRf/XZJz/1ytkZvCtxdllxfImCVxJReMeRVEqFCTCvy3YuJn0bce7ulcTFRvtgWOpQsr6GDK8YkcCCv2eZthVlrEwy6DEpAKTRiRLGgUj4dPO0MmO4cE2qD4ualY01PhNORJ8Q++I+EtkGt/VALkecwFuBkl18/gy+yxNJHpKc/8WVVinDeFrd/HhiY9yU0d [email protected]"
164+
key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCj13ozEBZ0s4el4k6mYqoyIKKKMh9hHY0sAYqSPXs2zGuVFZss1P8TPuwmdXVjHR7TiRXwC49zDrkyWJgiufggYJ1VilOohcMOODwZEJz+E5q4GCfHuh90UEh0nl8B2R0Uoy0LPeg93uZzy0hlHApsxRf/XZJz/1ytkZvCtxdllxfImCVxJReMeRVEqFCTCvy3YuJn0bce7ulcTFRvtgWOpQsr6GDK8YkcCCv2eZthVlrEwy6DEpAKTRiRLGgUj4dPO0MmO4cE2qD4ualY01PhNORJ8Q++I+EtkGt/VALkecwFuBkl18/gy+yxNJHpKc/8WVVinDeFrd/HhiY9yU0d [email protected]%s"
149165
}
150-
`, rInt, rInt)
166+
`, rInt, rInt, suffix)
151167
}
152168

153169
func testAccGitlabDeployKeyUpdateConfig(rInt int) string {

0 commit comments

Comments
 (0)