/
data_source_flexibleengine_vpc_subnet_v1.go
127 lines (113 loc) · 3.1 KB
/
data_source_flexibleengine_vpc_subnet_v1.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package flexibleengine
import (
"fmt"
"log"
"github.com/huaweicloud/golangsdk/openstack/networking/v1/subnets"
"github.com/hashicorp/terraform/helper/schema"
)
func dataSourceVpcSubnetV1() *schema.Resource {
return &schema.Resource{
Read: dataSourceVpcSubnetV1Read,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
},
"cidr": {
Type: schema.TypeString,
Optional: true,
},
"dns_list": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"status": {
Type: schema.TypeString,
Optional: true,
},
"vpc_id": {
Type: schema.TypeString,
Optional: true,
},
"gateway_ip": {
Type: schema.TypeString,
Optional: true,
},
"dhcp_enable": {
Type: schema.TypeBool,
Computed: true,
},
"primary_dns": {
Type: schema.TypeString,
Optional: true,
},
"secondary_dns": {
Type: schema.TypeString,
Optional: true,
},
"availability_zone": {
Type: schema.TypeString,
Optional: true,
},
"subnet_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceVpcSubnetV1Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
subnetClient, err := config.networkingV1Client(GetRegion(d, config))
listOpts := subnets.ListOpts{
ID: d.Get("id").(string),
Name: d.Get("name").(string),
CIDR: d.Get("cidr").(string),
Status: d.Get("status").(string),
GatewayIP: d.Get("gateway_ip").(string),
PRIMARY_DNS: d.Get("primary_dns").(string),
SECONDARY_DNS: d.Get("secondary_dns").(string),
AvailabilityZone: d.Get("availability_zone").(string),
VPC_ID: d.Get("vpc_id").(string),
}
refinedSubnets, err := subnets.List(subnetClient, listOpts)
if err != nil {
return fmt.Errorf("Unable to retrieve subnets: %s", err)
}
if refinedSubnets == nil || len(refinedSubnets) == 0 {
return fmt.Errorf("No matching subnet found. " +
"Please change your search criteria and try again.")
}
if len(refinedSubnets) > 1 {
return fmt.Errorf("multiple subnets matched; use additional constraints to reduce matches to a single subnet")
}
Subnets := refinedSubnets[0]
log.Printf("[INFO] Retrieved Subnet using given filter %s: %+v", Subnets.ID, Subnets)
d.SetId(Subnets.ID)
d.Set("name", Subnets.Name)
d.Set("cidr", Subnets.CIDR)
d.Set("dns_list", Subnets.DnsList)
d.Set("status", Subnets.Status)
d.Set("gateway_ip", Subnets.GatewayIP)
d.Set("dhcp_enable", Subnets.EnableDHCP)
d.Set("primary_dns", Subnets.PRIMARY_DNS)
d.Set("secondary_dns", Subnets.SECONDARY_DNS)
d.Set("availability_zone", Subnets.AvailabilityZone)
d.Set("vpc_id", Subnets.VPC_ID)
d.Set("subnet_id", Subnets.SubnetId)
d.Set("region", GetRegion(d, config))
return nil
}