|
| 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