-
Notifications
You must be signed in to change notification settings - Fork 154
/
data_source_huaweicloud_dcs_hotkey_analyses.go
171 lines (158 loc) · 5.09 KB
/
data_source_huaweicloud_dcs_hotkey_analyses.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
// Generated by PMS #36
package dcs
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/tidwall/gjson"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/filters"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/httphelper"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/schemas"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
)
func DataSourceDcsHotkeyAnalyses() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceDcsHotkeyAnalysesRead,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: `Specifies the region in which to query the resource. If omitted, the provider-level region will be used.`,
},
"analysis_id": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the ID of the hot key analysis.`,
},
"instance_id": {
Type: schema.TypeString,
Required: true,
Description: `Specifies the ID of the DCS instance.`,
},
"scan_type": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the mode of the hot key analysis.`,
},
"status": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the status of the hot key analysis.`,
},
"records": {
Type: schema.TypeList,
Computed: true,
Description: `Indicates the list of hot key analysis records.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the id of the hot key analysis.`,
},
"scan_type": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the mode of the hot key analysis`,
},
"status": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the status of the hot key analysis.`,
},
"started_at": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the creation time of the hot key analysis. The value is in UTC format.`,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the time when the hot key analysis started. The value is in UTC format.`,
},
"finished_at": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the time when the hot key analysis ended. The value is in UTC format.`,
},
},
},
},
},
}
}
type HotkeyAnalysesDSWrapper struct {
*schemas.ResourceDataWrapper
Config *config.Config
}
func newHotkeyAnalysesDSWrapper(d *schema.ResourceData, meta interface{}) *HotkeyAnalysesDSWrapper {
return &HotkeyAnalysesDSWrapper{
ResourceDataWrapper: schemas.NewSchemaWrapper(d),
Config: meta.(*config.Config),
}
}
func dataSourceDcsHotkeyAnalysesRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
wrapper := newHotkeyAnalysesDSWrapper(d, meta)
rst, err := wrapper.ListHotKeyScanTasks()
if err != nil {
return diag.FromErr(err)
}
id, _ := uuid.GenerateUUID()
d.SetId(id)
err = wrapper.listHotKeyScanTasksToSchema(rst)
if err != nil {
return diag.FromErr(err)
}
return nil
}
// @API DCS GET /v2/{project_id}/instances/{instance_id}/hotkey-tasks
func (w *HotkeyAnalysesDSWrapper) ListHotKeyScanTasks() (*gjson.Result, error) {
client, err := w.NewClient(w.Config, "dcs")
if err != nil {
return nil, err
}
d := w.ResourceData
uri := "/v2/{project_id}/instances/{instance_id}/hotkey-tasks"
uri = strings.ReplaceAll(uri, "{instance_id}", d.Get("instance_id").(string))
params := map[string]any{
"status": w.Get("status"),
}
params = utils.RemoveNil(params)
return httphelper.New(client).
Method("GET").
URI(uri).
Query(params).
OffsetPager("records", "offset", "limit", 0).
Filter(
filters.New().
From("records").
Where("id", "=", w.Get("analysis_id")).
Where("scan_type", "=", w.Get("scan_type")),
).
Request().
Result()
}
func (w *HotkeyAnalysesDSWrapper) listHotKeyScanTasksToSchema(body *gjson.Result) error {
d := w.ResourceData
mErr := multierror.Append(nil,
d.Set("region", w.Config.GetRegion(w.ResourceData)),
d.Set("records", schemas.SliceToList(body.Get("records"),
func(records gjson.Result) any {
return map[string]any{
"id": records.Get("id").Value(),
"scan_type": records.Get("scan_type").Value(),
"status": records.Get("status").Value(),
"started_at": records.Get("started_at").Value(),
"created_at": records.Get("created_at").Value(),
"finished_at": records.Get("finished_at").Value(),
}
},
)),
)
return mErr.ErrorOrNil()
}