mirror of
https://github.com/ysoftdevs/terraform-provider-bitbucketserver.git
synced 2026-04-10 11:13:40 +02:00
Added bitbucketserver_project_permissions_group
This commit is contained in:
@@ -6,20 +6,25 @@ import (
|
||||
"github.com/hashicorp/terraform/helper/schema"
|
||||
)
|
||||
|
||||
type ProjectPermissionsGroup struct {
|
||||
type PaginatedProjectPermissionsGroupsValue struct {
|
||||
Group struct {
|
||||
Name string `json:"name,omitempty"`
|
||||
} `json:"group,omitempty"`
|
||||
Permission string `json:"permission,omitempty"`
|
||||
}
|
||||
|
||||
type ProjectPermissionsGroup struct {
|
||||
Name string
|
||||
Permission string
|
||||
}
|
||||
|
||||
type PaginatedProjectPermissionsGroups struct {
|
||||
Values []ProjectPermissionsGroup `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"`
|
||||
Values []PaginatedProjectPermissionsGroupsValue `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 dataSourceProjectPermissionsGroups() *schema.Resource {
|
||||
@@ -31,6 +36,10 @@ func dataSourceProjectPermissionsGroups() *schema.Resource {
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
"filter": {
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
},
|
||||
"groups": {
|
||||
Type: schema.TypeList,
|
||||
Computed: true,
|
||||
@@ -52,46 +61,74 @@ func dataSourceProjectPermissionsGroups() *schema.Resource {
|
||||
}
|
||||
|
||||
func dataSourceProjectPermissionsGroupsRead(d *schema.ResourceData, m interface{}) error {
|
||||
groups, err := readProjectPermissionsGroups(m, d.Get("project").(string), d.Get("filter").(string))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
d.SetId(d.Get("project").(string))
|
||||
|
||||
var terraformGroups []interface{}
|
||||
for _, group := range groups {
|
||||
g := make(map[string]interface{})
|
||||
g["name"] = group.Name
|
||||
g["permission"] = group.Permission
|
||||
terraformGroups = append(terraformGroups, g)
|
||||
}
|
||||
|
||||
d.Set("groups", terraformGroups)
|
||||
return nil
|
||||
}
|
||||
|
||||
func readProjectPermissionsGroups(m interface{}, project string, filter string) ([]ProjectPermissionsGroup, error) {
|
||||
client := m.(*BitbucketClient)
|
||||
|
||||
resourceURL := fmt.Sprintf("/rest/api/1.0/projects/%s/permissions/groups",
|
||||
d.Get("project").(string),
|
||||
project,
|
||||
)
|
||||
|
||||
if filter != "" {
|
||||
resourceURL += "?filter=" + filter
|
||||
}
|
||||
|
||||
var projectGroups PaginatedProjectPermissionsGroups
|
||||
var terraformGroups []interface{}
|
||||
var groups []ProjectPermissionsGroup
|
||||
|
||||
for {
|
||||
reviewersResponse, err := client.Get(resourceURL)
|
||||
resp, err := client.Get(resourceURL)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
decoder := json.NewDecoder(reviewersResponse.Body)
|
||||
decoder := json.NewDecoder(resp.Body)
|
||||
err = decoder.Decode(&projectGroups)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, group := range projectGroups.Values {
|
||||
g := make(map[string]interface{})
|
||||
g["name"] = group.Group.Name
|
||||
g["permission"] = group.Permission
|
||||
terraformGroups = append(terraformGroups, g)
|
||||
g := ProjectPermissionsGroup{
|
||||
Name: group.Group.Name,
|
||||
Permission: group.Permission,
|
||||
}
|
||||
groups = append(groups, g)
|
||||
}
|
||||
|
||||
if projectGroups.IsLastPage == false {
|
||||
resourceURL = fmt.Sprintf("/rest/api/1.0/projects/%s/permissions/groups?start=%d",
|
||||
d.Get("project").(string),
|
||||
project,
|
||||
projectGroups.NextPageStart,
|
||||
)
|
||||
|
||||
if filter != "" {
|
||||
resourceURL += "&filter=" + filter
|
||||
}
|
||||
|
||||
projectGroups = PaginatedProjectPermissionsGroups{}
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
d.SetId(d.Get("project").(string))
|
||||
d.Set("groups", terraformGroups)
|
||||
return nil
|
||||
return groups, nil
|
||||
}
|
||||
|
||||
@@ -9,13 +9,47 @@ import (
|
||||
"github.com/hashicorp/terraform/helper/resource"
|
||||
)
|
||||
|
||||
func TestAccBitbucketDataProjectPermissionsGroups_check_empty(t *testing.T) {
|
||||
func TestAccBitbucketDataProjectPermissionsGroups_simple(t *testing.T) {
|
||||
config := fmt.Sprintf(`
|
||||
resource "bitbucketserver_project" "test" {
|
||||
key = "TEST%v"
|
||||
name = "test-repo-for-repository-test"
|
||||
}
|
||||
|
||||
resource "bitbucketserver_project_permissions_group" "test" {
|
||||
project = bitbucketserver_project.test.key
|
||||
group = "stash-users"
|
||||
permission = "PROJECT_WRITE"
|
||||
}
|
||||
|
||||
data "bitbucketserver_project_permissions_groups" "test" {
|
||||
project = bitbucketserver_project_permissions_group.test.project
|
||||
}
|
||||
`, 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_permissions_groups.test", "groups.#", "1"),
|
||||
resource.TestCheckResourceAttr("data.bitbucketserver_project_permissions_groups.test", "groups.0.name", "stash-users"),
|
||||
resource.TestCheckResourceAttr("data.bitbucketserver_project_permissions_groups.test", "groups.0.permission", "PROJECT_WRITE"),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccBitbucketDataProjectPermissionsGroups_check_empty(t *testing.T) {
|
||||
config := fmt.Sprintf(`
|
||||
resource "bitbucketserver_project" "test" {
|
||||
key = "TEST%v"
|
||||
name = "test-repo-for-repository-test"
|
||||
}
|
||||
|
||||
data "bitbucketserver_project_permissions_groups" "test" {
|
||||
project = bitbucketserver_project.test.key
|
||||
}
|
||||
|
||||
@@ -33,10 +33,11 @@ func Provider() terraform.ResourceProvider {
|
||||
"bitbucketserver_project_permissions_groups": dataSourceProjectPermissionsGroups(),
|
||||
},
|
||||
ResourcesMap: map[string]*schema.Resource{
|
||||
"bitbucketserver_admin_license": resourceAdminLicense(),
|
||||
"bitbucketserver_admin_mail_server": resourceAdminMailServer(),
|
||||
"bitbucketserver_project": resourceProject(),
|
||||
"bitbucketserver_repository": resourceRepository(),
|
||||
"bitbucketserver_admin_license": resourceAdminLicense(),
|
||||
"bitbucketserver_admin_mail_server": resourceAdminMailServer(),
|
||||
"bitbucketserver_project": resourceProject(),
|
||||
"bitbucketserver_project_permissions_group": resourceProjectPermissionsGroup(),
|
||||
"bitbucketserver_repository": resourceRepository(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
107
bitbucket/resource_project_permissions_group.go
Normal file
107
bitbucket/resource_project_permissions_group.go
Normal file
@@ -0,0 +1,107 @@
|
||||
package bitbucket
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/hashicorp/terraform/helper/schema"
|
||||
"github.com/hashicorp/terraform/helper/validation"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func resourceProjectPermissionsGroup() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Create: resourceProjectPermissionsGroupCreate,
|
||||
Update: resourceProjectPermissionsGroupUpdate,
|
||||
Read: resourceProjectPermissionsGroupRead,
|
||||
Delete: resourceProjectPermissionsGroupDelete,
|
||||
Importer: &schema.ResourceImporter{
|
||||
State: schema.ImportStatePassthrough,
|
||||
},
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
"project": {
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
ForceNew: true,
|
||||
},
|
||||
"group": {
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
ForceNew: true,
|
||||
},
|
||||
"permission": {
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
ValidateFunc: validation.StringInSlice([]string{"PROJECT_READ", "PROJECT_WRITE", "PROJECT_ADMIN"}, false),
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func resourceProjectPermissionsGroupUpdate(d *schema.ResourceData, m interface{}) error {
|
||||
client := m.(*BitbucketClient)
|
||||
_, err := client.Put(fmt.Sprintf("/rest/api/1.0/projects/%s/permissions/groups?permission=%s&name=%s",
|
||||
d.Get("project").(string),
|
||||
url.QueryEscape(d.Get("permission").(string)),
|
||||
url.QueryEscape(d.Get("group").(string)),
|
||||
), nil)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return resourceProjectPermissionsGroupRead(d, m)
|
||||
}
|
||||
|
||||
func resourceProjectPermissionsGroupCreate(d *schema.ResourceData, m interface{}) error {
|
||||
err := resourceProjectPermissionsGroupUpdate(d, m)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
d.SetId(fmt.Sprintf("%s/%s", d.Get("project").(string), d.Get("group").(string)))
|
||||
return resourceProjectPermissionsGroupRead(d, m)
|
||||
}
|
||||
|
||||
func resourceProjectPermissionsGroupRead(d *schema.ResourceData, m interface{}) error {
|
||||
id := d.Id()
|
||||
if id != "" {
|
||||
parts := strings.Split(id, "/")
|
||||
if len(parts) == 2 {
|
||||
d.Set("project", parts[0])
|
||||
d.Set("group", parts[1])
|
||||
} else {
|
||||
return fmt.Errorf("incorrect ID format, should match `project/group`")
|
||||
}
|
||||
}
|
||||
|
||||
group := d.Get("group").(string)
|
||||
groups, err := readProjectPermissionsGroups(m, d.Get("project").(string), group)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// API only filters but we need to find an exact match
|
||||
for _, g := range groups {
|
||||
if g.Name == group {
|
||||
d.Set("permission", g.Permission)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func resourceProjectPermissionsGroupDelete(d *schema.ResourceData, m interface{}) error {
|
||||
client := m.(*BitbucketClient)
|
||||
_, err := client.Delete(fmt.Sprintf("/rest/api/1.0/projects/%s/permissions/groups?name=%s",
|
||||
d.Get("project").(string),
|
||||
url.QueryEscape(d.Get("group").(string)),
|
||||
))
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return resourceProjectPermissionsGroupRead(d, m)
|
||||
}
|
||||
42
bitbucket/resource_project_permissions_group_test.go
Normal file
42
bitbucket/resource_project_permissions_group_test.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package bitbucket
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/terraform/helper/resource"
|
||||
)
|
||||
|
||||
func TestAccBitbucketResourceProjectPermissionsGroup(t *testing.T) {
|
||||
projectKey := fmt.Sprintf("TEST%v", rand.New(rand.NewSource(time.Now().UnixNano())).Int())
|
||||
config := fmt.Sprintf(`
|
||||
resource "bitbucketserver_project" "test" {
|
||||
key = "%v"
|
||||
name = "test-repo-for-repository-test"
|
||||
}
|
||||
|
||||
resource "bitbucketserver_project_permissions_group" "test" {
|
||||
project = bitbucketserver_project.test.key
|
||||
group = "stash-users"
|
||||
permission = "PROJECT_WRITE"
|
||||
}
|
||||
`, projectKey)
|
||||
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: config,
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
resource.TestCheckResourceAttr("bitbucketserver_project_permissions_group.test", "id", projectKey+"/stash-users"),
|
||||
resource.TestCheckResourceAttr("bitbucketserver_project_permissions_group.test", "project", projectKey),
|
||||
resource.TestCheckResourceAttr("bitbucketserver_project_permissions_group.test", "group", "stash-users"),
|
||||
resource.TestCheckResourceAttr("bitbucketserver_project_permissions_group.test", "permission", "PROJECT_WRITE"),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user