-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathresource_instance.go
149 lines (127 loc) · 3.52 KB
/
resource_instance.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
package main
import (
"fmt"
"log"
"strconv"
"github.com/hashicorp/terraform/helper/schema"
"github.com/waveaccounting/go-cloudamqp/cloudamqp"
)
func resourceInstance() *schema.Resource {
return &schema.Resource{
Create: resourceInstanceCreate,
Read: resourceInstanceRead,
Update: resourceInstanceUpdate,
Delete: resourceInstanceDelete,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "Name of the instance",
},
"plan": {
Type: schema.TypeString,
Required: true,
Description: "Name of the plan, valid options are: lemur, tiger, bunny, rabbit, panda, ape, hippo, lion",
},
"region": {
Type: schema.TypeString,
Required: true,
Description: "Name of the region you want to create your instance in",
},
"vpc_subnet": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "Dedicated VPC subnet, shouldn't overlap with your current VPC's subnet",
},
"nodes": {
Type: schema.TypeInt,
Optional: true,
Description: "Number of nodes in cluster (plan must support it)",
},
"rmq_version": {
Type: schema.TypeString,
Optional: true,
Description: "RabbitMQ version",
},
"url": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
Description: "URL of the CloudAMQP instance",
},
"apikey": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
Description: "API key for the CloudAMQP instance",
},
},
}
}
func resourceInstanceCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudamqp.Client)
fmt.Print(client)
params := &cloudamqp.CreateInstanceParams{
Name: d.Get("name").(string),
Plan: d.Get("plan").(string),
Region: d.Get("region").(string),
VpcSubnet: d.Get("vpc_subnet").(string),
Nodes: d.Get("nodes").(int),
RmqVersion: d.Get("rmq_version").(string),
}
instance, _, err := client.Instances.Create(params)
if err != nil {
return err
}
d.SetId(strconv.Itoa(instance.ID))
return resourceInstanceRead(d, meta)
}
func resourceInstanceRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudamqp.Client)
id, err := strconv.Atoi(d.Id())
if err != nil {
return err
}
instance, _, err := client.Instances.Get(id)
if err != nil {
// If the resource does not exist, inform Terraform. We want to immediately
// return here to prevent further processing.
d.SetId("")
return nil
}
d.SetId(strconv.Itoa(instance.ID))
d.Set("name", instance.Name)
d.Set("region", instance.Region)
d.Set("plan", instance.Plan)
d.Set("url", instance.URL)
d.Set("apikey", instance.APIKey)
return nil
}
func resourceInstanceUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudamqp.Client)
id, err := strconv.Atoi(d.Id())
if err != nil {
return err
}
params := &cloudamqp.UpdateInstanceParams{
Name: d.Get("name").(string),
Plan: d.Get("plan").(string),
Nodes: d.Get("nodes").(int),
}
log.Printf("[DEBUG] Updating CloudAMQP instance %s ", d.Get("name").(string))
_, _, err = client.Instances.Update(id, params)
if err != nil {
return err
}
return resourceInstanceRead(d, meta)
}
func resourceInstanceDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudamqp.Client)
id, err := strconv.Atoi(d.Id())
if err != nil {
return err
}
_, err = client.Instances.Delete(id)
return err
}