-
Notifications
You must be signed in to change notification settings - Fork 645
/
data_source_ibm_is_instance_volume_attachments.go
167 lines (149 loc) · 5.49 KB
/
data_source_ibm_is_instance_volume_attachments.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
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"fmt"
"time"
"github.com/IBM/vpc-go-sdk/vpcv1"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func DataSourceIBMISInstanceVolumeAttachments() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMISInstanceVolumeAttachmentsRead,
Schema: map[string]*schema.Schema{
isInstanceId: {
Type: schema.TypeString,
Required: true,
Description: "Instance id",
},
isInstanceVolumeAttachments: {
Type: schema.TypeList,
Description: "List of volume attachments on an instance",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isInstanceVolumeDeleteOnInstanceDelete: {
Type: schema.TypeBool,
Computed: true,
Description: "If set to true, when deleting the instance the volume will also be deleted.",
},
isInstanceVolAttName: {
Type: schema.TypeString,
Computed: true,
Description: "The user-defined name for this volume attachment.",
},
isInstanceVolumeAttDevice: {
Type: schema.TypeString,
Computed: true,
Description: "A unique identifier for the device which is exposed to the instance operating system",
},
isInstanceVolumeAttHref: {
Type: schema.TypeString,
Computed: true,
Description: "The URL for this volume attachment",
},
isInstanceVolAttId: {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this volume attachment",
},
isInstanceVolumeAttStatus: {
Type: schema.TypeString,
Computed: true,
Description: "The status of this volume attachment, one of [ attached, attaching, deleting, detaching ]",
},
isInstanceVolumeAttType: {
Type: schema.TypeString,
Computed: true,
Description: "The type of volume attachment one of [ boot, data ]",
},
isInstanceVolumeAttVolumeReferenceId: {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this volume",
},
isInstanceVolumeAttVolumeReferenceName: {
Type: schema.TypeString,
Computed: true,
Description: "The unique user-defined name for this volume",
},
isInstanceVolumeAttVolumeReferenceCrn: {
Type: schema.TypeString,
Computed: true,
Description: "The CRN for this volume",
},
isInstanceVolumeAttVolumeReferenceDeleted: {
Type: schema.TypeString,
Computed: true,
Description: "Link to documentation about deleted resources",
},
isInstanceVolumeAttVolumeReferenceHref: {
Type: schema.TypeString,
Computed: true,
Description: "The URL for this volume",
},
isInstanceVolumeAttVolumeReference: {
Type: schema.TypeList,
Computed: true,
Description: "The attached volume",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{},
},
},
},
},
},
},
}
}
func dataSourceIBMISInstanceVolumeAttachmentsRead(d *schema.ResourceData, meta interface{}) error {
instanceId := d.Get(isInstanceId).(string)
err := instanceGetVolumeAttachments(d, meta, instanceId)
if err != nil {
return err
}
return nil
}
func instanceGetVolumeAttachments(d *schema.ResourceData, meta interface{}, instanceId string) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
allrecs := []vpcv1.VolumeAttachment{}
listInstanceVolumeAttOptions := &vpcv1.ListInstanceVolumeAttachmentsOptions{
InstanceID: &instanceId,
}
volumeAtts, response, err := sess.ListInstanceVolumeAttachments(listInstanceVolumeAttOptions)
if err != nil {
return fmt.Errorf("[ERROR] Error Fetching Instance volume attachments %s\n%s", err, response)
}
allrecs = append(allrecs, volumeAtts.VolumeAttachments...)
volAttList := make([]map[string]interface{}, 0)
for _, volumeAtt := range allrecs {
currentVolAtt := map[string]interface{}{}
currentVolAtt[isInstanceVolAttName] = *volumeAtt.Name
currentVolAtt[isInstanceVolumeDeleteOnInstanceDelete] = *volumeAtt.DeleteVolumeOnInstanceDelete
currentVolAtt[isInstanceVolumeAttDevice] = *volumeAtt.Device.ID
currentVolAtt[isInstanceVolumeAttHref] = *volumeAtt.Href
currentVolAtt[isInstanceVolAttId] = *volumeAtt.ID
currentVolAtt[isInstanceVolumeAttStatus] = *volumeAtt.Status
currentVolAtt[isInstanceVolumeAttType] = *volumeAtt.Type
if volumeAtt.Volume != nil {
currentVolAtt[isInstanceVolumeAttVolumeReferenceId] = *volumeAtt.Volume.ID
currentVolAtt[isInstanceVolumeAttVolumeReferenceName] = *volumeAtt.Volume.Name
currentVolAtt[isInstanceVolumeAttVolumeReferenceCrn] = *volumeAtt.Volume.CRN
if volumeAtt.Volume.Deleted != nil {
currentVolAtt[isInstanceVolumeAttVolumeReferenceDeleted] = *volumeAtt.Volume.Deleted.MoreInfo
}
currentVolAtt[isInstanceVolumeAttVolumeReferenceHref] = *volumeAtt.Volume.Href
}
volAttList = append(volAttList, currentVolAtt)
}
d.SetId(dataSourceIBMISInstanceVolumeAttachmentsID(d))
d.Set(isInstanceVolumeAttachments, volAttList)
return nil
}
// dataSourceIBMISInstanceVolumeAttachmentsID returns a reasonable ID for a Instance volume attachments list.
func dataSourceIBMISInstanceVolumeAttachmentsID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}