-
Notifications
You must be signed in to change notification settings - Fork 16
/
resource_transip_sshkey.go
112 lines (95 loc) · 2.75 KB
/
resource_transip_sshkey.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
package main
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/transip/gotransip/v6/repository"
"github.com/transip/gotransip/v6/sshkey"
"strconv"
)
func resourceSSHKey() *schema.Resource {
return &schema.Resource{
Create: resourceSSHKeyCreate,
Read: resourceSSHKeyRead,
// Update: resourceSSHKeyUpdate,
Delete: resourceSSHKeyDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"description": {
Type: schema.TypeString,
Description: "The name that can be set by customer.",
Required: true,
ForceNew: true,
},
"key": {
Type: schema.TypeString,
Description: "The public part of the SSH key.",
Required: true,
ForceNew: true,
},
"md5_fingerprint": {
Type: schema.TypeString,
Description: "SSH key fingerprint.",
Computed: true,
},
"creation_date": {
Type: schema.TypeString,
Description: "Creation date of the SSH key.",
Computed: true,
},
},
}
}
func resourceSSHKeyCreate(d *schema.ResourceData, m interface{}) error {
key := d.Get("key").(string)
description := d.Get("description").(string)
client := m.(repository.Client)
repository := sshkey.Repository{Client: client}
err := repository.Add(key, description)
if err != nil {
return fmt.Errorf("failed to add SSH key %q: %s", key, err)
}
sshKeys, err := repository.GetAll()
if err != nil {
return fmt.Errorf("failed to get SSH keys for determining recently added key id: %s", err)
}
for _, sshKey := range sshKeys {
if sshKey.Key == key {
d.SetId(strconv.FormatInt(sshKey.ID, 10))
break
}
}
return resourceSSHKeyRead(d, m)
}
func resourceSSHKeyRead(d *schema.ResourceData, m interface{}) error {
id, err := strconv.ParseInt(d.Id(), 10, 64)
if err != nil {
return fmt.Errorf("failed to parse sshkey id %s: %s", d.Id(), err)
}
client := m.(repository.Client)
repository := sshkey.Repository{Client: client}
v, err := repository.GetByID(id)
if err != nil {
return fmt.Errorf("failed to lookup SSH key %q: %s", id, err)
}
// Description returned by TransIP API == user defined id.
d.Set("description", v.Description)
d.Set("key", v.Key)
d.Set("md5_fingerprint", v.MD5Fingerprint)
d.Set("creation_date", v.CreationDate)
return nil
}
func resourceSSHKeyDelete(d *schema.ResourceData, m interface{}) error {
id, err := strconv.ParseInt(d.Id(), 10, 64)
if err != nil {
return fmt.Errorf("failed to parse sshkey id %s: %s", d.Id(), err)
}
client := m.(repository.Client)
repository := sshkey.Repository{Client: client}
err = repository.Remove(id)
if err != nil {
return fmt.Errorf("failed to remove SSH key %q: %s", id, err)
}
return nil
}