Skip to content

Commit a9bf04b

Browse files
ringodssfang97
authored and
sfang97
committed
parent 1cbab81
author Ringo De Smet <[email protected]> 1592760067 +0200 committer sfang97 <[email protected]> 1600137187 -0500 parent 1cbab81 author Ringo De Smet <[email protected]> 1592760067 +0200 committer sfang97 <[email protected]> 1600137111 -0500 parent 1cbab81 author Ringo De Smet <[email protected]> 1592760067 +0200 committer sfang97 <[email protected]> 1600136946 -0500 Fix required Go version Add instance clusters to Terraform provider Add support for instance level cluster management via the Gitlab Terraform provider. Add instance cluster to provider Add instance cluster go file Add instance clusters to website Only test read instance cluster for now Add functionality to CRUD Tests mostly passing now, except a weird invalid URL escape thing, will investigate this Remove instance_clusters.go from this PR Contributed the instance API file to the upstream repo github.com/xanzy/go-gitlab, so removing from this PR Readding instance_clusters for failed pipeline Replace required libary in go mod Replace vendor with upstream in go mod Remove vendor files Remove changes to gitlab go Remove instance clusters from README Add management project param back in Rerun CI build Removing line for CI build Rerun CI pipeline Undo change to xanzy README Undo another change to README Change the commit to replace go-gitlab requirement Add go mod vendor to travis Move go mod vendors command to lint stage Change function names based on vendor change Remove replace package feat: Add support to create projects from templates Undo cherry pick This reverts commit 5191839. Update go mod to latest go-gitlab Remove changes to travis Remove newline for new pipeline Update docs, no instance ID Add backlashes so no italics Remove instance from example usage Remove white space, change instance link Address PR comments Add forcenew back to auth type run make fmt Prepare changelog for release v2.11.0 Cleanup after v2.11.0 release Fix Jira Service error handling (gitlabhq#363) Fix nil pointer dereferences during error handling (gitlabhq#364) * Fix error handling when deleting deploy key There was the possibility of a panic if response was nil, and the GitLab client already treats the 204 code as a success, so the extra check was not needed. * Fix nil pointer dereference issues in error messages correct management_project_id argument in documentation (gitlabhq#368) Fix incorrect description of valid access levels (gitlabhq#342) This minor change corrects a mistake showing `master` as a valid level of `group_access_level`. Import group labels (gitlabhq#339) * add support for importing gitlab group labels * fix resource name in the acceptance test * convert group ID to string Co-authored-by: David Townshend <[email protected]> Add importer to resource_gitlab_project_push_rules (gitlabhq#360) * Add importer to resource_gitlab_project_push_rules * Fix apparent copy-paste problem and inconsistency in push rules debug logs Bump go-gitlab to v.0.34.1 (gitlabhq#378) * Fix variable passed to ListLabelsOptions Needed to fix nested struct format * Don't need go mod changes * Add back go mod * Bump to 0.34.1 * Add 0.34.1 to modules.txt * Revert "Add 0.34.1 to modules.txt" This reverts commit 2ac4e3e. * Add vendor files for bump Co-authored-by: sfang97 <[email protected]> Renamed the Github organization after the migration to https://github.com/gitlabhq Change isRunningInEE test Fix import packages Call configure in provider test Only initialize provider in acceptance tests Remove undefined resource reference Add undefined testenvvar Had to run make fmt Remove unused variable Reference testenvvar resource correctly Add group_ldap_link to index Signed-off-by: Sune Keller <[email protected]> Adding project miorror and tests responding to PR comments, removing import state verify for acceptance test Setting all properties in SetToState removed commented out ignore adding docs page adding to index Store more attributes into the state Fix missing test checks in the resource user Allow to change user email address without creating a new resource Fix the external flag being mapped to admin instead Switching the external flag makes tests fail Add new data source gitlab_group_membership Allow to filter members by their access_level Add new doc page to gitlab.erb Update website/gitlab.erb Co-authored-by: Adam Snyder <[email protected]> Added Quotes in the example for group cluster Prepare changelog for release Cleanup after v2.11.0 release Fix Jira Service error handling (gitlabhq#363) Fix nil pointer dereferences during error handling (gitlabhq#364) * Fix error handling when deleting deploy key There was the possibility of a panic if response was nil, and the GitLab client already treats the 204 code as a success, so the extra check was not needed. * Fix nil pointer dereference issues in error messages correct management_project_id argument in documentation (gitlabhq#368) Fix incorrect description of valid access levels (gitlabhq#342) This minor change corrects a mistake showing `master` as a valid level of `group_access_level`. Import group labels (gitlabhq#339) * add support for importing gitlab group labels * fix resource name in the acceptance test * convert group ID to string Co-authored-by: David Townshend <[email protected]> Add importer to resource_gitlab_project_push_rules (gitlabhq#360) * Add importer to resource_gitlab_project_push_rules * Fix apparent copy-paste problem and inconsistency in push rules debug logs Bump go-gitlab to v.0.34.1 (gitlabhq#378) * Fix variable passed to ListLabelsOptions Needed to fix nested struct format * Don't need go mod changes * Add back go mod * Bump to 0.34.1 * Add 0.34.1 to modules.txt * Revert "Add 0.34.1 to modules.txt" This reverts commit 2ac4e3e. * Add vendor files for bump Co-authored-by: sfang97 <[email protected]> Change isRunningInEE test Fix import packages Call configure in provider test Only initialize provider in acceptance tests Remove undefined resource reference Add undefined testenvvar Had to run make fmt Remove unused variable Reference testenvvar resource correctly Add group_ldap_link to index Signed-off-by: Sune Keller <[email protected]> Adding project miorror and tests responding to PR comments, removing import state verify for acceptance test Setting all properties in SetToState removed commented out ignore adding docs page adding to index Store more attributes into the state Fix missing test checks in the resource user Allow to change user email address without creating a new resource Fix the external flag being mapped to admin instead Switching the external flag makes tests fail Allow to filter members by their access_level Update website/gitlab.erb Co-authored-by: Adam Snyder <[email protected]> Added Quotes in the example for group cluster Remove redundant test func Add managed test Address PR comments Revert "Address PR comments" This reverts commit a963eaf. Remove managed and enabled for now
1 parent 1cbab81 commit a9bf04b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+2528
-253
lines changed

CHANGELOG.md

Lines changed: 89 additions & 83 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,30 @@ Terraform Provider for Gitlab
77
- [![Gitter chat](https://badges.gitter.im/hashicorp-terraform/Lobby.png)](https://gitter.im/hashicorp-terraform/Lobby)
88
- Mailing list: [Google Groups](http://groups.google.com/group/terraform-tool)
99
- Build status:
10-
- ![Unit Tests](https://github.com/terraform-providers/terraform-provider-gitlab/workflows/Unit%20Tests/badge.svg?branch=master)
11-
- ![Acceptance Tests](https://github.com/terraform-providers/terraform-provider-gitlab/workflows/Acceptance%20Tests/badge.svg?branch=master)
12-
- ![Website Build](https://github.com/terraform-providers/terraform-provider-gitlab/workflows/Website%20Build/badge.svg?branch=master)
10+
- ![Unit Tests](https://github.com/gitlabhq/terraform-provider-gitlab/workflows/Unit%20Tests/badge.svg?branch=master)
11+
- ![Acceptance Tests](https://github.com/gitlabhq/terraform-provider-gitlab/workflows/Acceptance%20Tests/badge.svg?branch=master)
12+
- ![Website Build](https://github.com/gitlabhq/terraform-provider-gitlab/workflows/Website%20Build/badge.svg?branch=master)
1313

1414
Requirements
1515
------------
1616

1717
- [Terraform](https://www.terraform.io/downloads.html) 0.12.x
18-
- [Go](https://golang.org/doc/install) >= 1.13 (to build the provider plugin)
18+
- [Go](https://golang.org/doc/install) >= 1.14 (to build the provider plugin)
1919

2020
Building The Provider
2121
---------------------
2222

23-
Clone repository to: `$GOPATH/src/github.com/terraform-providers/terraform-provider-gitlab`
23+
Clone repository to: `$GOPATH/src/github.com/gitlabhq/terraform-provider-gitlab`
2424

2525
```sh
26-
$ mkdir -p $GOPATH/src/github.com/terraform-providers; cd $GOPATH/src/github.com/terraform-providers
27-
$ git clone [email protected]:terraform-providers/terraform-provider-gitlab
26+
$ mkdir -p $GOPATH/src/github.com/gitlabhq; cd $GOPATH/src/github.com/gitlabhq
27+
$ git clone [email protected]:gitlabhq/terraform-provider-gitlab
2828
```
2929

3030
Enter the provider directory and build the provider
3131

3232
```sh
33-
$ cd $GOPATH/src/github.com/terraform-providers/terraform-provider-gitlab
33+
$ cd $GOPATH/src/github.com/gitlabhq/terraform-provider-gitlab
3434
$ make build
3535
```
3636

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
package gitlab
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"strconv"
7+
"strings"
8+
9+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
10+
"github.com/xanzy/go-gitlab"
11+
)
12+
13+
func dataSourceGitlabGroupMembership() *schema.Resource {
14+
acceptedAccessLevels := make([]string, 0, len(accessLevelID))
15+
for k := range accessLevelID {
16+
acceptedAccessLevels = append(acceptedAccessLevels, k)
17+
}
18+
return &schema.Resource{
19+
Read: dataSourceGitlabGroupMembershipRead,
20+
Schema: map[string]*schema.Schema{
21+
"group_id": {
22+
Type: schema.TypeInt,
23+
Computed: true,
24+
Optional: true,
25+
ConflictsWith: []string{
26+
"full_path",
27+
},
28+
},
29+
"full_path": {
30+
Type: schema.TypeString,
31+
Computed: true,
32+
Optional: true,
33+
ConflictsWith: []string{
34+
"group_id",
35+
},
36+
},
37+
"access_level": {
38+
Type: schema.TypeString,
39+
Computed: true,
40+
Optional: true,
41+
ValidateFunc: validateValueFunc(acceptedAccessLevels),
42+
},
43+
"members": {
44+
Type: schema.TypeList,
45+
Computed: true,
46+
Elem: &schema.Resource{
47+
Schema: map[string]*schema.Schema{
48+
"id": {
49+
Type: schema.TypeInt,
50+
Computed: true,
51+
},
52+
"username": {
53+
Type: schema.TypeString,
54+
Computed: true,
55+
},
56+
"name": {
57+
Type: schema.TypeString,
58+
Computed: true,
59+
},
60+
"state": {
61+
Type: schema.TypeString,
62+
Computed: true,
63+
},
64+
"avatar_url": {
65+
Type: schema.TypeString,
66+
Computed: true,
67+
},
68+
"web_url": {
69+
Type: schema.TypeString,
70+
Computed: true,
71+
},
72+
"access_level": {
73+
Type: schema.TypeString,
74+
Computed: true,
75+
},
76+
"expires_at": {
77+
Type: schema.TypeString,
78+
Computed: true,
79+
},
80+
},
81+
},
82+
},
83+
},
84+
}
85+
}
86+
87+
func dataSourceGitlabGroupMembershipRead(d *schema.ResourceData, meta interface{}) error {
88+
client := meta.(*gitlab.Client)
89+
90+
var gm []*gitlab.GroupMember
91+
var group *gitlab.Group
92+
var err error
93+
94+
log.Printf("[INFO] Reading Gitlab group")
95+
96+
groupIDData, groupIDOk := d.GetOk("group_id")
97+
fullPathData, fullPathOk := d.GetOk("full_path")
98+
99+
if groupIDOk {
100+
// Get group by id
101+
group, _, err = client.Groups.GetGroup(groupIDData.(int))
102+
if err != nil {
103+
return err
104+
}
105+
} else if fullPathOk {
106+
// Get group by full path
107+
group, _, err = client.Groups.GetGroup(fullPathData.(string))
108+
if err != nil {
109+
return err
110+
}
111+
} else {
112+
return fmt.Errorf("one and only one of group_id or full_path must be set")
113+
}
114+
115+
log.Printf("[INFO] Reading Gitlab group memberships")
116+
117+
// Get group memberships
118+
gm, _, err = client.Groups.ListGroupMembers(group.ID, &gitlab.ListGroupMembersOptions{})
119+
if err != nil {
120+
return err
121+
}
122+
123+
d.Set("group_id", group.ID)
124+
d.Set("full_path", group.FullPath)
125+
126+
d.Set("members", flattenGitlabMembers(d, gm))
127+
128+
var optionsHash strings.Builder
129+
optionsHash.WriteString(strconv.Itoa(group.ID))
130+
131+
if data, ok := d.GetOk("access_level"); ok {
132+
optionsHash.WriteString(data.(string))
133+
}
134+
135+
id := schema.HashString(optionsHash.String())
136+
d.SetId(fmt.Sprintf("%d", id))
137+
138+
return nil
139+
}
140+
141+
func flattenGitlabMembers(d *schema.ResourceData, members []*gitlab.GroupMember) []interface{} {
142+
membersList := []interface{}{}
143+
144+
var filterAccessLevel gitlab.AccessLevelValue = gitlab.NoPermissions
145+
if data, ok := d.GetOk("access_level"); ok {
146+
filterAccessLevel = accessLevelID[data.(string)]
147+
}
148+
149+
for _, member := range members {
150+
if filterAccessLevel != gitlab.NoPermissions && filterAccessLevel != member.AccessLevel {
151+
continue
152+
}
153+
154+
values := map[string]interface{}{
155+
"id": member.ID,
156+
"username": member.Username,
157+
"name": member.Name,
158+
"state": member.State,
159+
"avatar_url": member.AvatarURL,
160+
"web_url": member.WebURL,
161+
"access_level": accessLevel[gitlab.AccessLevelValue(member.AccessLevel)],
162+
}
163+
164+
if member.ExpiresAt != nil {
165+
values["expires_at"] = member.ExpiresAt.String()
166+
}
167+
168+
membersList = append(membersList, values)
169+
}
170+
171+
return membersList
172+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package gitlab
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
9+
)
10+
11+
func TestAccDataSourceGitlabMembership_basic(t *testing.T) {
12+
rInt := acctest.RandInt()
13+
14+
resource.Test(t, resource.TestCase{
15+
PreCheck: func() { testAccPreCheck(t) },
16+
Providers: testAccProviders,
17+
Steps: []resource.TestStep{
18+
// Create the group and one member
19+
{
20+
Config: testAccDataSourceGitlabGroupMembershipConfig(rInt),
21+
Check: resource.ComposeTestCheckFunc(
22+
resource.TestCheckResourceAttr("gitlab_group.foo", "name", fmt.Sprintf("foo%d", rInt)),
23+
resource.TestCheckResourceAttr("gitlab_user.test", "name", fmt.Sprintf("foo%d", rInt)),
24+
resource.TestCheckResourceAttr("gitlab_group_membership.foo", "access_level", "developer"),
25+
),
26+
},
27+
{
28+
Config: testAccDataSourceGitlabGroupMembershipConfig_basic(rInt),
29+
Check: resource.ComposeTestCheckFunc(
30+
// Members is 2 because the user owning the token is always added to the group
31+
resource.TestCheckResourceAttr("data.gitlab_group_membership.foo", "members.#", "2"),
32+
resource.TestCheckResourceAttr("data.gitlab_group_membership.foo", "members.1.username", fmt.Sprintf("listest%d", rInt)),
33+
),
34+
},
35+
36+
// Get group using its ID, but return maintainers only
37+
{
38+
Config: testAccDataSourceGitlabGroupMembershipConfigFilterAccessLevel(rInt),
39+
Check: resource.ComposeTestCheckFunc(
40+
resource.TestCheckResourceAttr("data.gitlab_group_membership.foomaintainers", "members.#", "0"),
41+
),
42+
},
43+
},
44+
})
45+
}
46+
47+
func testAccDataSourceGitlabGroupMembershipConfig(rInt int) string {
48+
return fmt.Sprintf(`
49+
resource "gitlab_group" "foo" {
50+
name = "foo%d"
51+
path = "foo%d"
52+
}
53+
54+
resource "gitlab_user" "test" {
55+
name = "foo%d"
56+
username = "listest%d"
57+
password = "test%dtt"
58+
email = "listest%[email protected]"
59+
}
60+
61+
resource "gitlab_group_membership" "foo" {
62+
group_id = "${gitlab_group.foo.id}"
63+
user_id = "${gitlab_user.test.id}"
64+
access_level = "developer"
65+
}`, rInt, rInt, rInt, rInt, rInt, rInt)
66+
}
67+
68+
func testAccDataSourceGitlabGroupMembershipConfig_basic(rInt int) string {
69+
return fmt.Sprintf(`
70+
resource "gitlab_group" "foo" {
71+
name = "foo%d"
72+
path = "foo%d"
73+
}
74+
75+
data "gitlab_group_membership" "foo" {
76+
group_id = "${gitlab_group.foo.id}"
77+
}`, rInt, rInt)
78+
}
79+
80+
func testAccDataSourceGitlabGroupMembershipConfigFilterAccessLevel(rInt int) string {
81+
return fmt.Sprintf(`
82+
resource "gitlab_group" "foo" {
83+
name = "foo%d"
84+
path = "foo%d"
85+
}
86+
87+
data "gitlab_group_membership" "foomaintainers" {
88+
group_id = "${gitlab_group.foo.id}"
89+
access_level = "maintainer"
90+
}`, rInt, rInt)
91+
}

gitlab/provider.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,12 @@ func Provider() terraform.ResourceProvider {
5454
},
5555

5656
DataSourcesMap: map[string]*schema.Resource{
57-
"gitlab_group": dataSourceGitlabGroup(),
58-
"gitlab_project": dataSourceGitlabProject(),
59-
"gitlab_projects": dataSourceGitlabProjects(),
60-
"gitlab_user": dataSourceGitlabUser(),
61-
"gitlab_users": dataSourceGitlabUsers(),
57+
"gitlab_group": dataSourceGitlabGroup(),
58+
"gitlab_group_membership": dataSourceGitlabGroupMembership(),
59+
"gitlab_project": dataSourceGitlabProject(),
60+
"gitlab_projects": dataSourceGitlabProjects(),
61+
"gitlab_user": dataSourceGitlabUser(),
62+
"gitlab_users": dataSourceGitlabUsers(),
6263
},
6364

6465
ResourcesMap: map[string]*schema.Resource{
@@ -88,6 +89,8 @@ func Provider() terraform.ResourceProvider {
8889
"gitlab_project_share_group": resourceGitlabProjectShareGroup(),
8990
"gitlab_group_cluster": resourceGitlabGroupCluster(),
9091
"gitlab_group_ldap_link": resourceGitlabGroupLdapLink(),
92+
"gitlab_instance_cluster": resourceGitlabInstanceCluster(),
93+
"gitlab_project_mirror": resourceGitlabProjectMirror(),
9194
},
9295

9396
ConfigureFunc: providerConfigure,

gitlab/provider_test.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
package gitlab
22

33
import (
4-
"os"
5-
"testing"
6-
4+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
75
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
86
"github.com/hashicorp/terraform-plugin-sdk/terraform"
7+
"os"
8+
"testing"
99
)
1010

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

1414
func init() {
15-
testAccProvider = Provider().(*schema.Provider)
16-
testAccProviders = map[string]terraform.ResourceProvider{
17-
"gitlab": testAccProvider,
15+
if os.Getenv(resource.TestEnvVar) != "" {
16+
testAccProvider = Provider().(*schema.Provider)
17+
if err := testAccProvider.Configure(&terraform.ResourceConfig{}); err != nil {
18+
panic(err)
19+
}
20+
testAccProviders = map[string]terraform.ResourceProvider{
21+
"gitlab": testAccProvider,
22+
}
1823
}
1924
}
2025

gitlab/resource_gitlab_deploy_key.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,8 @@ func resourceGitlabDeployKeyDelete(d *schema.ResourceData, meta interface{}) err
9898
}
9999
log.Printf("[DEBUG] Delete gitlab deploy key %s", d.Id())
100100

101-
response, err := client.DeployKeys.DeleteDeployKey(project, deployKeyID)
101+
_, err = client.DeployKeys.DeleteDeployKey(project, deployKeyID)
102102

103-
// HTTP 204 is success with no body
104-
if response.StatusCode == 204 {
105-
return nil
106-
}
107103
return err
108104
}
109105

0 commit comments

Comments
 (0)