This repository has been archived by the owner on Nov 14, 2020. It is now read-only.
/
resource_openstack_db_configuration_v1.go
187 lines (158 loc) · 4.91 KB
/
resource_openstack_db_configuration_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
package openstack
import (
"fmt"
"log"
"time"
"github.com/gophercloud/gophercloud/openstack/db/v1/configurations"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func resourceDatabaseConfigurationV1() *schema.Resource {
return &schema.Resource{
Create: resourceDatabaseConfigurationV1Create,
Read: resourceDatabaseConfigurationV1Read,
Delete: resourceDatabaseConfigurationV1Delete,
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
},
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"description": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"datastore": {
Type: schema.TypeList,
Required: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"version": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
},
MaxItems: 1,
},
"configuration": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"value": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
},
},
},
}
}
func resourceDatabaseConfigurationV1Create(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
DatabaseV1Client, err := config.DatabaseV1Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenStack database client: %s", err)
}
createOpts := &configurations.CreateOpts{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
}
var datastore configurations.DatastoreOpts
if v, ok := d.GetOk("datastore"); ok {
datastore = expandDatabaseConfigurationV1Datastore(v.([]interface{}))
}
createOpts.Datastore = &datastore
values := make(map[string]interface{})
if v, ok := d.GetOk("configuration"); ok {
values = expandDatabaseConfigurationV1Values(v.([]interface{}))
}
createOpts.Values = values
log.Printf("[DEBUG] openstack_db_configuration_v1 create options: %#v", createOpts)
cgroup, err := configurations.Create(DatabaseV1Client, createOpts).Extract()
if err != nil {
return fmt.Errorf("Error creating openstack_db_configuration_v1: %s", err)
}
stateConf := &resource.StateChangeConf{
Pending: []string{"BUILD"},
Target: []string{"ACTIVE"},
Refresh: databaseConfigurationV1StateRefreshFunc(DatabaseV1Client, cgroup.ID),
Timeout: d.Timeout(schema.TimeoutCreate),
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
}
_, err = stateConf.WaitForState()
if err != nil {
return fmt.Errorf("Error waiting for openstack_db_configuration_v1 %s to become ready: %s", cgroup.ID, err)
}
// Store the ID now
d.SetId(cgroup.ID)
return resourceDatabaseConfigurationV1Read(d, meta)
}
func resourceDatabaseConfigurationV1Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
DatabaseV1Client, err := config.DatabaseV1Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenStack database client: %s", err)
}
cgroup, err := configurations.Get(DatabaseV1Client, d.Id()).Extract()
if err != nil {
return CheckDeleted(d, err, "Error retrieving openstack_db_configuration_v1")
}
log.Printf("[DEBUG] Retrieved openstack_db_configuration_v1 %s: %#v", d.Id(), cgroup)
d.Set("name", cgroup.Name)
d.Set("description", cgroup.Description)
d.Set("region", GetRegion(d, config))
return nil
}
func resourceDatabaseConfigurationV1Delete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
DatabaseV1Client, err := config.DatabaseV1Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenStack database client: %s", err)
}
err = configurations.Delete(DatabaseV1Client, d.Id()).ExtractErr()
if err != nil {
return fmt.Errorf("Error deleting openstack_db_configuration_v1 %s: %s", d.Id(), err)
}
stateConf := &resource.StateChangeConf{
Pending: []string{"ACTIVE", "SHUTOFF"},
Target: []string{"DELETED"},
Refresh: databaseConfigurationV1StateRefreshFunc(DatabaseV1Client, d.Id()),
Timeout: d.Timeout(schema.TimeoutDelete),
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
}
_, err = stateConf.WaitForState()
if err != nil {
return fmt.Errorf("Error waiting for openstack_db_configuration_v1 %s to delete: %s", d.Id(), err)
}
return nil
}