-
Notifications
You must be signed in to change notification settings - Fork 133
/
data_source_tc_api_gateway_api_plugins.go
175 lines (150 loc) · 4.72 KB
/
data_source_tc_api_gateway_api_plugins.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
package apigateway
import (
"context"
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 DataSourceTencentCloudApiGatewayApiPlugins() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudApiGatewayApiPluginsRead,
Schema: map[string]*schema.Schema{
"api_id": {
Required: true,
Type: schema.TypeString,
Description: "API ID to be queried.",
},
"service_id": {
Required: true,
Type: schema.TypeString,
Description: "The service ID to be queried.",
},
"environment_name": {
Optional: true,
Type: schema.TypeString,
Description: "Environment information.",
},
"result": {
Computed: true,
Type: schema.TypeList,
Description: "API list information that the plug-in can bind.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"plugin_id": {
Type: schema.TypeString,
Computed: true,
Description: "Plugin ID.",
},
"environment": {
Type: schema.TypeString,
Computed: true,
Description: "Environment information.",
},
"attached_time": {
Type: schema.TypeString,
Computed: true,
Description: "Binding time.",
},
"plugin_name": {
Type: schema.TypeString,
Computed: true,
Description: "Plugin name.",
},
"plugin_type": {
Type: schema.TypeString,
Computed: true,
Description: "Plugin type.",
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: "Plugin description.",
},
"plugin_data": {
Type: schema.TypeString,
Computed: true,
Description: "Plug-in definition statement.",
},
},
},
},
"result_output_file": {
Type: schema.TypeString,
Optional: true,
Description: "Used to save results.",
},
},
}
}
func dataSourceTencentCloudApiGatewayApiPluginsRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("data_source.tencentcloud_api_gateway_api_plugins.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()}
apiPlugins []*apigateway.AttachedPluginInfo
)
paramMap := make(map[string]interface{})
if v, ok := d.GetOk("api_id"); ok {
paramMap["APIId"] = helper.String(v.(string))
}
if v, ok := d.GetOk("service_id"); ok {
paramMap["ServiceId"] = helper.String(v.(string))
}
if v, ok := d.GetOk("environment_name"); ok {
paramMap["EnvironmentName"] = helper.String(v.(string))
}
err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
result, e := service.DescribeApiGatewayApiPluginsByFilter(ctx, paramMap)
if e != nil {
return tccommon.RetryError(e)
}
apiPlugins = result
return nil
})
if err != nil {
return err
}
ids := make([]string, 0, len(apiPlugins))
tmpList := make([]map[string]interface{}, 0, len(apiPlugins))
if apiPlugins != nil {
for _, pluginSummary := range apiPlugins {
pluginSummaryMap := map[string]interface{}{}
if pluginSummary.PluginId != nil {
pluginSummaryMap["plugin_id"] = pluginSummary.PluginId
}
if pluginSummary.Environment != nil {
pluginSummaryMap["environment"] = pluginSummary.Environment
}
if pluginSummary.AttachedTime != nil {
pluginSummaryMap["attached_time"] = pluginSummary.AttachedTime
}
if pluginSummary.PluginName != nil {
pluginSummaryMap["plugin_name"] = pluginSummary.PluginName
}
if pluginSummary.PluginType != nil {
pluginSummaryMap["plugin_type"] = pluginSummary.PluginType
}
if pluginSummary.Description != nil {
pluginSummaryMap["description"] = pluginSummary.Description
}
if pluginSummary.PluginData != nil {
pluginSummaryMap["plugin_data"] = pluginSummary.PluginData
}
tmpList = append(tmpList, pluginSummaryMap)
ids = append(ids, *pluginSummary.PluginId)
}
_ = d.Set("result", tmpList)
}
d.SetId(helper.DataResourceIdsHash(ids))
output, ok := d.GetOk("result_output_file")
if ok && output.(string) != "" {
if e := tccommon.WriteToFile(output.(string), d); e != nil {
return e
}
}
return nil
}