Skip to content

Commit 630a88e

Browse files
authored
Merge pull request #1210 from timofurrer/docs/project-protected-env-limitation
resource/gitlab_project_protected_environment: Add docs that users and groups must be shared with the project
2 parents 9f4a245 + d3b4614 commit 630a88e

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

docs/resources/project_protected_environment.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,24 @@ page_title: "gitlab_project_protected_environment Resource - terraform-provider-
44
subcategory: ""
55
description: |-
66
The gitlab_project_protected_environment resource allows to manage the lifecycle of a protected environment in a project.
7+
~> In order to use a user or group in the deploy_access_levels configuration,
8+
you need to make sure that users have access to the project and groups must have this project shared.
9+
You may use the gitlab_project_membership and gitlab_project_shared_group resources to achieve this.
10+
Unfortunately, the GitLab API does not complain about users and groups without access to the project and just ignores those.
11+
In case this happens you will get perpetual state diffs.
712
Upstream API: GitLab REST API docs https://docs.gitlab.com/ee/api/protected_environments.html
813
---
914

1015
# gitlab_project_protected_environment (Resource)
1116

1217
The `gitlab_project_protected_environment` resource allows to manage the lifecycle of a protected environment in a project.
1318

19+
~> In order to use a user or group in the `deploy_access_levels` configuration,
20+
you need to make sure that users have access to the project and groups must have this project shared.
21+
You may use the `gitlab_project_membership` and `gitlab_project_shared_group` resources to achieve this.
22+
Unfortunately, the GitLab API does not complain about users and groups without access to the project and just ignores those.
23+
In case this happens you will get perpetual state diffs.
24+
1425
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/protected_environments.html)
1526

1627
## Example Usage

internal/provider/resource_gitlab_project_protected_environment.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ var _ = registerResource("gitlab_project_protected_environment", func() *schema.
1515
return &schema.Resource{
1616
Description: `The ` + "`gitlab_project_protected_environment`" + ` resource allows to manage the lifecycle of a protected environment in a project.
1717
18+
~> In order to use a user or group in the ` + "`deploy_access_levels`" + ` configuration,
19+
you need to make sure that users have access to the project and groups must have this project shared.
20+
You may use the ` + "`gitlab_project_membership`" + ` and ` + "`gitlab_project_shared_group`" + ` resources to achieve this.
21+
Unfortunately, the GitLab API does not complain about users and groups without access to the project and just ignores those.
22+
In case this happens you will get perpetual state diffs.
23+
1824
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/protected_environments.html)`,
1925

2026
CreateContext: resourceGitlabProjectProtectedEnvironmentCreate,

internal/provider/resource_gitlab_project_protected_environment_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,59 @@ func TestAccGitlabProjectProtectedEnvironment_basic(t *testing.T) {
9898
})
9999
}
100100

101+
func TestAccGitlabProjectProtectedEnvironment_regressionIssue1132(t *testing.T) {
102+
testAccCheckEE(t)
103+
104+
// Set up project environment.
105+
project := testAccCreateProject(t)
106+
environment := testAccCreateProjectEnvironment(t, project.ID, &gitlab.CreateEnvironmentOptions{
107+
Name: gitlab.String(acctest.RandomWithPrefix("test-protected-environment")),
108+
})
109+
110+
// Set up project user.
111+
user := testAccCreateUsers(t, 1)[0]
112+
testAccAddProjectMembers(t, project.ID, []*gitlab.User{user})
113+
114+
// Set up group access.
115+
group := testAccCreateGroups(t, 1)[0]
116+
if _, err := testGitlabClient.Projects.ShareProjectWithGroup(project.ID, &gitlab.ShareWithGroupOptions{
117+
GroupID: &group.ID,
118+
GroupAccess: gitlab.AccessLevel(gitlab.MaintainerPermissions),
119+
}); err != nil {
120+
t.Fatalf("unable to share project %d with group %d", project.ID, group.ID)
121+
}
122+
123+
additionalGroup := testAccCreateGroups(t, 1)[0]
124+
if _, err := testGitlabClient.Projects.ShareProjectWithGroup(project.ID, &gitlab.ShareWithGroupOptions{
125+
GroupID: &additionalGroup.ID,
126+
GroupAccess: gitlab.AccessLevel(gitlab.MaintainerPermissions),
127+
}); err != nil {
128+
t.Fatalf("unable to share project %d with group %d", project.ID, additionalGroup.ID)
129+
}
130+
131+
resource.ParallelTest(t, resource.TestCase{
132+
ProviderFactories: providerFactories,
133+
CheckDestroy: testAccCheckGitlabProjectProtectedEnvironmentDestroy(project.ID, environment.Name),
134+
Steps: []resource.TestStep{
135+
// Create a basic protected environment.
136+
{
137+
Config: fmt.Sprintf(`
138+
resource "gitlab_project_protected_environment" "this" {
139+
project = %d
140+
environment = %q
141+
deploy_access_levels {
142+
access_level = "developer"
143+
}
144+
145+
deploy_access_levels {
146+
group_id = %d
147+
}
148+
}`, project.ID, environment.Name, additionalGroup.ID),
149+
},
150+
},
151+
})
152+
}
153+
101154
func testAccCheckGitlabProjectProtectedEnvironmentDestroy(projectID int, environmentName string) resource.TestCheckFunc {
102155
return func(s *terraform.State) error {
103156
_, _, err := testGitlabClient.ProtectedEnvironments.GetProtectedEnvironment(projectID, environmentName)

0 commit comments

Comments
 (0)