-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Along with docs.
- Loading branch information
Showing
5 changed files
with
146 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
52
bitbucket/data_source_bitbucket_workspace_projects_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
), | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |