/
data_source_ibm_is_instance_profiles.go
79 lines (69 loc) · 2.08 KB
/
data_source_ibm_is_instance_profiles.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
package ibm
import (
"time"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.ibm.com/Bluemix/riaas-go-client/clients/compute"
)
const (
isInstanceProfiles = "profiles"
)
func dataSourceIBMISInstanceProfiles() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMISInstanceProfilesRead,
Schema: map[string]*schema.Schema{
isInstanceProfiles: {
Type: schema.TypeList,
Description: "List of instance profile maps",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
"family": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
func dataSourceIBMISInstanceProfilesRead(d *schema.ResourceData, meta interface{}) error {
sess, err := meta.(ClientSession).ISSession()
if err != nil {
return err
}
clientC := compute.NewInstanceClient(sess)
availableProfiles, _, err := clientC.ListProfiles("")
if err != nil {
return err
}
profiles := make([]map[string]string, len(availableProfiles))
for i, profile := range availableProfiles {
p := make(map[string]string)
p["name"] = profile.Name
p["family"] = profile.Family
profiles[i] = p
}
d.SetId(dataSourceIBMISInstanceProfilesID(d))
d.Set(isInstanceProfiles, profiles)
return nil
}
// dataSourceIBMISZonesId returns a reasonable ID for a zone list.
func dataSourceIBMISInstanceProfilesID(d *schema.ResourceData) string {
// Our zone list is not guaranteed to be stable because the content
// of the list can vary between two calls if any of the following
// events occur between calls:
// - a zone is added to our region
// - a zone is dropped from our region
//
// For simplicity we are using a timestamp for the required terraform id.
// If we find through usage that this choice is too ephemeral for our users
// then we can change this function to use a more stable id, perhaps
// composed from a hash of the list contents. But, for now, a timestamp
// is good enough.
return time.Now().UTC().String()
}