-
Notifications
You must be signed in to change notification settings - Fork 9k
/
resource_aws_storagegateway_cached_iscsi_volume.go
236 lines (208 loc) · 7.6 KB
/
resource_aws_storagegateway_cached_iscsi_volume.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
226
227
228
229
230
231
232
233
234
235
236
package aws
import (
"fmt"
"log"
"strings"
"time"
"github.com/hashicorp/terraform/helper/resource"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/storagegateway"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsStorageGatewayCachedIscsiVolume() *schema.Resource {
return &schema.Resource{
Create: resourceAwsStorageGatewayCachedIscsiVolumeCreate,
Read: resourceAwsStorageGatewayCachedIscsiVolumeRead,
Delete: resourceAwsStorageGatewayCachedIscsiVolumeDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"chap_enabled": {
Type: schema.TypeBool,
Computed: true,
},
"gateway_arn": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateArn,
},
"lun_number": {
Type: schema.TypeInt,
Computed: true,
},
// Poor API naming: this accepts the IP address of the network interface
"network_interface_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"network_interface_port": {
Type: schema.TypeInt,
Computed: true,
},
"snapshot_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"source_volume_arn": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validateArn,
},
"target_arn": {
Type: schema.TypeString,
Computed: true,
},
"target_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"volume_arn": {
Type: schema.TypeString,
Computed: true,
},
"volume_id": {
Type: schema.TypeString,
Computed: true,
},
"volume_size_in_bytes": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
},
},
}
}
func resourceAwsStorageGatewayCachedIscsiVolumeCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).storagegatewayconn
input := &storagegateway.CreateCachediSCSIVolumeInput{
ClientToken: aws.String(resource.UniqueId()),
GatewayARN: aws.String(d.Get("gateway_arn").(string)),
NetworkInterfaceId: aws.String(d.Get("network_interface_id").(string)),
TargetName: aws.String(d.Get("target_name").(string)),
VolumeSizeInBytes: aws.Int64(int64(d.Get("volume_size_in_bytes").(int))),
}
if v, ok := d.GetOk("snapshot_id"); ok {
input.SnapshotId = aws.String(v.(string))
}
if v, ok := d.GetOk("source_volume_arn"); ok {
input.SourceVolumeARN = aws.String(v.(string))
}
log.Printf("[DEBUG] Creating Storage Gateway cached iSCSI volume: %s", input)
output, err := conn.CreateCachediSCSIVolume(input)
if err != nil {
return fmt.Errorf("error creating Storage Gateway cached iSCSI volume: %s", err)
}
d.SetId(aws.StringValue(output.VolumeARN))
return resourceAwsStorageGatewayCachedIscsiVolumeRead(d, meta)
}
func resourceAwsStorageGatewayCachedIscsiVolumeRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).storagegatewayconn
input := &storagegateway.DescribeCachediSCSIVolumesInput{
VolumeARNs: []*string{aws.String(d.Id())},
}
log.Printf("[DEBUG] Reading Storage Gateway cached iSCSI volume: %s", input)
output, err := conn.DescribeCachediSCSIVolumes(input)
if err != nil {
if isAWSErr(err, storagegateway.ErrorCodeVolumeNotFound, "") {
log.Printf("[WARN] Storage Gateway cached iSCSI volume %q not found, removing from state", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("error reading Storage Gateway cached iSCSI volume %q: %s", d.Id(), err)
}
if output == nil || len(output.CachediSCSIVolumes) == 0 || output.CachediSCSIVolumes[0] == nil || aws.StringValue(output.CachediSCSIVolumes[0].VolumeARN) != d.Id() {
log.Printf("[WARN] Storage Gateway cached iSCSI volume %q not found, removing from state", d.Id())
d.SetId("")
return nil
}
volume := output.CachediSCSIVolumes[0]
d.Set("arn", aws.StringValue(volume.VolumeARN))
d.Set("snapshot_id", aws.StringValue(volume.SourceSnapshotId))
d.Set("volume_arn", aws.StringValue(volume.VolumeARN))
d.Set("volume_id", aws.StringValue(volume.VolumeId))
d.Set("volume_size_in_bytes", int(aws.Int64Value(volume.VolumeSizeInBytes)))
if volume.VolumeiSCSIAttributes != nil {
d.Set("chap_enabled", aws.BoolValue(volume.VolumeiSCSIAttributes.ChapEnabled))
d.Set("lun_number", int(aws.Int64Value(volume.VolumeiSCSIAttributes.LunNumber)))
d.Set("network_interface_id", aws.StringValue(volume.VolumeiSCSIAttributes.NetworkInterfaceId))
d.Set("network_interface_port", int(aws.Int64Value(volume.VolumeiSCSIAttributes.NetworkInterfacePort)))
targetARN := aws.StringValue(volume.VolumeiSCSIAttributes.TargetARN)
d.Set("target_arn", targetARN)
gatewayARN, targetName, err := parseStorageGatewayVolumeGatewayARNAndTargetNameFromARN(targetARN)
if err != nil {
return fmt.Errorf("error parsing Storage Gateway volume gateway ARN and target name from target ARN %q: %s", targetARN, err)
}
d.Set("gateway_arn", gatewayARN)
d.Set("target_name", targetName)
}
return nil
}
func resourceAwsStorageGatewayCachedIscsiVolumeDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).storagegatewayconn
input := &storagegateway.DeleteVolumeInput{
VolumeARN: aws.String(d.Id()),
}
log.Printf("[DEBUG] Deleting Storage Gateway cached iSCSI volume: %s", input)
err := resource.Retry(2*time.Minute, func() *resource.RetryError {
_, err := conn.DeleteVolume(input)
if err != nil {
if isAWSErr(err, storagegateway.ErrorCodeVolumeNotFound, "") {
return nil
}
// InvalidGatewayRequestException: The specified gateway is not connected.
// Can occur during concurrent DeleteVolume operations
if isAWSErr(err, storagegateway.ErrCodeInvalidGatewayRequestException, "The specified gateway is not connected") {
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})
if err != nil {
return fmt.Errorf("error deleting Storage Gateway cached iSCSI volume %q: %s", d.Id(), err)
}
return nil
}
func parseStorageGatewayVolumeGatewayARNAndTargetNameFromARN(inputARN string) (string, string, error) {
// inputARN = arn:aws:storagegateway:us-east-2:111122223333:gateway/sgw-12A3456B/target/iqn.1997-05.com.amazon:TargetName
targetARN, err := arn.Parse(inputARN)
if err != nil {
return "", "", err
}
// We need to get:
// * The Gateway ARN portion of the target ARN resource (gateway/sgw-12A3456B)
// * The target name portion of the target ARN resource (TargetName)
// First, let's split up the resource of the target ARN
// targetARN.Resource = gateway/sgw-12A3456B/target/iqn.1997-05.com.amazon:TargetName
expectedFormatErr := fmt.Errorf("expected resource format gateway/sgw-12A3456B/target/iqn.1997-05.com.amazon:TargetName, received: %s", targetARN.Resource)
resourceParts := strings.SplitN(targetARN.Resource, "/", 4)
if len(resourceParts) != 4 {
return "", "", expectedFormatErr
}
gatewayARN := arn.ARN{
AccountID: targetARN.AccountID,
Partition: targetARN.Partition,
Region: targetARN.Region,
Resource: fmt.Sprintf("%s/%s", resourceParts[0], resourceParts[1]),
Service: targetARN.Service,
}.String()
// Second, let's split off the target name from the initiator name
// resourceParts[3] = iqn.1997-05.com.amazon:TargetName
initiatorParts := strings.SplitN(resourceParts[3], ":", 2)
if len(initiatorParts) != 2 {
return "", "", expectedFormatErr
}
targetName := initiatorParts[1]
return gatewayARN, targetName, nil
}