@@ -2,56 +2,36 @@ package cvm
2
2
3
3
import (
4
4
"context"
5
- "log"
6
-
7
- tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
8
- svctag "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tag"
9
- svcvpc "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/vpc"
10
5
11
6
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
12
7
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
13
8
vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
14
9
10
+ tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
15
11
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
16
12
)
17
13
18
14
func DataSourceTencentCloudEips () * schema.Resource {
19
15
return & schema.Resource {
20
16
Read : dataSourceTencentCloudEipsRead ,
21
-
22
17
Schema : map [string ]* schema.Schema {
23
18
"eip_id" : {
24
19
Type : schema .TypeString ,
25
20
Optional : true ,
26
21
Description : "ID of the EIP to be queried." ,
27
22
},
28
- "eip_name" : {
29
- Type : schema .TypeString ,
30
- Optional : true ,
31
- Description : "Name of the EIP to be queried." ,
32
- },
33
- "public_ip" : {
34
- Type : schema .TypeString ,
35
- Optional : true ,
36
- Description : "The elastic ip address." ,
37
- },
38
- "tags" : {
39
- Type : schema .TypeMap ,
40
- Optional : true ,
41
- Description : "The tags of EIP." ,
42
- },
43
- "result_output_file" : {
44
- Type : schema .TypeString ,
45
- Optional : true ,
46
- Description : "Used to save results." ,
47
- },
48
23
49
24
"eip_list" : {
50
25
Type : schema .TypeList ,
51
26
Computed : true ,
52
27
Description : "An information list of EIP. Each element contains the following attributes:" ,
53
28
Elem : & schema.Resource {
54
29
Schema : map [string ]* schema.Schema {
30
+ "create_time" : {
31
+ Type : schema .TypeString ,
32
+ Computed : true ,
33
+ Description : "Creation time of the EIP." ,
34
+ },
55
35
"eip_id" : {
56
36
Type : schema .TypeString ,
57
37
Computed : true ,
@@ -67,30 +47,25 @@ func DataSourceTencentCloudEips() *schema.Resource {
67
47
Computed : true ,
68
48
Description : "Type of the EIP." ,
69
49
},
70
- "status" : {
71
- Type : schema .TypeString ,
72
- Computed : true ,
73
- Description : "The EIP current status." ,
74
- },
75
- "public_ip" : {
50
+ "eni_id" : {
76
51
Type : schema .TypeString ,
77
52
Computed : true ,
78
- Description : "The elastic ip address ." ,
53
+ Description : "The eni id to bind with the EIP ." ,
79
54
},
80
55
"instance_id" : {
81
56
Type : schema .TypeString ,
82
57
Computed : true ,
83
58
Description : "The instance id to bind with the EIP." ,
84
59
},
85
- "eni_id " : {
60
+ "public_ip " : {
86
61
Type : schema .TypeString ,
87
62
Computed : true ,
88
- Description : "The eni id to bind with the EIP ." ,
63
+ Description : "The elastic ip address ." ,
89
64
},
90
- "create_time " : {
65
+ "status " : {
91
66
Type : schema .TypeString ,
92
67
Computed : true ,
93
- Description : "Creation time of the EIP ." ,
68
+ Description : "The EIP current status ." ,
94
69
},
95
70
"tags" : {
96
71
Type : schema .TypeMap ,
@@ -100,91 +75,101 @@ func DataSourceTencentCloudEips() *schema.Resource {
100
75
},
101
76
},
102
77
},
78
+
79
+ "eip_name" : {
80
+ Type : schema .TypeString ,
81
+ Optional : true ,
82
+ Description : "Name of the EIP to be queried." ,
83
+ },
84
+
85
+ "public_ip" : {
86
+ Type : schema .TypeString ,
87
+ Optional : true ,
88
+ Description : "The elastic ip address." ,
89
+ },
90
+
91
+ "tags" : {
92
+ Type : schema .TypeMap ,
93
+ Optional : true ,
94
+ Description : "The tags of EIP." ,
95
+ },
96
+
97
+ "result_output_file" : {
98
+ Type : schema .TypeString ,
99
+ Optional : true ,
100
+ Description : "Used to save results." ,
101
+ },
103
102
},
104
103
}
105
104
}
106
105
107
106
func dataSourceTencentCloudEipsRead (d * schema.ResourceData , meta interface {}) error {
108
107
defer tccommon .LogElapsed ("data_source.tencentcloud_eips.read" )()
109
- logId := tccommon .GetLogId (tccommon .ContextNil )
110
- ctx := context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
108
+ defer tccommon .InconsistentCheck (d , meta )()
109
+
110
+ logId := tccommon .GetLogId (nil )
111
+ ctx := tccommon .NewResourceLifeCycleHandleFuncContext (context .Background (), logId , d , meta )
111
112
112
- client := meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()
113
- vpcService := svcvpc .NewVpcService (client )
114
- tagService := svctag .NewTagService (client )
115
- region := client .Region
113
+ service := VpcService {client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()}
116
114
117
- filter := make (map [string ][]string )
115
+ paramMap := make (map [string ]interface {})
116
+ var filtersList []* vpc.Filter
117
+ filtersMap := map [string ]* vpc.Filter {}
118
+ filter := vpc.Filter {}
119
+ name := "address-id"
120
+ filter .Name = & name
118
121
if v , ok := d .GetOk ("eip_id" ); ok {
119
- filter [ "address-id" ] = []string {v .(string )}
122
+ filter . Values = []* string {helper . String ( v .(string ) )}
120
123
}
124
+ filtersMap ["Temp0" ] = & filter
125
+ if v , ok := filtersMap ["Temp0" ]; ok && len (v .Values ) > 0 {
126
+ filtersList = append (filtersList , v )
127
+ }
128
+ filter2 := vpc.Filter {}
129
+ name2 := "address-name"
130
+ filter2 .Name = & name2
121
131
if v , ok := d .GetOk ("eip_name" ); ok {
122
- filter ["address-name" ] = []string {v .(string )}
132
+ filter2 .Values = []* string {helper .String (v .(string ))}
133
+ }
134
+ filtersMap ["Temp1" ] = & filter2
135
+ if v , ok := filtersMap ["Temp1" ]; ok && len (v .Values ) > 0 {
136
+ filtersList = append (filtersList , v )
123
137
}
138
+ filter3 := vpc.Filter {}
139
+ name3 := "public-ip"
140
+ filter3 .Name = & name3
124
141
if v , ok := d .GetOk ("public_ip" ); ok {
125
- filter [ "public-ip" ] = []string {v .(string )}
142
+ filter3 . Values = []* string {helper . String ( v .(string ) )}
126
143
}
144
+ filtersMap ["Temp2" ] = & filter3
145
+ if v , ok := filtersMap ["Temp2" ]; ok && len (v .Values ) > 0 {
146
+ filtersList = append (filtersList , v )
147
+ }
148
+ paramMap ["Filters" ] = filtersList
127
149
128
- tags := helper .GetTags (d , "tags" )
129
-
130
- var eips []* vpc.Address
131
- var errRet error
150
+ var respData * vpc.DescribeAddressesResponseParams
132
151
err := resource .Retry (tccommon .ReadRetryTimeout , func () * resource.RetryError {
133
- eips , errRet = vpcService . DescribeEipByFilter (ctx , filter )
134
- if errRet != nil {
135
- return tccommon .RetryError (errRet , tccommon . InternalError )
152
+ result , e := service . DescribeEipsByFilter (ctx , paramMap )
153
+ if e != nil {
154
+ return tccommon .RetryError (e )
136
155
}
156
+ respData = result
137
157
return nil
138
158
})
139
159
if err != nil {
140
160
return err
141
161
}
142
162
143
- eipList := make ([]map [string ]interface {}, 0 , len (eips ))
144
- ids := make ([]string , 0 , len (eips ))
145
-
146
- EIP_LOOP:
147
- for _ , eip := range eips {
148
- respTags , err := tagService .DescribeResourceTags (ctx , svcvpc .VPC_SERVICE_TYPE , svcvpc .EIP_RESOURCE_TYPE , region , * eip .AddressId )
149
- if err != nil {
150
- log .Printf ("[CRITAL]%s describe eip tags failed: %+v" , logId , err )
151
- return err
152
- }
153
-
154
- for k , v := range tags {
155
- if respTags [k ] != v {
156
- continue EIP_LOOP
157
- }
158
- }
159
-
160
- mapping := map [string ]interface {}{
161
- "eip_id" : eip .AddressId ,
162
- "eip_name" : eip .AddressName ,
163
- "eip_type" : eip .AddressType ,
164
- "status" : eip .AddressStatus ,
165
- "public_ip" : eip .AddressIp ,
166
- "instance_id" : eip .InstanceId ,
167
- "eni_id" : eip .NetworkInterfaceId ,
168
- "create_time" : eip .CreatedTime ,
169
- "tags" : respTags ,
170
- }
171
-
172
- eipList = append (eipList , mapping )
173
- ids = append (ids , * eip .AddressId )
174
- }
175
-
176
- d .SetId (helper .DataResourceIdsHash (ids ))
177
- err = d .Set ("eip_list" , eipList )
178
- if err != nil {
179
- log .Printf ("[CRITAL]%s provider set eip list fail, reason:%s\n " , logId , err .Error ())
163
+ if err := dataSourceTencentCloudEipsReadPostHandleResponse0 (ctx , paramMap , respData ); err != nil {
180
164
return err
181
165
}
182
166
183
167
output , ok := d .GetOk ("result_output_file" )
184
168
if ok && output .(string ) != "" {
185
- if err := tccommon .WriteToFile (output .(string ), eipList ); err != nil {
186
- return err
169
+ if e := tccommon .WriteToFile (output .(string ), dataSourceTencentCloudEipsReadOutputContent ( ctx )); e != nil {
170
+ return e
187
171
}
188
172
}
173
+
189
174
return nil
190
175
}
0 commit comments