Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow getting deployment environment by name #159

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 9 additions & 7 deletions bitbucket/data_source_bitbucket_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@ import (

func dataSourceBitbucketDeployment() *schema.Resource {
return &schema.Resource{
ReadContext: resourceBitbucketDeploymentRead,
ReadContext: resourceBitbucketDeploymentReadByNameOrId,
Schema: map[string]*schema.Schema{
"id": {
Description: "The ID of the deployment.",
Type: schema.TypeString,
Required: true,
Optional: true,
Computed: true,
},
"name": {
Description: "The name of the deployment environment.",
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"workspace": {
Description: "The slug or UUID (including the enclosing `{}`) of the workspace.",
Expand All @@ -24,11 +31,6 @@ func dataSourceBitbucketDeployment() *schema.Resource {
Required: true,
ValidateDiagFunc: validateRepositoryName,
},
"name": {
Description: "The name of the deployment environment.",
Type: schema.TypeString,
Computed: true,
},
"environment": {
Description: "The environment of the deployment (will be one of 'Test', 'Staging', or 'Production').",
Type: schema.TypeString,
Expand Down
45 changes: 43 additions & 2 deletions bitbucket/data_source_bitbucket_deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,47 @@ func TestAccBitbucketDeploymentDataSource_basic(t *testing.T) {
resource.TestCheckResourceAttrSet("data.bitbucket_deployment.testacc", "id"),
),
},
{
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"
enable_pipelines = true
}

resource "bitbucket_deployment" "testacc" {
workspace = data.bitbucket_workspace.testacc.id
repository = bitbucket_repository.testacc.name
name = "%s"
environment = "Production"
}

data "bitbucket_deployment" "testacc" {
id = bitbucket_deployment.testacc.id
workspace = data.bitbucket_workspace.testacc.id
repository = bitbucket_repository.testacc.name
}`, workspaceSlug, projectName, projectKey, repoName, deploymentName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.bitbucket_deployment.testacc", "workspace", workspaceSlug),
resource.TestCheckResourceAttr("data.bitbucket_deployment.testacc", "repository", repoName),
resource.TestCheckResourceAttr("data.bitbucket_deployment.testacc", "name", deploymentName),
resource.TestCheckResourceAttr("data.bitbucket_deployment.testacc", "environment", "Production"),

resource.TestCheckResourceAttrSet("data.bitbucket_deployment.testacc", "id"),
),
},
{
Config: fmt.Sprintf(`
data "bitbucket_workspace" "testacc" {
Expand Down Expand Up @@ -131,10 +172,10 @@ func TestAccBitbucketDeploymentDataSource_basic(t *testing.T) {
}

data "bitbucket_deployment" "testacc" {
id = bitbucket_deployment.testacc.id
name = "%s"
workspace = data.bitbucket_workspace.testacc.id
repository = bitbucket_repository.testacc.name
}`, workspaceSlug, projectName, projectKey, repoName, deploymentName),
}`, workspaceSlug, projectName, projectKey, repoName, deploymentName, deploymentName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.bitbucket_deployment.testacc", "workspace", workspaceSlug),
resource.TestCheckResourceAttr("data.bitbucket_deployment.testacc", "repository", repoName),
Expand Down
52 changes: 52 additions & 0 deletions bitbucket/resource_bitbucket_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -103,6 +104,57 @@ func resourceBitbucketDeploymentRead(ctx context.Context, resourceData *schema.R
return nil
}

func resourceBitbucketDeploymentReadByNameOrId(ctx context.Context, resourceData *schema.ResourceData, meta interface{}) diag.Diagnostics {
id := resourceData.Get("id")
if id != nil && id != "" {
return resourceBitbucketDeploymentRead(ctx, resourceData, meta)
}

name := resourceData.Get("name")
if name != nil && name != "" {
return resourceBitbucketDeploymentReadByName(ctx, resourceData, meta)
}

return diag.Errorf("Either name or id must be provided")
}

func resourceBitbucketDeploymentReadByName(ctx context.Context, resourceData *schema.ResourceData, meta interface{}) diag.Diagnostics {
// Artificial sleep due to Bitbucket's API taking time to return newly created deployments
time.Sleep(3 * time.Second)

client := meta.(*Clients).V2

deployments, err := client.Repositories.Repository.ListEnvironments(
&gobb.RepositoryEnvironmentsOptions{
Owner: resourceData.Get("workspace").(string),
RepoSlug: resourceData.Get("repository").(string),
},
)
if err != nil {
return diag.FromErr(fmt.Errorf("unable to get deployment variable with error: %s", err))
}

name := resourceData.Get("name").(string)

var deployment *gobb.Environment
for _, item := range deployments.Environments {
if strings.EqualFold(item.Name, name) {
deployment = &item
break
}
}

if deployment == nil {
return diag.FromErr(errors.New("unable to get deployment, Bitbucket API did not return it"))
}

_ = resourceData.Set("name", deployment.Name)
_ = resourceData.Set("environment", gobb.RepositoryEnvironmentTypeOption(deployment.Rank).String())
resourceData.SetId(deployment.Uuid)

return nil
}

func resourceBitbucketDeploymentDelete(ctx context.Context, resourceData *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*Clients).V2

Expand Down
20 changes: 19 additions & 1 deletion docs/data-sources/bitbucket_deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,32 @@ data "bitbucket_deployment" "example" {
repository = "example-repo"
}
```
```hcl
data "bitbucket_deployment" "example" {
name = "deployment-name"
workspace = "workspace-slug"
repository = "example-repo"
}
```
```hcl
data "bitbucket_deployment" "example" {
name = "deployment-name"
workspace = "{workspace-uuid}"
repository = "example-repo"
}
```

## Argument Reference
The following arguments are supported:
* `id` - (Required) The ID of the deployment.
* `id` - (Optional) The ID of the deployment.
* `name` - (Optional) The name of the deployment.
~> **NOTE:** Either `id` or `name` must be set. If both are set, `name` will be ignored.
* `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, hyphens and periods).


## Attribute Reference
In addition to the arguments above, the following additional attributes are exported:
* `id` - The ID of the deployment environment.
* `name` - The name of the deployment environment.
* `environment` - The environment of the deployment (will be one of 'Test', 'Staging', or 'Production').