/
resource_cloudamqp_custom_domain.go
135 lines (112 loc) · 3.4 KB
/
resource_cloudamqp_custom_domain.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
package cloudamqp
import (
"fmt"
"log"
"strconv"
"github.com/84codes/go-api/api"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func resourceCustomDomain() *schema.Resource {
return &schema.Resource{
Create: resourceCustomDomainCreate,
Read: resourceCustomDomainRead,
Update: resourceCustomDomainUpdate,
Delete: resourceCustomDomainDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"instance_id": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
Description: "Instance identifier",
},
"hostname": {
Type: schema.TypeString,
Required: true,
Description: "The custom hostname.",
},
},
}
}
func resourceCustomDomainCreate(d *schema.ResourceData, meta interface{}) error {
api := meta.(*api.API)
instanceID := d.Get("instance_id").(int)
log.Printf("[DEBUG] cloudamqp::resource::custom_domain::create instance id: %v", instanceID)
hostname := d.Get("hostname").(string)
data, err := api.CreateCustomDomain(instanceID, hostname)
log.Printf("[DEBUG] cloudamqp::resource::custom_domain::create data: %v", data)
if err != nil {
return err
}
d.SetId(strconv.Itoa(instanceID))
d.Set("instance_id", instanceID)
for k, v := range data {
if validateCustomDomainSchemaAttribute(k) {
if err = d.Set(k, v); err != nil {
return fmt.Errorf("error setting %s for resource %s: %s", k, d.Id(), err)
}
}
}
return nil
}
func resourceCustomDomainRead(d *schema.ResourceData, meta interface{}) error {
api := meta.(*api.API)
instanceID, _ := strconv.Atoi(d.Id())
log.Printf("[DEBUG] cloudamqp::resource::custom_domain::read instance id: %v", instanceID)
data, err := api.ReadCustomDomain(instanceID)
log.Printf("[DEBUG] cloudamqp::resource::custom_domain::read data: %v", data)
if err != nil {
return err
}
d.SetId(strconv.Itoa(instanceID))
d.Set("instance_id", instanceID)
for k, v := range data {
if validateCustomDomainSchemaAttribute(k) {
if err = d.Set(k, v); err != nil {
return fmt.Errorf("error setting %s for resource %s: %s", k, d.Id(), err)
}
}
}
return nil
}
func resourceCustomDomainUpdate(d *schema.ResourceData, meta interface{}) error {
api := meta.(*api.API)
instanceID, _ := strconv.Atoi(d.Id())
log.Printf("[DEBUG] cloudamqp::resource::custom_domain::update instance id: %v", instanceID)
hostname := d.Get("hostname").(string)
data, err := api.UpdateCustomDomain(instanceID, hostname)
log.Printf("[DEBUG] cloudamqp::resource::custom_domain::create data: %v", data)
if err != nil {
return err
}
d.SetId(strconv.Itoa(instanceID))
d.Set("instance_id", instanceID)
for k, v := range data {
if validateCustomDomainSchemaAttribute(k) {
if err = d.Set(k, v); err != nil {
return fmt.Errorf("error setting %s for resource %s: %s", k, d.Id(), err)
}
}
}
return nil
}
func resourceCustomDomainDelete(d *schema.ResourceData, meta interface{}) error {
api := meta.(*api.API)
instanceID, _ := strconv.Atoi(d.Id())
log.Printf("[DEBUG] cloudamqp::resource::custom_domain::delete instance id: %v", instanceID)
data, err := api.DeleteCustomDomain(instanceID)
log.Printf("[DEBUG] cloudamqp::resource::custom_domain::delete data: %v", data)
if err != nil {
return err
}
return nil
}
func validateCustomDomainSchemaAttribute(key string) bool {
switch key {
case "hostname":
return true
}
return false
}