/
resource_pin.go
78 lines (63 loc) · 1.48 KB
/
resource_pin.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
package main
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
type PinInfo struct {
Type string `json:"Type"`
}
type PinListResponse struct {
Keys map[string]PinInfo `json:"Keys"`
}
func resourcePin() *schema.Resource {
return &schema.Resource{
Create: resourcePinCreate,
Read: resourcePinRead,
Delete: resourcePinDelete,
Schema: map[string]*schema.Schema{
"cid": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func resourcePinCreate(d *schema.ResourceData, m interface{}) error {
client := m.(*Client)
cid := d.Get("cid").(string)
err := client.shell.Pin(cid)
if err != nil {
return fmt.Errorf("Error creating pin: %s", err)
}
d.SetId(cid)
return resourcePinRead(d, m)
}
func resourcePinRead(d *schema.ResourceData, m interface{}) error {
client := m.(*Client)
cid := d.Get("cid").(string)
var resp PinListResponse
req := client.shell.Request("pin/ls", cid)
req.Option("quit", true)
err := req.Exec(context.Background(), &resp)
if err != nil {
d.SetId("")
return fmt.Errorf("Error reading pin: %s", err)
}
if len(resp.Keys) == 1 && resp.Keys[cid].Type != "indirect" {
return nil
}
d.SetId("")
return nil
}
func resourcePinDelete(d *schema.ResourceData, m interface{}) error {
client := m.(*Client)
cid := d.Get("cid").(string)
err := client.shell.Unpin(cid)
if err != nil {
return fmt.Errorf("Error deleting pin: %s", err)
}
d.SetId("")
return nil
}