-
Notifications
You must be signed in to change notification settings - Fork 658
/
data_source_ibm_is_volume_profiles.go
101 lines (87 loc) · 2.47 KB
/
data_source_ibm_is_volume_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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// 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/vpc-go-sdk/vpcv1"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const (
isVolumeProfiles = "profiles"
)
func DataSourceIBMISVolumeProfiles() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMISVolumeProfilesRead,
Schema: map[string]*schema.Schema{
isVolumeProfiles: {
Type: schema.TypeList,
Description: "List of Volume 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 dataSourceIBMISVolumeProfilesRead(d *schema.ResourceData, meta interface{}) error {
err := volumeProfilesList(d, meta)
if err != nil {
return err
}
return nil
}
func volumeProfilesList(d *schema.ResourceData, meta interface{}) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
start := ""
allrecs := []vpcv1.VolumeProfile{}
for {
listVolumeProfilesOptions := &vpcv1.ListVolumeProfilesOptions{}
if start != "" {
listVolumeProfilesOptions.Start = &start
}
availableProfiles, response, err := sess.ListVolumeProfiles(listVolumeProfilesOptions)
if err != nil {
return fmt.Errorf("[ERROR] Error Fetching Volume Profiles %s\n%s", err, response)
}
start = flex.GetNext(availableProfiles.Next)
allrecs = append(allrecs, availableProfiles.Profiles...)
if start == "" {
break
}
}
// listVolumeProfilesOptions := &vpcv1.ListVolumeProfilesOptions{}
// availableProfiles, response, err := sess.ListVolumeProfiles(listVolumeProfilesOptions)
// if err != nil {
// return fmt.Errorf("[ERROR] Error Fetching Volume Profiles %s\n%s", err, response)
// }
profilesInfo := make([]map[string]interface{}, 0)
for _, profile := range allrecs {
l := map[string]interface{}{
"name": *profile.Name,
"family": *profile.Family,
}
profilesInfo = append(profilesInfo, l)
}
d.SetId(dataSourceIBMISVolumeProfilesID(d))
d.Set(isVolumeProfiles, profilesInfo)
return nil
}
// dataSourceIBMISVolumeProfilesID returns a reasonable ID for a Volume Profile list.
func dataSourceIBMISVolumeProfilesID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}