Skip to content

Commit ad2afa8

Browse files
Marek Mościchowskimmoscichowski-miquido
authored and
techknowlogick
committed
feat/variables (#88)
**Is your feature request related to a problem? Please describe.** I hate to manually set Action secrets and variables. It is error-prone. It is super useful to set them via terraform as those values are probably in your state 😃. Because my changes to gitea go-sdk were approved, it is now possible! https://gitea.com/gitea/go-sdk/commit/a239deff5a96c327c7d6f58c2a61eb95a5c7ac17 **Describe the solution you'd like** ``` resource "gitea_repository_actions_variable" "test" { repository_owner = "marek" repository = "nginx" variable_name = "FROM_TF" value = "Test value" } resource "gitea_repository_actions_secret" "test" { repository_owner = "marek" repository = "nginx" secret_name = "SECRET_FROM_TF" secret_value = "Test secret" } ``` closes #89 Co-authored-by: Marek Mościchowski <[email protected]> Reviewed-on: https://gitea.com/gitea/terraform-provider-gitea/pulls/88 Co-authored-by: Marek Mościchowski <[email protected]> Co-committed-by: Marek Mościchowski <[email protected]>
1 parent 6a7d2f6 commit ad2afa8

5 files changed

+442
-355
lines changed

gitea/provider.go

+2
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ func Provider() *schema.Provider {
8888
"gitea_repository_key": resourceGiteaRepositoryKey(),
8989
"gitea_repository_webhook": resourceGiteaRepositoryWebhook(),
9090
"gitea_repository_branch_protection": resourceGiteaRepositoryBranchProtection(),
91+
"gitea_repository_actions_variable": resourceGiteaRepositoryActionsVariable(),
92+
"gitea_repository_actions_secret": resourceGiteaRepositoryActionsSecret(),
9193
},
9294

9395
ConfigureFunc: providerConfigure,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
package gitea
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"code.gitea.io/sdk/gitea"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
)
10+
11+
func resourceGiteaRepositoryActionsSecret() *schema.Resource {
12+
return &schema.Resource{
13+
Create: resourceGiteaRepositoryActionsSecretCreate,
14+
Read: resourceGiteaRepositoryActionsSecretRead,
15+
Update: resourceGiteaRepositoryActionsSecretUpdate,
16+
Delete: resourceGiteaRepositoryActionsSecretDelete,
17+
Importer: &schema.ResourceImporter{
18+
StateContext: schema.ImportStatePassthroughContext,
19+
},
20+
21+
Schema: map[string]*schema.Schema{
22+
"repository_owner": {
23+
Type: schema.TypeString,
24+
Required: true,
25+
ForceNew: true,
26+
Description: "Owner of the repository.",
27+
},
28+
"repository": {
29+
Type: schema.TypeString,
30+
Required: true,
31+
ForceNew: true,
32+
Description: "Name of the repository.",
33+
},
34+
"secret_name": {
35+
Type: schema.TypeString,
36+
Required: true,
37+
ForceNew: true,
38+
Description: "Name of the secret.",
39+
},
40+
"secret_value": {
41+
Type: schema.TypeString,
42+
Required: true,
43+
Description: "Value of the secret.",
44+
Sensitive: true,
45+
},
46+
"created_at": {
47+
Type: schema.TypeString,
48+
Computed: true,
49+
Description: "Date of 'actions_secret' creation.",
50+
},
51+
},
52+
}
53+
}
54+
55+
func resourceGiteaRepositoryActionsSecretCreate(d *schema.ResourceData, meta interface{}) error {
56+
client := meta.(*gitea.Client)
57+
58+
repoOwnerData, usernameOk := d.GetOk("repository_owner")
59+
if !usernameOk {
60+
return fmt.Errorf("name of repo owner must be passed")
61+
}
62+
repoOwner := strings.ToLower(repoOwnerData.(string))
63+
64+
repositoryData, nameOk := d.GetOk("repository")
65+
if !nameOk {
66+
return fmt.Errorf("CREATE name of repo must be passed")
67+
}
68+
repository := strings.ToLower(repositoryData.(string))
69+
70+
secretNameData, nameOk := d.GetOk("secret_name")
71+
if !nameOk {
72+
return fmt.Errorf("secret_name must be passed")
73+
}
74+
secretName := secretNameData.(string)
75+
76+
valueData, nameOk := d.GetOk("secret_value")
77+
if !nameOk {
78+
return fmt.Errorf("value must be passed")
79+
}
80+
value := valueData.(string)
81+
82+
_, err := client.CreateRepoActionSecret(repoOwner, repository, gitea.CreateSecretOption{
83+
Name: secretName,
84+
Data: value,
85+
})
86+
if err != nil {
87+
return err
88+
}
89+
90+
d.SetId(buildThreePartID(repoOwner, repository, secretName))
91+
92+
return resourceGiteaRepositoryActionsSecretRead(d, meta)
93+
}
94+
95+
func resourceGiteaRepositoryActionsSecretUpdate(d *schema.ResourceData, meta interface{}) error {
96+
client := meta.(*gitea.Client)
97+
98+
repoOwnerData, usernameOk := d.GetOk("repository_owner")
99+
if !usernameOk {
100+
return fmt.Errorf("name of repo owner must be passed")
101+
}
102+
repoOwner := strings.ToLower(repoOwnerData.(string))
103+
104+
repositoryData, nameOk := d.GetOk("repository")
105+
if !nameOk {
106+
return fmt.Errorf("name of repo must be passed")
107+
}
108+
repository := strings.ToLower(repositoryData.(string))
109+
110+
variableNameData, nameOk := d.GetOk("secret_name")
111+
if !nameOk {
112+
return fmt.Errorf("secret_name of repo must be passed")
113+
}
114+
variableName := variableNameData.(string)
115+
116+
valueData, nameOk := d.GetOk("secret_value")
117+
if !nameOk {
118+
return fmt.Errorf("secret_value must be passed")
119+
}
120+
value := valueData.(string)
121+
122+
_, err := client.CreateRepoActionSecret(repoOwner, repository, gitea.CreateSecretOption{
123+
Name: variableName,
124+
Data: value,
125+
})
126+
if err != nil {
127+
return err
128+
}
129+
130+
return resourceGiteaRepositoryActionsSecretRead(d, meta)
131+
}
132+
133+
func resourceGiteaRepositoryActionsSecretRead(d *schema.ResourceData, meta interface{}) error {
134+
client := meta.(*gitea.Client)
135+
136+
repoOwner, repository, secretName, _ := parseThreePartID(d.Id(), "repository_owner", "repository", "secret_name")
137+
138+
var requestedSecret *gitea.Secret
139+
140+
page := 0
141+
for requestedSecret == nil {
142+
page = page + 1
143+
144+
secrets, _, _ := client.ListRepoActionSecret(repoOwner, repository, gitea.ListRepoActionSecretOption{
145+
ListOptions: gitea.ListOptions{
146+
Page: page,
147+
PageSize: 100,
148+
},
149+
})
150+
151+
if len(secrets) == 0 {
152+
d.SetId("")
153+
return nil
154+
}
155+
156+
for _, secret := range secrets {
157+
if secret.Name == secretName {
158+
requestedSecret = secret
159+
break
160+
}
161+
}
162+
}
163+
164+
createdAtData, dateOk := d.GetOk("created_at")
165+
166+
if requestedSecret == nil {
167+
d.SetId("")
168+
return nil
169+
}
170+
171+
if dateOk {
172+
if requestedSecret.Created.String() != createdAtData.(string) {
173+
d.SetId("")
174+
return nil
175+
}
176+
}
177+
178+
if err := d.Set("repository_owner", repoOwner); err != nil {
179+
return err
180+
}
181+
182+
if err := d.Set("repository", repository); err != nil {
183+
return err
184+
}
185+
186+
if err := d.Set("secret_name", secretName); err != nil {
187+
return err
188+
}
189+
190+
if err := d.Set("created_at", requestedSecret.Created.String()); err != nil {
191+
return err
192+
}
193+
194+
return nil
195+
}
196+
197+
func resourceGiteaRepositoryActionsSecretDelete(d *schema.ResourceData, meta interface{}) error {
198+
client := meta.(*gitea.Client)
199+
200+
repoOwner, repository, secretName, _ := parseThreePartID(d.Id(), "repository_owner", "repository", "secret_name")
201+
202+
_, err := client.DeleteRepoActionSecret(repoOwner, repository, secretName)
203+
204+
return err
205+
}

0 commit comments

Comments
 (0)