-
Notifications
You must be signed in to change notification settings - Fork 7
/
data_source_hcs_vpc_subnet.go
170 lines (156 loc) · 4.29 KB
/
data_source_hcs_vpc_subnet.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package vpc
import (
"context"
"log"
"github.com/huaweicloud/terraform-provider-hcs/huaweicloudstack/config"
"github.com/huaweicloud/terraform-provider-hcs/huaweicloudstack/sdk/huaweicloud/openstack/networking/v1/subnets"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func DataSourceVpcSubnetV1() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceVpcSubnetV1Read,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"cidr": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"dns_list": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"status": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"vpc_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"gateway_ip": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"dhcp_enable": {
Type: schema.TypeBool,
Computed: true,
},
"primary_dns": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"secondary_dns": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"availability_zone": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"subnet_id": {
Type: schema.TypeString,
Computed: true,
Description: "schema: Deprecated",
},
"ipv4_subnet_id": {
Type: schema.TypeString,
Computed: true,
},
"ipv6_subnet_id": {
Type: schema.TypeString,
Computed: true,
},
"ipv6_enable": {
Type: schema.TypeBool,
Computed: true,
},
"ipv6_cidr": {
Type: schema.TypeString,
Computed: true,
},
"ipv6_gateway": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceVpcSubnetV1Read(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
hcsConfig := config.GetHcsConfig(meta)
subnetClient, err := hcsConfig.NetworkingV1Client(hcsConfig.GetRegion(d))
if err != nil {
return diag.Errorf("error creating Vpc client: %s", err)
}
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 diag.Errorf("unable to retrieve subnets: %s", err)
}
if len(refinedSubnets) == 0 {
return diag.Errorf("no matching subnet found. " +
"Please change your search criteria and try again.")
}
if len(refinedSubnets) > 1 {
return diag.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("description", Subnets.Description)
d.Set("cidr", Subnets.CIDR)
d.Set("dns_list", Subnets.DnsList)
d.Set("status", Subnets.Status)
d.Set("gateway_ip", Subnets.GatewayIP)
d.Set("ipv6_enable", Subnets.EnableIPv6)
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("ipv4_subnet_id", Subnets.SubnetId)
d.Set("ipv6_subnet_id", Subnets.IPv6SubnetId)
d.Set("ipv6_cidr", Subnets.IPv6CIDR)
d.Set("ipv6_gateway", Subnets.IPv6Gateway)
d.Set("region", hcsConfig.GetRegion(d))
return nil
}