Skip to content

Commit

Permalink
Merge branch 'master' into project-template-support
Browse files Browse the repository at this point in the history
  • Loading branch information
sfang97 authored Sep 14, 2020
2 parents 5dfd9a6 + 2d6b08c commit b6d5e38
Show file tree
Hide file tree
Showing 82 changed files with 3,481 additions and 465 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/acceptance-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ jobs:
run: |
openssl version
mkdir license
[[ -n "${{ secrets.ENCRYPT_PASSWORD }}" ]] && echo decrypt
[[ -n "${{ secrets.ENCRYPT_PASSWORD }}" ]] && openssl enc -d -aes-256-cbc -pbkdf2 -iter 20000 -in Gitlab-license.encrypted -out license/Gitlab-license.txt -pass "pass:${{ secrets.ENCRYPT_PASSWORD }}"
[[ -n "${{ secrets.LICENSE_ENCRYPTION_PASSWORD }}" ]] && echo decrypt
[[ -n "${{ secrets.LICENSE_ENCRYPTION_PASSWORD }}" ]] && openssl enc -d -aes-256-cbc -pbkdf2 -iter 20000 -in Gitlab-license.encrypted -out license/Gitlab-license.txt -pass "pass:${{ secrets.LICENSE_ENCRYPTION_PASSWORD }}"
chmod 666 license/Gitlab-license.txt || true
echo "::set-env name=GITLAB_LICENSE_FILE::Gitlab-license.txt"
Expand Down
194 changes: 113 additions & 81 deletions CHANGELOG.md

Large diffs are not rendered by default.

Binary file modified Gitlab-license.encrypted
Binary file not shown.
Binary file removed JulienPivotto.gitlab-license.enc
Binary file not shown.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down
172 changes: 172 additions & 0 deletions gitlab/data_source_gitlab_group_membership.go
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
}
91 changes: 91 additions & 0 deletions gitlab/data_source_gitlab_group_membership_test.go
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)
}
5 changes: 5 additions & 0 deletions gitlab/data_source_gitlab_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ func dataSourceGitlabProject() *schema.Resource {
Optional: true,
Computed: true,
},
"path_with_namespace": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -131,6 +135,7 @@ func dataSourceGitlabProjectRead(d *schema.ResourceData, meta interface{}) error
d.SetId(fmt.Sprintf("%d", found.ID))
d.Set("name", found.Name)
d.Set("path", found.Path)
d.Set("path_with_namespace", found.PathWithNamespace)
d.Set("description", found.Description)
d.Set("default_branch", found.DefaultBranch)
d.Set("request_access_enabled", found.RequestAccessEnabled)
Expand Down
14 changes: 9 additions & 5 deletions gitlab/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,12 @@ func Provider() terraform.ResourceProvider {
},

DataSourcesMap: map[string]*schema.Resource{
"gitlab_group": dataSourceGitlabGroup(),
"gitlab_project": dataSourceGitlabProject(),
"gitlab_projects": dataSourceGitlabProjects(),
"gitlab_user": dataSourceGitlabUser(),
"gitlab_users": dataSourceGitlabUsers(),
"gitlab_group": dataSourceGitlabGroup(),
"gitlab_group_membership": dataSourceGitlabGroupMembership(),
"gitlab_project": dataSourceGitlabProject(),
"gitlab_projects": dataSourceGitlabProjects(),
"gitlab_user": dataSourceGitlabUser(),
"gitlab_users": dataSourceGitlabUsers(),
},

ResourcesMap: map[string]*schema.Resource{
Expand All @@ -85,9 +86,12 @@ func Provider() terraform.ResourceProvider {
"gitlab_service_slack": resourceGitlabServiceSlack(),
"gitlab_service_jira": resourceGitlabServiceJira(),
"gitlab_service_github": resourceGitlabServiceGithub(),
"gitlab_service_pipelines_email": resourceGitlabServicePipelinesEmail(),
"gitlab_project_share_group": resourceGitlabProjectShareGroup(),
"gitlab_group_cluster": resourceGitlabGroupCluster(),
"gitlab_group_ldap_link": resourceGitlabGroupLdapLink(),
"gitlab_project_mirror": resourceGitlabProjectMirror(),
"gitlab_project_level_mr_approvals": resourceGitlabProjectLevelMRApprovals(),
},

ConfigureFunc: providerConfigure,
Expand Down
17 changes: 11 additions & 6 deletions gitlab/provider_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
package gitlab

import (
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"os"
"testing"
)

var testAccProviders map[string]terraform.ResourceProvider
var testAccProvider *schema.Provider

func init() {
testAccProvider = Provider().(*schema.Provider)
testAccProviders = map[string]terraform.ResourceProvider{
"gitlab": testAccProvider,
if os.Getenv(resource.TestEnvVar) != "" {
testAccProvider = Provider().(*schema.Provider)
if err := testAccProvider.Configure(&terraform.ResourceConfig{}); err != nil {
panic(err)
}
testAccProviders = map[string]terraform.ResourceProvider{
"gitlab": testAccProvider,
}
}
}

Expand Down
Loading

0 comments on commit b6d5e38

Please sign in to comment.