Skip to content

Commit fe7a0ae

Browse files
authored
Merge pull request #189 from Sherlock-Holo/eip-tag
add tags support for eip
2 parents 83bed84 + 734c36c commit fe7a0ae

File tree

9 files changed

+161
-16
lines changed

9 files changed

+161
-16
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
## 1.22.1 (Unreleased)
2+
3+
ENHANCEMENTS:
4+
5+
* Resource: `tencentcloud_eip` add optional argument `tags`.
6+
* Data Source: `tencentcloud_eips` add optional argument `tags`.
7+
28
BUG FIXES:
39
* Fixed docs of CAM
410

examples/tencentcloud-eip/main.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
resource "tencentcloud_eip" "foo" {
22
name = "awesome_eip_example"
3+
4+
tags = {
5+
"test" = "test"
6+
}
37
}
8+
9+
data "tencentcloud_eips" "tags" {
10+
tags = "${tencentcloud_eip.foo.tags}"
11+
}

tencentcloud/data_source_tc_eips.go

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ func dataSourceTencentCloudEips() *schema.Resource {
4040
Optional: true,
4141
Description: "The elastic ip address.",
4242
},
43+
"tags": {
44+
Type: schema.TypeMap,
45+
Optional: true,
46+
Description: "The tags of eip.",
47+
},
4348
"result_output_file": {
4449
Type: schema.TypeString,
4550
Optional: true,
@@ -92,6 +97,11 @@ func dataSourceTencentCloudEips() *schema.Resource {
9297
Computed: true,
9398
Description: "Creation time of the eip.",
9499
},
100+
"tags": {
101+
Type: schema.TypeMap,
102+
Computed: true,
103+
Description: "Tags of the eip.",
104+
},
95105
},
96106
},
97107
},
@@ -103,9 +113,11 @@ func dataSourceTencentCloudEipsRead(d *schema.ResourceData, meta interface{}) er
103113
defer logElapsed("data_source.tencentcloud_eips.read")()
104114
logId := getLogId(contextNil)
105115
ctx := context.WithValue(context.TODO(), "logId", logId)
106-
vpcService := VpcService{
107-
client: meta.(*TencentCloudClient).apiV3Conn,
108-
}
116+
117+
client := meta.(*TencentCloudClient).apiV3Conn
118+
vpcService := VpcService{client: client}
119+
tagService := TagService{client: client}
120+
region := client.Region
109121

110122
filter := make(map[string]string)
111123
if v, ok := d.GetOk("eip_id"); ok {
@@ -118,6 +130,8 @@ func dataSourceTencentCloudEipsRead(d *schema.ResourceData, meta interface{}) er
118130
filter["public-ip"] = v.(string)
119131
}
120132

133+
tags := getTags(d, "tags")
134+
121135
var eips []*vpc.Address
122136
var errRet error
123137
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
@@ -133,7 +147,21 @@ func dataSourceTencentCloudEipsRead(d *schema.ResourceData, meta interface{}) er
133147

134148
eipList := make([]map[string]interface{}, 0, len(eips))
135149
ids := make([]string, 0, len(eips))
150+
151+
EIP_LOOP:
136152
for _, eip := range eips {
153+
respTags, err := tagService.DescribeResourceTags(ctx, VPC_SERVICE_TYPE, EIP_RESOURCE_TYPE, region, *eip.AddressId)
154+
if err != nil {
155+
log.Printf("[CRITAL]%s describe eip tags failed: %+v", logId, err)
156+
return err
157+
}
158+
159+
for k, v := range tags {
160+
if respTags[k] != v {
161+
continue EIP_LOOP
162+
}
163+
}
164+
137165
mapping := map[string]interface{}{
138166
"eip_id": eip.AddressId,
139167
"eip_name": eip.AddressName,
@@ -143,7 +171,9 @@ func dataSourceTencentCloudEipsRead(d *schema.ResourceData, meta interface{}) er
143171
"instance_id": eip.InstanceId,
144172
"eni_id": eip.NetworkInterfaceId,
145173
"create_time": eip.CreatedTime,
174+
"tags": respTags,
146175
}
176+
147177
eipList = append(eipList, mapping)
148178
ids = append(ids, *eip.AddressId)
149179
}

tencentcloud/data_source_tc_eips_test.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@ func TestAccTencentCloudEipsDataSource(t *testing.T) {
1515
{
1616
Config: testAccEipsDataSource,
1717
Check: resource.ComposeTestCheckFunc(
18-
testAccCheckEipExists("tencentcloud_eip.eip"),
18+
testAccCheckTencentCloudDataSourceID("data.tencentcloud_eips.data_eips"),
1919
resource.TestCheckResourceAttr("data.tencentcloud_eips.data_eips", "eip_list.#", "1"),
2020
resource.TestCheckResourceAttrSet("data.tencentcloud_eips.data_eips", "eip_list.0.eip_id"),
2121
resource.TestCheckResourceAttr("data.tencentcloud_eips.data_eips", "eip_list.0.eip_name", "tf-test-eip"),
2222
resource.TestCheckResourceAttrSet("data.tencentcloud_eips.data_eips", "eip_list.0.eip_type"),
2323
resource.TestCheckResourceAttrSet("data.tencentcloud_eips.data_eips", "eip_list.0.status"),
2424
resource.TestCheckResourceAttrSet("data.tencentcloud_eips.data_eips", "eip_list.0.public_ip"),
2525
resource.TestCheckResourceAttrSet("data.tencentcloud_eips.data_eips", "eip_list.0.create_time"),
26+
27+
testAccCheckTencentCloudDataSourceID("data.tencentcloud_eips.tags"),
28+
resource.TestCheckResourceAttr("data.tencentcloud_eips.tags", "eip_list.0.tags.test", "test"),
2629
),
2730
},
2831
},
@@ -31,10 +34,18 @@ func TestAccTencentCloudEipsDataSource(t *testing.T) {
3134

3235
const testAccEipsDataSource = `
3336
resource "tencentcloud_eip" "eip" {
34-
name = "tf-test-eip"
37+
name = "tf-test-eip"
38+
39+
tags = {
40+
"test" = "test"
41+
}
3542
}
3643
3744
data "tencentcloud_eips" "data_eips" {
38-
eip_id = "${tencentcloud_eip.eip.id}"
45+
eip_id = "${tencentcloud_eip.eip.id}"
46+
}
47+
48+
data "tencentcloud_eips" "tags" {
49+
tags = "${tencentcloud_eip.eip.tags}"
3950
}
4051
`

tencentcloud/extension_vpc.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ var ALL_GATE_WAY_TYPES = []string{GATE_WAY_TYPE_CVM,
2323
GATE_WAY_TYPE_CCN,
2424
}
2525

26+
const VPC_SERVICE_TYPE = "vpc"
27+
2628
/*
2729
EIP
2830
*/
@@ -45,6 +47,8 @@ const (
4547
EIP_INTERNET_PROVIDER_CMCC = "CMCC"
4648
EIP_INTERNET_PROVIDER_CTCC = "CTCC"
4749
EIP_INTERNET_PROVIDER_CUCC = "CUCC"
50+
51+
EIP_RESOURCE_TYPE = "eip"
4852
)
4953

5054
var EIP_INTERNET_PROVIDER = []string{

tencentcloud/resource_tc_eip.go

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ func resourceTencentCloudEip() *schema.Resource {
9090
ValidateFunc: validateIntegerInRange(1, 1000),
9191
Description: "The bandwidth limit of eip, unit is Mbps, and the range is 1-1000.",
9292
},
93+
"tags": {
94+
Type: schema.TypeMap,
95+
Optional: true,
96+
Description: "The tags of eip.",
97+
},
9398

9499
// computed
95100
"public_ip": {
@@ -111,9 +116,11 @@ func resourceTencentCloudEipCreate(d *schema.ResourceData, meta interface{}) err
111116

112117
logId := getLogId(contextNil)
113118
ctx := context.WithValue(context.TODO(), "logId", logId)
114-
vpcService := VpcService{
115-
client: meta.(*TencentCloudClient).apiV3Conn,
116-
}
119+
120+
client := meta.(*TencentCloudClient).apiV3Conn
121+
vpcService := VpcService{client: client}
122+
tagService := TagService{client: client}
123+
region := client.Region
117124

118125
request := vpc.NewAllocateAddressesRequest()
119126
if v, ok := d.GetOk("type"); ok {
@@ -139,7 +146,7 @@ func resourceTencentCloudEipCreate(d *schema.ResourceData, meta interface{}) err
139146
eipId := ""
140147
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
141148
ratelimit.Check(request.GetAction())
142-
response, err := meta.(*TencentCloudClient).apiV3Conn.UseVpcClient().AllocateAddresses(request)
149+
response, err := client.UseVpcClient().AllocateAddresses(request)
143150
if err != nil {
144151
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n",
145152
logId, request.GetAction(), request.ToJsonString(), err.Error())
@@ -188,6 +195,14 @@ func resourceTencentCloudEipCreate(d *schema.ResourceData, meta interface{}) err
188195
}
189196
}
190197

198+
if tags := getTags(d, "tags"); len(tags) > 0 {
199+
resourceName := BuildTagResourceName(VPC_SERVICE_TYPE, EIP_RESOURCE_TYPE, region, eipId)
200+
if err := tagService.ModifyTags(ctx, resourceName, tags, nil); err != nil {
201+
log.Printf("[CRITAL]%s set eip tags failed: %+v", logId, err)
202+
return err
203+
}
204+
}
205+
191206
return resourceTencentCloudEipRead(d, meta)
192207
}
193208

@@ -196,9 +211,11 @@ func resourceTencentCloudEipRead(d *schema.ResourceData, meta interface{}) error
196211

197212
logId := getLogId(contextNil)
198213
ctx := context.WithValue(context.TODO(), "logId", logId)
199-
vpcService := VpcService{
200-
client: meta.(*TencentCloudClient).apiV3Conn,
201-
}
214+
215+
client := meta.(*TencentCloudClient).apiV3Conn
216+
vpcService := VpcService{client: client}
217+
tagService := TagService{client: client}
218+
region := client.Region
202219

203220
eipId := d.Id()
204221
var eip *vpc.Address
@@ -218,10 +235,17 @@ func resourceTencentCloudEipRead(d *schema.ResourceData, meta interface{}) error
218235
return nil
219236
}
220237

238+
tags, err := tagService.DescribeResourceTags(ctx, VPC_SERVICE_TYPE, EIP_RESOURCE_TYPE, region, eipId)
239+
if err != nil {
240+
log.Printf("[CRITAL]%s describe eip tags failed: %+v", logId, err)
241+
return err
242+
}
243+
221244
d.Set("name", eip.AddressName)
222245
d.Set("type", eip.AddressType)
223246
d.Set("public_ip", eip.AddressIp)
224247
d.Set("status", eip.AddressStatus)
248+
d.Set("tags", tags)
225249
return nil
226250
}
227251

@@ -230,18 +254,38 @@ func resourceTencentCloudEipUpdate(d *schema.ResourceData, meta interface{}) err
230254

231255
logId := getLogId(contextNil)
232256
ctx := context.WithValue(context.TODO(), "logId", logId)
233-
vpcService := VpcService{
234-
client: meta.(*TencentCloudClient).apiV3Conn,
235-
}
257+
258+
client := meta.(*TencentCloudClient).apiV3Conn
259+
vpcService := VpcService{client: client}
260+
tagService := TagService{client: client}
261+
region := client.Region
236262

237263
eipId := d.Id()
264+
265+
d.Partial(true)
266+
238267
if d.HasChange("name") {
239268
name := d.Get("name").(string)
240269
err := vpcService.ModifyEipName(ctx, eipId, name)
241270
if err != nil {
242271
return err
243272
}
273+
274+
d.SetPartial("name")
244275
}
276+
277+
if d.HasChange("tags") {
278+
oldTags, newTags := d.GetChange("tags")
279+
replaceTags, deleteTags := diffTags(oldTags.(map[string]interface{}), newTags.(map[string]interface{}))
280+
resourceName := BuildTagResourceName(VPC_SERVICE_TYPE, EIP_RESOURCE_TYPE, region, eipId)
281+
282+
if err := tagService.ModifyTags(ctx, resourceName, replaceTags, deleteTags); err != nil {
283+
log.Printf("[CRITAL]%s update eip tags failed: %+v", logId, err)
284+
return err
285+
}
286+
d.SetPartial("tags")
287+
}
288+
245289
return resourceTencentCloudEipRead(d, meta)
246290
}
247291

tencentcloud/resource_tc_eip_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,25 @@ func TestAccTencentCloudEip_basic(t *testing.T) {
3333
resource.TestCheckResourceAttrSet("tencentcloud_eip.foo", "public_ip"),
3434
),
3535
},
36+
{
37+
Config: testAccEipBasicWithTags,
38+
Check: resource.ComposeTestCheckFunc(
39+
testAccCheckEipExists("tencentcloud_eip.foo"),
40+
resource.TestCheckResourceAttr("tencentcloud_eip.foo", "tags.test", "test"),
41+
resource.TestCheckResourceAttr("tencentcloud_eip.foo", "status", "UNBIND"),
42+
resource.TestCheckResourceAttrSet("tencentcloud_eip.foo", "public_ip"),
43+
),
44+
},
45+
{
46+
Config: testAccEipBasicWithNewTags,
47+
Check: resource.ComposeTestCheckFunc(
48+
testAccCheckEipExists("tencentcloud_eip.foo"),
49+
resource.TestCheckNoResourceAttr("tencentcloud_eip.foo", "tags.test"),
50+
resource.TestCheckResourceAttr("tencentcloud_eip.foo", "tags.abc", "abc"),
51+
resource.TestCheckResourceAttr("tencentcloud_eip.foo", "status", "UNBIND"),
52+
resource.TestCheckResourceAttrSet("tencentcloud_eip.foo", "public_ip"),
53+
),
54+
},
3655
{
3756
Config: testAccEipBasicWithoutName,
3857
Check: resource.ComposeTestCheckFunc(
@@ -182,6 +201,26 @@ resource "tencentcloud_eip" "foo" {
182201
}
183202
`
184203

204+
const testAccEipBasicWithTags = `
205+
resource "tencentcloud_eip" "foo" {
206+
name = "new_name"
207+
208+
tags = {
209+
"test" = "test"
210+
}
211+
}
212+
`
213+
214+
const testAccEipBasicWithNewTags = `
215+
resource "tencentcloud_eip" "foo" {
216+
name = "new_name"
217+
218+
tags = {
219+
"abc" = "abc"
220+
}
221+
}
222+
`
223+
185224
const testAccEipBasicWithoutName = `
186225
resource "tencentcloud_eip" "bar" {
187226
}

website/docs/d/eips.html.markdown

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ The following arguments are supported:
2626
* `eip_name` - (Optional) Name of the eip to be queried.
2727
* `public_ip` - (Optional) The elastic ip address.
2828
* `result_output_file` - (Optional) Used to save results.
29+
* `tags` - (Optional) The tags of eip.
2930

3031
## Attributes Reference
3132

@@ -40,5 +41,6 @@ In addition to all arguments above, the following attributes are exported:
4041
* `instance_id` - The instance id to bind with the eip.
4142
* `public_ip` - The elastic ip address.
4243
* `status` - The eip current status.
44+
* `tags` - Tags of the eip.
4345

4446

website/docs/r/eip.html.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ The following arguments are supported:
2828
* `internet_max_bandwidth_out` - (Optional, ForceNew) The bandwidth limit of eip, unit is Mbps, and the range is 1-1000.
2929
* `internet_service_provider` - (Optional, ForceNew) Internet service provider of eip, and available values include `BGP`, `CMCC`, `CTCC` and `CUCC`.
3030
* `name` - (Optional) The name of eip.
31+
* `tags` - (Optional) The tags of eip.
3132
* `type` - (Optional, ForceNew) The type of eip, and available values include `EIP` and `AnycastEIP`. Default is `EIP`.
3233

3334
## Attributes Reference

0 commit comments

Comments
 (0)