/
data_source_tc_nats.go
167 lines (148 loc) · 4.04 KB
/
data_source_tc_nats.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
package tencentcloud
import (
"encoding/json"
"fmt"
"log"
"github.com/hashicorp/terraform/helper/schema"
"github.com/zqfan/tencentcloud-sdk-go/common"
vpc "github.com/zqfan/tencentcloud-sdk-go/services/vpc/unversioned"
)
func dataSourceTencentCloudNats() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudNatsRead,
Schema: map[string]*schema.Schema{
"id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"vpc_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateStringLengthInRange(1, 60),
},
"state": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
},
"max_concurrent": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
},
"bandwidth": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
},
// Computed values
"nats": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"vpc_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"state": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
},
"max_concurrent": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
},
"bandwidth": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
},
"assigned_eip_set": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"create_time": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
func dataSourceTencentCloudNatsRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*TencentCloudClient).vpcConn
args := vpc.NewDescribeNatGatewayRequest()
args.Offset = common.IntPtr(0)
args.Limit = common.IntPtr(50)
if v, ok := d.GetOk("id"); ok {
args.NatId = common.StringPtr(v.(string))
}
if v, ok := d.GetOk("vpc_id"); ok {
args.VpcId = common.StringPtr(v.(string))
}
if v, ok := d.GetOk("name"); ok {
args.NatName = common.StringPtr(v.(string))
}
response, err := conn.DescribeNatGateway(args)
b, _ := json.Marshal(response)
log.Printf("[DEBUG] conn.DescribeNatGateway response: %s", b)
if _, ok := err.(*common.APIError); ok {
return fmt.Errorf("conn.DescribeNatGateway error: %v", err)
} else if err != nil {
return err
} else if response == nil || len(response.Data) == 0 {
return fmt.Errorf("no matching NAT gateway found: %v", args)
}
var s []map[string]interface{}
var ids []string
for _, nat := range response.Data {
if state, ok := d.GetOk("state"); ok && *nat.State != state.(int) {
continue
}
if max_concurrent, ok := d.GetOk("max_concurrent"); ok && *nat.MaxConcurrent != max_concurrent.(int) {
continue
}
if bandwidth, ok := d.GetOk("bandwidth"); ok && *nat.Bandwidth != bandwidth.(int) {
continue
}
mapping := map[string]interface{}{
"id": *nat.NatId,
"vpc_id": *nat.UnVpcId,
"name": *nat.NatName,
"state": *nat.State,
"max_concurrent": *nat.MaxConcurrent,
"bandwidth": *nat.Bandwidth,
"create_time": *nat.CreateTime,
}
var eips []string
if len(nat.EipSet) > 0 {
for _, eip := range nat.EipSet {
eips = append(eips, *eip)
}
}
mapping["assigned_eip_set"] = eips
log.Printf("[DEBUG] tencentcloud_nat - adding nat: %v", mapping)
s = append(s, mapping)
ids = append(ids, *nat.NatId)
}
if len(ids) < 1 {
return fmt.Errorf("Your query returned no results. Please change your search criteria and try again.")
}
d.SetId(dataResourceIdsHash(ids))
if err := d.Set("nats", s); err != nil {
return err
}
return nil
}