Skip to content

Commit

Permalink
deployments data source
Browse files Browse the repository at this point in the history
  • Loading branch information
DrFaust92 committed Feb 10, 2024
1 parent 48260b3 commit ae87f80
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 0 deletions.
70 changes: 70 additions & 0 deletions bitbucket/data_deployments.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package bitbucket

import (
"context"
"fmt"
"log"

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

func dataDeployments() *schema.Resource {
return &schema.Resource{
ReadWithoutTimeout: dataReadDeployments,

Schema: map[string]*schema.Schema{
"uuids": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
},
"workspace": {
Type: schema.TypeString,
Required: true,
},
"repository": {
Type: schema.TypeString,
Required: true,
},
"names": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
},
},
}
}

func dataReadDeployments(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(Clients).genClient
deployApi := c.ApiClient.DeploymentsApi

workspace := d.Get("workspace").(string)
repoId := d.Get("repository").(string)

deploymentsResp, _, err := deployApi.GetEnvironmentsForRepository(c.AuthContext, workspace, repoId, nil)
if err := handleClientError(err); err != nil {
return diag.FromErr(err)
}

deployments := deploymentsResp.Values

log.Printf("haha %#v", deployments)

var uuids []string
for _, deployment := range deployments {
uuids = append(uuids, deployment.Uuid)
}

var names []string
for _, deployment := range deployments {
names = append(names, deployment.Name)
}

d.SetId(fmt.Sprintf("%s/%s", workspace, repoId))
d.Set("uuids", uuids)
d.Set("names", names)

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

import (
"fmt"
"os"
"testing"

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

func TestAccDataSourceDeployments_basic(t *testing.T) {
dataSourceName := "data.bitbucket_deployments.test"
resourceName := "bitbucket_deployment.test"

rName := acctest.RandomWithPrefix("tf-test")

workspace := os.Getenv("BITBUCKET_TEAM")
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccBitbucketDeploymentsConfig(workspace, rName, rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "workspace", workspace),
resource.TestCheckResourceAttr(dataSourceName, "uuids.#", "1"),
resource.TestCheckResourceAttr(dataSourceName, "names.#", "1"),
resource.TestCheckResourceAttrPair(dataSourceName, "uuids.0", resourceName, "uuid"),
resource.TestCheckResourceAttrPair(dataSourceName, "names.0", resourceName, "name"),
),
},
},
})
}

func testAccBitbucketDeploymentsConfig(workspace, repoName, deployName string) string {
return testAccBitbucketDeployment(workspace, repoName, deployName) + fmt.Sprintf(`
data "bitbucket_deployments" "test" {
workspace = %[1]q
repository = bitbucket_repository.test.slug
depends_on = [bitbucket_deployment.test]
}
`, workspace)
}
1 change: 1 addition & 0 deletions bitbucket/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func Provider() *schema.Provider {
DataSourcesMap: map[string]*schema.Resource{
"bitbucket_current_user": dataCurrentUser(),
"bitbucket_deployment": dataDeployment(),
"bitbucket_deployments": dataDeployments(),
"bitbucket_group": dataGroup(),
"bitbucket_group_members": dataGroupMembers(),
"bitbucket_groups": dataGroups(),
Expand Down
34 changes: 34 additions & 0 deletions docs/data-sources/deployments.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
layout: "bitbucket"
page_title: "Bitbucket: bitbucket_deployments"
sidebar_current: "docs-bitbucket-data-deployments"
description: |-
Provides a data for Bitbucket Deployments
---

# bitbucket\_deployments

Provides a way to fetch data on Deployments.

OAuth2 Scopes: `none`

## Example Usage

```hcl
data "bitbucket_deployments" "example" {
repository = "example"
workspace = "example"
}
```

## Argument Reference

The following arguments are supported:

* `repository` - (Required) The repository name.
* `workspace` - (Required) The workspace name.

## Attributes Reference

* `uuids` - UUIDs of deployments for a repository.
* `names` - Names of deployments for a repository.

0 comments on commit ae87f80

Please sign in to comment.