Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions bitbucket/data_source_bitbucket_deploy_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package bitbucket

import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceBitbucketDeployKey() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceBitbucketDeployKeyRead,
Schema: map[string]*schema.Schema{
"id": {
Description: "The ID of the deploy key.",
Type: schema.TypeString,
Required: true,
},
"workspace": {
Description: "The slug or UUID (including the enclosing `{}`) of the workspace.",
Type: schema.TypeString,
Required: true,
},
"repository": {
Description: "The name of the repository (must consist of only lowercase ASCII letters, numbers, underscores and hyphens).",
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: validateRepositoryName,
},
"label": {
Description: "The label of the deploy key.",
Type: schema.TypeString,
Computed: true,
},
"key": {
Description: "The public SSH key to attach to this deploy key.",
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
},
}
}

func dataSourceBitbucketDeployKeyRead(ctx context.Context, resourceData *schema.ResourceData, meta interface{}) diag.Diagnostics {
return resourceBitbucketDeployKeyRead(ctx, resourceData, meta)
}
67 changes: 67 additions & 0 deletions bitbucket/data_source_bitbucket_deploy_key_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package bitbucket

import (
"fmt"
"os"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccBitbucketDeployKeyDataSource_basic(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"

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"),
),
},
},
})
}
2 changes: 2 additions & 0 deletions bitbucket/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func Provider() *schema.Provider {

DataSourcesMap: map[string]*schema.Resource{
"bitbucket_default_reviewer": dataSourceBitbucketDefaultReviewer(),
"bitbucket_deploy_key": dataSourceBitbucketDeployKey(),
"bitbucket_project": dataSourceBitbucketProject(),
"bitbucket_repository": dataSourceBitbucketRepository(),
"bitbucket_user": dataSourceBitbucketUser(),
Expand All @@ -36,6 +37,7 @@ func Provider() *schema.Provider {

ResourcesMap: map[string]*schema.Resource{
"bitbucket_default_reviewer": resourceBitbucketDefaultReviewer(),
"bitbucket_deploy_key": resourceBitbucketDeployKey(),
"bitbucket_project": resourceBitbucketProject(),
"bitbucket_repository": resourceBitbucketRepository(),
"bitbucket_webhook": resourceBitbucketWebhook(),
Expand Down
135 changes: 135 additions & 0 deletions bitbucket/resource_bitbucket_deploy_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package bitbucket

import (
"context"
"fmt"
"strconv"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
gobb "github.com/ktrysmt/go-bitbucket"
)

func resourceBitbucketDeployKey() *schema.Resource {
return &schema.Resource{
CreateContext: resourceBitbucketDeployKeyCreate,
ReadContext: resourceBitbucketDeployKeyRead,
DeleteContext: resourceBitbucketDeployKeyDelete,
Importer: &schema.ResourceImporter{
StateContext: resourceBitbucketDeployKeyImport,
},
Schema: map[string]*schema.Schema{
"id": {
Description: "The ID of the deploy key.",
Type: schema.TypeString,
Computed: true,
},
"workspace": {
Description: "The slug or UUID (including the enclosing `{}`) of the workspace.",
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"repository": {
Description: "The name of the repository (must consist of only lowercase ASCII letters, numbers, underscores and hyphens).",
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateDiagFunc: validateRepositoryName,
},
"label": {
Description: "The label of the deploy key.",
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"key": {
Description: "The public SSH key to attach to this deploy key.",
Type: schema.TypeString,
Required: true,
ForceNew: true,
Sensitive: true,
},
},
}
}

func resourceBitbucketDeployKeyCreate(ctx context.Context, resourceData *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*gobb.Client)

deployKey, err := client.Repositories.DeployKeys.Create(
&gobb.DeployKeyOptions{
Owner: resourceData.Get("workspace").(string),
RepoSlug: resourceData.Get("repository").(string),
Label: resourceData.Get("label").(string),
Key: resourceData.Get("key").(string),
},
)
if err != nil {
return diag.FromErr(fmt.Errorf("unable to create deploy key with error: %s", err))
}

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

return resourceBitbucketDeployKeyRead(ctx, resourceData, meta)
}

func resourceBitbucketDeployKeyRead(ctx context.Context, resourceData *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*gobb.Client)

deployKeyId, _ := strconv.Atoi(resourceData.Get("id").(string))
deployKey, err := client.Repositories.DeployKeys.Get(
&gobb.DeployKeyOptions{
Owner: resourceData.Get("workspace").(string),
RepoSlug: resourceData.Get("repository").(string),
Id: deployKeyId,
},
)
if err != nil {
return diag.FromErr(fmt.Errorf("unable to get deploy key with error: %s", err))
}

_ = resourceData.Set("label", deployKey.Label)
_ = resourceData.Set("key", deployKey.Key)
resourceData.SetId(strconv.Itoa(deployKey.Id))

return nil
}

func resourceBitbucketDeployKeyDelete(ctx context.Context, resourceData *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*gobb.Client)

deployKeyId, _ := strconv.Atoi(resourceData.Id())
_, err := client.Repositories.DeployKeys.Delete(
&gobb.DeployKeyOptions{
Owner: resourceData.Get("workspace").(string),
RepoSlug: resourceData.Get("repository").(string),
Id: deployKeyId,
},
)
if err != nil {
return diag.FromErr(fmt.Errorf("unable to delete deploy key with error: %s", err))
}

resourceData.SetId("")

return nil
}

func resourceBitbucketDeployKeyImport(ctx context.Context, resourceData *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
ret := []*schema.ResourceData{resourceData}

splitID := strings.Split(resourceData.Id(), "/")
if len(splitID) < 2 {
return ret, fmt.Errorf("invalid import ID. It must to be in this format \"<workspace-slug|workspace-uuid>/<repository-name>/<deploy-key-id>\"")
}

_ = resourceData.Set("workspace", splitID[0])
_ = resourceData.Set("repository", splitID[1])
resourceData.SetId(splitID[2])

_ = resourceBitbucketWebhookRead(ctx, resourceData, meta)

return ret, nil
}
60 changes: 60 additions & 0 deletions bitbucket/resource_bitbucket_deploy_key_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package bitbucket

import (
"fmt"
"os"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccBitbucketDeployKeyResource_basic(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"

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"),
),
},
},
})
}
29 changes: 29 additions & 0 deletions docs/data-sources/bitbucket_deploy_key.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Data Source: bitbucket_deploy_key
Use this data source to get the deploy key resource, you can then reference its attributes without having to hardcode them.

## Example Usage
```hcl
data "bitbucket_deploy_key" "example" {
id = "{deploy-key-id}"
workspace = "workspace-slug"
repository = "example-repo"
}
```
```hcl
data "bitbucket_deploy_key" "example" {
id = "{deploy-key-id}"
workspace = "{workspace-uuid}"
repository = "example-repo"
}
```

## Argument Reference
The following arguments are supported:
* `id` - (Required) The ID of the deploy key.
* `workspace` - (Required) The slug or UUID (including the enclosing `{}`) of the workspace.
* `repository` - (Required) The name of the repository (must consist of only lowercase ASCII letters, numbers, underscores and hyphens).

## Attribute Reference
In addition to the arguments above, the following additional attributes are exported:
* `label` - The label of the deploy key.
* `key` - The public SSH key attached to this deploy key.
Loading