Added data.bitbucketserver_project_groups

This commit is contained in:
Gavin Bunney
2019-10-09 14:30:31 -07:00
parent c5ef834f05
commit 69092ae19a
4 changed files with 146 additions and 0 deletions

View 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
}

View 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"),
),
},
},
})
}

View File

@@ -30,6 +30,7 @@ func Provider() terraform.ResourceProvider {
ConfigureFunc: providerConfigure,
DataSourcesMap: map[string]*schema.Resource{
"bitbucketserver_application_properties": dataSourceApplicationProperties(),
"bitbucketserver_project_groups": dataSourceProjectGroups(),
},
ResourcesMap: map[string]*schema.Resource{
"bitbucketserver_admin_license": resourceAdminLicense(),