Skip to content

Commit 710bff5

Browse files
author
anonymous
committed
feat: support application_service and unit
1 parent 20c440c commit 710bff5

File tree

5 files changed

+161
-34
lines changed

5 files changed

+161
-34
lines changed

tencentcloud/basic_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,3 +861,11 @@ const (
861861
)
862862

863863
// End of DTS
864+
865+
// TEM
866+
const (
867+
defaultEnvironmentId = "en-758oo2ej"
868+
defaultApplicationId = "app-joqr9bd5"
869+
)
870+
871+
// End of TEM

tencentcloud/resource_tc_tem_application_service.go

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,23 @@ Example Usage
55
66
```hcl
77
resource "tencentcloud_tem_application_service" "application_service" {
8-
environment_id = "en-xxx"
9-
application_id = "xxx"
8+
environment_id = "en-dpxyydl5"
9+
application_id = "app-jrl3346j"
1010
service {
1111
type = "CLUSTER"
12-
service_name = "consumer"
12+
service_name = "test0-1"
1313
port_mapping_item_list {
1414
port = 80
1515
target_port = 80
1616
protocol = "tcp"
1717
}
18-
1918
}
2019
}
2120
```
2221
2322
Import
2423
25-
tem application_service can be imported using the id, e.g.
24+
tem application_service can be imported using the environmentId#applicationId#serviceName, e.g.
2625
2726
```
2827
terraform import tencentcloud_tem_application_service.application_service application_service_id
@@ -122,6 +121,7 @@ func resourceTencentCloudTemApplicationServiceCreate(d *schema.ResourceData, met
122121
request = tem.NewCreateApplicationServiceRequest()
123122
environmentId string
124123
applicationId string
124+
serviceName string
125125
)
126126
if v, ok := d.GetOk("environment_id"); ok {
127127
environmentId = v.(string)
@@ -139,6 +139,7 @@ func resourceTencentCloudTemApplicationServiceCreate(d *schema.ResourceData, met
139139
servicePortMapping.Type = helper.String(v.(string))
140140
}
141141
if v, ok := dMap["service_name"]; ok {
142+
serviceName = v.(string)
142143
servicePortMapping.ServiceName = helper.String(v.(string))
143144
}
144145
if v, ok := dMap["port_mapping_item_list"]; ok {
@@ -174,7 +175,7 @@ func resourceTencentCloudTemApplicationServiceCreate(d *schema.ResourceData, met
174175
return err
175176
}
176177

177-
d.SetId(environmentId + FILED_SP + applicationId)
178+
d.SetId(environmentId + FILED_SP + applicationId + FILED_SP + serviceName)
178179

179180
return resourceTencentCloudTemApplicationServiceRead(d, meta)
180181
}
@@ -190,11 +191,12 @@ func resourceTencentCloudTemApplicationServiceRead(d *schema.ResourceData, meta
190191
service := TemService{client: meta.(*TencentCloudClient).apiV3Conn}
191192

192193
idSplit := strings.Split(d.Id(), FILED_SP)
193-
if len(idSplit) != 2 {
194+
if len(idSplit) != 3 {
194195
return fmt.Errorf("id is broken,%s", d.Id())
195196
}
196197
environmentId := idSplit[0]
197198
applicationId := idSplit[1]
199+
serviceName := idSplit[2]
198200

199201
res, err := service.DescribeTemApplicationServiceById(ctx, environmentId, applicationId)
200202
if err != nil {
@@ -209,7 +211,12 @@ func resourceTencentCloudTemApplicationServiceRead(d *schema.ResourceData, meta
209211
_ = d.Set("environment_id", environmentId)
210212
_ = d.Set("application_id", applicationId)
211213

212-
applicationService := res.Result.ServicePortMappingList[0]
214+
var applicationService *tem.ServicePortMapping
215+
for _, v := range res.Result.ServicePortMappingList {
216+
if *v.ServiceName == serviceName {
217+
applicationService = v
218+
}
219+
}
213220

214221
if applicationService != nil {
215222
serviceMap := map[string]interface{}{}
@@ -242,10 +249,13 @@ func resourceTencentCloudTemApplicationServiceRead(d *schema.ResourceData, meta
242249
portMappingItemListList = append(portMappingItemListList, portMappingItemListMap)
243250
}
244251

245-
serviceMap["port_mapping_item_list"] = []interface{}{portMappingItemListList}
252+
serviceMap["port_mapping_item_list"] = portMappingItemListList
246253
}
247254

248-
_ = d.Set("service", []interface{}{serviceMap})
255+
err = d.Set("service", []interface{}{serviceMap})
256+
if err != nil {
257+
return err
258+
}
249259
}
250260

251261
return nil
@@ -257,14 +267,24 @@ func resourceTencentCloudTemApplicationServiceUpdate(d *schema.ResourceData, met
257267

258268
logId := getLogId(contextNil)
259269

270+
unsupportedUpdateFields := []string{
271+
"service",
272+
}
273+
for _, field := range unsupportedUpdateFields {
274+
if d.HasChange(field) {
275+
return fmt.Errorf("tencentcloud_tem_application_service update on %s is not support yet", field)
276+
}
277+
}
278+
260279
request := tem.NewModifyApplicationServiceRequest()
261280

262281
idSplit := strings.Split(d.Id(), FILED_SP)
263-
if len(idSplit) != 2 {
282+
if len(idSplit) != 3 {
264283
return fmt.Errorf("id is broken,%s", d.Id())
265284
}
266285
environmentId := idSplit[0]
267286
applicationId := idSplit[1]
287+
serviceName := idSplit[2]
268288

269289
request.EnvironmentId = &environmentId
270290
request.ApplicationId = &applicationId
@@ -274,9 +294,8 @@ func resourceTencentCloudTemApplicationServiceUpdate(d *schema.ResourceData, met
274294
if v, ok := dMap["type"]; ok {
275295
servicePortMapping.Type = helper.String(v.(string))
276296
}
277-
if v, ok := dMap["service_name"]; ok {
278-
servicePortMapping.ServiceName = helper.String(v.(string))
279-
}
297+
298+
servicePortMapping.ServiceName = &serviceName
280299
if v, ok := dMap["port_mapping_item_list"]; ok {
281300
for _, item := range v.([]interface{}) {
282301
portMappingItemListMap := item.(map[string]interface{})
@@ -323,14 +342,15 @@ func resourceTencentCloudTemApplicationServiceDelete(d *schema.ResourceData, met
323342

324343
service := TemService{client: meta.(*TencentCloudClient).apiV3Conn}
325344
idSplit := strings.Split(d.Id(), FILED_SP)
326-
if len(idSplit) != 2 {
345+
if len(idSplit) != 3 {
327346
return fmt.Errorf("id is broken,%s", d.Id())
328347
}
329348
environmentId := idSplit[0]
330349
applicationId := idSplit[1]
350+
serviceName := idSplit[2]
331351

332-
if err := service.DeleteTemApplicationServiceById(ctx, environmentId, applicationId); err != nil {
333-
return nil
352+
if err := service.DeleteTemApplicationServiceById(ctx, environmentId, applicationId, serviceName); err != nil {
353+
return err
334354
}
335355

336356
return nil
Lines changed: 110 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,40 @@
11
package tencentcloud
22

33
import (
4-
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
4+
"context"
5+
"fmt"
6+
"strings"
57
"testing"
8+
9+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
10+
"github.com/hashicorp/terraform-plugin-sdk/terraform"
11+
tem "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tem/v20210701"
612
)
713

14+
// go test -i; go test -test.run TestAccTencentCloudTemApplicationServiceResource_basic -v
815
func TestAccTencentCloudTemApplicationServiceResource_basic(t *testing.T) {
916
t.Parallel()
1017
resource.Test(t, resource.TestCase{
1118
PreCheck: func() {
1219
testAccPreCheck(t)
1320
},
14-
Providers: testAccProviders,
21+
Providers: testAccProviders,
22+
CheckDestroy: testAccCheckTemApplicationServiceDestroy,
1523
Steps: []resource.TestStep{
1624
{
1725
Config: testAccTemApplicationService,
18-
Check: resource.ComposeTestCheckFunc(resource.TestCheckResourceAttrSet("tencentcloud_tem_application_service.application_service", "id")),
26+
Check: resource.ComposeTestCheckFunc(
27+
testAccCheckTemApplicationServiceExists("tencentcloud_tem_application_service.application_service"),
28+
resource.TestCheckResourceAttr("tencentcloud_tem_application_service.application_service", "environment_id", defaultEnvironmentId),
29+
resource.TestCheckResourceAttr("tencentcloud_tem_application_service.application_service", "application_id", defaultApplicationId),
30+
resource.TestCheckResourceAttr("tencentcloud_tem_application_service.application_service", "service.#", "1"),
31+
resource.TestCheckResourceAttr("tencentcloud_tem_application_service.application_service", "service.0.type", "CLUSTER"),
32+
resource.TestCheckResourceAttr("tencentcloud_tem_application_service.application_service", "service.0.service_name", "terraform-test-0"),
33+
resource.TestCheckResourceAttr("tencentcloud_tem_application_service.application_service", "service.0.port_mapping_item_list.#", "1"),
34+
resource.TestCheckResourceAttr("tencentcloud_tem_application_service.application_service", "service.0.port_mapping_item_list.0.port", "80"),
35+
resource.TestCheckResourceAttr("tencentcloud_tem_application_service.application_service", "service.0.port_mapping_item_list.0.target_port", "80"),
36+
resource.TestCheckResourceAttr("tencentcloud_tem_application_service.application_service", "service.0.port_mapping_item_list.0.protocol", "TCP"),
37+
),
1938
},
2039
{
2140
ResourceName: "tencentcloud_tem_application_service.application_service",
@@ -26,21 +45,101 @@ func TestAccTencentCloudTemApplicationServiceResource_basic(t *testing.T) {
2645
})
2746
}
2847

29-
const testAccTemApplicationService = `
48+
func testAccCheckTemApplicationServiceDestroy(s *terraform.State) error {
49+
logId := getLogId(contextNil)
50+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
51+
service := TemService{client: testAccProvider.Meta().(*TencentCloudClient).apiV3Conn}
52+
for _, rs := range s.RootModule().Resources {
53+
if rs.Type != "tencentcloud_tem_application_service" {
54+
continue
55+
}
56+
idSplit := strings.Split(rs.Primary.ID, FILED_SP)
57+
if len(idSplit) != 3 {
58+
return fmt.Errorf("id is broken,%s", rs.Primary.ID)
59+
}
60+
environmentId := idSplit[0]
61+
applicationId := idSplit[1]
62+
serviceName := idSplit[2]
63+
64+
res, err := service.DescribeTemApplicationServiceById(ctx, environmentId, applicationId)
65+
if res == nil || res.Result == nil {
66+
for _, v := range res.Result.ServicePortMappingList {
67+
if *v.ServiceName == serviceName {
68+
return fmt.Errorf("tem applicationService %s still exists", rs.Primary.ID)
69+
}
70+
}
71+
}
72+
if err != nil {
73+
return err
74+
}
75+
}
76+
return nil
77+
}
78+
79+
func testAccCheckTemApplicationServiceExists(r string) resource.TestCheckFunc {
80+
return func(s *terraform.State) error {
81+
logId := getLogId(contextNil)
82+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
83+
84+
rs, ok := s.RootModule().Resources[r]
85+
if !ok {
86+
return fmt.Errorf("resource %s is not found", r)
87+
}
88+
89+
idSplit := strings.Split(rs.Primary.ID, FILED_SP)
90+
if len(idSplit) != 3 {
91+
return fmt.Errorf("id is broken,%s", rs.Primary.ID)
92+
}
93+
environmentId := idSplit[0]
94+
applicationId := idSplit[1]
95+
serviceName := idSplit[2]
96+
97+
service := TemService{client: testAccProvider.Meta().(*TencentCloudClient).apiV3Conn}
98+
res, err := service.DescribeTemApplicationServiceById(ctx, environmentId, applicationId)
99+
100+
if res == nil || res.Result == nil {
101+
var applicationService *tem.ServicePortMapping
102+
for _, v := range res.Result.ServicePortMappingList {
103+
if *v.ServiceName == serviceName {
104+
applicationService = v
105+
}
106+
}
107+
if applicationService == nil {
108+
return fmt.Errorf("tem applicationService %s is not found", rs.Primary.ID)
109+
}
110+
return nil
111+
}
112+
if err != nil {
113+
return err
114+
}
115+
116+
return nil
117+
}
118+
}
119+
120+
const testAccTemApplicationServiceVar = `
121+
variable "environment_id" {
122+
default = "` + defaultEnvironmentId + `"
123+
}
124+
variable "application_id" {
125+
default = "` + defaultApplicationId + `"
126+
}
127+
`
128+
129+
const testAccTemApplicationService = testAccTemApplicationServiceVar + `
30130
31131
resource "tencentcloud_tem_application_service" "application_service" {
32-
environment_id = "en-xxx"
33-
application_id = "xxx"
34-
service {
132+
environment_id = var.environment_id
133+
application_id = var.application_id
134+
service {
35135
type = "CLUSTER"
36-
service_name = "consumer"
136+
service_name = "terraform-test-0"
37137
port_mapping_item_list {
38138
port = 80
39139
target_port = 80
40-
protocol = "tcp"
140+
protocol = "TCP"
41141
}
42-
43-
}
142+
}
44143
}
45144
46145
`

tencentcloud/service_tencentcloud_tem.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,12 +495,13 @@ func (me *TemService) DescribeTemApplicationServiceById(ctx context.Context, env
495495
return
496496
}
497497

498-
func (me *TemService) DeleteTemApplicationServiceById(ctx context.Context, environmentId string, applicationId string) (errRet error) {
498+
func (me *TemService) DeleteTemApplicationServiceById(ctx context.Context, environmentId string, applicationId string, serviceName string) (errRet error) {
499499
logId := getLogId(ctx)
500500

501501
request := tem.NewDeleteApplicationServiceRequest()
502502
request.EnvironmentId = &environmentId
503503
request.ApplicationId = &applicationId
504+
request.ServiceName = &serviceName
504505

505506
defer func() {
506507
if errRet != nil {

website/docs/r/tem_application_service.html.markdown

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,16 @@ Provides a resource to create a tem application_service
1515

1616
```hcl
1717
resource "tencentcloud_tem_application_service" "application_service" {
18-
environment_id = "en-xxx"
19-
application_id = "xxx"
18+
environment_id = "en-dpxyydl5"
19+
application_id = "app-jrl3346j"
2020
service {
2121
type = "CLUSTER"
22-
service_name = "consumer"
22+
service_name = "test0-1"
2323
port_mapping_item_list {
2424
port = 80
2525
target_port = 80
2626
protocol = "tcp"
2727
}
28-
2928
}
3029
}
3130
```
@@ -60,7 +59,7 @@ In addition to all arguments above, the following attributes are exported:
6059

6160
## Import
6261

63-
tem application_service can be imported using the id, e.g.
62+
tem application_service can be imported using the environmentId#applicationId#serviceName, e.g.
6463

6564
```
6665
terraform import tencentcloud_tem_application_service.application_service application_service_id

0 commit comments

Comments
 (0)