-
Notifications
You must be signed in to change notification settings - Fork 24
/
resource_exoscale_ssh_key.go
161 lines (127 loc) · 4.29 KB
/
resource_exoscale_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
package exoscale
import (
"context"
"errors"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
egoscale "github.com/exoscale/egoscale/v2"
exoapi "github.com/exoscale/egoscale/v2/api"
"github.com/exoscale/terraform-provider-exoscale/pkg/config"
"github.com/exoscale/terraform-provider-exoscale/pkg/general"
)
const (
resSSHKeyAttrFingerprint = "fingerprint"
resSSHKeyAttrName = "name"
resSSHKeyAttrPublicKey = "public_key"
)
func resourceSSHKeyIDString(d general.ResourceIDStringer) string {
return general.ResourceIDString(d, "exoscale_ssh_key")
}
func resourceSSHKey() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
resSSHKeyAttrFingerprint: {
Type: schema.TypeString,
Computed: true,
Description: "The SSH key unique identifier.",
},
resSSHKeyAttrName: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The SSH key name.",
},
resSSHKeyAttrPublicKey: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The SSH *public* key that will be authorized in compute instances.",
},
},
CreateContext: resourceSSHKeyCreate,
ReadContext: resourceSSHKeyRead,
DeleteContext: resourceSSHKeyDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(config.DefaultTimeout),
Read: schema.DefaultTimeout(config.DefaultTimeout),
Delete: schema.DefaultTimeout(config.DefaultTimeout),
},
}
}
func resourceSSHKeyCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
tflog.Debug(ctx, "beginning create", map[string]interface{}{
"id": resourceSSHKeyIDString(d),
})
zone := defaultZone
ctx, cancel := context.WithTimeout(ctx, d.Timeout(schema.TimeoutCreate))
ctx = exoapi.WithEndpoint(ctx, exoapi.NewReqEndpoint(getEnvironment(meta), zone))
defer cancel()
client := getClient(meta)
sshKey, err := client.RegisterSSHKey(
ctx,
zone,
d.Get(resSSHKeyAttrName).(string),
d.Get(resSSHKeyAttrPublicKey).(string),
)
if err != nil {
return diag.FromErr(err)
}
d.SetId(*sshKey.Name)
tflog.Debug(ctx, "create finished successfully", map[string]interface{}{
"id": resourceSSHKeyIDString(d),
})
return resourceSSHKeyRead(ctx, d, meta)
}
func resourceSSHKeyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
tflog.Debug(ctx, "beginning read", map[string]interface{}{
"id": resourceSSHKeyIDString(d),
})
zone := defaultZone
ctx, cancel := context.WithTimeout(ctx, d.Timeout(schema.TimeoutRead))
ctx = exoapi.WithEndpoint(ctx, exoapi.NewReqEndpoint(getEnvironment(meta), zone))
defer cancel()
client := getClient(meta)
sshKey, err := client.GetSSHKey(ctx, zone, d.Id())
if err != nil {
if errors.Is(err, exoapi.ErrNotFound) {
// Resource doesn't exist anymore, signaling the core to remove it from the state.
d.SetId("")
return nil
}
return diag.FromErr(err)
}
tflog.Debug(ctx, "read finished successfully", map[string]interface{}{
"id": resourceSSHKeyIDString(d),
})
return diag.FromErr(resourceSSHKeyApply(ctx, d, sshKey))
}
func resourceSSHKeyDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
tflog.Debug(ctx, "beginning delete", map[string]interface{}{
"id": resourceSSHKeyIDString(d),
})
zone := defaultZone
ctx, cancel := context.WithTimeout(ctx, d.Timeout(schema.TimeoutDelete))
ctx = exoapi.WithEndpoint(ctx, exoapi.NewReqEndpoint(getEnvironment(meta), zone))
defer cancel()
client := getClient(meta)
if err := client.DeleteSSHKey(ctx, zone, &egoscale.SSHKey{Name: nonEmptyStringPtr(d.Id())}); err != nil {
return diag.FromErr(err)
}
tflog.Debug(ctx, "delete finished successfully", map[string]interface{}{
"id": resourceSSHKeyIDString(d),
})
return nil
}
func resourceSSHKeyApply(_ context.Context, d *schema.ResourceData, sshKey *egoscale.SSHKey) error {
if err := d.Set(resSSHKeyAttrName, *sshKey.Name); err != nil {
return err
}
if err := d.Set(resSSHKeyAttrFingerprint, *sshKey.Fingerprint); err != nil {
return err
}
return nil
}