Skip to content

Commit 92b531c

Browse files
committed
resource/gitlab_project_*_environment: fix a few bugs and testing
1 parent dad1f89 commit 92b531c

6 files changed

+157
-115
lines changed

docs/resources/project_environment.md

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,22 @@
33
page_title: "gitlab_project_environment Resource - terraform-provider-gitlab"
44
subcategory: ""
55
description: |-
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
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
811
---
912

1013
# gitlab_project_environment (Resource)
1114

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)
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)
1422

1523
## Example Usage
1624

@@ -39,17 +47,21 @@ resource "gitlab_project_environment" "this" {
3947

4048
### Required
4149

42-
- **name** (String) The name of the environment
43-
- **project** (String) The ID or full path of the project to environment is created for.
50+
- `name` (String) The name of the environment.
51+
- `project` (String) The ID or full path of the project to environment is created for.
4452

4553
### Optional
4654

47-
- **external_url** (String) Place to link to for this environment
48-
- **id** (String) The ID of this resource.
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.
4958

5059
### Read-Only
5160

52-
- **state** (String) State the environment is in. Valid values are `available`, `stopped`.
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.
5365

5466
## Import
5567

docs/resources/project_protected_environment.md

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
page_title: "gitlab_project_protected_environment Resource - terraform-provider-gitlab"
44
subcategory: ""
55
description: |-
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
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
88
---
99

1010
# gitlab_project_protected_environment (Resource)
1111

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)
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)
1415

1516
## Example Usage
1617

@@ -67,26 +68,26 @@ resource "gitlab_project_protected_environment" "this" {
6768

6869
### Required
6970

70-
- **deploy_access_levels** (Block List, Min: 1) Array of access levels allowed to deploy, with each described by a hash. (see [below for nested schema](#nestedblock--deploy_access_levels))
71-
- **environment** (String) The name of the environment.
72-
- **project** (String) The ID or full path of the project which the protected environment is created against.
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.
7374

7475
### Optional
7576

76-
- **id** (String) The ID of this resource.
77+
- `id` (String) The ID of this resource.
7778

7879
<a id="nestedblock--deploy_access_levels"></a>
7980
### Nested Schema for `deploy_access_levels`
8081

8182
Optional:
8283

83-
- **access_level** (String) Levels of access required to deploy to this protected environment. Valid values are `developer`, `maintainer`.
84-
- **group_id** (Number) The ID of the group allowed to deploy to this protected environment.
85-
- **user_id** (Number) The ID of the user allowed to deploy to this protected environment.
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.
8687

8788
Read-Only:
8889

89-
- **access_level_description** (String) Readable description of level of access.
90+
- `access_level_description` (String) Readable description of level of access.
9091

9192
## Import
9293

internal/provider/resource_gitlab_project_environment.go

Lines changed: 40 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ var _ = registerResource("gitlab_project_environment", func() *schema.Resource {
1717
return &schema.Resource{
1818
Description: `The ` + "`gitlab_project_environment`" + ` resource allows to manage the lifecycle of an environment in a project.
1919
20-
-> During a terraform destroy this resource by default will not attempt to stop the environment first.
21-
An environment is required to be in a stopped state before a deletetion of the environment can occur.
20+
-> During a terraform destroy this resource by default will not attempt to stop the environment first.
21+
An environment is required to be in a stopped state before a deletetion of the environment can occur.
2222
Set the ` + "`stop_before_destroy`" + ` flag to attempt to automatically stop the environment before deletion.
2323
24-
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/environments.html`,
24+
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/environments.html)`,
2525

2626
CreateContext: resourceGitlabProjectEnvironmentCreate,
2727
ReadContext: resourceGitlabProjectEnvironmentRead,
@@ -39,14 +39,14 @@ Set the ` + "`stop_before_destroy`" + ` flag to attempt to automatically stop th
3939
ValidateFunc: validation.StringIsNotEmpty,
4040
},
4141
"name": {
42-
Description: "The name of the environment",
42+
Description: "The name of the environment.",
4343
Type: schema.TypeString,
4444
ForceNew: true,
4545
Required: true,
4646
ValidateFunc: validation.StringIsNotEmpty,
4747
},
4848
"external_url": {
49-
Description: "Place to link to for this environment",
49+
Description: "Place to link to for this environment.",
5050
Type: schema.TypeString,
5151
Optional: true,
5252
ValidateFunc: validation.IsURLWithHTTPorHTTPS,
@@ -56,19 +56,13 @@ Set the ` + "`stop_before_destroy`" + ` flag to attempt to automatically stop th
5656
Type: schema.TypeString,
5757
Computed: true,
5858
},
59-
"stop_before_destroy": {
60-
Description: "Determines whether the environment is attempted to be stopped before the environment is deleted.",
61-
Type: schema.TypeBool,
62-
Optional: true,
63-
Default: false,
64-
},
6559
"created_at": {
66-
Description: "The ISO8601 date/time that this environment was created at in UTC",
60+
Description: "The ISO8601 date/time that this environment was created at in UTC.",
6761
Type: schema.TypeString,
6862
Computed: true,
6963
},
7064
"updated_at": {
71-
Description: "The ISO8601 date/time that this environment was last updated at in UTC",
65+
Description: "The ISO8601 date/time that this environment was last updated at in UTC.",
7266
Type: schema.TypeString,
7367
Computed: true,
7468
},
@@ -77,6 +71,12 @@ Set the ` + "`stop_before_destroy`" + ` flag to attempt to automatically stop th
7771
Type: schema.TypeString,
7872
Computed: true,
7973
},
74+
"stop_before_destroy": {
75+
Description: "Determines whether the environment is attempted to be stopped before the environment is deleted.",
76+
Type: schema.TypeBool,
77+
Optional: true,
78+
Default: false,
79+
},
8080
},
8181
}
8282
})
@@ -106,38 +106,36 @@ func resourceGitlabProjectEnvironmentCreate(ctx context.Context, d *schema.Resou
106106

107107
environmentID := fmt.Sprintf("%d", environment.ID)
108108
d.SetId(buildTwoPartID(&project, &environmentID))
109-
110109
return resourceGitlabProjectEnvironmentRead(ctx, d, meta)
111110
}
112111

113112
func resourceGitlabProjectEnvironmentRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
114113
log.Printf("[DEBUG] read gitlab environment %s", d.Id())
115114

116-
project, environmentIDInt, err := environmentAndProjectFromResource(d)
115+
project, environmentID, err := resourceGitlabProjectEnvironmentParseID(d)
117116
if err != nil {
118117
return diag.FromErr(err)
119118
}
120119

121-
log.Printf("[DEBUG] Project %s read gitlab environment %d", project, environmentIDInt)
120+
log.Printf("[DEBUG] Project %s read gitlab environment %d", project, environmentID)
122121

123122
client := meta.(*gitlab.Client)
124123

125-
environment, _, err := client.Environments.GetEnvironment(project, environmentIDInt, gitlab.WithContext(ctx))
124+
environment, _, err := client.Environments.GetEnvironment(project, environmentID, gitlab.WithContext(ctx))
126125
if err != nil {
127126
if is404(err) {
128-
log.Printf("[DEBUG] Project %s gitlab environment %q not found, removing from state", project, environmentIDInt)
127+
log.Printf("[DEBUG] Project %s gitlab environment %d not found, removing from state", project, environmentID)
129128
d.SetId("")
130129
return nil
131130
}
132-
return diag.Errorf("error getting gitlab project %q environment %q: %v", project, environmentIDInt, err)
131+
return diag.Errorf("error getting gitlab project %s environment %d: %v", project, environmentID, err)
133132
}
134133

135134
d.Set("project", project)
136135
d.Set("name", environment.Name)
137136
d.Set("state", environment.State)
138137
d.Set("external_url", environment.ExternalURL)
139138
d.Set("created_at", environment.CreatedAt.Format(time.RFC3339))
140-
141139
if environment.UpdatedAt != nil {
142140
d.Set("updated_at", environment.UpdatedAt.Format(time.RFC3339))
143141
}
@@ -148,7 +146,7 @@ func resourceGitlabProjectEnvironmentRead(ctx context.Context, d *schema.Resourc
148146
func resourceGitlabProjectEnvironmentUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
149147
log.Printf("[DEBUG] update gitlab environment %s", d.Id())
150148

151-
project, environmentIDInt, err := environmentAndProjectFromResource(d)
149+
project, environmentID, err := resourceGitlabProjectEnvironmentParseID(d)
152150
if err != nil {
153151
return diag.FromErr(err)
154152
}
@@ -161,70 +159,69 @@ func resourceGitlabProjectEnvironmentUpdate(ctx context.Context, d *schema.Resou
161159
options.ExternalURL = gitlab.String(d.Get("external_url").(string))
162160
}
163161

164-
log.Printf("[DEBUG] Project %s update gitlab environment %d", project, environmentIDInt)
162+
log.Printf("[DEBUG] Project %s update gitlab environment %d", project, environmentID)
165163

166164
client := meta.(*gitlab.Client)
167165

168-
if _, _, err := client.Environments.EditEnvironment(project, environmentIDInt, options, gitlab.WithContext(ctx)); err != nil {
169-
return diag.Errorf("error editing gitlab project %q environment %q: %v", project, environmentIDInt, err)
166+
if _, _, err := client.Environments.EditEnvironment(project, environmentID, options, gitlab.WithContext(ctx)); err != nil {
167+
return diag.Errorf("error editing gitlab project %s environment %d: %v", project, environmentID, err)
170168
}
171169

172170
return resourceGitlabProjectEnvironmentRead(ctx, d, meta)
173171
}
174172

175173
func resourceGitlabProjectEnvironmentDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
176174
client := meta.(*gitlab.Client)
177-
project, environmentIDInt, err := environmentAndProjectFromResource(d)
175+
project, environmentID, err := resourceGitlabProjectEnvironmentParseID(d)
178176
if err != nil {
179177
return diag.FromErr(err)
180178
}
181179

182180
stopBeforeDestroy := d.Get("stop_before_destroy").(bool)
183181

184182
if stopBeforeDestroy {
185-
log.Printf("[DEBUG] Stopping environment %v for Project %s before destruction", environmentIDInt, project)
186-
_, err = client.Environments.StopEnvironment(project, environmentIDInt, gitlab.WithContext(ctx))
183+
log.Printf("[DEBUG] Stopping environment %d for Project %s before destruction", environmentID, project)
184+
_, err = client.Environments.StopEnvironment(project, environmentID, gitlab.WithContext(ctx))
187185
if err != nil {
188-
return diag.Errorf("error stopping gitlab project %q environment %q: %v", project, environmentIDInt, err)
186+
return diag.Errorf("error stopping gitlab project %s environment %q: %v", project, environmentID, err)
189187
}
190188
} else {
191-
environment, _, err := client.Environments.GetEnvironment(project, environmentIDInt, gitlab.WithContext(ctx))
189+
environment, _, err := client.Environments.GetEnvironment(project, environmentID, gitlab.WithContext(ctx))
192190
if err != nil {
193191
if is404(err) {
194-
log.Printf("[DEBUG] Project %s gitlab environment %q not found, removing from state", project, environmentIDInt)
192+
log.Printf("[DEBUG] Project %s gitlab environment %d not found, removing from state", project, environmentID)
195193
d.SetId("")
196194
return nil
197195
}
198-
return diag.Errorf("error getting gitlab project %q environment %q: %v", project, environmentIDInt, err)
196+
return diag.Errorf("error getting gitlab project %s environment %d: %v", project, environmentID, err)
199197
}
200198

201199
if environment.State != "stopped" {
202-
return diag.Errorf("[ERROR] cannot destroy gitlab project %q environment %q: Environment must be in a stopped state before deletion. Set stop_before_destroy flag to attempt to auto stop the environment on destruction", project, environmentIDInt)
200+
return diag.Errorf("[ERROR] cannot destroy gitlab project %s environment %d: Environment must be in a stopped state before deletion. Set stop_before_destroy flag to attempt to auto stop the environment on destruction", project, environmentID)
203201
}
204202
}
205203

206-
_, err = client.Environments.DeleteEnvironment(project, environmentIDInt, gitlab.WithContext(ctx))
204+
_, err = client.Environments.DeleteEnvironment(project, environmentID, gitlab.WithContext(ctx))
207205
if err != nil {
208-
return diag.Errorf("error deleting gitlab project %q environment %q: %v", project, environmentIDInt, err)
206+
return diag.Errorf("error deleting gitlab project %s environment %d: %v", project, environmentID, err)
209207
}
210208

211209
return nil
212210
}
213211

214-
func environmentAndProjectFromResource(d *schema.ResourceData) (string, int, error) {
215-
project, environmentID, err := parseTwoPartID(d.Id())
212+
func resourceGitlabProjectEnvironmentParseID(d *schema.ResourceData) (string, int, error) {
213+
project, rawEnvironmentID, err := parseTwoPartID(d.Id())
216214

217215
if err != nil {
218-
log.Printf("[ERROR] cannot get project and environmentID from input: %v", d.Id())
216+
log.Printf("[ERROR] cannot get project and environment ID from input: %v", d.Id())
219217
return "", 0, err
220218
}
221219

222-
environmentIDInt, e := strconv.Atoi(environmentID)
220+
environmentID, err := strconv.Atoi(rawEnvironmentID)
223221

224-
if e != nil {
225-
log.Printf("[ERROR] cannot convert environment ID to int: %v", e)
226-
return "", 0, e
222+
if err != nil {
223+
log.Printf("[ERROR] cannot convert environment ID to int: %v", err)
224+
return "", 0, err
227225
}
228-
229-
return project, environmentIDInt, nil
226+
return project, environmentID, nil
230227
}

0 commit comments

Comments
 (0)