/
resource_ibm_pi_volume_attach.go
225 lines (177 loc) · 6.43 KB
/
resource_ibm_pi_volume_attach.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package ibm
import (
"errors"
"fmt"
"log"
"time"
st "github.com/IBM-Cloud/power-go-client/clients/instance"
"github.com/IBM-Cloud/power-go-client/helpers"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
const (
/* Fix for PowerVC taking time to attach volume depending on load*/
attachVolumeTimeOut = 240 * time.Second
)
func resourceIBMPIVolumeAttach() *schema.Resource {
return &schema.Resource{
Create: resourceIBMPIVolumeAttachCreate,
Read: resourceIBMPIVolumeAttachRead,
Update: resourceIBMPIVolumeAttachUpdate,
Delete: resourceIBMPIVolumeAttachDelete,
//Exists: resourceIBMPowerVolumeExists,
Importer: &schema.ResourceImporter{},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(60 * time.Minute),
Delete: schema.DefaultTimeout(60 * time.Minute),
},
Schema: map[string]*schema.Schema{
"volumeattachid": {
Type: schema.TypeString,
Computed: true,
ForceNew: true,
Optional: true,
Description: "Volume attachment ID",
},
helpers.PICloudInstanceId: {
Type: schema.TypeString,
Required: true,
Description: " Cloud Instance ID - This is the service_instance_id.",
},
helpers.PIVolumeAttachName: {
Type: schema.TypeString,
Required: true,
Description: "Name of the volume to attach. Note these volumes should have been created",
},
helpers.PIInstanceName: {
Type: schema.TypeString,
Required: true,
Description: "PI Instance name",
},
helpers.PIVolumeAttachStatus: {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
helpers.PIVolumeShareable: {
Type: schema.TypeBool,
Computed: true,
Optional: true,
},
},
}
}
func resourceIBMPIVolumeAttachCreate(d *schema.ResourceData, meta interface{}) error {
sess, err := meta.(ClientSession).IBMPISession()
if err != nil {
return err
}
name := d.Get(helpers.PIVolumeAttachName).(string)
servername := d.Get(helpers.PIInstanceName).(string)
powerinstanceid := d.Get(helpers.PICloudInstanceId).(string)
client := st.NewIBMPIVolumeClient(sess, powerinstanceid)
volinfo, err := client.Get(name, powerinstanceid, getTimeOut)
if err != nil {
return fmt.Errorf("The volume [ %s] cannot be attached since it's not available", name)
}
//log.Print("The volume info is %s", volinfo)
if volinfo.State == "available" || *volinfo.Shareable == true {
log.Printf(" In the current state the volume can be attached to the instance ")
}
if volinfo.State == "in-use" && *volinfo.Shareable == true {
log.Printf("Volume State /Status is permitted and hence attaching the volume to the instance")
}
if volinfo.State == helpers.PIVolumeAllowableAttachStatus && *volinfo.Shareable == false {
return errors.New("The volume cannot be attached in the current state. The volume must be in the *available* state. No other states are permissible")
}
resp, err := client.Attach(servername, name, powerinstanceid, attachVolumeTimeOut)
if err != nil {
return err
}
log.Printf("Printing the resp %+v", resp)
d.SetId(*volinfo.VolumeID)
if err != nil {
log.Printf("[DEBUG] err %s", err)
return err
}
_, err = isWaitForIBMPIVolumeAttachAvailable(client, d.Id(), powerinstanceid, d.Timeout(schema.TimeoutCreate))
if err != nil {
return err
}
//return nil
return resourceIBMPIVolumeAttachRead(d, meta)
}
func resourceIBMPIVolumeAttachRead(d *schema.ResourceData, meta interface{}) error {
sess, _ := meta.(ClientSession).IBMPISession()
powerinstanceid := d.Get(helpers.PICloudInstanceId).(string)
servername := d.Get(helpers.PIInstanceName).(string)
client := st.NewIBMPIVolumeClient(sess, powerinstanceid)
vol, err := client.CheckVolumeAttach(powerinstanceid, servername, d.Id(), getTimeOut)
if err != nil {
return err
}
//d.SetId(vol.ID.String())
d.Set(helpers.PIVolumeAttachName, vol.Name)
d.Set(helpers.PIVolumeSize, vol.Size)
d.Set(helpers.PIVolumeShareable, vol.Shareable)
return nil
}
func resourceIBMPIVolumeAttachUpdate(d *schema.ResourceData, meta interface{}) error {
sess, _ := meta.(ClientSession).IBMPISession()
powerinstanceid := d.Get(helpers.PICloudInstanceId).(string)
client := st.NewIBMPIVolumeClient(sess, powerinstanceid)
name := ""
if d.HasChange(helpers.PIVolumeAttachName) {
name = d.Get(helpers.PIVolumeAttachName).(string)
}
size := float64(d.Get(helpers.PIVolumeSize).(float64))
shareable := bool(d.Get(helpers.PIVolumeShareable).(bool))
volrequest, err := client.Update(d.Id(), name, size, shareable, powerinstanceid, postTimeOut)
if err != nil {
return err
}
_, err = isWaitForIBMPIVolumeAttachAvailable(client, *volrequest.VolumeID, powerinstanceid, d.Timeout(schema.TimeoutCreate))
if err != nil {
return err
}
return resourceIBMPIVolumeRead(d, meta)
}
func resourceIBMPIVolumeAttachDelete(d *schema.ResourceData, meta interface{}) error {
sess, _ := meta.(ClientSession).IBMPISession()
powerinstanceid := d.Get(helpers.PICloudInstanceId).(string)
name := d.Get(helpers.PIVolumeAttachName).(string)
servername := d.Get(helpers.PIInstanceName).(string)
client := st.NewIBMPIVolumeClient(sess, powerinstanceid)
log.Printf("the id of the volume to detach is%s ", d.Id())
_, err := client.Detach(servername, name, powerinstanceid, deleteTimeOut)
if err != nil {
return err
}
// wait for power volume states to be back as available. if it's attached it will be in-use
d.SetId("")
return nil
}
func isWaitForIBMPIVolumeAttachAvailable(client *st.IBMPIVolumeClient, id, powerinstanceid string, timeout time.Duration) (interface{}, error) {
log.Printf("Waiting for Volume (%s) to be available for attachment", id)
stateConf := &resource.StateChangeConf{
Pending: []string{"retry", helpers.PIVolumeProvisioning},
Target: []string{helpers.PIVolumeAllowableAttachStatus},
Refresh: isIBMPIVolumeAttachRefreshFunc(client, id, powerinstanceid),
Delay: 10 * time.Second,
MinTimeout: 2 * time.Minute,
Timeout: 10 * time.Minute,
}
return stateConf.WaitForState()
}
func isIBMPIVolumeAttachRefreshFunc(client *st.IBMPIVolumeClient, id, powerinstanceid string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
vol, err := client.Get(id, powerinstanceid, getTimeOut)
if err != nil {
return nil, "", err
}
if vol.State == "in-use" {
return vol, helpers.PIVolumeAllowableAttachStatus, nil
}
return vol, helpers.PIVolumeProvisioning, nil
}
}