-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_ssh_key.go
168 lines (154 loc) · 4.03 KB
/
resource_ssh_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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// 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
sshKeyName = "sourcehut_user_ssh_key"
// Schema keys
commentKey = "comment"
fingerprintKey = "fingerprint"
keyKey = "key"
lastUsedKey = "last_used"
lastUsedTimestampKey = "last_used_timestamp"
)
func resourceSSHKey() *schema.Resource {
return &schema.Resource{
Create: resourceSSHKeyCreate,
Read: resourceSSHKeyRead,
Delete: resourceSSHKeyDelete,
Importer: &schema.ResourceImporter{
State: resourceSSHKeyImport,
},
Schema: map[string]*schema.Schema{
keyKey: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The key in authorized_keys format.",
},
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').",
},
commentKey: {
Type: schema.TypeString,
Computed: true,
Description: "The comment on the key.",
},
fingerprintKey: {
Type: schema.TypeString,
Computed: true,
Description: "The fingerprint of the key.",
},
lastUsedKey: {
Type: schema.TypeString,
Computed: true,
Description: "The date on which the key was last used in RFC3339 format.",
},
lastUsedTimestampKey: {
Type: schema.TypeString,
Computed: true,
Description: "The date on which the key was last used as a unix timestamp.",
},
},
}
}
func resourceSSHKeyImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
err := resourceSSHKeyRead(d, meta)
if err != nil {
return nil, err
}
return []*schema.ResourceData{d}, nil
}
func resourceSSHKeyRead(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.GetSSHKey(id)
if err != nil {
return err
}
return setKey(d, key)
}
func resourceSSHKeyCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(config)
key, err := config.metaClient.NewSSHKey(d.Get(keyKey).(string))
if err != nil {
return err
}
return setKey(d, key)
}
func resourceSSHKeyDelete(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.DeleteSSHKey(id)
}
func setKey(d *schema.ResourceData, key meta.SSHKey) 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(commentKey, key.Comment)
if err != nil {
return err
}
err = d.Set(fingerprintKey, key.Fingerprint)
if err != nil {
return err
}
err = d.Set(lastUsedKey, key.LastUsed.Format(time.RFC3339))
if err != nil {
return err
}
err = d.Set(lastUsedTimestampKey, key.LastUsed.Unix())
if err != nil {
return err
}
return d.Set(keyKey, key.Key)
}