Skip to content

Commit

Permalink
resource/github_repository_deploy_key: Trim key
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Aug 13, 2018
1 parent 6d6cb7d commit 85d0086
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 12 deletions.
19 changes: 15 additions & 4 deletions github/resource_github_repository_deploy_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package github

import (
"context"
"regexp"
"strconv"
"strings"

"github.com/google/go-github/github"
"github.com/hashicorp/terraform/helper/schema"
Expand All @@ -12,17 +14,18 @@ func resourceGithubRepositoryDeployKey() *schema.Resource {
return &schema.Resource{
Create: resourceGithubRepositoryDeployKeyCreate,
Read: resourceGithubRepositoryDeployKeyRead,
// Deploy keys are defined immutable in the API. Updating results in force new.
Delete: resourceGithubRepositoryDeployKeyDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

// Deploy keys are defined immutable in the API. Updating results in force new.
Schema: map[string]*schema.Schema{
"key": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
DiffSuppressFunc: suppressDeployKeyDiff,
},
"read_only": &schema.Schema{
Type: schema.TypeBool,
Expand Down Expand Up @@ -118,3 +121,11 @@ func resourceGithubRepositoryDeployKeyDelete(d *schema.ResourceData, meta interf

return err
}

func suppressDeployKeyDiff(k, oldV, newV string, d *schema.ResourceData) bool {
newV = strings.TrimSpace(newV)
keyRe := regexp.MustCompile(`^([a-z0-9-]+ [^\s]+)( [^\s]+)?$`)
newTrimmed := keyRe.ReplaceAllString(newV, "$1")

return oldV == newTrimmed
}
40 changes: 32 additions & 8 deletions github/resource_github_repository_deploy_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package github
import (
"context"
"fmt"
"path/filepath"
"regexp"
"strconv"
"testing"

Expand All @@ -11,21 +13,43 @@ import (
"github.com/hashicorp/terraform/terraform"
)

func TestSuppressDeployKeyDiff(t *testing.T) {
oldV := "ssh-rsa AAAABB...cd+== terraform-acctest@hashicorp.com\n"
newV := "ssh-rsa AAAABB...cd+=="
if suppressDeployKeyDiff("test", oldV, newV, nil) {
t.Fatalf("Expected %q and %q to be suppressed", oldV, newV)
}

oldV = "ssh-rsa AAAABB...cd+=="
newV = "ssh-rsa AAAABB...cd+=="
if suppressDeployKeyDiff("test", oldV, newV, nil) {
t.Fatalf("Expected %q and %q to be suppressed", oldV, newV)
}

oldV = "ssh-rsa AAAABV...cd+=="
newV = "ssh-rsa AAAABC...cd+=="
if !suppressDeployKeyDiff("test", oldV, newV, nil) {
t.Fatalf("Expected %q and %q NOT to be suppressed", oldV, newV)
}
}

func TestAccGithubRepositoryDeployKey_basic(t *testing.T) {
rs := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
repositoryName := fmt.Sprintf("acctest-%s", rs)
keyPath := filepath.Join("test-fixtures", "id_rsa.pub")

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckGithubRepositoryDeployKeyDestroy,
Steps: []resource.TestStep{
{
Config: testAccGithubRepositoryDeployKeyConfig(repositoryName),
Config: testAccGithubRepositoryDeployKeyConfig(repositoryName, keyPath),
Check: resource.ComposeTestCheckFunc(
testAccCheckGithubRepositoryDeployKeyExists("github_repository_deploy_key.test_repo_deploy_key"),
resource.TestCheckResourceAttr("github_repository_deploy_key.test_repo_deploy_key", "read_only", "false"),
resource.TestCheckResourceAttr("github_repository_deploy_key.test_repo_deploy_key", "repository", repositoryName),
resource.TestCheckResourceAttr("github_repository_deploy_key.test_repo_deploy_key", "key", testAccGithubRepositoryDeployKeytestDeployKey),
resource.TestMatchResourceAttr("github_repository_deploy_key.test_repo_deploy_key", "key", regexp.MustCompile(`^ssh-rsa [^\s]+$`)),
resource.TestCheckResourceAttr("github_repository_deploy_key.test_repo_deploy_key", "title", "title"),
),
},
Expand All @@ -36,13 +60,15 @@ func TestAccGithubRepositoryDeployKey_basic(t *testing.T) {
func TestAccGithubRepositoryDeployKey_importBasic(t *testing.T) {
rs := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
repositoryName := fmt.Sprintf("acctest-%s", rs)
keyPath := filepath.Join("test-fixtures", "id_rsa.pub")

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckGithubRepositoryDeployKeyDestroy,
Steps: []resource.TestStep{
{
Config: testAccGithubRepositoryDeployKeyConfig(repositoryName),
Config: testAccGithubRepositoryDeployKeyConfig(repositoryName, keyPath),
},
{
ResourceName: "github_repository_deploy_key.test_repo_deploy_key",
Expand Down Expand Up @@ -115,19 +141,17 @@ func testAccCheckGithubRepositoryDeployKeyExists(n string) resource.TestCheckFun
}
}

const testAccGithubRepositoryDeployKeytestDeployKey = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDnDk1liOxXwE27fjOVVHl6RNVgQznGqGIfhsoa5QNfLOcoWJR3EIv44dSUx1GSvxQ7uR9qBY/i/SEdAbKdupo3Ru5sykc0GqaMRVys+Cin/Lgnl6+ntmTZOudNjIbz10Vfu/dKmexSzqlD3XWzPGXRI5WyKWzvc2XKjRdfnOOzogJpqJ5kh/CN0ZhCzBPTu/b4mJl2ionTEzEeLK2g4Re4IuU/dGoyf0LGLidjmqhSY7dQtL+mfte9m3x/BQTrDf0+AW3kGWXR8EL0EyIJ2HRtHW67YnoOcTAFK0hDCuKgvt78rqdUQ2bVjcsIhNqnvQMPf3ZeZ5bP2JqB9zKaFl8uaRJv+TdxEeFTkgnbYb85M+aBggBYr6xxeb24g7WlU0iPxJ8GmjvCizxe2I1DOJDRDozn1sinKjapNRdJy00iuo46TJC5Wgmid0vnMJ7SMZtubz+btxhoFLt4F4U2JnILaYG4/buJg4H/GkqmkE8G3hr4b4mgsFXBtBFgK6uCTFQSvvV7TyyWkZxHL6DRCxL/Dp0bSj+EM8Tw1K304EvkBEO3rMyvPs4nXL7pepyKWalmUI8U4Qp2xMXSq7fmfZY55osb03MUAtKl0wJ/ykyKOwYWeLbubSVcc6VPx5bXZmnM5bTcZdYW9+vNt86X2F2b0h/sIkGNEPpqQQBzElY+fQ=="

func testAccGithubRepositoryDeployKeyConfig(name string) string {
func testAccGithubRepositoryDeployKeyConfig(name, keyPath string) string {
return fmt.Sprintf(`
resource "github_repository" "test_repo" {
name = "%s"
}
resource "github_repository_deploy_key" "test_repo_deploy_key" {
key = "%s"
key = "${file("%s")}"
read_only = "false"
repository = "${github_repository.test_repo.name}"
title = "title"
}
`, name, testAccGithubRepositoryDeployKeytestDeployKey)
`, name, keyPath)
}
1 change: 1 addition & 0 deletions github/test-fixtures/id_rsa.pub
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDnDk1liOxXwE27fjOVVHl6RNVgQznGqGIfhsoa5QNfLOcoWJR3EIv44dSUx1GSvxQ7uR9qBY/i/SEdAbKdupo3Ru5sykc0GqaMRVys+Cin/Lgnl6+ntmTZOudNjIbz10Vfu/dKmexSzqlD3XWzPGXRI5WyKWzvc2XKjRdfnOOzogJpqJ5kh/CN0ZhCzBPTu/b4mJl2ionTEzEeLK2g4Re4IuU/dGoyf0LGLidjmqhSY7dQtL+mfte9m3x/BQTrDf0+AW3kGWXR8EL0EyIJ2HRtHW67YnoOcTAFK0hDCuKgvt78rqdUQ2bVjcsIhNqnvQMPf3ZeZ5bP2JqB9zKaFl8uaRJv+TdxEeFTkgnbYb85M+aBggBYr6xxeb24g7WlU0iPxJ8GmjvCizxe2I1DOJDRDozn1sinKjapNRdJy00iuo46TJC5Wgmid0vnMJ7SMZtubz+btxhoFLt4F4U2JnILaYG4/buJg4H/GkqmkE8G3hr4b4mgsFXBtBFgK6uCTFQSvvV7TyyWkZxHL6DRCxL/Dp0bSj+EM8Tw1K304EvkBEO3rMyvPs4nXL7pepyKWalmUI8U4Qp2xMXSq7fmfZY55osb03MUAtKl0wJ/ykyKOwYWeLbubSVcc6VPx5bXZmnM5bTcZdYW9+vNt86X2F2b0h/sIkGNEPpqQQBzElY+fQ== terraform-acctest@hashicorp.com

0 comments on commit 85d0086

Please sign in to comment.