-
Notifications
You must be signed in to change notification settings - Fork 154
/
data_source_huaweicloud_dms_rabbitmq_plugins.go
194 lines (173 loc) · 5.36 KB
/
data_source_huaweicloud_dms_rabbitmq_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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package dms
import (
"context"
"strings"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/chnsz/golangsdk"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/common"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
)
// @API RabbitMQ GET /v2/{project_id}/instances/{instance_id}/rabbitmq/plugins
func DataSourceDmsRabbitmqPlugins() *schema.Resource {
return &schema.Resource{
ReadContext: resourceDmsRabbitmqPluginsRead,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"instance_id": {
Type: schema.TypeString,
Required: true,
Description: `Specifies the ID of the RabbitMQ instance.`,
},
"name": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the name of the plugin.`,
},
"enable": {
Type: schema.TypeBool,
Optional: true,
Description: `Specifies whether the plugin is enabled.`,
},
"running": {
Type: schema.TypeBool,
Optional: true,
Description: `Specifies whether the plugin is running.`,
},
"version": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the version of the plugin.`,
},
"plugins": {
Type: schema.TypeList,
Elem: pluginsSchema(),
Computed: true,
Description: `Indicates the list of the plugins.`,
},
},
}
}
func pluginsSchema() *schema.Resource {
sc := schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the name of the plugin.`,
},
"enable": {
Type: schema.TypeBool,
Computed: true,
Description: `Indicates whether the plugin is enabled.`,
},
"running": {
Type: schema.TypeBool,
Computed: true,
Description: `Indicates whether the plugin is running.`,
},
"version": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the version of the plugin.`,
},
},
}
return &sc
}
func resourceDmsRabbitmqPluginsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
var mErr *multierror.Error
// getRabbitmqPlugins: query DMS RabbitMQ plugins
var (
getRabbitmqPluginsHttpUrl = "v2/{project_id}/instances/{instance_id}/rabbitmq/plugins"
getRabbitmqPluginsProduct = "dms"
)
getRabbitmqPluginsClient, err := cfg.NewServiceClient(getRabbitmqPluginsProduct, region)
if err != nil {
return diag.Errorf("error creating DMS Client: %s", err)
}
instanceID := d.Get("instance_id").(string)
getRabbitmqPluginsPath := getRabbitmqPluginsClient.Endpoint + getRabbitmqPluginsHttpUrl
getRabbitmqPluginsPath = strings.ReplaceAll(getRabbitmqPluginsPath, "{project_id}",
getRabbitmqPluginsClient.ProjectID)
getRabbitmqPluginsPath = strings.ReplaceAll(getRabbitmqPluginsPath, "{instance_id}", instanceID)
getRabbitmqPluginsOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
}
getRabbitmqPluginsResp, err := getRabbitmqPluginsClient.Request("GET", getRabbitmqPluginsPath,
&getRabbitmqPluginsOpt)
if err != nil {
return common.CheckDeletedDiag(d, err, "error retrieving DMS RabbitMQ plugins")
}
getRabbitmqPluginsRespBody, err := utils.FlattenResponse(getRabbitmqPluginsResp)
if err != nil {
return diag.FromErr(err)
}
dataSourceId, err := uuid.GenerateUUID()
if err != nil {
return diag.Errorf("unable to generate ID: %s", err)
}
d.SetId(dataSourceId)
mErr = multierror.Append(
mErr,
d.Set("region", region),
d.Set("plugins", flattenPlugins(filterPlugins(d, getRabbitmqPluginsRespBody))),
)
return diag.FromErr(mErr.ErrorOrNil())
}
func flattenPlugins(pluginRespBody []interface{}) []interface{} {
if pluginRespBody == nil {
return nil
}
rst := make([]interface{}, 0, len(pluginRespBody))
for _, v := range pluginRespBody {
rst = append(rst, map[string]interface{}{
"name": utils.PathSearch("name", v, nil),
"enable": utils.PathSearch("enable", v, nil),
"running": utils.PathSearch("running", v, nil),
"version": utils.PathSearch("version", v, nil),
})
}
return rst
}
func filterPlugins(d *schema.ResourceData, resp interface{}) []interface{} {
pluginJson := utils.PathSearch("plugins", resp, make([]interface{}, 0))
pluginArray := pluginJson.([]interface{})
if len(pluginArray) < 1 {
return nil
}
result := make([]interface{}, 0, len(pluginArray))
rawName, rawNameOK := d.GetOk("name")
rawEnable := d.Get("enable")
rawRunning := d.Get("running")
rawVersion, rawVersionOK := d.GetOk("version")
for _, plugin := range pluginArray {
name := utils.PathSearch("name", plugin, nil)
enable := utils.PathSearch("enable", plugin, false).(bool)
running := utils.PathSearch("running", plugin, false).(bool)
version := utils.PathSearch("version", plugin, nil)
if rawNameOK && rawName != name {
continue
}
if rawEnable != enable {
continue
}
if rawRunning != running {
continue
}
if rawVersionOK && rawVersion != version {
continue
}
result = append(result, plugin)
}
return result
}