-
Notifications
You must be signed in to change notification settings - Fork 134
/
resource_tc_api_gateway_api_app_attachment.go
186 lines (155 loc) · 5.54 KB
/
resource_tc_api_gateway_api_app_attachment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package apigateway
import (
"context"
"fmt"
"log"
"strings"
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
apigateway "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apigateway/v20180808"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)
func ResourceTencentCloudAPIGatewayApiAppAttachment() *schema.Resource {
return &schema.Resource{
Create: resourceTencentCloudAPIGatewayApiAppAttachmentCreate,
Read: resourceTencentCloudAPIGatewayApiAppAttachmentRead,
Delete: resourceTencentCloudAPIGatewayApiAppAttachmentDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"api_app_id": {
Required: true,
ForceNew: true,
Type: schema.TypeString,
Description: "The unique ID of the application to be bound.",
},
"environment": {
Required: true,
ForceNew: true,
Type: schema.TypeString,
Description: "The environment to be bound.",
},
"service_id": {
Required: true,
ForceNew: true,
Type: schema.TypeString,
Description: "The unique ID of the service to be bound.",
},
"api_id": {
Required: true,
ForceNew: true,
Type: schema.TypeString,
Description: "The unique ID of the API to be bound.",
},
},
}
}
func resourceTencentCloudAPIGatewayApiAppAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_api_gateway_api_app_attachment.create")()
defer tccommon.InconsistentCheck(d, meta)()
var (
logId = tccommon.GetLogId(tccommon.ContextNil)
request = apigateway.NewBindApiAppRequest()
apiAppId string
environment string
serviceId string
apiId string
)
if v, ok := d.GetOk("api_app_id"); ok {
request.ApiAppId = helper.String(v.(string))
apiAppId = v.(string)
}
if v, ok := d.GetOk("environment"); ok {
request.Environment = helper.String(v.(string))
environment = v.(string)
}
if v, ok := d.GetOk("service_id"); ok {
request.ServiceId = helper.String(v.(string))
serviceId = v.(string)
}
if v, ok := d.GetOk("api_id"); ok {
request.ApiId = helper.String(v.(string))
apiId = v.(string)
}
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseAPIGatewayClient().BindApiApp(request)
if e != nil {
return tccommon.RetryError(e)
} else {
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
}
if !*result.Response.Result {
e = fmt.Errorf(" create apigateway apiAppAttachment result: false.")
return resource.NonRetryableError(e)
}
return nil
})
if err != nil {
log.Printf("[CRITAL]%s create apigateway apiAppAttachment failed, reason:%+v", logId, err)
return err
}
d.SetId(strings.Join([]string{apiAppId, environment, serviceId, apiId}, tccommon.FILED_SP))
return resourceTencentCloudAPIGatewayApiAppAttachmentRead(d, meta)
}
func resourceTencentCloudAPIGatewayApiAppAttachmentRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_api_gateway_api_app_attachment.read")()
defer tccommon.InconsistentCheck(d, meta)()
var (
logId = tccommon.GetLogId(tccommon.ContextNil)
ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
service = APIGatewayService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
)
idSplit := strings.Split(d.Id(), tccommon.FILED_SP)
if len(idSplit) != 4 {
return fmt.Errorf("api_gateway_api_app_attachment id is broken, id is %s", d.Id())
}
apiAppId := idSplit[0]
environment := idSplit[1]
serviceId := idSplit[2]
apiId := idSplit[3]
apiAppAttachment, err := service.DescribeAPIGatewayApiAppAttachmentById(ctx, apiAppId, environment, serviceId, apiId)
if err != nil {
return err
}
if apiAppAttachment == nil {
d.SetId("")
log.Printf("[WARN]%s resource `ApigatewayApiAppAttachment` [%s] not found, please check if it has been deleted.\n", logId, d.Id())
return nil
}
if apiAppAttachment.ApiAppId != nil {
_ = d.Set("api_app_id", apiAppAttachment.ApiAppId)
}
if apiAppAttachment.EnvironmentName != nil {
_ = d.Set("environment", apiAppAttachment.EnvironmentName)
}
if apiAppAttachment.ServiceId != nil {
_ = d.Set("service_id", apiAppAttachment.ServiceId)
}
if apiAppAttachment.ApiId != nil {
_ = d.Set("api_id", apiAppAttachment.ApiId)
}
return nil
}
func resourceTencentCloudAPIGatewayApiAppAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_api_gateway_api_app_attachment.delete")()
defer tccommon.InconsistentCheck(d, meta)()
var (
logId = tccommon.GetLogId(tccommon.ContextNil)
ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
service = APIGatewayService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
)
idSplit := strings.Split(d.Id(), tccommon.FILED_SP)
if len(idSplit) != 4 {
return fmt.Errorf("api_gateway_api_app_attachment id is broken, id is %s", d.Id())
}
apiAppId := idSplit[0]
environment := idSplit[1]
serviceId := idSplit[2]
apiId := idSplit[3]
if err := service.DeleteAPIGatewayApiAppAttachmentById(ctx, apiAppId, environment, serviceId, apiId); err != nil {
return err
}
return nil
}