-
Notifications
You must be signed in to change notification settings - Fork 670
/
data_source_ibm_is_region.go
73 lines (62 loc) · 1.56 KB
/
data_source_ibm_is_region.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
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM/vpc-go-sdk/vpcv1"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const (
isRegionEndpoint = "endpoint"
isRegionName = "name"
isRegionStatus = "status"
)
func DataSourceIBMISRegion() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMISRegionRead,
Schema: map[string]*schema.Schema{
isRegionEndpoint: {
Type: schema.TypeString,
Computed: true,
},
isRegionName: {
Type: schema.TypeString,
Optional: true,
},
isRegionStatus: {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceIBMISRegionRead(d *schema.ResourceData, meta interface{}) error {
name := d.Get("name").(string)
if name == "" {
bmxSess, err := meta.(conns.ClientSession).BluemixSession()
if err != nil {
return err
}
name = bmxSess.Config.Region
}
return regionGet(d, meta, name)
}
func regionGet(d *schema.ResourceData, meta interface{}, name string) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
getRegionOptions := &vpcv1.GetRegionOptions{
Name: &name,
}
region, _, err := sess.GetRegion(getRegionOptions)
if err != nil {
return err
}
// For lack of anything better, compose our id from region name.
d.SetId(*region.Name)
d.Set(isRegionEndpoint, *region.Endpoint)
d.Set(isRegionName, *region.Name)
d.Set(isRegionStatus, *region.Status)
return nil
}