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
146 lines (128 loc) · 4.13 KB
/
resource_github_actions_environment_variable.go
File metadata and controls
146 lines (128 loc) · 4.13 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
package github
import (
"context"
"errors"
"log"
"net/http"
"net/url"
"github.com/google/go-github/v67/github"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceGithubActionsEnvironmentVariable() *schema.Resource {
return &schema.Resource{
Create: resourceGithubActionsEnvironmentVariableCreateOrUpdate,
Read: resourceGithubActionsEnvironmentVariableRead,
Update: resourceGithubActionsEnvironmentVariableCreateOrUpdate,
Delete: resourceGithubActionsEnvironmentVariableDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
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(d *schema.ResourceData, meta any) error {
client := meta.(*Owner).v3client
owner := meta.(*Owner).name
ctx := context.Background()
repoName := d.Get("repository").(string)
envName := d.Get("environment").(string)
escapedEnvName := url.PathEscape(envName)
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, escapedEnvName, 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, escapedEnvName, variable)
if err != nil {
return err
}
} else {
return err
}
}
d.SetId(buildThreePartID(repoName, envName, name))
return resourceGithubActionsEnvironmentVariableRead(d, meta)
}
func resourceGithubActionsEnvironmentVariableRead(d *schema.ResourceData, meta any) error {
client := meta.(*Owner).v3client
owner := meta.(*Owner).name
ctx := context.Background()
repoName, envName, name, err := parseThreePartID(d.Id(), "repository", "environment", "variable_name")
if err != nil {
return err
}
escapedEnvName := url.PathEscape(envName)
variable, _, err := client.Actions.GetEnvVariable(ctx, owner, repoName, escapedEnvName, 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 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(d *schema.ResourceData, meta any) error {
client := meta.(*Owner).v3client
owner := meta.(*Owner).name
ctx := context.WithValue(context.Background(), ctxId, d.Id())
repoName, envName, name, err := parseThreePartID(d.Id(), "repository", "environment", "variable_name")
if err != nil {
return err
}
escapedEnvName := url.PathEscape(envName)
_, err = client.Actions.DeleteEnvVariable(ctx, owner, repoName, escapedEnvName, name)
return err
}