This repository has been archived by the owner on May 6, 2020. It is now read-only.
forked from cloudamqp/terraform-provider-cloudamqp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_instance.go
110 lines (102 loc) · 2.55 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
package main
import (
"github.com/84codes/go-api/api"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceInstance() *schema.Resource {
return &schema.Resource{
Create: resourceCreate,
Read: resourceRead,
Update: resourceUpdate,
Delete: resourceDelete,
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,
Default: 1,
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 resourceCreate(d *schema.ResourceData, meta interface{}) error {
api := meta.(*api.API)
keys := []string{"name", "plan", "region", "nodes"}
params := make(map[string]interface{})
for _, k := range keys {
if v := d.Get(k); v != nil {
params[k] = v
}
}
data, err := api.Create(params)
if err != nil {
return err
}
d.SetId(data["id"].(string))
for k, v := range data {
d.Set(k, v)
}
return nil
}
func resourceRead(d *schema.ResourceData, meta interface{}) error {
api := meta.(*api.API)
data, err := api.Read(d.Id())
if err != nil {
return err
}
for k, v := range data {
d.Set(k, v)
}
return nil
}
func resourceUpdate(d *schema.ResourceData, meta interface{}) error {
api := meta.(*api.API)
keys := []string{"name", "plan", "nodes"}
params := make(map[string]interface{})
for _, k := range keys {
params[k] = d.Get(k)
}
return api.Update(d.Id(), params)
}
func resourceDelete(d *schema.ResourceData, meta interface{}) error {
api := meta.(*api.API)
return api.Delete(d.Id())
}