Skip to content

Commit a12fc3d

Browse files
authored
Merge pull request #164 from Sherlock-Holo/gaap-import-support
Add more import support for gaap resources
2 parents c77e6e4 + 7c66794 commit a12fc3d

8 files changed

+213
-130
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
## 1.20.2 (Unreleased)
2+
3+
ENHANCEMENTS:
4+
5+
* Resource: `tencentcloud_gaap_http_domain` support import
6+
* Resource: `tencentcloud_gaap_layer7_listener` support import
7+
28
## 1.20.1 (October 08, 2019)
39

410
ENHANCEMENTS:

tencentcloud/data_source_tc_gaap_layer7_listeners.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,10 @@ func dataSourceTencentCloudGaapLayer7ListenersRead(d *schema.ResourceData, m int
144144
ctx := context.WithValue(context.TODO(), "logId", logId)
145145

146146
protocol := d.Get("protocol").(string)
147-
proxyId := d.Get("proxy_id").(string)
147+
var proxyId *string
148+
if raw, ok := d.GetOk("proxy_id"); ok {
149+
proxyId = stringToPointer(raw.(string))
150+
}
148151

149152
var (
150153
listenerId *string
@@ -168,7 +171,7 @@ func dataSourceTencentCloudGaapLayer7ListenersRead(d *schema.ResourceData, m int
168171

169172
switch protocol {
170173
case "HTTP":
171-
httpListeners, err := service.DescribeHTTPListeners(ctx, &proxyId, listenerId, name, port)
174+
httpListeners, err := service.DescribeHTTPListeners(ctx, proxyId, listenerId, name, port)
172175
if err != nil {
173176
return err
174177
}
@@ -206,7 +209,7 @@ func dataSourceTencentCloudGaapLayer7ListenersRead(d *schema.ResourceData, m int
206209
}
207210

208211
case "HTTPS":
209-
httpsListeners, err := service.DescribeHTTPSListeners(ctx, &proxyId, listenerId, name, port)
212+
httpsListeners, err := service.DescribeHTTPSListeners(ctx, proxyId, listenerId, name, port)
210213
if err != nil {
211214
return err
212215
}

tencentcloud/resource_tc_gaap_http_domain.go

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ resource "tencentcloud_gaap_http_domain" "foo" {
2424
domain = "www.qq.com"
2525
}
2626
```
27+
28+
Import
29+
30+
GAAP http domain can be imported using the id, e.g.
31+
32+
-> **NOTE:** The format of tencentcloud_gaap_http_domain id is `[listener-id]+[protocol]+[domain]`.
33+
34+
```
35+
$ terraform import tencentcloud_gaap_http_domain.foo listener-11112222+HTTP+www.qq.com
36+
```
2737
*/
2838
package tencentcloud
2939

@@ -43,6 +53,10 @@ func resourceTencentCloudGaapHttpDomain() *schema.Resource {
4353
Read: resourceTencentCloudGaapHttpDomainRead,
4454
Update: resourceTencentCloudGaapHttpDomainUpdate,
4555
Delete: resourceTencentCloudGaapHttpDomainDelete,
56+
Importer: &schema.ResourceImporter{
57+
State: schema.ImportStatePassthrough,
58+
},
59+
4660
Schema: map[string]*schema.Schema{
4761
"listener_id": {
4862
Type: schema.TypeString,
@@ -236,7 +250,6 @@ func resourceTencentCloudGaapHttpDomainRead(d *schema.ResourceData, m interface{
236250
id := d.Id()
237251
var (
238252
listenerId string
239-
protocol string
240253
domain string
241254
)
242255
split := strings.Split(id, "+")
@@ -247,7 +260,7 @@ func resourceTencentCloudGaapHttpDomainRead(d *schema.ResourceData, m interface{
247260
return nil
248261
}
249262

250-
listenerId, protocol, domain = split[0], split[1], split[2]
263+
listenerId, _, domain = split[0], split[1], split[2]
251264

252265
service := GaapService{client: m.(*TencentCloudClient).apiV3Conn}
253266

@@ -262,22 +275,7 @@ func resourceTencentCloudGaapHttpDomainRead(d *schema.ResourceData, m interface{
262275
}
263276

264277
d.Set("domain", httpDomain.Domain)
265-
266-
if protocol == "HTTP" {
267-
return nil
268-
}
269-
270-
listeners, err := service.DescribeHTTPSListeners(ctx, nil, &listenerId, nil, nil)
271-
if err != nil {
272-
return err
273-
}
274-
275-
// if listener doesn't exist, domain won't exist
276-
if len(listeners) == 0 {
277-
log.Printf("[DEBUG]%s domain listener doesn't exist", logId)
278-
d.SetId("")
279-
return nil
280-
}
278+
d.Set("listener_id", listenerId)
281279

282280
if httpDomain.CertificateId == nil {
283281
httpDomain.CertificateId = stringToPointer("default")

tencentcloud/resource_tc_gaap_http_domain_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ func TestAccTencentCloudGaapHttpDomain_basic(t *testing.T) {
3737
resource.TestCheckNoResourceAttr("tencentcloud_gaap_http_domain.foo", "gaap_auth_id"),
3838
),
3939
},
40+
{
41+
ResourceName: "tencentcloud_gaap_http_domain.foo",
42+
ImportState: true,
43+
ImportStateVerify: true,
44+
},
4045
},
4146
})
4247
}
@@ -66,6 +71,11 @@ func TestAccTencentCloudGaapHttpDomain_https(t *testing.T) {
6671
resource.TestCheckNoResourceAttr("tencentcloud_gaap_http_domain.foo", "gaap_auth_id"),
6772
),
6873
},
74+
{
75+
ResourceName: "tencentcloud_gaap_http_domain.foo",
76+
ImportState: true,
77+
ImportStateVerify: true,
78+
},
6979
},
7080
})
7181
}

tencentcloud/resource_tc_gaap_layer7_listener.go

Lines changed: 125 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ resource "tencentcloud_gaap_layer7_listener" "foo" {
1919
proxy_id = "${tencentcloud_gaap_proxy.foo.id}"
2020
}
2121
```
22+
23+
Import
24+
25+
GAAP layer7 listener can be imported using the id, e.g.
26+
27+
```
28+
$ terraform import tencentcloud_gaap_layer7_listener.foo listener-11112222
29+
```
2230
*/
2331
package tencentcloud
2432

@@ -37,6 +45,10 @@ func resourceTencentCloudGaapLayer7Listener() *schema.Resource {
3745
Read: resourceTencentCloudGaapLayer7ListenerRead,
3846
Update: resourceTencentCloudGaapLayer7ListenerUpdate,
3947
Delete: resourceTencentCloudGaapLayer7ListenerDelete,
48+
Importer: &schema.ResourceImporter{
49+
State: schema.ImportStatePassthrough,
50+
},
51+
4052
Schema: map[string]*schema.Schema{
4153
"protocol": {
4254
Type: schema.TypeString,
@@ -177,7 +189,6 @@ func resourceTencentCloudGaapLayer7ListenerRead(d *schema.ResourceData, m interf
177189
ctx := context.WithValue(context.TODO(), "logId", logId)
178190

179191
id := d.Id()
180-
proxyId := d.Get("proxy_id").(string)
181192
protocol := d.Get("protocol").(string)
182193
var (
183194
name string
@@ -192,110 +203,142 @@ func resourceTencentCloudGaapLayer7ListenerRead(d *schema.ResourceData, m interf
192203

193204
service := GaapService{client: m.(*TencentCloudClient).apiV3Conn}
194205

195-
switch protocol {
196-
case "HTTP":
197-
listeners, err := service.DescribeHTTPListeners(ctx, &proxyId, &id, nil, nil)
198-
if err != nil {
199-
return err
200-
}
206+
LOOP:
207+
for {
208+
switch protocol {
209+
case "HTTP":
210+
listeners, err := service.DescribeHTTPListeners(ctx, nil, &id, nil, nil)
211+
if err != nil {
212+
return err
213+
}
201214

202-
var listener *gaap.HTTPListener
203-
for _, l := range listeners {
204-
if l.ListenerId == nil {
205-
return errors.New("listener id is nil")
215+
var listener *gaap.HTTPListener
216+
for _, l := range listeners {
217+
if l.ListenerId == nil {
218+
return errors.New("listener id is nil")
219+
}
220+
if *l.ListenerId == id {
221+
listener = l
222+
break
223+
}
206224
}
207-
if *l.ListenerId == id {
208-
listener = l
209-
break
225+
226+
if listener == nil {
227+
d.SetId("")
228+
return nil
210229
}
211-
}
212230

213-
if listener == nil {
214-
d.SetId("")
215-
return nil
216-
}
231+
if listener.ListenerName == nil {
232+
return errors.New("listener name is nil")
233+
}
234+
name = *listener.ListenerName
217235

218-
if listener.ListenerName == nil {
219-
return errors.New("listener name is nil")
220-
}
221-
name = *listener.ListenerName
236+
if listener.Port == nil {
237+
return errors.New("listener port is nil")
238+
}
239+
port = int(*listener.Port)
222240

223-
if listener.Port == nil {
224-
return errors.New("listener port is nil")
225-
}
226-
port = int(*listener.Port)
241+
if listener.ListenerStatus == nil {
242+
return errors.New("listener status is nil")
243+
}
244+
status = int(*listener.ListenerStatus)
227245

228-
if listener.ListenerStatus == nil {
229-
return errors.New("listener status is nil")
230-
}
231-
status = int(*listener.ListenerStatus)
246+
if listener.CreateTime == nil {
247+
return errors.New("listener create time is nil")
248+
}
249+
createTime = formatUnixTime(*listener.CreateTime)
232250

233-
if listener.CreateTime == nil {
234-
return errors.New("listener create time is nil")
235-
}
236-
createTime = formatUnixTime(*listener.CreateTime)
251+
break LOOP
237252

238-
case "HTTPS":
239-
listeners, err := service.DescribeHTTPSListeners(ctx, &proxyId, &id, nil, nil)
240-
if err != nil {
241-
return err
242-
}
253+
case "HTTPS":
254+
listeners, err := service.DescribeHTTPSListeners(ctx, nil, &id, nil, nil)
255+
if err != nil {
256+
return err
257+
}
243258

244-
var listener *gaap.HTTPSListener
245-
for _, l := range listeners {
246-
if l.ListenerId == nil {
247-
return errors.New("listener id is nil")
259+
var listener *gaap.HTTPSListener
260+
for _, l := range listeners {
261+
if l.ListenerId == nil {
262+
return errors.New("listener id is nil")
263+
}
264+
if *l.ListenerId == id {
265+
listener = l
266+
break
267+
}
248268
}
249-
if *l.ListenerId == id {
250-
listener = l
251-
break
269+
270+
if listener == nil {
271+
d.SetId("")
272+
return nil
252273
}
253-
}
254274

255-
if listener == nil {
256-
d.SetId("")
257-
return nil
258-
}
275+
if listener.ListenerName == nil {
276+
return errors.New("listener name is nil")
277+
}
278+
name = *listener.ListenerName
259279

260-
if listener.ListenerName == nil {
261-
return errors.New("listener name is nil")
262-
}
263-
name = *listener.ListenerName
280+
if listener.Port == nil {
281+
return errors.New("listener port is nil")
282+
}
283+
port = int(*listener.Port)
264284

265-
if listener.Port == nil {
266-
return errors.New("listener port is nil")
267-
}
268-
port = int(*listener.Port)
285+
if listener.CertificateId == nil {
286+
return errors.New("listener certificate id is nil")
287+
}
288+
certificateId = *listener.CertificateId
269289

270-
if listener.CertificateId == nil {
271-
return errors.New("listener certificate id is nil")
272-
}
273-
certificateId = *listener.CertificateId
290+
if listener.ForwardProtocol == nil {
291+
return errors.New("listener forward protocol is nil")
292+
}
293+
forwardProtocol = listener.ForwardProtocol
274294

275-
if listener.ForwardProtocol == nil {
276-
return errors.New("listener forward protocol is nil")
277-
}
278-
forwardProtocol = listener.ForwardProtocol
295+
if listener.AuthType == nil {
296+
return errors.New("listener auth type is nil")
297+
}
298+
authType = common.IntPtr(int(*listener.AuthType))
279299

280-
if listener.AuthType == nil {
281-
return errors.New("listener auth type is nil")
282-
}
283-
authType = common.IntPtr(int(*listener.AuthType))
300+
if listener.ClientCertificateId != nil {
301+
clientCertificateId = *listener.ClientCertificateId
302+
}
284303

285-
if listener.ClientCertificateId != nil {
286-
clientCertificateId = *listener.ClientCertificateId
287-
}
304+
if listener.ListenerStatus == nil {
305+
return errors.New("listener status is nil")
306+
}
307+
status = int(*listener.ListenerStatus)
288308

289-
if listener.ListenerStatus == nil {
290-
return errors.New("listener status is nil")
291-
}
292-
status = int(*listener.ListenerStatus)
309+
if listener.CreateTime == nil {
310+
return errors.New("listener create time is nil")
311+
}
312+
createTime = formatUnixTime(*listener.CreateTime)
293313

294-
if listener.CreateTime == nil {
295-
return errors.New("listener create time is nil")
314+
break LOOP
315+
316+
case "":
317+
// import mode, need check protocol
318+
httpListeners, err := service.DescribeHTTPListeners(ctx, nil, &id, nil, nil)
319+
if err != nil {
320+
return err
321+
}
322+
if len(httpListeners) > 0 {
323+
protocol = "HTTP"
324+
continue
325+
}
326+
327+
httpsListeners, err := service.DescribeHTTPSListeners(ctx, nil, &id, nil, nil)
328+
if err != nil {
329+
return err
330+
}
331+
if len(httpsListeners) > 0 {
332+
protocol = "HTTPS"
333+
continue
334+
}
335+
336+
// layer7 listener is not found
337+
d.SetId("")
338+
return nil
296339
}
297-
createTime = formatUnixTime(*listener.CreateTime)
298340
}
341+
d.Set("protocol", protocol)
299342

300343
d.Set("name", name)
301344
d.Set("port", port)

0 commit comments

Comments
 (0)