-
Notifications
You must be signed in to change notification settings - Fork 649
/
data_source_ibm_is_image_export_job.go
276 lines (248 loc) · 10.3 KB
/
data_source_ibm_is_image_export_job.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Copyright IBM Corp. 2022 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"context"
"fmt"
"log"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/IBM/vpc-go-sdk/vpcv1"
)
func DataSourceIBMIsImageExport() *schema.Resource {
return &schema.Resource{
ReadContext: DataSourceIBMIsImageExportRead,
Schema: map[string]*schema.Schema{
"image": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The image identifier.",
},
"image_export_job": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The image export job identifier.",
},
"completed_at": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The date and time that the image export job was completed.If absent, the export job has not yet completed.",
},
"created_at": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The date and time that the image export job was created.",
},
"encrypted_data_key": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "A base64-encoded, encrypted representation of the key that was used to encrypt the data for the exported image. This key can be unwrapped with the image's `encryption_key` root key using either Key Protect or Hyper Protect Crypto Service.If absent, the export job is for an unencrypted image.",
},
"format": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The format of the exported image.",
},
"href": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The URL for this image export job.",
},
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The user-defined name for this image export job.",
},
"resource_type": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The type of resource referenced.",
},
"started_at": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The date and time that the image export job started running.If absent, the export job has not yet started.",
},
"status": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The status of this image export job:- `deleting`: Export job is being deleted- `failed`: Export job could not be completed successfully- `queued`: Export job is queued- `running`: Export job is in progress- `succeeded`: Export job was completed successfullyThe exported image object is automatically deleted for `failed` jobs.",
},
"status_reasons": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The reasons for the current status (if any).The enumerated reason code values for this property will expand in the future. When processing this property, check for and log unknown values. Optionally halt processing and surface the error, or bypass the resource on which the unexpected reason code was encountered.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"code": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "A snake case string succinctly identifying the status reason.",
},
"message": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "An explanation of the status reason.",
},
"more_info": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Link to documentation about this status reason.",
},
},
},
},
"storage_bucket": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The Cloud Object Storage bucket of the exported image object.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"crn": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The CRN of this Cloud Object Storage bucket.",
},
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The globally unique name of this Cloud Object Storage bucket.",
},
},
},
},
"storage_href": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The Cloud Object Storage location of the exported image object. The object at this location may not exist until the job is started, and will be incomplete while the job is running.After the job completes, the exported image object is not managed by the IBM VPC service, and may be removed or replaced with a different object by any user or service with IAM authorization to the bucket.",
},
"storage_object": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The Cloud Object Storage object for the exported image. This object may not exist untilthe job is started, and will not be complete until the job completes.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The name of this Cloud Object Storage object. Names are unique within a Cloud Object Storage bucket.",
},
},
},
},
},
}
}
func DataSourceIBMIsImageExportRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
vpcClient, err := meta.(conns.ClientSession).VpcV1API()
if err != nil {
return diag.FromErr(err)
}
getImageExportJobOptions := &vpcv1.GetImageExportJobOptions{}
getImageExportJobOptions.SetImageID(d.Get("image").(string))
getImageExportJobOptions.SetID(d.Get("image_export_job").(string))
imageExportJob, response, err := vpcClient.GetImageExportJobWithContext(context, getImageExportJobOptions)
if err != nil {
log.Printf("[DEBUG] GetImageExportJobWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("GetImageExportJobWithContext failed %s\n%s", err, response))
}
d.SetId(fmt.Sprintf("%s/%s", *getImageExportJobOptions.ImageID, *getImageExportJobOptions.ID))
if err = d.Set("completed_at", flex.DateTimeToString(imageExportJob.CompletedAt)); err != nil {
return diag.FromErr(fmt.Errorf("Error setting completed_at: %s", err))
}
if err = d.Set("created_at", flex.DateTimeToString(imageExportJob.CreatedAt)); err != nil {
return diag.FromErr(fmt.Errorf("Error setting created_at: %s", err))
}
if err = d.Set("encrypted_data_key", imageExportJob.EncryptedDataKey); err != nil {
return diag.FromErr(fmt.Errorf("Error setting encrypted_data_key: %s", err))
}
if err = d.Set("format", imageExportJob.Format); err != nil {
return diag.FromErr(fmt.Errorf("Error setting format: %s", err))
}
if err = d.Set("href", imageExportJob.Href); err != nil {
return diag.FromErr(fmt.Errorf("Error setting href: %s", err))
}
if err = d.Set("name", imageExportJob.Name); err != nil {
return diag.FromErr(fmt.Errorf("Error setting name: %s", err))
}
if err = d.Set("resource_type", imageExportJob.ResourceType); err != nil {
return diag.FromErr(fmt.Errorf("Error setting resource_type: %s", err))
}
if err = d.Set("started_at", flex.DateTimeToString(imageExportJob.StartedAt)); err != nil {
return diag.FromErr(fmt.Errorf("Error setting started_at: %s", err))
}
if err = d.Set("status", imageExportJob.Status); err != nil {
return diag.FromErr(fmt.Errorf("Error setting status: %s", err))
}
statusReasons := []map[string]interface{}{}
if imageExportJob.StatusReasons != nil {
for _, modelItem := range imageExportJob.StatusReasons {
modelMap, err := DataSourceIBMIsImageExportImageExportJobStatusReasonToMap(&modelItem)
if err != nil {
return diag.FromErr(err)
}
statusReasons = append(statusReasons, modelMap)
}
}
if err = d.Set("status_reasons", statusReasons); err != nil {
return diag.FromErr(fmt.Errorf("Error setting status_reasons %s", err))
}
storageBucket := []map[string]interface{}{}
if imageExportJob.StorageBucket != nil {
modelMap, err := DataSourceIBMIsImageExportCloudObjectStorageBucketReferenceToMap(imageExportJob.StorageBucket)
if err != nil {
return diag.FromErr(err)
}
storageBucket = append(storageBucket, modelMap)
}
if err = d.Set("storage_bucket", storageBucket); err != nil {
return diag.FromErr(fmt.Errorf("Error setting storage_bucket %s", err))
}
if err = d.Set("storage_href", imageExportJob.StorageHref); err != nil {
return diag.FromErr(fmt.Errorf("Error setting storage_href: %s", err))
}
storageObject := []map[string]interface{}{}
if imageExportJob.StorageObject != nil {
modelMap, err := DataSourceIBMIsImageExportCloudObjectStorageObjectReferenceToMap(imageExportJob.StorageObject)
if err != nil {
return diag.FromErr(err)
}
storageObject = append(storageObject, modelMap)
}
if err = d.Set("storage_object", storageObject); err != nil {
return diag.FromErr(fmt.Errorf("Error setting storage_object %s", err))
}
return nil
}
func DataSourceIBMIsImageExportImageExportJobStatusReasonToMap(model *vpcv1.ImageExportJobStatusReason) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.Code != nil {
modelMap["code"] = *model.Code
}
if model.Message != nil {
modelMap["message"] = *model.Message
}
if model.MoreInfo != nil {
modelMap["more_info"] = *model.MoreInfo
}
return modelMap, nil
}
func DataSourceIBMIsImageExportCloudObjectStorageBucketReferenceToMap(model *vpcv1.CloudObjectStorageBucketReference) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.CRN != nil {
modelMap["crn"] = *model.CRN
}
if model.Name != nil {
modelMap["name"] = *model.Name
}
return modelMap, nil
}
func DataSourceIBMIsImageExportCloudObjectStorageObjectReferenceToMap(model *vpcv1.CloudObjectStorageObjectReference) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.Name != nil {
modelMap["name"] = *model.Name
}
return modelMap, nil
}