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