forked from hashicorp/terraform-provider-aws
/
resource_aws_storagegateway_cache.go
159 lines (134 loc) · 4.52 KB
/
resource_aws_storagegateway_cache.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
package aws
import (
"fmt"
"log"
"strings"
"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 resourceAwsStorageGatewayCache() *schema.Resource {
return &schema.Resource{
Create: resourceAwsStorageGatewayCacheCreate,
Read: resourceAwsStorageGatewayCacheRead,
Delete: schema.Noop,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"disk_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"gateway_arn": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateArn,
},
},
}
}
func resourceAwsStorageGatewayCacheCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).storagegatewayconn
diskID := d.Get("disk_id").(string)
gatewayARN := d.Get("gateway_arn").(string)
input := &storagegateway.AddCacheInput{
DiskIds: []*string{aws.String(diskID)},
GatewayARN: aws.String(gatewayARN),
}
log.Printf("[DEBUG] Adding Storage Gateway cache: %s", input)
_, err := conn.AddCache(input)
if err != nil {
return fmt.Errorf("error adding Storage Gateway cache: %s", err)
}
d.SetId(fmt.Sprintf("%s:%s", gatewayARN, diskID))
// Depending on the Storage Gateway software, it will sometimes relabel a local DiskId
// with a UUID if previously unlabeled, e.g.
// Before conn.AddCache(): "DiskId": "/dev/xvdb",
// After conn.AddCache(): "DiskId": "112764d7-7e83-42ce-9af3-d482985a31cc",
// This prevents us from successfully reading the disk after creation.
// Here we try to refresh the local disks to see if we can find a new DiskId.
listLocalDisksInput := &storagegateway.ListLocalDisksInput{
GatewayARN: aws.String(gatewayARN),
}
log.Printf("[DEBUG] Reading Storage Gateway Local Disk: %s", listLocalDisksInput)
output, err := conn.ListLocalDisks(listLocalDisksInput)
if err != nil {
return fmt.Errorf("error reading Storage Gateway Local Disk: %s", err)
}
if output != nil {
for _, disk := range output.Disks {
if aws.StringValue(disk.DiskId) == diskID || aws.StringValue(disk.DiskNode) == diskID || aws.StringValue(disk.DiskPath) == diskID {
diskID = aws.StringValue(disk.DiskId)
break
}
}
}
d.SetId(fmt.Sprintf("%s:%s", gatewayARN, diskID))
return resourceAwsStorageGatewayCacheRead(d, meta)
}
func resourceAwsStorageGatewayCacheRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).storagegatewayconn
gatewayARN, diskID, err := decodeStorageGatewayCacheID(d.Id())
if err != nil {
return err
}
input := &storagegateway.DescribeCacheInput{
GatewayARN: aws.String(gatewayARN),
}
log.Printf("[DEBUG] Reading Storage Gateway cache: %s", input)
output, err := conn.DescribeCache(input)
if err != nil {
if isAWSErrStorageGatewayGatewayNotFound(err) {
log.Printf("[WARN] Storage Gateway cache %q not found - removing from state", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("error reading Storage Gateway cache: %s", err)
}
if output == nil || len(output.DiskIds) == 0 {
log.Printf("[WARN] Storage Gateway cache %q not found - removing from state", d.Id())
d.SetId("")
return nil
}
found := false
for _, existingDiskID := range output.DiskIds {
if aws.StringValue(existingDiskID) == diskID {
found = true
break
}
}
if !found {
log.Printf("[WARN] Storage Gateway cache %q not found - removing from state", d.Id())
d.SetId("")
return nil
}
d.Set("disk_id", diskID)
d.Set("gateway_arn", gatewayARN)
return nil
}
func decodeStorageGatewayCacheID(id string) (string, string, error) {
// id = arn:aws:storagegateway:us-east-1:123456789012:gateway/sgw-12345678:pci-0000:03:00.0-scsi-0:0:0:0
idFormatErr := fmt.Errorf("expected ID in form of GatewayARN:DiskId, received: %s", id)
gatewayARNAndDisk, err := arn.Parse(id)
if err != nil {
return "", "", idFormatErr
}
// gatewayARNAndDisk.Resource = gateway/sgw-12345678:pci-0000:03:00.0-scsi-0:0:0:0
resourceParts := strings.SplitN(gatewayARNAndDisk.Resource, ":", 2)
if len(resourceParts) != 2 {
return "", "", idFormatErr
}
// resourceParts = ["gateway/sgw-12345678", "pci-0000:03:00.0-scsi-0:0:0:0"]
gatewayARN := &arn.ARN{
AccountID: gatewayARNAndDisk.AccountID,
Partition: gatewayARNAndDisk.Partition,
Region: gatewayARNAndDisk.Region,
Service: gatewayARNAndDisk.Service,
Resource: resourceParts[0],
}
return gatewayARN.String(), resourceParts[1], nil
}