mirror of
https://github.com/ysoftdevs/terraform-provider-bitbucketserver.git
synced 2026-04-05 08:47:02 +02:00
Added data.bitbucketserver_project_groups
This commit is contained in:
12
README.md
12
README.md
@@ -173,6 +173,18 @@ data "bitbucketserver_application_properties" "main" {}
|
|||||||
* `build_date` - Date the Bitbucket build was made,
|
* `build_date` - Date the Bitbucket build was made,
|
||||||
* `display_name` - Name of the Bitbucket instance.
|
* `display_name` - Name of the Bitbucket instance.
|
||||||
|
|
||||||
|
### Application Properties
|
||||||
|
|
||||||
|
```hcl
|
||||||
|
data "bitbucketserver_project_groups" "proj" {
|
||||||
|
project = "TEST"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Attributes
|
||||||
|
|
||||||
|
* `groups` - List of maps containing `name` and `permission` keys.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Development Guide
|
## Development Guide
|
||||||
|
|||||||
97
bitbucket/data_project_groups.go
Normal file
97
bitbucket/data_project_groups.go
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
package bitbucket
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"github.com/hashicorp/terraform/helper/schema"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ProjectGroup struct {
|
||||||
|
Group struct {
|
||||||
|
Name string `json:"name,omitempty"`
|
||||||
|
} `json:"group,omitempty"`
|
||||||
|
Permission string `json:"permission,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type PaginatedProjectGroups struct {
|
||||||
|
Values []ProjectGroup `json:"values,omitempty"`
|
||||||
|
Size int `json:"size,omitempty"`
|
||||||
|
Limit int `json:"limit,omitempty"`
|
||||||
|
IsLastPage bool `json:"isLastPage,omitempty"`
|
||||||
|
Start int `json:"start,omitempty"`
|
||||||
|
NextPageStart int `json:"nextPageStart,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func dataSourceProjectGroups() *schema.Resource {
|
||||||
|
return &schema.Resource{
|
||||||
|
Read: dataSourceProjectGroupsRead,
|
||||||
|
|
||||||
|
Schema: map[string]*schema.Schema{
|
||||||
|
"project": {
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Required: true,
|
||||||
|
},
|
||||||
|
"groups": {
|
||||||
|
Type: schema.TypeList,
|
||||||
|
Computed: true,
|
||||||
|
Elem: &schema.Resource{
|
||||||
|
Schema: map[string]*schema.Schema{
|
||||||
|
"name": {
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Computed: true,
|
||||||
|
},
|
||||||
|
"permission": {
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Computed: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func dataSourceProjectGroupsRead(d *schema.ResourceData, m interface{}) error {
|
||||||
|
client := m.(*BitbucketClient)
|
||||||
|
|
||||||
|
resourceURL := fmt.Sprintf("/rest/api/1.0/projects/%s/permissions/groups",
|
||||||
|
d.Get("project").(string),
|
||||||
|
)
|
||||||
|
|
||||||
|
var projectGroups PaginatedProjectGroups
|
||||||
|
var terraformGroups []interface{}
|
||||||
|
|
||||||
|
for {
|
||||||
|
reviewersResponse, err := client.Get(resourceURL)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
decoder := json.NewDecoder(reviewersResponse.Body)
|
||||||
|
err = decoder.Decode(&projectGroups)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, group := range projectGroups.Values {
|
||||||
|
g := make(map[string]interface{})
|
||||||
|
g["name"] = group.Group.Name
|
||||||
|
g["permission"] = group.Permission
|
||||||
|
terraformGroups = append(terraformGroups, g)
|
||||||
|
}
|
||||||
|
|
||||||
|
if projectGroups.IsLastPage == false {
|
||||||
|
resourceURL = fmt.Sprintf("/rest/api/1.0/projects/%s/permissions/groups?start=%d",
|
||||||
|
d.Get("project").(string),
|
||||||
|
projectGroups.NextPageStart,
|
||||||
|
)
|
||||||
|
projectGroups = PaginatedProjectGroups{}
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
d.SetId(d.Get("project").(string))
|
||||||
|
d.Set("groups", terraformGroups)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
36
bitbucket/data_project_groups_test.go
Normal file
36
bitbucket/data_project_groups_test.go
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
package bitbucket
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"math/rand"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/hashicorp/terraform/helper/resource"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestAccBitbucketDatProjectGroups_check_empty(t *testing.T) {
|
||||||
|
config := fmt.Sprintf(`
|
||||||
|
resource "bitbucketserver_project" "test" {
|
||||||
|
key = "TEST%v"
|
||||||
|
name = "test-repo-for-repository-test"
|
||||||
|
}
|
||||||
|
|
||||||
|
data "bitbucketserver_project_groups" "test" {
|
||||||
|
project = bitbucketserver_project.test.key
|
||||||
|
}
|
||||||
|
`, rand.New(rand.NewSource(time.Now().UnixNano())).Int())
|
||||||
|
|
||||||
|
resource.Test(t, resource.TestCase{
|
||||||
|
PreCheck: func() { testAccPreCheck(t) },
|
||||||
|
Providers: testAccProviders,
|
||||||
|
Steps: []resource.TestStep{
|
||||||
|
{
|
||||||
|
Config: config,
|
||||||
|
Check: resource.ComposeTestCheckFunc(
|
||||||
|
resource.TestCheckResourceAttr("data.bitbucketserver_project_groups.test", "groups.#", "0"),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -30,6 +30,7 @@ func Provider() terraform.ResourceProvider {
|
|||||||
ConfigureFunc: providerConfigure,
|
ConfigureFunc: providerConfigure,
|
||||||
DataSourcesMap: map[string]*schema.Resource{
|
DataSourcesMap: map[string]*schema.Resource{
|
||||||
"bitbucketserver_application_properties": dataSourceApplicationProperties(),
|
"bitbucketserver_application_properties": dataSourceApplicationProperties(),
|
||||||
|
"bitbucketserver_project_groups": dataSourceProjectGroups(),
|
||||||
},
|
},
|
||||||
ResourcesMap: map[string]*schema.Resource{
|
ResourcesMap: map[string]*schema.Resource{
|
||||||
"bitbucketserver_admin_license": resourceAdminLicense(),
|
"bitbucketserver_admin_license": resourceAdminLicense(),
|
||||||
|
|||||||
Reference in New Issue
Block a user