forked from integrations/terraform-provider-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresource_github_actions_environment_variable.go
More file actions
172 lines (148 loc) · 4.8 KB
/
resource_github_actions_environment_variable.go
File metadata and controls
172 lines (148 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package github
import (
"context"
"errors"
"log"
"net/http"
"net/url"
"github.com/google/go-github/v82/github"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceGithubActionsEnvironmentVariable() *schema.Resource {
return &schema.Resource{
CreateContext: resourceGithubActionsEnvironmentVariableCreateOrUpdate,
ReadContext: resourceGithubActionsEnvironmentVariableRead,
UpdateContext: resourceGithubActionsEnvironmentVariableCreateOrUpdate,
DeleteContext: resourceGithubActionsEnvironmentVariableDelete,
Importer: &schema.ResourceImporter{
StateContext: resourceGithubActionsEnvironmentVariableImport,
},
Schema: map[string]*schema.Schema{
"repository": {
Type: schema.TypeString,
Required: true,
Description: "Name of the repository.",
},
"environment": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Name of the environment.",
},
"variable_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Name of the variable.",
ValidateDiagFunc: validateSecretNameFunc,
},
"value": {
Type: schema.TypeString,
Required: true,
Description: "Value of the variable.",
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: "Date of 'actions_variable' creation.",
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
Description: "Date of 'actions_variable' update.",
},
},
}
}
func resourceGithubActionsEnvironmentVariableCreateOrUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
client := meta.(*Owner).v3client
owner := meta.(*Owner).name
repoName := d.Get("repository").(string)
envName := d.Get("environment").(string)
name := d.Get("variable_name").(string)
variable := &github.ActionsVariable{
Name: name,
Value: d.Get("value").(string),
}
// Try to create the variable first
_, err := client.Actions.CreateEnvVariable(ctx, owner, repoName, url.PathEscape(envName), variable)
if err != nil {
var ghErr *github.ErrorResponse
if errors.As(err, &ghErr) && ghErr.Response.StatusCode == http.StatusConflict {
// Variable already exists, try to update instead
// If it fails here, we want to return the error otherwise continue
_, err = client.Actions.UpdateEnvVariable(ctx, owner, repoName, url.PathEscape(envName), variable)
if err != nil {
return diag.FromErr(err)
}
} else {
return diag.FromErr(err)
}
}
if id, err := buildID(repoName, escapeIDPart(envName), name); err != nil {
return diag.FromErr(err)
} else {
d.SetId(id)
}
return resourceGithubActionsEnvironmentVariableRead(ctx, d, meta)
}
func resourceGithubActionsEnvironmentVariableRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
client := meta.(*Owner).v3client
owner := meta.(*Owner).name
repoName, envNamePart, name, err := parseID3(d.Id())
if err != nil {
return diag.FromErr(err)
}
envName := unescapeIDPart(envNamePart)
variable, _, err := client.Actions.GetEnvVariable(ctx, owner, repoName, url.PathEscape(envName), name)
if err != nil {
var ghErr *github.ErrorResponse
if errors.As(err, &ghErr) {
if ghErr.Response.StatusCode == http.StatusNotFound {
log.Printf("[INFO] Removing actions variable %s from state because it no longer exists in GitHub",
d.Id())
d.SetId("")
return nil
}
}
return diag.FromErr(err)
}
_ = d.Set("repository", repoName)
_ = d.Set("environment", envName)
_ = d.Set("variable_name", name)
_ = d.Set("value", variable.Value)
_ = d.Set("created_at", variable.CreatedAt.String())
_ = d.Set("updated_at", variable.UpdatedAt.String())
return nil
}
func resourceGithubActionsEnvironmentVariableDelete(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
client := meta.(*Owner).v3client
owner := meta.(*Owner).name
repoName, envNamePart, name, err := parseID3(d.Id())
if err != nil {
return diag.FromErr(err)
}
envName := unescapeIDPart(envNamePart)
_, err = client.Actions.DeleteEnvVariable(ctx, owner, repoName, url.PathEscape(envName), name)
if err != nil {
return diag.FromErr(err)
}
return nil
}
func resourceGithubActionsEnvironmentVariableImport(ctx context.Context, d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
repoName, envNamePart, name, err := parseID3(d.Id())
if err != nil {
return nil, err
}
if err := d.Set("repository", repoName); err != nil {
return nil, err
}
if err := d.Set("environment", unescapeIDPart(envNamePart)); err != nil {
return nil, err
}
if err := d.Set("variable_name", name); err != nil {
return nil, err
}
return []*schema.ResourceData{d}, nil
}