Skip to content

Commit

Permalink
GH-161 Add workspace projects data source (#162)
Browse files Browse the repository at this point in the history
Along with docs.
  • Loading branch information
zahiar committed May 20, 2023
1 parent ce5d7fe commit a574a3d
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 1 deletion.
2 changes: 1 addition & 1 deletion bitbucket/data_source_bitbucket_workspace_members.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func dataSourceBitbucketWorkspaceMembers() *schema.Resource {
Required: true,
},
"members": {
Description: "List of member UUID's (including the enclosing `{}`).",
Description: "List of Members.",
Type: schema.TypeSet,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand Down
68 changes: 68 additions & 0 deletions bitbucket/data_source_bitbucket_workspace_projects.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package bitbucket

import (
"context"
"fmt"

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

func dataSourceBitbucketWorkspaceProjects() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceBitbucketWorkspaceProjectsRead,
Schema: map[string]*schema.Schema{
"workspace": {
Description: "The slug of the workspace.",
Type: schema.TypeString,
Required: true,
},
"projects": {
Description: "List of Projects.",
Type: schema.TypeSet,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Description: "The Project's UUID.",
Type: schema.TypeString,
Computed: true,
},
"name": {
Description: "The Project's name.",
Type: schema.TypeString,
Computed: true,
},
"key": {
Description: "The Project's key.",
Type: schema.TypeString,
Computed: true,
},
},
},
Computed: true,
},
},
}
}

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

membership, err := client.Workspaces.Projects(resourceData.Get("workspace").(string))
if err != nil {
return diag.FromErr(fmt.Errorf("unable to get workspace projects with error: %s", err))
}

var projects []interface{}
for _, project := range membership.Items {
projects = append(projects, map[string]interface{}{
"id": project.Uuid,
"name": project.Name,
"key": project.Key,
})
}
_ = resourceData.Set("projects", projects)
resourceData.SetId(resourceData.Get("workspace").(string))

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

import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"os"
"strings"
"testing"

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

func TestAccBitbucketWorkspaceProjectsDataSource_basic(t *testing.T) {
projectName := "tf-acc-test-" + acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
projectKey := strings.ToUpper(acctest.RandStringFromCharSet(3, acctest.CharSetAlpha))
projectDescription := "TF ACC Test Project"

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"
description = "%s"
is_private = true
}
data "bitbucket_workspace_projects" "testacc" {
workspace = data.bitbucket_workspace.testacc.id
depends_on = [bitbucket_project.testacc]
}`, os.Getenv("BITBUCKET_USERNAME"), projectName, projectKey, projectDescription),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.bitbucket_workspace_projects.testacc", "id", os.Getenv("BITBUCKET_USERNAME")),
resource.TestCheckResourceAttr("data.bitbucket_workspace_projects.testacc", "workspace", os.Getenv("BITBUCKET_USERNAME")),

resource.TestCheckResourceAttrSet("data.bitbucket_workspace_projects.testacc", "projects.#"),
resource.TestCheckResourceAttrSet("data.bitbucket_workspace_projects.testacc", "projects.0.id"),
resource.TestCheckResourceAttr("data.bitbucket_workspace_projects.testacc", "projects.0.name", projectName),
resource.TestCheckResourceAttr("data.bitbucket_workspace_projects.testacc", "projects.0.key", projectKey),
),
},
},
})
}
1 change: 1 addition & 0 deletions bitbucket/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func Provider() *schema.Provider {
"bitbucket_webhook": dataSourceBitbucketWebhook(),
"bitbucket_workspace": dataSourceBitbucketWorkspace(),
"bitbucket_workspace_members": dataSourceBitbucketWorkspaceMembers(),
"bitbucket_workspace_projects": dataSourceBitbucketWorkspaceProjects(),
},

ResourcesMap: map[string]*schema.Resource{
Expand Down
24 changes: 24 additions & 0 deletions docs/data-sources/bitbucket_workspace_projects.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Data Source: bitbucket_workspace_projects
Use this data source to get a list of projects belonging to a workspace, you can then reference its attributes without having to hardcode them.

## Example Usage
```hcl
data "bitbucket_workspace" "example" {
id = "example-slug"
}
data "bitbucket_workspace_projects" "example" {
workspace = data.bitbucket_workspace.id
}
```

## Argument Reference
The following arguments are supported:
* `workspace` - (Required) The slug or UUID (including the enclosing `{}`) of the workspace the user belongs to.

## Attribute Reference
In addition to the arguments above, the following attributes are exported:
* `projects` - A list of Project information, of which each entry in the list contains:
* `id` - The Project's UUID.
* `name` - The Project's name.
* `key` - The Project's key.

0 comments on commit a574a3d

Please sign in to comment.