Skip to content

Commit e64d00a

Browse files
mathieu-lemaytechknowlogick
andcommitted
feat: Add gitea_gpg_key resource (#106)
Add a new resource to manage a user's GPG keys. Due to a limitation in Gitea's API, it is only possible to manage GPG keys for the current user. This is not ideal, but still better than nothing. If such an API is created, we could extend this resource with an optional username, while keeping the current behavior as is. Co-authored-by: techknowlogick <[email protected]> Reviewed-on: https://gitea.com/gitea/terraform-provider-gitea/pulls/106 Reviewed-by: techknowlogick <[email protected]> Co-authored-by: Mathieu Lemay <[email protected]> Co-committed-by: Mathieu Lemay <[email protected]>
1 parent 1d31451 commit e64d00a

File tree

3 files changed

+142
-0
lines changed

3 files changed

+142
-0
lines changed

docs/resources/gpg_key.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "gitea_gpg_key Resource - terraform-provider-gitea"
4+
subcategory: ""
5+
description: |-
6+
gitea_gpg_key manages gpg keys that are associated with the current user.
7+
---
8+
9+
# gitea_gpg_key (Resource)
10+
11+
`gitea_gpg_key` manages gpg keys that are associated with the current user.
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- `armored_public_key` (String) An armored GPG public key
21+
22+
### Read-Only
23+
24+
- `id` (String) The ID of this resource.
25+
- `key_id` (String) The ID of the GPG key

gitea/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ func Provider() *schema.Provider {
8080
"gitea_repository": resourceGiteaRepository(),
8181
"gitea_fork": resourceGiteaFork(),
8282
"gitea_public_key": resourceGiteaPublicKey(),
83+
"gitea_gpg_key": resourceGiteaGPGKey(),
8384
"gitea_team": resourceGiteaTeam(),
8485
"gitea_team_membership": resourceGiteaTeamMembership(),
8586
"gitea_team_members": resourceGiteaTeamMembers(),

gitea/resource_gitea_gpg_key.go

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package gitea
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"strconv"
7+
8+
"code.gitea.io/sdk/gitea"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11+
)
12+
13+
const (
14+
GPGKeyId string = "id"
15+
GPGKeyArmored string = "armored_public_key"
16+
GPGKeyGPGId string = "key_id"
17+
)
18+
19+
func resourceGPGKeyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
20+
client := meta.(*gitea.Client)
21+
22+
id, err := strconv.ParseInt(d.Id(), 10, 64)
23+
24+
var resp *gitea.Response
25+
var pubKey *gitea.GPGKey
26+
27+
pubKey, resp, err = client.GetGPGKey(id)
28+
29+
if err != nil {
30+
if resp.StatusCode == 404 {
31+
d.SetId("")
32+
return nil
33+
} else {
34+
return diag.FromErr(err)
35+
}
36+
}
37+
38+
err = setGPGKeyResourceData(pubKey, d)
39+
40+
return diag.FromErr(err)
41+
}
42+
43+
func resourceGPGKeyCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
44+
client := meta.(*gitea.Client)
45+
46+
var pubKey *gitea.GPGKey
47+
var err error
48+
49+
opts := gitea.CreateGPGKeyOption{
50+
ArmoredKey: d.Get(GPGKeyArmored).(string),
51+
}
52+
53+
pubKey, _, err = client.CreateGPGKey(opts)
54+
if err != nil {
55+
return diag.FromErr(fmt.Errorf("error creating gpg key: %w", err))
56+
}
57+
58+
err = setGPGKeyResourceData(pubKey, d)
59+
60+
return diag.FromErr(err)
61+
}
62+
63+
func resourceGPGKeyDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
64+
client := meta.(*gitea.Client)
65+
66+
id, err := strconv.ParseInt(d.Id(), 10, 64)
67+
if err != nil {
68+
return diag.FromErr(fmt.Errorf("error deleting gpg key: invalid id: %w", err))
69+
}
70+
71+
var resp *gitea.Response
72+
73+
resp, err = client.DeleteGPGKey(id)
74+
75+
if err != nil {
76+
if resp.StatusCode == 404 {
77+
return nil
78+
} else {
79+
return diag.FromErr(fmt.Errorf("error deleting gpg key: %w", err))
80+
}
81+
}
82+
83+
return nil
84+
}
85+
86+
func setGPGKeyResourceData(pubKey *gitea.GPGKey, d *schema.ResourceData) error {
87+
d.SetId(fmt.Sprintf("%d", pubKey.ID))
88+
d.Set(GPGKeyGPGId, pubKey.KeyID)
89+
90+
return nil
91+
}
92+
93+
func resourceGiteaGPGKey() *schema.Resource {
94+
return &schema.Resource{
95+
ReadContext: resourceGPGKeyRead,
96+
CreateContext: resourceGPGKeyCreate,
97+
DeleteContext: resourceGPGKeyDelete,
98+
Importer: &schema.ResourceImporter{
99+
StateContext: schema.ImportStatePassthroughContext,
100+
},
101+
Schema: map[string]*schema.Schema{
102+
"armored_public_key": {
103+
Type: schema.TypeString,
104+
Required: true,
105+
ForceNew: true,
106+
Description: "An armored GPG public key",
107+
},
108+
"key_id": {
109+
Type: schema.TypeString,
110+
Computed: true,
111+
Description: "The ID of the GPG key",
112+
},
113+
},
114+
Description: "`gitea_gpg_key` manages gpg keys that are associated with the current user.",
115+
}
116+
}

0 commit comments

Comments
 (0)