-
Notifications
You must be signed in to change notification settings - Fork 656
/
data_source_ibm_is_zone.go
69 lines (58 loc) · 1.51 KB
/
data_source_ibm_is_zone.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
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"fmt"
"github.com/IBM/vpc-go-sdk/vpcv1"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const (
isZoneName = "name"
isZoneRegion = "region"
isZoneStatus = "status"
)
func DataSourceIBMISZone() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMISZoneRead,
Schema: map[string]*schema.Schema{
isZoneName: {
Type: schema.TypeString,
Required: true,
},
isZoneRegion: {
Type: schema.TypeString,
Required: true,
},
isZoneStatus: {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceIBMISZoneRead(d *schema.ResourceData, meta interface{}) error {
regionName := d.Get(isZoneRegion).(string)
zoneName := d.Get(isZoneName).(string)
return zoneGet(d, meta, regionName, zoneName)
}
func zoneGet(d *schema.ResourceData, meta interface{}, regionName, zoneName string) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
getRegionZoneOptions := &vpcv1.GetRegionZoneOptions{
RegionName: ®ionName,
Name: &zoneName,
}
zone, _, err := sess.GetRegionZone(getRegionZoneOptions)
if err != nil {
return err
}
// For lack of anything better, compose our id from region name + zone name.
id := fmt.Sprintf("%s.%s", *zone.Region.Name, *zone.Name)
d.SetId(id)
d.Set(isZoneName, *zone.Name)
d.Set(isZoneRegion, *zone.Region.Name)
d.Set(isZoneStatus, *zone.Status)
return nil
}