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_variable.go
More file actions
151 lines (130 loc) · 3.89 KB
/
resource_github_actions_variable.go
File metadata and controls
151 lines (130 loc) · 3.89 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
package github
import (
"context"
"errors"
"log"
"net/http"
"github.com/google/go-github/v67/github"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceGithubActionsVariable() *schema.Resource {
return &schema.Resource{
Create: resourceGithubActionsVariableCreate,
Read: resourceGithubActionsVariableRead,
Update: resourceGithubActionsVariableUpdate,
Delete: resourceGithubActionsVariableDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"repository": {
Type: schema.TypeString,
Required: true,
Description: "Name of the repository.",
},
"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 resourceGithubActionsVariableCreate(d *schema.ResourceData, meta any) error {
client := meta.(*Owner).v3client
owner := meta.(*Owner).name
ctx := context.Background()
repo := d.Get("repository").(string)
variable := &github.ActionsVariable{
Name: d.Get("variable_name").(string),
Value: d.Get("value").(string),
}
_, err := client.Actions.CreateRepoVariable(ctx, owner, repo, variable)
if err != nil {
return err
}
d.SetId(buildTwoPartID(repo, d.Get("variable_name").(string)))
return resourceGithubActionsVariableRead(d, meta)
}
func resourceGithubActionsVariableUpdate(d *schema.ResourceData, meta any) error {
client := meta.(*Owner).v3client
owner := meta.(*Owner).name
ctx := context.Background()
repo := d.Get("repository").(string)
variable := &github.ActionsVariable{
Name: d.Get("variable_name").(string),
Value: d.Get("value").(string),
}
_, err := client.Actions.UpdateRepoVariable(ctx, owner, repo, variable)
if err != nil {
return err
}
d.SetId(buildTwoPartID(repo, d.Get("variable_name").(string)))
return resourceGithubActionsVariableRead(d, meta)
}
func resourceGithubActionsVariableRead(d *schema.ResourceData, meta any) error {
client := meta.(*Owner).v3client
owner := meta.(*Owner).name
ctx := context.Background()
repoName, variableName, err := parseTwoPartID(d.Id(), "repository", "variable_name")
if err != nil {
return err
}
variable, _, err := client.Actions.GetRepoVariable(ctx, owner, repoName, variableName)
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
}
if err = d.Set("repository", repoName); err != nil {
return err
}
if err = d.Set("variable_name", variableName); err != nil {
return err
}
if err = d.Set("value", variable.Value); err != nil {
return err
}
if err = d.Set("created_at", variable.CreatedAt.String()); err != nil {
return err
}
if err = d.Set("updated_at", variable.UpdatedAt.String()); err != nil {
return err
}
return nil
}
func resourceGithubActionsVariableDelete(d *schema.ResourceData, meta any) error {
client := meta.(*Owner).v3client
orgName := meta.(*Owner).name
ctx := context.WithValue(context.Background(), ctxId, d.Id())
repoName, variableName, err := parseTwoPartID(d.Id(), "repository", "variable_name")
if err != nil {
return err
}
_, err = client.Actions.DeleteRepoVariable(ctx, orgName, repoName, variableName)
return err
}