/
resource_flexibleengine_sdrs_protectiongroup_v1.go
211 lines (183 loc) · 6.07 KB
/
resource_flexibleengine_sdrs_protectiongroup_v1.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package flexibleengine
import (
"fmt"
"log"
"time"
"github.com/chnsz/golangsdk"
"github.com/chnsz/golangsdk/openstack/sdrs/v1/protectiongroups"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceSdrsProtectiongroupV1() *schema.Resource {
return &schema.Resource{
Create: resourceSdrsProtectiongroupV1Create,
Read: resourceSdrsProtectiongroupV1Read,
Update: resourceSdrsProtectiongroupV1Update,
Delete: resourceSdrsProtectiongroupV1Delete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: false,
},
"description": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"source_availability_zone": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"target_availability_zone": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"domain_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"source_vpc_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"dr_type": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"enable": {
Type: schema.TypeBool,
Optional: true,
ForceNew: false,
},
},
}
}
func resourceSdrsProtectiongroupV1Create(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
sdrsClient, err := sdrsV1Client(config, GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating FlexibleEngine SDRS Client: %s", err)
}
createOpts := protectiongroups.CreateOpts{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
SourceAZ: d.Get("source_availability_zone").(string),
TargetAZ: d.Get("target_availability_zone").(string),
DomainID: d.Get("domain_id").(string),
SourceVpcID: d.Get("source_vpc_id").(string),
DrType: d.Get("dr_type").(string),
}
log.Printf("[DEBUG] CreateOpts: %#v", createOpts)
n, err := protectiongroups.Create(sdrsClient, createOpts).ExtractJobResponse()
if err != nil {
return fmt.Errorf("Error creating FlexibleEngine SDRS Protectiongroup: %s", err)
}
if err := protectiongroups.WaitForJobSuccess(sdrsClient, int(d.Timeout(schema.TimeoutCreate)/time.Second), n.JobID); err != nil {
return err
}
entity, err := protectiongroups.GetJobEntity(sdrsClient, n.JobID, "server_group_id")
if err != nil {
return err
}
if id, ok := entity.(string); ok {
d.SetId(id)
if hasFilledOpt(d, "enable") && d.Get("enable").(bool) == true {
n, err := protectiongroups.Enable(sdrsClient, id).ExtractJobResponse()
if err != nil {
return fmt.Errorf("Error Enabling FlexibleEngine SDRS Protectiongroup: %s", err)
}
if err := protectiongroups.WaitForJobSuccess(sdrsClient, int(d.Timeout(schema.TimeoutCreate)/time.Second), n.JobID); err != nil {
return err
}
}
return resourceSdrsProtectiongroupV1Read(d, meta)
}
return fmt.Errorf("Unexpected conversion error in resourceSdrsProtectiongroupV1Create.")
}
func resourceSdrsProtectiongroupV1Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
sdrsClient, err := sdrsV1Client(config, GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating FlexibleEngine SDRS client: %s", err)
}
n, err := protectiongroups.Get(sdrsClient, d.Id()).Extract()
if err != nil {
if _, ok := err.(golangsdk.ErrDefault404); ok {
d.SetId("")
return nil
}
return fmt.Errorf("Error retrieving FlexibleEngine SDRS Protectiongroup: %s", err)
}
d.Set("name", n.Name)
d.Set("description", n.Description)
d.Set("source_availability_zone", n.SourceAZ)
d.Set("target_availability_zone", n.TargetAZ)
d.Set("domain_id", n.DomainID)
d.Set("source_vpc_id", n.SourceVpcID)
d.Set("dr_type", n.DrType)
return nil
}
func resourceSdrsProtectiongroupV1Update(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
sdrsClient, err := sdrsV1Client(config, GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating FlexibleEngine SDRS Client: %s", err)
}
if d.HasChange("name") {
var updateOpts protectiongroups.UpdateOpts
updateOpts.Name = d.Get("name").(string)
log.Printf("[DEBUG] updateOpts: %#v", updateOpts)
_, err = protectiongroups.Update(sdrsClient, d.Id(), updateOpts).Extract()
if err != nil {
return fmt.Errorf("Error updating FlexibleEngine SDRS Protectiongroup: %s", err)
}
}
if d.HasChange("enable") {
if d.Get("enable").(bool) == true {
n, err := protectiongroups.Enable(sdrsClient, d.Id()).ExtractJobResponse()
if err != nil {
return fmt.Errorf("Error enabling FlexibleEngine SDRS Protectiongroup: %s", err)
}
if err := protectiongroups.WaitForJobSuccess(sdrsClient, int(d.Timeout(schema.TimeoutCreate)/time.Second), n.JobID); err != nil {
return err
}
} else {
n, err := protectiongroups.Disable(sdrsClient, d.Id()).ExtractJobResponse()
if err != nil {
return fmt.Errorf("Error disabling FlexibleEngine SDRS Protectiongroup: %s", err)
}
if err := protectiongroups.WaitForJobSuccess(sdrsClient, int(d.Timeout(schema.TimeoutCreate)/time.Second), n.JobID); err != nil {
return err
}
}
}
return resourceSdrsProtectiongroupV1Read(d, meta)
}
func resourceSdrsProtectiongroupV1Delete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
sdrsClient, err := sdrsV1Client(config, GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating FlexibleEngine SDRS client: %s", err)
}
n, err := protectiongroups.Delete(sdrsClient, d.Id()).ExtractJobResponse()
if err != nil {
return fmt.Errorf("Error deleting FlexibleEngine SDRS Protectiongroup: %s", err)
}
if err := protectiongroups.WaitForJobSuccess(sdrsClient, int(d.Timeout(schema.TimeoutDelete)/time.Second), n.JobID); err != nil {
return err
}
d.SetId("")
return nil
}