Skip to content

Commit d18aee4

Browse files
authored
Merge pull request #938 from Shocktrooper/main
Third times a charm for protected environments
2 parents 2c1529e + 92b531c commit d18aee4

12 files changed

+1314
-2
lines changed

docs/resources/project_environment.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "gitlab_project_environment Resource - terraform-provider-gitlab"
4+
subcategory: ""
5+
description: |-
6+
The gitlab_project_environment resource allows to manage the lifecycle of an environment in a project.
7+
-> During a terraform destroy this resource by default will not attempt to stop the environment first.
8+
An environment is required to be in a stopped state before a deletetion of the environment can occur.
9+
Set the stop_before_destroy flag to attempt to automatically stop the environment before deletion.
10+
Upstream API: GitLab REST API docs https://docs.gitlab.com/ee/api/environments.html
11+
---
12+
13+
# gitlab_project_environment (Resource)
14+
15+
The `gitlab_project_environment` resource allows to manage the lifecycle of an environment in a project.
16+
17+
-> During a terraform destroy this resource by default will not attempt to stop the environment first.
18+
An environment is required to be in a stopped state before a deletetion of the environment can occur.
19+
Set the `stop_before_destroy` flag to attempt to automatically stop the environment before deletion.
20+
21+
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/environments.html)
22+
23+
## Example Usage
24+
25+
```terraform
26+
resource "gitlab_group" "this" {
27+
name = "example"
28+
path = "example"
29+
description = "An example group"
30+
}
31+
32+
resource "gitlab_project" "this" {
33+
name = "example"
34+
namespace_id = gitlab_group.this.id
35+
initialize_with_readme = true
36+
}
37+
38+
resource "gitlab_project_environment" "this" {
39+
project = gitlab_project.this.id
40+
name = "example"
41+
external_url = "www.example.com"
42+
}
43+
```
44+
45+
<!-- schema generated by tfplugindocs -->
46+
## Schema
47+
48+
### Required
49+
50+
- `name` (String) The name of the environment.
51+
- `project` (String) The ID or full path of the project to environment is created for.
52+
53+
### Optional
54+
55+
- `external_url` (String) Place to link to for this environment.
56+
- `id` (String) The ID of this resource.
57+
- `stop_before_destroy` (Boolean) Determines whether the environment is attempted to be stopped before the environment is deleted.
58+
59+
### Read-Only
60+
61+
- `created_at` (String) The ISO8601 date/time that this environment was created at in UTC.
62+
- `slug` (String) The name of the environment in lowercase, shortened to 63 bytes, and with everything except 0-9 and a-z replaced with -. No leading / trailing -. Use in URLs, host names and domain names.
63+
- `state` (String) State the environment is in. Valid values are `available`, `stopped`.
64+
- `updated_at` (String) The ISO8601 date/time that this environment was last updated at in UTC.
65+
66+
## Import
67+
68+
Import is supported using the following syntax:
69+
70+
```shell
71+
# GitLab project environments can be imported using an id made up of `projectId:environmenId`, e.g.
72+
terraform import gitlab_project_environment.bar 123:321
73+
```
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "gitlab_project_protected_environment Resource - terraform-provider-gitlab"
4+
subcategory: ""
5+
description: |-
6+
The gitlab_project_protected_environment resource allows to manage the lifecycle of a protected environment in a project.
7+
Upstream API: GitLab REST API docs https://docs.gitlab.com/ee/api/protected_environments.html
8+
---
9+
10+
# gitlab_project_protected_environment (Resource)
11+
12+
The `gitlab_project_protected_environment` resource allows to manage the lifecycle of a protected environment in a project.
13+
14+
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/protected_environments.html)
15+
16+
## Example Usage
17+
18+
```terraform
19+
resource "gitlab_group" "this" {
20+
name = "example"
21+
path = "example"
22+
description = "An example group"
23+
}
24+
25+
resource "gitlab_project" "this" {
26+
name = "example"
27+
namespace_id = gitlab_group.this.id
28+
initialize_with_readme = true
29+
}
30+
31+
resource "gitlab_project_environment" "this" {
32+
project = gitlab_project.this.id
33+
name = "example"
34+
external_url = "www.example.com"
35+
}
36+
37+
resource "gitlab_project_protected_environment" "this" {
38+
project = gitlab_project.this.id
39+
environment = gitlab_project_environment.this.name
40+
41+
deploy_access_levels {
42+
access_level = "developer"
43+
}
44+
}
45+
46+
resource "gitlab_project_protected_environment" "this" {
47+
project = gitlab_project.this.id
48+
environment = gitlab_project_environment.this.name
49+
50+
deploy_access_levels {
51+
group_id = gitlab_group.test.id
52+
}
53+
}
54+
55+
resource "gitlab_project_protected_environment" "this" {
56+
project = gitlab_project.this.id
57+
environment = gitlab_project_environment.this.name
58+
59+
deploy_access_levels {
60+
user_id = gitlab_user.test.id
61+
}
62+
63+
}
64+
```
65+
66+
<!-- schema generated by tfplugindocs -->
67+
## Schema
68+
69+
### Required
70+
71+
- `deploy_access_levels` (Block List, Min: 1, Max: 1) Array of access levels allowed to deploy, with each described by a hash. (see [below for nested schema](#nestedblock--deploy_access_levels))
72+
- `environment` (String) The name of the environment.
73+
- `project` (String) The ID or full path of the project which the protected environment is created against.
74+
75+
### Optional
76+
77+
- `id` (String) The ID of this resource.
78+
79+
<a id="nestedblock--deploy_access_levels"></a>
80+
### Nested Schema for `deploy_access_levels`
81+
82+
Optional:
83+
84+
- `access_level` (String) Levels of access required to deploy to this protected environment. Valid values are `developer`, `maintainer`.
85+
- `group_id` (Number) The ID of the group allowed to deploy to this protected environment. The project must be shared with the group.
86+
- `user_id` (Number) The ID of the user allowed to deploy to this protected environment. The user must be a member of the project.
87+
88+
Read-Only:
89+
90+
- `access_level_description` (String) Readable description of level of access.
91+
92+
## Import
93+
94+
Import is supported using the following syntax:
95+
96+
```shell
97+
# GitLab protected environments can be imported using an id made up of `projectId:environmentName`, e.g.
98+
terraform import gitlab_project_protected_environment.bar 123:production
99+
```
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# GitLab project environments can be imported using an id made up of `projectId:environmenId`, e.g.
2+
terraform import gitlab_project_environment.bar 123:321
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
resource "gitlab_group" "this" {
2+
name = "example"
3+
path = "example"
4+
description = "An example group"
5+
}
6+
7+
resource "gitlab_project" "this" {
8+
name = "example"
9+
namespace_id = gitlab_group.this.id
10+
initialize_with_readme = true
11+
}
12+
13+
resource "gitlab_project_environment" "this" {
14+
project = gitlab_project.this.id
15+
name = "example"
16+
external_url = "www.example.com"
17+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# GitLab protected environments can be imported using an id made up of `projectId:environmentName`, e.g.
2+
terraform import gitlab_project_protected_environment.bar 123:production
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
resource "gitlab_group" "this" {
2+
name = "example"
3+
path = "example"
4+
description = "An example group"
5+
}
6+
7+
resource "gitlab_project" "this" {
8+
name = "example"
9+
namespace_id = gitlab_group.this.id
10+
initialize_with_readme = true
11+
}
12+
13+
resource "gitlab_project_environment" "this" {
14+
project = gitlab_project.this.id
15+
name = "example"
16+
external_url = "www.example.com"
17+
}
18+
19+
resource "gitlab_project_protected_environment" "this" {
20+
project = gitlab_project.this.id
21+
environment = gitlab_project_environment.this.name
22+
23+
deploy_access_levels {
24+
access_level = "developer"
25+
}
26+
}
27+
28+
resource "gitlab_project_protected_environment" "this" {
29+
project = gitlab_project.this.id
30+
environment = gitlab_project_environment.this.name
31+
32+
deploy_access_levels {
33+
group_id = gitlab_group.test.id
34+
}
35+
}
36+
37+
resource "gitlab_project_protected_environment" "this" {
38+
project = gitlab_project.this.id
39+
environment = gitlab_project_environment.this.name
40+
41+
deploy_access_levels {
42+
user_id = gitlab_user.test.id
43+
}
44+
45+
}

internal/provider/access_level_helpers.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,22 @@ var validProtectedBranchUnprotectAccessLevelNames = []string{
5555
"developer", "maintainer",
5656
}
5757

58+
var validProtectedEnvironmentDeploymentLevelNames = []string{
59+
"developer", "maintainer",
60+
}
61+
62+
var validProjectEnvironmentStates = []string{
63+
"available", "stopped",
64+
}
65+
5866
var accessLevelNameToValue = map[string]gitlab.AccessLevelValue{
5967
"no one": gitlab.NoPermissions,
6068
"minimal": gitlab.MinimalAccessPermissions,
6169
"guest": gitlab.GuestPermissions,
6270
"reporter": gitlab.ReporterPermissions,
6371
"developer": gitlab.DeveloperPermissions,
6472
"maintainer": gitlab.MaintainerPermissions,
65-
"owner": gitlab.OwnerPermission,
73+
"owner": gitlab.OwnerPermissions,
6674

6775
// Deprecated and should be removed in v4 of this provider
6876
"master": gitlab.MaintainerPermissions,

internal/provider/helper_test.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func testAccCurrentUser(t *testing.T) *gitlab.User {
116116
return user
117117
}
118118

119-
// testAccCreateGroups is a test helper for creating a project.
119+
// testAccCreateProject is a test helper for creating a project.
120120
func testAccCreateProject(t *testing.T) *gitlab.Project {
121121
t.Helper()
122122

@@ -326,6 +326,30 @@ func testAccCreateDeployKey(t *testing.T, projectID int, options *gitlab.AddDepl
326326
return deployKey
327327
}
328328

329+
// testAccCreateProjectEnvironment is a test helper function for creating a project environment
330+
func testAccCreateProjectEnvironment(t *testing.T, projectID int, options *gitlab.CreateEnvironmentOptions) *gitlab.Environment {
331+
t.Helper()
332+
333+
projectEnvironment, _, err := testGitlabClient.Environments.CreateEnvironment(projectID, options)
334+
if err != nil {
335+
t.Fatal(err)
336+
}
337+
338+
t.Cleanup(func() {
339+
if projectEnvironment.State != "stopped" {
340+
_, err = testGitlabClient.Environments.StopEnvironment(projectID, projectEnvironment.ID)
341+
if err != nil {
342+
t.Fatal(err)
343+
}
344+
}
345+
if _, err := testGitlabClient.Environments.DeleteEnvironment(projectID, projectEnvironment.ID); err != nil {
346+
t.Fatal(err)
347+
}
348+
})
349+
350+
return projectEnvironment
351+
}
352+
329353
// testAccGitlabProjectContext encapsulates a GitLab client and test project to be used during an
330354
// acceptance test.
331355
type testAccGitlabProjectContext struct {

0 commit comments

Comments
 (0)