Skip to content

Commit 85965e2

Browse files
Ryan Gtimofurrer
authored andcommitted
resource/gitlab_project_*_environment: initial draft of resources
1 parent c663d29 commit 85965e2

7 files changed

+926
-1
lines changed

docs/resources/project_environment.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
This resource allows you to create and manage GitLab environments.
7+
---
8+
9+
# gitlab_project_environment (Resource)
10+
11+
This resource allows you to create and manage GitLab environments.
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- **name** (String) The name of the environment
21+
- **project** (String) The ID or full path of the project which the environment is created against.
22+
23+
### Optional
24+
25+
- **external_url** (String) Place to link to for this environment
26+
- **id** (String) The ID of this resource.
27+
28+
### Read-Only
29+
30+
- **state** (String) State the environment is in. Accepted values: available or stopped.
31+
32+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
This resource allows you to create and manage Project Access Token for your GitLab projects.
7+
---
8+
9+
# gitlab_project_protected_environment (Resource)
10+
11+
This resource allows you to create and manage Project Access Token for your GitLab projects.
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- **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))
21+
- **environment** (String) The name of the environment.
22+
- **project** (String) The ID or full path of the project which the protected environment is created against.
23+
24+
### Optional
25+
26+
- **id** (String) The ID of this resource.
27+
28+
<a id="nestedblock--deploy_access_levels"></a>
29+
### Nested Schema for `deploy_access_levels`
30+
31+
Optional:
32+
33+
- **access_level** (String) Level of access required to deploy to this protected environment (developer, maintainer)
34+
- **group_id** (Number) The ID of the group allowed to deploy to this protected environment.
35+
- **user_id** (Number) The ID of the user allowed to deploy to this protected environment.
36+
37+
Read-Only:
38+
39+
- **access_level_description** (String) Readable description of level of access.
40+
41+

internal/provider/access_level_helpers.go

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

58+
var validProtectedEnvironmentDeploymentLevelNames = []string{
59+
"developer", "maintainer",
60+
}
61+
5862
var accessLevelNameToValue = map[string]gitlab.AccessLevelValue{
5963
"no one": gitlab.NoPermissions,
6064
"minimal": gitlab.MinimalAccessPermissions,
6165
"guest": gitlab.GuestPermissions,
6266
"reporter": gitlab.ReporterPermissions,
6367
"developer": gitlab.DeveloperPermissions,
6468
"maintainer": gitlab.MaintainerPermissions,
65-
"owner": gitlab.OwnerPermission,
69+
"owner": gitlab.OwnerPermissions,
6670

6771
// Deprecated and should be removed in v4 of this provider
6872
"master": gitlab.MaintainerPermissions,
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"strconv"
8+
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
12+
gitlab "github.com/xanzy/go-gitlab"
13+
)
14+
15+
// https://docs.gitlab.com/ee/ci/environments/protected_environments.html
16+
var _ = registerResource("gitlab_project_environment", func() *schema.Resource {
17+
return &schema.Resource{
18+
Description: `The ` + "`gitlab_project_environment`" + ` resource you to create and manage an environment in your GitLab project`,
19+
20+
CreateContext: resourceGitlabProjectEnvironmentCreate,
21+
ReadContext: resourceGitlabProjectEnvironmentRead,
22+
UpdateContext: resourceGitlabProjectEnvironmentUpdate,
23+
DeleteContext: resourceGitlabProjectEnvironmentDelete,
24+
Importer: &schema.ResourceImporter{
25+
StateContext: schema.ImportStatePassthroughContext,
26+
},
27+
Schema: map[string]*schema.Schema{
28+
"project": {
29+
Description: "The ID or full path of the project to environment is created for.",
30+
Type: schema.TypeString,
31+
ForceNew: true,
32+
Required: true,
33+
ValidateFunc: validation.StringIsNotEmpty,
34+
},
35+
"name": {
36+
Description: "The name of the environment",
37+
Type: schema.TypeString,
38+
Required: true,
39+
ValidateFunc: validation.StringIsNotEmpty,
40+
},
41+
"external_url": {
42+
Description: "Place to link to for this environment",
43+
Type: schema.TypeString,
44+
Optional: true,
45+
ValidateFunc: validation.StringIsNotEmpty,
46+
},
47+
"state": {
48+
Description: "State the environment is in. Accepted values: available or stopped.",
49+
Type: schema.TypeString,
50+
Computed: true,
51+
},
52+
},
53+
}
54+
})
55+
56+
func resourceGitlabProjectEnvironmentCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
57+
name := d.Get("name").(string)
58+
options := gitlab.CreateEnvironmentOptions{
59+
Name: &name,
60+
}
61+
if externalURL, ok := d.GetOk("external_url"); ok {
62+
options.ExternalURL = gitlab.String(externalURL.(string))
63+
}
64+
65+
project := d.Get("project").(string)
66+
67+
log.Printf("[DEBUG] Project %s create gitlab environment %q", project, *options.Name)
68+
69+
client := meta.(*gitlab.Client)
70+
71+
environment, _, err := client.Environments.CreateEnvironment(project, &options, gitlab.WithContext(ctx))
72+
if err != nil {
73+
if is404(err) {
74+
return diag.Errorf("feature Environments is not available")
75+
}
76+
return diag.FromErr(err)
77+
}
78+
79+
d.SetId(buildTwoPartID(&project, gitlab.String(fmt.Sprintf("%v", environment.ID))))
80+
81+
return resourceGitlabProjectEnvironmentRead(ctx, d, meta)
82+
}
83+
84+
func resourceGitlabProjectEnvironmentRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
85+
log.Printf("[DEBUG] read gitlab environment %s", d.Id())
86+
87+
project, environmentID, err := parseTwoPartID(d.Id())
88+
if err != nil {
89+
return diag.FromErr(err)
90+
}
91+
environmentIDInt, err := strconv.Atoi(environmentID)
92+
93+
if err == nil {
94+
fmt.Printf("%T \n %v", environmentIDInt, environmentIDInt)
95+
}
96+
97+
log.Printf("[DEBUG] Project %s read gitlab environment %d", project, environmentIDInt)
98+
99+
client := meta.(*gitlab.Client)
100+
101+
environment, _, err := client.Environments.GetEnvironment(project, environmentIDInt, gitlab.WithContext(ctx))
102+
if err != nil {
103+
if is404(err) {
104+
log.Printf("[DEBUG] Project %s gitlab environment %q not found", project, environmentID)
105+
d.SetId("")
106+
return nil
107+
}
108+
return diag.Errorf("error getting gitlab project %q environment %q: %v", project, environmentID, err)
109+
}
110+
111+
d.SetId(buildTwoPartID(&project, gitlab.String(fmt.Sprintf("%v", environment.ID))))
112+
d.Set("project", project)
113+
d.Set("name", environment.Name)
114+
d.Set("state", environment.State)
115+
116+
return nil
117+
}
118+
119+
func resourceGitlabProjectEnvironmentUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
120+
log.Printf("[DEBUG] update gitlab environment %s", d.Id())
121+
122+
project, environmentID, err := parseTwoPartID(d.Id())
123+
if err != nil {
124+
return diag.FromErr(err)
125+
}
126+
environmentIDInt, err := strconv.Atoi(environmentID)
127+
if err != nil {
128+
return diag.Errorf("error converting environment ID to int: %v", err)
129+
}
130+
131+
name := d.Get("name").(string)
132+
options := gitlab.EditEnvironmentOptions{
133+
Name: &name,
134+
}
135+
if d.HasChange("external_url") {
136+
options.ExternalURL = gitlab.String(d.Get("external_url").(string))
137+
}
138+
139+
log.Printf("[DEBUG] Project %s update gitlab environment %d", project, environmentIDInt)
140+
141+
client := meta.(*gitlab.Client)
142+
143+
if _, _, err := client.Environments.EditEnvironment(project, environmentIDInt, &options, gitlab.WithContext(ctx)); err != nil {
144+
return diag.Errorf("error editing gitlab project %q environment %q: %v", project, environmentID, err)
145+
}
146+
147+
return resourceGitlabProjectEnvironmentRead(ctx, d, meta)
148+
}
149+
150+
func resourceGitlabProjectEnvironmentDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
151+
project, environmentID, err := parseTwoPartID(d.Id())
152+
if err != nil {
153+
return diag.FromErr(err)
154+
}
155+
environmentIDInt, err := strconv.Atoi(environmentID)
156+
if err != nil {
157+
return diag.Errorf("error converting environment ID to int: %v", err)
158+
}
159+
160+
log.Printf("[DEBUG] Project %s delete gitlab project-level environment %v", project, environmentIDInt)
161+
162+
client := meta.(*gitlab.Client)
163+
164+
_, err = client.Environments.StopEnvironment(project, environmentIDInt, gitlab.WithContext(ctx))
165+
if err != nil {
166+
return diag.Errorf("error stopping gitlab project %q environment %q: %v", project, environmentID, err)
167+
}
168+
_, err = client.Environments.DeleteEnvironment(project, environmentIDInt, gitlab.WithContext(ctx))
169+
if err != nil {
170+
return diag.Errorf("error deleting gitlab project %q environment %q: %v", project, environmentID, err)
171+
}
172+
173+
return nil
174+
}

0 commit comments

Comments
 (0)