Skip to content

Commit

Permalink
feat/variables (#88)
Browse files Browse the repository at this point in the history
**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 <marek.moscichowski@miquido.com>
Reviewed-on: https://gitea.com/gitea/terraform-provider-gitea/pulls/88
Co-authored-by: Marek Mościchowski <marek@noreply.gitea.com>
Co-committed-by: Marek Mościchowski <marek@noreply.gitea.com>
  • Loading branch information
2 people authored and techknowlogick committed Jan 10, 2025
1 parent 6a7d2f6 commit ad2afa8
Show file tree
Hide file tree
Showing 5 changed files with 442 additions and 355 deletions.
2 changes: 2 additions & 0 deletions gitea/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ func Provider() *schema.Provider {
"gitea_repository_key": resourceGiteaRepositoryKey(),
"gitea_repository_webhook": resourceGiteaRepositoryWebhook(),
"gitea_repository_branch_protection": resourceGiteaRepositoryBranchProtection(),
"gitea_repository_actions_variable": resourceGiteaRepositoryActionsVariable(),
"gitea_repository_actions_secret": resourceGiteaRepositoryActionsSecret(),
},

ConfigureFunc: providerConfigure,
Expand Down
205 changes: 205 additions & 0 deletions gitea/resource_gitea_repository_actions_secret.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
package gitea

import (
"fmt"
"strings"

"code.gitea.io/sdk/gitea"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func resourceGiteaRepositoryActionsSecret() *schema.Resource {
return &schema.Resource{
Create: resourceGiteaRepositoryActionsSecretCreate,
Read: resourceGiteaRepositoryActionsSecretRead,
Update: resourceGiteaRepositoryActionsSecretUpdate,
Delete: resourceGiteaRepositoryActionsSecretDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},

Schema: map[string]*schema.Schema{
"repository_owner": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Owner of the repository.",
},
"repository": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Name of the repository.",
},
"secret_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Name of the secret.",
},
"secret_value": {
Type: schema.TypeString,
Required: true,
Description: "Value of the secret.",
Sensitive: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: "Date of 'actions_secret' creation.",
},
},
}
}

func resourceGiteaRepositoryActionsSecretCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*gitea.Client)

repoOwnerData, usernameOk := d.GetOk("repository_owner")
if !usernameOk {
return fmt.Errorf("name of repo owner must be passed")
}
repoOwner := strings.ToLower(repoOwnerData.(string))

repositoryData, nameOk := d.GetOk("repository")
if !nameOk {
return fmt.Errorf("CREATE name of repo must be passed")
}
repository := strings.ToLower(repositoryData.(string))

secretNameData, nameOk := d.GetOk("secret_name")
if !nameOk {
return fmt.Errorf("secret_name must be passed")
}
secretName := secretNameData.(string)

valueData, nameOk := d.GetOk("secret_value")
if !nameOk {
return fmt.Errorf("value must be passed")
}
value := valueData.(string)

_, err := client.CreateRepoActionSecret(repoOwner, repository, gitea.CreateSecretOption{
Name: secretName,
Data: value,
})
if err != nil {
return err
}

d.SetId(buildThreePartID(repoOwner, repository, secretName))

return resourceGiteaRepositoryActionsSecretRead(d, meta)
}

func resourceGiteaRepositoryActionsSecretUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*gitea.Client)

repoOwnerData, usernameOk := d.GetOk("repository_owner")
if !usernameOk {
return fmt.Errorf("name of repo owner must be passed")
}
repoOwner := strings.ToLower(repoOwnerData.(string))

repositoryData, nameOk := d.GetOk("repository")
if !nameOk {
return fmt.Errorf("name of repo must be passed")
}
repository := strings.ToLower(repositoryData.(string))

variableNameData, nameOk := d.GetOk("secret_name")
if !nameOk {
return fmt.Errorf("secret_name of repo must be passed")
}
variableName := variableNameData.(string)

valueData, nameOk := d.GetOk("secret_value")
if !nameOk {
return fmt.Errorf("secret_value must be passed")
}
value := valueData.(string)

_, err := client.CreateRepoActionSecret(repoOwner, repository, gitea.CreateSecretOption{
Name: variableName,
Data: value,
})
if err != nil {
return err
}

return resourceGiteaRepositoryActionsSecretRead(d, meta)
}

func resourceGiteaRepositoryActionsSecretRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*gitea.Client)

repoOwner, repository, secretName, _ := parseThreePartID(d.Id(), "repository_owner", "repository", "secret_name")

var requestedSecret *gitea.Secret

page := 0
for requestedSecret == nil {
page = page + 1

secrets, _, _ := client.ListRepoActionSecret(repoOwner, repository, gitea.ListRepoActionSecretOption{
ListOptions: gitea.ListOptions{
Page: page,
PageSize: 100,
},
})

if len(secrets) == 0 {
d.SetId("")
return nil
}

for _, secret := range secrets {
if secret.Name == secretName {
requestedSecret = secret
break
}
}
}

createdAtData, dateOk := d.GetOk("created_at")

if requestedSecret == nil {
d.SetId("")
return nil
}

if dateOk {
if requestedSecret.Created.String() != createdAtData.(string) {
d.SetId("")
return nil
}
}

if err := d.Set("repository_owner", repoOwner); err != nil {
return err
}

if err := d.Set("repository", repository); err != nil {
return err
}

if err := d.Set("secret_name", secretName); err != nil {
return err
}

if err := d.Set("created_at", requestedSecret.Created.String()); err != nil {
return err
}

return nil
}

func resourceGiteaRepositoryActionsSecretDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*gitea.Client)

repoOwner, repository, secretName, _ := parseThreePartID(d.Id(), "repository_owner", "repository", "secret_name")

_, err := client.DeleteRepoActionSecret(repoOwner, repository, secretName)

return err
}
Loading

0 comments on commit ad2afa8

Please sign in to comment.