-
Notifications
You must be signed in to change notification settings - Fork 658
/
data_source_ibm_is_regions.go
84 lines (73 loc) · 1.87 KB
/
data_source_ibm_is_regions.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
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"time"
"github.com/IBM/vpc-go-sdk/vpcv1"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const (
isRegions = "regions"
isRegionHref = "href"
)
func DataSourceIBMISRegions() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMISRegionsRead,
Schema: map[string]*schema.Schema{
isRegions: {
Type: schema.TypeList,
Description: "List of regions",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isRegionEndpoint: {
Type: schema.TypeString,
Computed: true,
},
isRegionHref: {
Type: schema.TypeString,
Computed: true,
},
isRegionName: {
Type: schema.TypeString,
Computed: true,
},
isRegionStatus: {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
func dataSourceIBMISRegionsRead(d *schema.ResourceData, meta interface{}) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
listRegionOptions := &vpcv1.ListRegionsOptions{}
regioncollection, _, err := sess.ListRegions(listRegionOptions)
if err != nil {
return err
}
regions := regioncollection.Regions
regionInfo := make([]map[string]interface{}, 0)
for _, region := range regions {
l := map[string]interface{}{
isRegionEndpoint: *region.Endpoint,
isRegionName: *region.Name,
isRegionStatus: *region.Status,
isRegionHref: *region.Href,
}
regionInfo = append(regionInfo, l)
}
d.SetId(dataSourceIBMISRegionsID(d))
d.Set(isRegions, regionInfo)
return nil
}
// dataSourceIBMISRegionsID returns a reasonable ID for the region list.
func dataSourceIBMISRegionsID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}