-
Notifications
You must be signed in to change notification settings - Fork 319
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into project-template-support
- Loading branch information
Showing
82 changed files
with
3,481 additions
and
465 deletions.
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
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
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 |
---|---|---|
|
@@ -7,30 +7,30 @@ Terraform Provider for Gitlab | |
- [![Gitter chat](https://badges.gitter.im/hashicorp-terraform/Lobby.png)](https://gitter.im/hashicorp-terraform/Lobby) | ||
- Mailing list: [Google Groups](http://groups.google.com/group/terraform-tool) | ||
- Build status: | ||
- ![Unit Tests](https://github.com/terraform-providers/terraform-provider-gitlab/workflows/Unit%20Tests/badge.svg?branch=master) | ||
- ![Acceptance Tests](https://github.com/terraform-providers/terraform-provider-gitlab/workflows/Acceptance%20Tests/badge.svg?branch=master) | ||
- ![Website Build](https://github.com/terraform-providers/terraform-provider-gitlab/workflows/Website%20Build/badge.svg?branch=master) | ||
- ![Unit Tests](https://github.com/gitlabhq/terraform-provider-gitlab/workflows/Unit%20Tests/badge.svg?branch=master) | ||
- ![Acceptance Tests](https://github.com/gitlabhq/terraform-provider-gitlab/workflows/Acceptance%20Tests/badge.svg?branch=master) | ||
- ![Website Build](https://github.com/gitlabhq/terraform-provider-gitlab/workflows/Website%20Build/badge.svg?branch=master) | ||
|
||
Requirements | ||
------------ | ||
|
||
- [Terraform](https://www.terraform.io/downloads.html) 0.12.x | ||
- [Go](https://golang.org/doc/install) >= 1.13 (to build the provider plugin) | ||
- [Go](https://golang.org/doc/install) >= 1.14 (to build the provider plugin) | ||
|
||
Building The Provider | ||
--------------------- | ||
|
||
Clone repository to: `$GOPATH/src/github.com/terraform-providers/terraform-provider-gitlab` | ||
Clone repository to: `$GOPATH/src/github.com/gitlabhq/terraform-provider-gitlab` | ||
|
||
```sh | ||
$ mkdir -p $GOPATH/src/github.com/terraform-providers; cd $GOPATH/src/github.com/terraform-providers | ||
$ git clone [email protected]:terraform-providers/terraform-provider-gitlab | ||
$ mkdir -p $GOPATH/src/github.com/gitlabhq; cd $GOPATH/src/github.com/gitlabhq | ||
$ git clone [email protected]:gitlabhq/terraform-provider-gitlab | ||
``` | ||
|
||
Enter the provider directory and build the provider | ||
|
||
```sh | ||
$ cd $GOPATH/src/github.com/terraform-providers/terraform-provider-gitlab | ||
$ cd $GOPATH/src/github.com/gitlabhq/terraform-provider-gitlab | ||
$ make build | ||
``` | ||
|
||
|
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,172 @@ | ||
package gitlab | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"github.com/xanzy/go-gitlab" | ||
) | ||
|
||
func dataSourceGitlabGroupMembership() *schema.Resource { | ||
acceptedAccessLevels := make([]string, 0, len(accessLevelID)) | ||
for k := range accessLevelID { | ||
acceptedAccessLevels = append(acceptedAccessLevels, k) | ||
} | ||
return &schema.Resource{ | ||
Read: dataSourceGitlabGroupMembershipRead, | ||
Schema: map[string]*schema.Schema{ | ||
"group_id": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
Optional: true, | ||
ConflictsWith: []string{ | ||
"full_path", | ||
}, | ||
}, | ||
"full_path": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Optional: true, | ||
ConflictsWith: []string{ | ||
"group_id", | ||
}, | ||
}, | ||
"access_level": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Optional: true, | ||
ValidateFunc: validateValueFunc(acceptedAccessLevels), | ||
}, | ||
"members": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"id": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
"username": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"state": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"avatar_url": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"web_url": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"access_level": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"expires_at": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceGitlabGroupMembershipRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*gitlab.Client) | ||
|
||
var gm []*gitlab.GroupMember | ||
var group *gitlab.Group | ||
var err error | ||
|
||
log.Printf("[INFO] Reading Gitlab group") | ||
|
||
groupIDData, groupIDOk := d.GetOk("group_id") | ||
fullPathData, fullPathOk := d.GetOk("full_path") | ||
|
||
if groupIDOk { | ||
// Get group by id | ||
group, _, err = client.Groups.GetGroup(groupIDData.(int)) | ||
if err != nil { | ||
return err | ||
} | ||
} else if fullPathOk { | ||
// Get group by full path | ||
group, _, err = client.Groups.GetGroup(fullPathData.(string)) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
return fmt.Errorf("one and only one of group_id or full_path must be set") | ||
} | ||
|
||
log.Printf("[INFO] Reading Gitlab group memberships") | ||
|
||
// Get group memberships | ||
gm, _, err = client.Groups.ListGroupMembers(group.ID, &gitlab.ListGroupMembersOptions{}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d.Set("group_id", group.ID) | ||
d.Set("full_path", group.FullPath) | ||
|
||
d.Set("members", flattenGitlabMembers(d, gm)) | ||
|
||
var optionsHash strings.Builder | ||
optionsHash.WriteString(strconv.Itoa(group.ID)) | ||
|
||
if data, ok := d.GetOk("access_level"); ok { | ||
optionsHash.WriteString(data.(string)) | ||
} | ||
|
||
id := schema.HashString(optionsHash.String()) | ||
d.SetId(fmt.Sprintf("%d", id)) | ||
|
||
return nil | ||
} | ||
|
||
func flattenGitlabMembers(d *schema.ResourceData, members []*gitlab.GroupMember) []interface{} { | ||
membersList := []interface{}{} | ||
|
||
var filterAccessLevel gitlab.AccessLevelValue = gitlab.NoPermissions | ||
if data, ok := d.GetOk("access_level"); ok { | ||
filterAccessLevel = accessLevelID[data.(string)] | ||
} | ||
|
||
for _, member := range members { | ||
if filterAccessLevel != gitlab.NoPermissions && filterAccessLevel != member.AccessLevel { | ||
continue | ||
} | ||
|
||
values := map[string]interface{}{ | ||
"id": member.ID, | ||
"username": member.Username, | ||
"name": member.Name, | ||
"state": member.State, | ||
"avatar_url": member.AvatarURL, | ||
"web_url": member.WebURL, | ||
"access_level": accessLevel[gitlab.AccessLevelValue(member.AccessLevel)], | ||
} | ||
|
||
if member.ExpiresAt != nil { | ||
values["expires_at"] = member.ExpiresAt.String() | ||
} | ||
|
||
membersList = append(membersList, values) | ||
} | ||
|
||
return membersList | ||
} |
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,91 @@ | ||
package gitlab | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
) | ||
|
||
func TestAccDataSourceGitlabMembership_basic(t *testing.T) { | ||
rInt := acctest.RandInt() | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
// Create the group and one member | ||
{ | ||
Config: testAccDataSourceGitlabGroupMembershipConfig(rInt), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr("gitlab_group.foo", "name", fmt.Sprintf("foo%d", rInt)), | ||
resource.TestCheckResourceAttr("gitlab_user.test", "name", fmt.Sprintf("foo%d", rInt)), | ||
resource.TestCheckResourceAttr("gitlab_group_membership.foo", "access_level", "developer"), | ||
), | ||
}, | ||
{ | ||
Config: testAccDataSourceGitlabGroupMembershipConfig_basic(rInt), | ||
Check: resource.ComposeTestCheckFunc( | ||
// Members is 2 because the user owning the token is always added to the group | ||
resource.TestCheckResourceAttr("data.gitlab_group_membership.foo", "members.#", "2"), | ||
resource.TestCheckResourceAttr("data.gitlab_group_membership.foo", "members.1.username", fmt.Sprintf("listest%d", rInt)), | ||
), | ||
}, | ||
|
||
// Get group using its ID, but return maintainers only | ||
{ | ||
Config: testAccDataSourceGitlabGroupMembershipConfigFilterAccessLevel(rInt), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr("data.gitlab_group_membership.foomaintainers", "members.#", "0"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataSourceGitlabGroupMembershipConfig(rInt int) string { | ||
return fmt.Sprintf(` | ||
resource "gitlab_group" "foo" { | ||
name = "foo%d" | ||
path = "foo%d" | ||
} | ||
resource "gitlab_user" "test" { | ||
name = "foo%d" | ||
username = "listest%d" | ||
password = "test%dtt" | ||
email = "listest%[email protected]" | ||
} | ||
resource "gitlab_group_membership" "foo" { | ||
group_id = "${gitlab_group.foo.id}" | ||
user_id = "${gitlab_user.test.id}" | ||
access_level = "developer" | ||
}`, rInt, rInt, rInt, rInt, rInt, rInt) | ||
} | ||
|
||
func testAccDataSourceGitlabGroupMembershipConfig_basic(rInt int) string { | ||
return fmt.Sprintf(` | ||
resource "gitlab_group" "foo" { | ||
name = "foo%d" | ||
path = "foo%d" | ||
} | ||
data "gitlab_group_membership" "foo" { | ||
group_id = "${gitlab_group.foo.id}" | ||
}`, rInt, rInt) | ||
} | ||
|
||
func testAccDataSourceGitlabGroupMembershipConfigFilterAccessLevel(rInt int) string { | ||
return fmt.Sprintf(` | ||
resource "gitlab_group" "foo" { | ||
name = "foo%d" | ||
path = "foo%d" | ||
} | ||
data "gitlab_group_membership" "foomaintainers" { | ||
group_id = "${gitlab_group.foo.id}" | ||
access_level = "maintainer" | ||
}`, rInt, rInt) | ||
} |
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
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
Oops, something went wrong.