Skip to content

Commit dad1f89

Browse files
Shocktroopertimofurrer
authored andcommitted
resource/gitlab_project_*_environments: follow up on initial draft of resources
1 parent 85965e2 commit dad1f89

12 files changed

+569
-224
lines changed

docs/resources/project_environment.md

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,44 @@
33
page_title: "gitlab_project_environment Resource - terraform-provider-gitlab"
44
subcategory: ""
55
description: |-
6-
This resource allows you to create and manage GitLab environments.
6+
The gitlab_project_environment resource you to create and manage an environment in your GitLab project
7+
Upstream API: GitLab API docs https://docs.gitlab.com/ee/ci/environments/index.html
78
---
89

910
# gitlab_project_environment (Resource)
1011

11-
This resource allows you to create and manage GitLab environments.
12-
13-
12+
The `gitlab_project_environment` resource you to create and manage an environment in your GitLab project
13+
**Upstream API**: [GitLab API docs](https://docs.gitlab.com/ee/ci/environments/index.html)
14+
15+
## Example Usage
16+
17+
```terraform
18+
resource "gitlab_group" "this" {
19+
name = "example"
20+
path = "example"
21+
description = "An example group"
22+
}
23+
24+
resource "gitlab_project" "this" {
25+
name = "example"
26+
namespace_id = gitlab_group.this.id
27+
initialize_with_readme = true
28+
}
29+
30+
resource "gitlab_project_environment" "this" {
31+
project = gitlab_project.this.id
32+
name = "example"
33+
external_url = "www.example.com"
34+
}
35+
```
1436

1537
<!-- schema generated by tfplugindocs -->
1638
## Schema
1739

1840
### Required
1941

2042
- **name** (String) The name of the environment
21-
- **project** (String) The ID or full path of the project which the environment is created against.
43+
- **project** (String) The ID or full path of the project to environment is created for.
2244

2345
### Optional
2446

@@ -27,6 +49,13 @@ This resource allows you to create and manage GitLab environments.
2749

2850
### Read-Only
2951

30-
- **state** (String) State the environment is in. Accepted values: available or stopped.
52+
- **state** (String) State the environment is in. Valid values are `available`, `stopped`.
53+
54+
## Import
3155

56+
Import is supported using the following syntax:
3257

58+
```shell
59+
# GitLab project environments can be imported using an id made up of `projectId:environmenId`, e.g.
60+
terraform import gitlab_project_environment.bar 123:321
61+
```

docs/resources/project_protected_environment.md

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,64 @@
33
page_title: "gitlab_project_protected_environment Resource - terraform-provider-gitlab"
44
subcategory: ""
55
description: |-
6-
This resource allows you to create and manage Project Access Token for your GitLab projects.
6+
The gitlab_project_protected_environment resource you to create and manage a protected environment in your GitLab project
7+
Upstream API: GitLab API docs https://docs.gitlab.com/ee/ci/environments/protected_environments.html
78
---
89

910
# gitlab_project_protected_environment (Resource)
1011

11-
This resource allows you to create and manage Project Access Token for your GitLab projects.
12-
13-
12+
The `gitlab_project_protected_environment` resource you to create and manage a protected environment in your GitLab project
13+
**Upstream API**: [GitLab API docs](https://docs.gitlab.com/ee/ci/environments/protected_environments.html)
14+
15+
## Example Usage
16+
17+
```terraform
18+
resource "gitlab_group" "this" {
19+
name = "example"
20+
path = "example"
21+
description = "An example group"
22+
}
23+
24+
resource "gitlab_project" "this" {
25+
name = "example"
26+
namespace_id = gitlab_group.this.id
27+
initialize_with_readme = true
28+
}
29+
30+
resource "gitlab_project_environment" "this" {
31+
project = gitlab_project.this.id
32+
name = "example"
33+
external_url = "www.example.com"
34+
}
35+
36+
resource "gitlab_project_protected_environment" "this" {
37+
project = gitlab_project.this.id
38+
environment = gitlab_project_environment.this.name
39+
40+
deploy_access_levels {
41+
access_level = "developer"
42+
}
43+
}
44+
45+
resource "gitlab_project_protected_environment" "this" {
46+
project = gitlab_project.this.id
47+
environment = gitlab_project_environment.this.name
48+
49+
deploy_access_levels {
50+
group_id = gitlab_group.test.id
51+
}
52+
}
53+
54+
resource "gitlab_project_protected_environment" "this" {
55+
project = gitlab_project.this.id
56+
environment = gitlab_project_environment.this.name
57+
58+
deploy_access_levels {
59+
user_id = gitlab_user.test.id
60+
}
61+
62+
}
63+
```
1464

1565
<!-- schema generated by tfplugindocs -->
1666
## Schema
@@ -30,12 +80,19 @@ This resource allows you to create and manage Project Access Token for your GitL
3080

3181
Optional:
3282

33-
- **access_level** (String) Level of access required to deploy to this protected environment (developer, maintainer)
83+
- **access_level** (String) Levels of access required to deploy to this protected environment. Valid values are `developer`, `maintainer`.
3484
- **group_id** (Number) The ID of the group allowed to deploy to this protected environment.
3585
- **user_id** (Number) The ID of the user allowed to deploy to this protected environment.
3686

3787
Read-Only:
3888

3989
- **access_level_description** (String) Readable description of level of access.
4090

91+
## Import
92+
93+
Import is supported using the following syntax:
4194

95+
```shell
96+
# GitLab protected environments can be imported using an id made up of `projectId:environmentName`, e.g.
97+
terraform import gitlab_project_protected_environment.bar 123:production
98+
```
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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ var validProtectedEnvironmentDeploymentLevelNames = []string{
5959
"developer", "maintainer",
6060
}
6161

62+
var validProjectEnvironmentStates = []string{
63+
"available", "stopped",
64+
}
65+
6266
var accessLevelNameToValue = map[string]gitlab.AccessLevelValue{
6367
"no one": gitlab.NoPermissions,
6468
"minimal": gitlab.MinimalAccessPermissions,

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)