Skip to content

Commit

Permalink
GH-35 Correctly handle SSH Keys with comments (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
zahiar authored Nov 17, 2021
1 parent 664086e commit 8f9b49b
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 2 deletions.
56 changes: 56 additions & 0 deletions bitbucket/data_source_bitbucket_deploy_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,59 @@ func TestAccBitbucketDeployKeyDataSource_basic(t *testing.T) {
},
})
}

func TestAccBitbucketDeployKeyDataSource_keyWithComment(t *testing.T) {
workspaceSlug := os.Getenv("BITBUCKET_USERNAME")
projectName := "tf-acc-test-" + acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
projectKey := strings.ToUpper(acctest.RandStringFromCharSet(3, acctest.CharSetAlpha))
repoName := "tf-acc-test-" + acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
deployKeyLabel := "TF ACC Test Deploy Key"
deployKeyPublicSSHKey := "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDAK/b1cHHDr/TEV1JGQl+WjCwStKG6Bhrv0rFpEsYlyTBm1fzN0VOJJYn4ZOPCPJwqse6fGbXntEs+BbXiptR+++HycVgl65TMR0b5ul5AgwrVdZdT7qjCOCgaSV74/9xlHDK8oqgGnfA7ZoBBU+qpVyaloSjBdJfLtPY/xqj4yHnXKYzrtn/uFc4Kp9Tb7PUg9Io3qohSTGJGVHnsVblq/rToJG7L5xIo0OxK0SJSQ5vuId93ZuFZrCNMXj8JDHZeSEtjJzpRCBEXHxpOPhAcbm4MzULgkFHhAVgp4JbkrT99/wpvZ7r9AdkTg7HGqL3rlaDrEcWfL7Lu6TnhBdq5 [email protected]"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviders,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
data "bitbucket_workspace" "testacc" {
id = "%s"
}
resource "bitbucket_project" "testacc" {
workspace = data.bitbucket_workspace.testacc.id
name = "%s"
key = "%s"
is_private = true
}
resource "bitbucket_repository" "testacc" {
workspace = data.bitbucket_workspace.testacc.id
project_key = bitbucket_project.testacc.key
name = "%s"
}
resource "bitbucket_deploy_key" "testacc" {
workspace = data.bitbucket_workspace.testacc.id
repository = bitbucket_repository.testacc.name
label = "%s"
key = "%s"
}
data "bitbucket_deploy_key" "testacc" {
id = bitbucket_deploy_key.testacc.id
workspace = data.bitbucket_workspace.testacc.id
repository = bitbucket_repository.testacc.name
}`, workspaceSlug, projectName, projectKey, repoName, deployKeyLabel, deployKeyPublicSSHKey),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.bitbucket_deploy_key.testacc", "workspace", workspaceSlug),
resource.TestCheckResourceAttr("data.bitbucket_deploy_key.testacc", "repository", repoName),
resource.TestCheckResourceAttr("data.bitbucket_deploy_key.testacc", "label", deployKeyLabel),
resource.TestCheckResourceAttr("data.bitbucket_deploy_key.testacc", "key", deployKeyPublicSSHKey),

resource.TestCheckResourceAttrSet("data.bitbucket_deploy_key.testacc", "id"),
),
},
},
})
}
10 changes: 8 additions & 2 deletions bitbucket/resource_bitbucket_deploy_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,13 @@ func resourceBitbucketDeployKeyRead(ctx context.Context, resourceData *schema.Re
}

_ = resourceData.Set("label", deployKey.Label)
_ = resourceData.Set("key", deployKey.Key)

if deployKey.Comment != "" {
_ = resourceData.Set("key", fmt.Sprintf("%s %s", deployKey.Key, deployKey.Comment))
} else {
_ = resourceData.Set("key", deployKey.Key)
}

resourceData.SetId(strconv.Itoa(deployKey.Id))

return nil
Expand Down Expand Up @@ -129,7 +135,7 @@ func resourceBitbucketDeployKeyImport(ctx context.Context, resourceData *schema.
_ = resourceData.Set("repository", splitID[1])
resourceData.SetId(splitID[2])

_ = resourceBitbucketWebhookRead(ctx, resourceData, meta)
_ = resourceBitbucketDeployKeyRead(ctx, resourceData, meta)

return ret, nil
}
49 changes: 49 additions & 0 deletions bitbucket/resource_bitbucket_deploy_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,52 @@ func TestAccBitbucketDeployKeyResource_basic(t *testing.T) {
},
})
}

func TestAccBitbucketDeployKeyResource_keyWithComment(t *testing.T) {
workspaceSlug := os.Getenv("BITBUCKET_USERNAME")
projectName := "tf-acc-test-" + acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
projectKey := strings.ToUpper(acctest.RandStringFromCharSet(3, acctest.CharSetAlpha))
repoName := "tf-acc-test-" + acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
deployKeyLabel := "TF ACC Test Deploy Key"
deployKeyPublicSSHKey := "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDAK/b1cHHDr/TEV1JGQl+WjCwStKG6Bhrv0rFpEsYlyTBm1fzN0VOJJYn4ZOPCPJwqse6fGbXntEs+BbXiptR+++HycVgl65TMR0b5ul5AgwrVdZdT7qjCOCgaSV74/9xlHDK8oqgGnfA7ZoBBU+qpVyaloSjBdJfLtPY/xqj4yHnXKYzrtn/uFc4Kp9Tb7PUg9Io3qohSTGJGVHnsVblq/rToJG7L5xIo0OxK0SJSQ5vuId93ZuFZrCNMXj8JDHZeSEtjJzpRCBEXHxpOPhAcbm4MzULgkFHhAVgp4JbkrT99/wpvZ7r9AdkTg7HGqL3rlaDrEcWfL7Lu6TnhBdq5 [email protected]"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviders,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
data "bitbucket_workspace" "testacc" {
id = "%s"
}
resource "bitbucket_project" "testacc" {
workspace = data.bitbucket_workspace.testacc.id
name = "%s"
key = "%s"
}
resource "bitbucket_repository" "testacc" {
workspace = data.bitbucket_workspace.testacc.id
project_key = bitbucket_project.testacc.key
name = "%s"
}
resource "bitbucket_deploy_key" "testacc" {
workspace = data.bitbucket_workspace.testacc.id
repository = bitbucket_repository.testacc.name
label = "%s"
key = "%s"
}`, workspaceSlug, projectName, projectKey, repoName, deployKeyLabel, deployKeyPublicSSHKey),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("bitbucket_deploy_key.testacc", "workspace", workspaceSlug),
resource.TestCheckResourceAttr("bitbucket_deploy_key.testacc", "repository", repoName),
resource.TestCheckResourceAttr("bitbucket_deploy_key.testacc", "label", deployKeyLabel),
resource.TestCheckResourceAttr("bitbucket_deploy_key.testacc", "key", deployKeyPublicSSHKey),

resource.TestCheckResourceAttrSet("bitbucket_deploy_key.testacc", "id"),
),
},
},
})
}

0 comments on commit 8f9b49b

Please sign in to comment.