/
data_source_ibm_pi_images.go
107 lines (85 loc) · 2.32 KB
/
data_source_ibm_pi_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
package ibm
import (
"github.com/IBM-Cloud/power-go-client/helpers"
"github.com/IBM-Cloud/power-go-client/power/models"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
//"fmt"
"github.com/IBM-Cloud/power-go-client/clients/instance"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
)
/*
Datasource to get the list of images that are available when a power instance is created
*/
func dataSourceIBMPIImages() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMPIImagesAllRead,
Schema: map[string]*schema.Schema{
helpers.PIImageName: {
Type: schema.TypeString,
Optional: true,
Description: "Imagename Name to be used for pvminstances",
ValidateFunc: validation.NoZeroValues,
Deprecated: "This field is deprectaed.",
},
helpers.PICloudInstanceId: {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.NoZeroValues,
},
// Computed Attributes
"image_info": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"href": {
Type: schema.TypeString,
Computed: true,
},
"state": {
Type: schema.TypeString,
Computed: true},
},
},
},
},
}
}
func dataSourceIBMPIImagesAllRead(d *schema.ResourceData, meta interface{}) error {
sess, err := meta.(ClientSession).IBMPISession()
if err != nil {
return err
}
powerinstanceid := d.Get(helpers.PICloudInstanceId).(string)
imageC := instance.NewIBMPIImageClient(sess, powerinstanceid)
imagedata, err := imageC.GetAll(powerinstanceid)
if err != nil {
return err
}
var clientgenU, _ = uuid.GenerateUUID()
d.SetId(clientgenU)
_ = d.Set("image_info", flattenStockImages(imagedata.Images))
return nil
}
func flattenStockImages(list []*models.ImageReference) []map[string]interface{} {
result := make([]map[string]interface{}, 0, len(list))
for _, i := range list {
l := map[string]interface{}{
"id": *i.ImageID,
"state": *i.State,
"href": *i.Href,
"name": *i.Name,
}
result = append(result, l)
}
return result
}