-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_pgp_key.go
134 lines (121 loc) · 3.12 KB
/
resource_pgp_key.go
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
// SPDX-FileCopyrightText: 2024 Dominik Wombacher <[email protected]>
// SPDX-FileCopyrightText: 2019 The SourceHut API Contributors
//
// SPDX-License-Identifier: BSD-2-Clause
package main
import (
"strconv"
"time"
"git.sr.ht/~wombelix/sourcehut-go/meta"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const (
// Resource Name
pgpKeyName = "sourcehut_user_pgp_key"
)
func resourcePGPKey() *schema.Resource {
return &schema.Resource{
Create: resourcePGPKeyCreate,
Read: resourcePGPKeyRead,
Delete: resourcePGPKeyDelete,
Importer: &schema.ResourceImporter{
State: resourcePGPKeyImport,
},
Schema: map[string]*schema.Schema{
keyKey: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The armored PGP key.",
},
createdKey: {
Type: schema.TypeString,
Computed: true,
Description: "The date on which the key was authorized in RFC3339 format.",
},
createdTimestampKey: {
Type: schema.TypeInt,
Computed: true,
Description: "The date on which the key was authorized as a unix timestamp.",
},
userKey: {
Type: schema.TypeString,
Computed: true,
Description: "The name of the user that owns the key (eg. 'example').",
},
canonicalUserKey: {
Type: schema.TypeString,
Computed: true,
Description: "The canonical name of the user that owns the key (eg. '~example').",
},
fingerprintKey: {
Type: schema.TypeString,
Computed: true,
Description: "The fingerprint of the key.",
},
},
}
}
func resourcePGPKeyImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
err := resourcePGPKeyRead(d, meta)
if err != nil {
return nil, err
}
return []*schema.ResourceData{d}, nil
}
func resourcePGPKeyRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(config)
id, err := strconv.ParseInt(d.Id(), 10, 64)
if err != nil {
return err
}
key, err := config.metaClient.GetPGPKey(id)
if err != nil {
return err
}
return setPGPKey(d, key)
}
func resourcePGPKeyCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(config)
key, err := config.metaClient.NewPGPKey(d.Get(keyKey).(string))
if err != nil {
return err
}
return setPGPKey(d, key)
}
func resourcePGPKeyDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(config)
id, err := strconv.ParseInt(d.Id(), 10, 64)
if err != nil {
return err
}
return config.metaClient.DeletePGPKey(id)
}
func setPGPKey(d *schema.ResourceData, key meta.PGPKey) error {
d.SetId(strconv.FormatInt(key.ID, 10))
err := d.Set(idKey, key.ID)
if err != nil {
return err
}
err = d.Set(createdKey, key.Authorized.Format(time.RFC3339))
if err != nil {
return err
}
err = d.Set(createdTimestampKey, key.Authorized.Unix())
if err != nil {
return err
}
err = d.Set(userKey, key.Owner.Name)
if err != nil {
return err
}
err = d.Set(canonicalUserKey, key.Owner.CanonicalName)
if err != nil {
return err
}
err = d.Set(fingerprintKey, key.KeyID)
if err != nil {
return err
}
return d.Set(keyKey, key.Key)
}