Skip to content

Commit 46cd68a

Browse files
committed
add application service
1 parent e795588 commit 46cd68a

File tree

6 files changed

+508
-0
lines changed

6 files changed

+508
-0
lines changed

tencentcloud/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,7 @@ TencentCloud Elastic Microservice(TEM)
680680
tencentcloud_tem_log_config
681681
tencentcloud_tem_scale_rule
682682
tencentcloud_tem_gateway
683+
tencentcloud_tem_application_service
683684
684685
TencentCloud EdgeOne(TEO)
685686
Data Source
@@ -1435,6 +1436,7 @@ func Provider() terraform.ResourceProvider {
14351436
"tencentcloud_tem_log_config": resourceTencentCloudTemLogConfig(),
14361437
"tencentcloud_tem_scale_rule": resourceTencentCloudTemScaleRule(),
14371438
"tencentcloud_tem_gateway": resourceTencentCloudTemGateway(),
1439+
"tencentcloud_tem_application_service": resourceTencentCloudTemApplicationService(),
14381440
"tencentcloud_teo_zone": resourceTencentCloudTeoZone(),
14391441
"tencentcloud_teo_zone_setting": resourceTencentCloudTeoZoneSetting(),
14401442
"tencentcloud_teo_dns_record": resourceTencentCloudTeoDnsRecord(),
Lines changed: 338 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,338 @@
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-xxx"
9+
application_id = "xxx"
10+
service {
11+
type = "CLUSTER"
12+
service_name = "consumer"
13+
port_mapping_item_list {
14+
port = 80
15+
target_port = 80
16+
protocol = "tcp"
17+
}
18+
19+
}
20+
}
21+
```
22+
23+
Import
24+
25+
tem application_service can be imported using the id, e.g.
26+
27+
```
28+
terraform import tencentcloud_tem_application_service.application_service application_service_id
29+
```
30+
*/
31+
package tencentcloud
32+
33+
import (
34+
"context"
35+
"fmt"
36+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
37+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
38+
tem "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tem/v20210701"
39+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
40+
"log"
41+
"strings"
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+
response = tem.NewCreateApplicationServiceResponse()
123+
environmentId string
124+
applicationId 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+
servicePortMapping.ServiceName = helper.String(v.(string))
143+
}
144+
if v, ok := dMap["port_mapping_item_list"]; ok {
145+
for _, item := range v.([]interface{}) {
146+
portMappingItemListMap := item.(map[string]interface{})
147+
servicePortMappingItem := tem.ServicePortMappingItem{}
148+
if v, ok := portMappingItemListMap["port"]; ok {
149+
servicePortMappingItem.Port = helper.IntInt64(v.(int))
150+
}
151+
if v, ok := portMappingItemListMap["target_port"]; ok {
152+
servicePortMappingItem.TargetPort = helper.IntInt64(v.(int))
153+
}
154+
if v, ok := portMappingItemListMap["protocol"]; ok {
155+
servicePortMappingItem.Protocol = helper.String(v.(string))
156+
}
157+
servicePortMapping.PortMappingItemList = append(servicePortMapping.PortMappingItemList, &servicePortMappingItem)
158+
}
159+
}
160+
request.Service = &servicePortMapping
161+
}
162+
163+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
164+
result, e := meta.(*TencentCloudClient).apiV3Conn.UseTemClient().CreateApplicationService(request)
165+
if e != nil {
166+
return retryError(e)
167+
} else {
168+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
169+
}
170+
response = result
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)
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) != 2 {
195+
return fmt.Errorf("id is broken,%s", d.Id())
196+
}
197+
environmentId := idSplit[0]
198+
applicationId := idSplit[1]
199+
200+
res, err := service.DescribeTemApplicationServiceById(ctx, environmentId, applicationId)
201+
if err != nil {
202+
return err
203+
}
204+
205+
if res == nil {
206+
d.SetId("")
207+
return fmt.Errorf("resource `track` %s does not exist", d.Id())
208+
}
209+
210+
_ = d.Set("environment_id", environmentId)
211+
_ = d.Set("application_id", applicationId)
212+
213+
applicationService := res.Result.ServicePortMappingList[0]
214+
215+
if applicationService != nil {
216+
serviceMap := map[string]interface{}{}
217+
218+
if applicationService.Type != nil {
219+
serviceMap["type"] = applicationService.Type
220+
}
221+
222+
if applicationService.ServiceName != nil {
223+
serviceMap["service_name"] = applicationService.ServiceName
224+
}
225+
226+
if applicationService.PortMappingItemList != nil {
227+
portMappingItemListList := []interface{}{}
228+
for _, portMappingItemList := range applicationService.PortMappingItemList {
229+
portMappingItemListMap := map[string]interface{}{}
230+
231+
if portMappingItemList.Port != nil {
232+
portMappingItemListMap["port"] = portMappingItemList.Port
233+
}
234+
235+
if portMappingItemList.TargetPort != nil {
236+
portMappingItemListMap["target_port"] = portMappingItemList.TargetPort
237+
}
238+
239+
if portMappingItemList.Protocol != nil {
240+
portMappingItemListMap["protocol"] = portMappingItemList.Protocol
241+
}
242+
243+
portMappingItemListList = append(portMappingItemListList, portMappingItemListMap)
244+
}
245+
246+
serviceMap["port_mapping_item_list"] = []interface{}{portMappingItemListList}
247+
}
248+
249+
_ = d.Set("service", []interface{}{serviceMap})
250+
}
251+
252+
return nil
253+
}
254+
255+
func resourceTencentCloudTemApplicationServiceUpdate(d *schema.ResourceData, meta interface{}) error {
256+
defer logElapsed("resource.tencentcloud_tem_application_service.update")()
257+
defer inconsistentCheck(d, meta)()
258+
259+
logId := getLogId(contextNil)
260+
261+
request := tem.NewModifyApplicationServiceRequest()
262+
263+
idSplit := strings.Split(d.Id(), FILED_SP)
264+
if len(idSplit) != 2 {
265+
return fmt.Errorf("id is broken,%s", d.Id())
266+
}
267+
environmentId := idSplit[0]
268+
applicationId := idSplit[1]
269+
270+
request.EnvironmentId = &environmentId
271+
request.ApplicationId = &applicationId
272+
if d.HasChange("service") {
273+
if dMap, ok := helper.InterfacesHeadMap(d, "service"); ok {
274+
servicePortMapping := tem.ServicePortMapping{}
275+
if v, ok := dMap["type"]; ok {
276+
servicePortMapping.Type = helper.String(v.(string))
277+
}
278+
if v, ok := dMap["service_name"]; ok {
279+
servicePortMapping.ServiceName = helper.String(v.(string))
280+
}
281+
if v, ok := dMap["port_mapping_item_list"]; ok {
282+
for _, item := range v.([]interface{}) {
283+
portMappingItemListMap := item.(map[string]interface{})
284+
servicePortMappingItem := tem.ServicePortMappingItem{}
285+
if v, ok := portMappingItemListMap["port"]; ok {
286+
servicePortMappingItem.Port = helper.IntInt64(v.(int))
287+
}
288+
if v, ok := portMappingItemListMap["target_port"]; ok {
289+
servicePortMappingItem.TargetPort = helper.IntInt64(v.(int))
290+
}
291+
if v, ok := portMappingItemListMap["protocol"]; ok {
292+
servicePortMappingItem.Protocol = helper.String(v.(string))
293+
}
294+
servicePortMapping.PortMappingItemList = append(servicePortMapping.PortMappingItemList, &servicePortMappingItem)
295+
}
296+
}
297+
request.Data = &servicePortMapping
298+
}
299+
}
300+
301+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
302+
result, e := meta.(*TencentCloudClient).apiV3Conn.UseTemClient().ModifyApplicationService(request)
303+
if e != nil {
304+
return retryError(e)
305+
} else {
306+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
307+
}
308+
return nil
309+
})
310+
if err != nil {
311+
log.Printf("[CRITAL]%s create tem applicationService failed, reason:%+v", logId, err)
312+
return err
313+
}
314+
315+
return resourceTencentCloudTemApplicationServiceRead(d, meta)
316+
}
317+
318+
func resourceTencentCloudTemApplicationServiceDelete(d *schema.ResourceData, meta interface{}) error {
319+
defer logElapsed("resource.tencentcloud_tem_application_service.delete")()
320+
defer inconsistentCheck(d, meta)()
321+
322+
logId := getLogId(contextNil)
323+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
324+
325+
service := TemService{client: meta.(*TencentCloudClient).apiV3Conn}
326+
idSplit := strings.Split(d.Id(), FILED_SP)
327+
if len(idSplit) != 2 {
328+
return fmt.Errorf("id is broken,%s", d.Id())
329+
}
330+
environmentId := idSplit[0]
331+
applicationId := idSplit[1]
332+
333+
if err := service.DeleteTemApplicationServiceById(ctx, environmentId, applicationId); err != nil {
334+
return nil
335+
}
336+
337+
return nil
338+
}

0 commit comments

Comments
 (0)