-
Notifications
You must be signed in to change notification settings - Fork 645
/
data_source_ibm_is_images.go
239 lines (217 loc) · 6.38 KB
/
data_source_ibm_is_images.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
237
238
239
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"fmt"
"time"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/validate"
"github.com/IBM/vpc-go-sdk/vpcv1"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const (
isImages = "images"
isImagesResourceGroupID = "resource_group"
)
func DataSourceIBMISImages() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMISImagesRead,
Schema: map[string]*schema.Schema{
isImagesResourceGroupID: {
Type: schema.TypeString,
Description: "The id of the resource group",
Optional: true,
},
isImageName: {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validate.InvokeValidator("ibm_is_image", isImageName),
Description: "The name of the image",
},
isImageStatus: {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validate.InvokeDataSourceValidator("ibm_is_images", isImageStatus),
Description: "The status of the image",
},
isImageVisibility: {
Type: schema.TypeString,
Optional: true,
Description: "Whether the image is publicly visible or private to the account",
},
isImages: {
Type: schema.TypeList,
Description: "List of images",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
Description: "Image name",
},
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this image",
},
"status": {
Type: schema.TypeString,
Computed: true,
Description: "The status of this image",
},
"visibility": {
Type: schema.TypeString,
Computed: true,
Description: "Whether the image is publicly visible or private to the account",
},
"os": {
Type: schema.TypeString,
Computed: true,
Description: "Image Operating system",
},
"architecture": {
Type: schema.TypeString,
Computed: true,
Description: "The operating system architecture",
},
"crn": {
Type: schema.TypeString,
Computed: true,
Description: "The CRN for this image",
},
isImageCheckSum: {
Type: schema.TypeString,
Computed: true,
Description: "The SHA256 Checksum for this image",
},
isImageEncryptionKey: {
Type: schema.TypeString,
Computed: true,
Description: "The CRN of the Key Protect Root Key or Hyper Protect Crypto Service Root Key for this resource",
},
isImageEncryption: {
Type: schema.TypeString,
Computed: true,
Description: "The type of encryption used on the image",
},
"source_volume": {
Type: schema.TypeString,
Computed: true,
Description: "Source volume id of the image",
},
},
},
},
},
}
}
func DataSourceIBMISImagesValidator() *validate.ResourceValidator {
status := "available, deleting, deprecated, failed, obsolete, pending, tentative, unusable"
validateSchema := make([]validate.ValidateSchema, 0)
validateSchema = append(validateSchema,
validate.ValidateSchema{
Identifier: isImageStatus,
ValidateFunctionIdentifier: validate.ValidateAllowedStringValue,
Type: validate.TypeString,
Optional: true,
AllowedValues: status})
ibmISImageResourceValidator := validate.ResourceValidator{ResourceName: "ibm_is_images", Schema: validateSchema}
return &ibmISImageResourceValidator
}
func dataSourceIBMISImagesRead(d *schema.ResourceData, meta interface{}) error {
err := imageList(d, meta)
if err != nil {
return err
}
return nil
}
func imageList(d *schema.ResourceData, meta interface{}) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
start := ""
allrecs := []vpcv1.Image{}
var resourceGroupID string
if v, ok := d.GetOk(isImagesResourceGroupID); ok {
resourceGroupID = v.(string)
}
var imageName string
if v, ok := d.GetOk(isImageName); ok {
imageName = v.(string)
}
var visibility string
if v, ok := d.GetOk(isImageVisibility); ok {
visibility = v.(string)
}
var status string
if v, ok := d.GetOk(isImageStatus); ok {
status = v.(string)
}
listImagesOptions := &vpcv1.ListImagesOptions{}
if resourceGroupID != "" {
listImagesOptions.SetResourceGroupID(resourceGroupID)
}
if imageName != "" {
listImagesOptions.SetName(imageName)
}
if visibility != "" {
listImagesOptions.SetVisibility(visibility)
}
for {
if start != "" {
listImagesOptions.Start = &start
}
availableImages, response, err := sess.ListImages(listImagesOptions)
if err != nil {
return fmt.Errorf("[ERROR] Error Fetching Images %s\n%s", err, response)
}
start = flex.GetNext(availableImages.Next)
allrecs = append(allrecs, availableImages.Images...)
if start == "" {
break
}
}
if status != "" {
allrecsTemp := []vpcv1.Image{}
for _, image := range allrecs {
if status == *image.Status {
allrecsTemp = append(allrecsTemp, image)
}
}
allrecs = allrecsTemp
}
imagesInfo := make([]map[string]interface{}, 0)
for _, image := range allrecs {
l := map[string]interface{}{
"name": *image.Name,
"id": *image.ID,
"status": *image.Status,
"crn": *image.CRN,
"visibility": *image.Visibility,
"os": *image.OperatingSystem.Name,
"architecture": *image.OperatingSystem.Architecture,
}
if image.File != nil && image.File.Checksums != nil {
l[isImageCheckSum] = *image.File.Checksums.Sha256
}
if image.Encryption != nil {
l["encryption"] = *image.Encryption
}
if image.EncryptionKey != nil {
l["encryption_key"] = *image.EncryptionKey.CRN
}
if image.SourceVolume != nil {
l["source_volume"] = *image.SourceVolume.ID
}
imagesInfo = append(imagesInfo, l)
}
d.SetId(dataSourceIBMISSubnetsID(d))
d.Set(isImages, imagesInfo)
return nil
}
// dataSourceIBMISImagesId returns a reasonable ID for a image list.
func dataSourceIBMISImagesID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}