Skip to content

Commit 50f5d63

Browse files
authored
Merge pull request #1467 from tencentcloudstack/feat/add_temm_application_service
add application service
2 parents 79d5c8a + 0d32457 commit 50f5d63

File tree

8 files changed

+637
-0
lines changed

8 files changed

+637
-0
lines changed

.changelog/1467.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-resource
2+
tencentcloud_tem_application_service
3+
```

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/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,7 @@ TencentCloud Elastic Microservice(TEM)
685685
tencentcloud_tem_log_config
686686
tencentcloud_tem_scale_rule
687687
tencentcloud_tem_gateway
688+
tencentcloud_tem_application_service
688689
689690
TencentCloud EdgeOne(TEO)
690691
Data Source
@@ -1440,6 +1441,7 @@ func Provider() terraform.ResourceProvider {
14401441
"tencentcloud_tem_log_config": resourceTencentCloudTemLogConfig(),
14411442
"tencentcloud_tem_scale_rule": resourceTencentCloudTemScaleRule(),
14421443
"tencentcloud_tem_gateway": resourceTencentCloudTemGateway(),
1444+
"tencentcloud_tem_application_service": resourceTencentCloudTemApplicationService(),
14431445
"tencentcloud_teo_zone": resourceTencentCloudTeoZone(),
14441446
"tencentcloud_teo_zone_setting": resourceTencentCloudTeoZoneSetting(),
14451447
"tencentcloud_teo_dns_record": resourceTencentCloudTeoDnsRecord(),
Lines changed: 357 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,357 @@
1+
/*
2+
Provides a resource to create a tem application_service
3+
4+
Example Usage
5+
6+
```hcl
7+
resource "tencentcloud_tem_application_service" "application_service" {
8+
environment_id = "en-dpxyydl5"
9+
application_id = "app-jrl3346j"
10+
service {
11+
type = "CLUSTER"
12+
service_name = "test0-1"
13+
port_mapping_item_list {
14+
port = 80
15+
target_port = 80
16+
protocol = "tcp"
17+
}
18+
}
19+
}
20+
```
21+
22+
Import
23+
24+
tem application_service can be imported using the environmentId#applicationId#serviceName, e.g.
25+
26+
```
27+
terraform import tencentcloud_tem_application_service.application_service en-dpxyydl5#app-jrl3346j#test0-1
28+
```
29+
*/
30+
package tencentcloud
31+
32+
import (
33+
"context"
34+
"fmt"
35+
"log"
36+
"strings"
37+
38+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
39+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
40+
tem "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tem/v20210701"
41+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
42+
)
43+
44+
func resourceTencentCloudTemApplicationService() *schema.Resource {
45+
return &schema.Resource{
46+
Create: resourceTencentCloudTemApplicationServiceCreate,
47+
Read: resourceTencentCloudTemApplicationServiceRead,
48+
Update: resourceTencentCloudTemApplicationServiceUpdate,
49+
Delete: resourceTencentCloudTemApplicationServiceDelete,
50+
Importer: &schema.ResourceImporter{
51+
State: schema.ImportStatePassthrough,
52+
},
53+
Schema: map[string]*schema.Schema{
54+
"environment_id": {
55+
Required: true,
56+
Type: schema.TypeString,
57+
Description: "environment ID.",
58+
},
59+
60+
"application_id": {
61+
Required: true,
62+
Type: schema.TypeString,
63+
Description: "application ID.",
64+
},
65+
66+
"service": {
67+
Optional: true,
68+
Type: schema.TypeList,
69+
MaxItems: 1,
70+
Description: "service detail list.",
71+
Elem: &schema.Resource{
72+
Schema: map[string]*schema.Schema{
73+
"type": {
74+
Type: schema.TypeString,
75+
Optional: true,
76+
Description: "application service type: EXTERNAL | VPC | CLUSTER.",
77+
},
78+
"service_name": {
79+
Type: schema.TypeString,
80+
Optional: true,
81+
Description: "application service name.",
82+
},
83+
"port_mapping_item_list": {
84+
Type: schema.TypeList,
85+
Optional: true,
86+
Description: "port mapping item list.",
87+
Elem: &schema.Resource{
88+
Schema: map[string]*schema.Schema{
89+
"port": {
90+
Type: schema.TypeInt,
91+
Optional: true,
92+
Description: "container port.",
93+
},
94+
"target_port": {
95+
Type: schema.TypeInt,
96+
Optional: true,
97+
Description: "application listen port.",
98+
},
99+
"protocol": {
100+
Type: schema.TypeString,
101+
Optional: true,
102+
Description: "udp or tcp.",
103+
},
104+
},
105+
},
106+
},
107+
},
108+
},
109+
},
110+
},
111+
}
112+
}
113+
114+
func resourceTencentCloudTemApplicationServiceCreate(d *schema.ResourceData, meta interface{}) error {
115+
defer logElapsed("resource.tencentcloud_tem_application_service.create")()
116+
defer inconsistentCheck(d, meta)()
117+
118+
logId := getLogId(contextNil)
119+
120+
var (
121+
request = tem.NewCreateApplicationServiceRequest()
122+
environmentId string
123+
applicationId string
124+
serviceName string
125+
)
126+
if v, ok := d.GetOk("environment_id"); ok {
127+
environmentId = v.(string)
128+
request.EnvironmentId = helper.String(v.(string))
129+
}
130+
131+
if v, ok := d.GetOk("application_id"); ok {
132+
applicationId = v.(string)
133+
request.ApplicationId = helper.String(v.(string))
134+
}
135+
136+
if dMap, ok := helper.InterfacesHeadMap(d, "service"); ok {
137+
servicePortMapping := tem.ServicePortMapping{}
138+
if v, ok := dMap["type"]; ok {
139+
servicePortMapping.Type = helper.String(v.(string))
140+
}
141+
if v, ok := dMap["service_name"]; ok {
142+
serviceName = v.(string)
143+
servicePortMapping.ServiceName = helper.String(v.(string))
144+
}
145+
if v, ok := dMap["port_mapping_item_list"]; ok {
146+
for _, item := range v.([]interface{}) {
147+
portMappingItemListMap := item.(map[string]interface{})
148+
servicePortMappingItem := tem.ServicePortMappingItem{}
149+
if v, ok := portMappingItemListMap["port"]; ok {
150+
servicePortMappingItem.Port = helper.IntInt64(v.(int))
151+
}
152+
if v, ok := portMappingItemListMap["target_port"]; ok {
153+
servicePortMappingItem.TargetPort = helper.IntInt64(v.(int))
154+
}
155+
if v, ok := portMappingItemListMap["protocol"]; ok {
156+
servicePortMappingItem.Protocol = helper.String(v.(string))
157+
}
158+
servicePortMapping.PortMappingItemList = append(servicePortMapping.PortMappingItemList, &servicePortMappingItem)
159+
}
160+
}
161+
request.Service = &servicePortMapping
162+
}
163+
164+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
165+
result, e := meta.(*TencentCloudClient).apiV3Conn.UseTemClient().CreateApplicationService(request)
166+
if e != nil {
167+
return retryError(e)
168+
} else {
169+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
170+
}
171+
return nil
172+
})
173+
if err != nil {
174+
log.Printf("[CRITAL]%s create tem applicationService failed, reason:%+v", logId, err)
175+
return err
176+
}
177+
178+
d.SetId(environmentId + FILED_SP + applicationId + FILED_SP + serviceName)
179+
180+
return resourceTencentCloudTemApplicationServiceRead(d, meta)
181+
}
182+
183+
func resourceTencentCloudTemApplicationServiceRead(d *schema.ResourceData, meta interface{}) error {
184+
defer logElapsed("resource.tencentcloud_tem_application_service.read")()
185+
defer inconsistentCheck(d, meta)()
186+
187+
logId := getLogId(contextNil)
188+
189+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
190+
191+
service := TemService{client: meta.(*TencentCloudClient).apiV3Conn}
192+
193+
idSplit := strings.Split(d.Id(), FILED_SP)
194+
if len(idSplit) != 3 {
195+
return fmt.Errorf("id is broken,%s", d.Id())
196+
}
197+
environmentId := idSplit[0]
198+
applicationId := idSplit[1]
199+
serviceName := idSplit[2]
200+
201+
res, err := service.DescribeTemApplicationServiceById(ctx, environmentId, applicationId)
202+
if err != nil {
203+
return err
204+
}
205+
206+
if res == nil {
207+
d.SetId("")
208+
return fmt.Errorf("resource `track` %s does not exist", d.Id())
209+
}
210+
211+
_ = d.Set("environment_id", environmentId)
212+
_ = d.Set("application_id", applicationId)
213+
214+
var applicationService *tem.ServicePortMapping
215+
for _, v := range res.Result.ServicePortMappingList {
216+
if *v.ServiceName == serviceName {
217+
applicationService = v
218+
}
219+
}
220+
221+
if applicationService != nil {
222+
serviceMap := map[string]interface{}{}
223+
224+
if applicationService.Type != nil {
225+
serviceMap["type"] = applicationService.Type
226+
}
227+
228+
if applicationService.ServiceName != nil {
229+
serviceMap["service_name"] = applicationService.ServiceName
230+
}
231+
232+
if applicationService.PortMappingItemList != nil {
233+
portMappingItemListList := []interface{}{}
234+
for _, portMappingItemList := range applicationService.PortMappingItemList {
235+
portMappingItemListMap := map[string]interface{}{}
236+
237+
if portMappingItemList.Port != nil {
238+
portMappingItemListMap["port"] = portMappingItemList.Port
239+
}
240+
241+
if portMappingItemList.TargetPort != nil {
242+
portMappingItemListMap["target_port"] = portMappingItemList.TargetPort
243+
}
244+
245+
if portMappingItemList.Protocol != nil {
246+
portMappingItemListMap["protocol"] = portMappingItemList.Protocol
247+
}
248+
249+
portMappingItemListList = append(portMappingItemListList, portMappingItemListMap)
250+
}
251+
252+
serviceMap["port_mapping_item_list"] = portMappingItemListList
253+
}
254+
255+
err = d.Set("service", []interface{}{serviceMap})
256+
if err != nil {
257+
return err
258+
}
259+
}
260+
261+
return nil
262+
}
263+
264+
func resourceTencentCloudTemApplicationServiceUpdate(d *schema.ResourceData, meta interface{}) error {
265+
defer logElapsed("resource.tencentcloud_tem_application_service.update")()
266+
defer inconsistentCheck(d, meta)()
267+
268+
logId := getLogId(contextNil)
269+
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+
279+
request := tem.NewModifyApplicationServiceRequest()
280+
281+
idSplit := strings.Split(d.Id(), FILED_SP)
282+
if len(idSplit) != 3 {
283+
return fmt.Errorf("id is broken,%s", d.Id())
284+
}
285+
environmentId := idSplit[0]
286+
applicationId := idSplit[1]
287+
serviceName := idSplit[2]
288+
289+
request.EnvironmentId = &environmentId
290+
request.ApplicationId = &applicationId
291+
if d.HasChange("service") {
292+
if dMap, ok := helper.InterfacesHeadMap(d, "service"); ok {
293+
servicePortMapping := tem.ServicePortMapping{}
294+
if v, ok := dMap["type"]; ok {
295+
servicePortMapping.Type = helper.String(v.(string))
296+
}
297+
298+
servicePortMapping.ServiceName = &serviceName
299+
if v, ok := dMap["port_mapping_item_list"]; ok {
300+
for _, item := range v.([]interface{}) {
301+
portMappingItemListMap := item.(map[string]interface{})
302+
servicePortMappingItem := tem.ServicePortMappingItem{}
303+
if v, ok := portMappingItemListMap["port"]; ok {
304+
servicePortMappingItem.Port = helper.IntInt64(v.(int))
305+
}
306+
if v, ok := portMappingItemListMap["target_port"]; ok {
307+
servicePortMappingItem.TargetPort = helper.IntInt64(v.(int))
308+
}
309+
if v, ok := portMappingItemListMap["protocol"]; ok {
310+
servicePortMappingItem.Protocol = helper.String(v.(string))
311+
}
312+
servicePortMapping.PortMappingItemList = append(servicePortMapping.PortMappingItemList, &servicePortMappingItem)
313+
}
314+
}
315+
request.Data = &servicePortMapping
316+
}
317+
}
318+
319+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
320+
result, e := meta.(*TencentCloudClient).apiV3Conn.UseTemClient().ModifyApplicationService(request)
321+
if e != nil {
322+
return retryError(e)
323+
} else {
324+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
325+
}
326+
return nil
327+
})
328+
if err != nil {
329+
log.Printf("[CRITAL]%s create tem applicationService failed, reason:%+v", logId, err)
330+
return err
331+
}
332+
333+
return resourceTencentCloudTemApplicationServiceRead(d, meta)
334+
}
335+
336+
func resourceTencentCloudTemApplicationServiceDelete(d *schema.ResourceData, meta interface{}) error {
337+
defer logElapsed("resource.tencentcloud_tem_application_service.delete")()
338+
defer inconsistentCheck(d, meta)()
339+
340+
logId := getLogId(contextNil)
341+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
342+
343+
service := TemService{client: meta.(*TencentCloudClient).apiV3Conn}
344+
idSplit := strings.Split(d.Id(), FILED_SP)
345+
if len(idSplit) != 3 {
346+
return fmt.Errorf("id is broken,%s", d.Id())
347+
}
348+
environmentId := idSplit[0]
349+
applicationId := idSplit[1]
350+
serviceName := idSplit[2]
351+
352+
if err := service.DeleteTemApplicationServiceById(ctx, environmentId, applicationId, serviceName); err != nil {
353+
return err
354+
}
355+
356+
return nil
357+
}

0 commit comments

Comments
 (0)