-
Notifications
You must be signed in to change notification settings - Fork 4
/
qid.go
135 lines (124 loc) · 3.72 KB
/
qid.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
package qradar
import (
"context"
"fmt"
"net/http"
)
// QIDService handles methods related to QIDs of the QRadar API.
type QIDService service
const (
qidAPIPrefix = "api/data_classification/qid_records"
)
// QID represents QRadar's QID.
type QID struct {
Severity *int `json:"severity,omitempty"`
Name *string `json:"name,omitempty"`
Description *string `json:"description,omitempty"`
LogSourceTypeID *int `json:"log_source_type_id,omitempty"`
ID *int `json:"id,omitempty"`
LowLevelCategoryID *int `json:"low_level_category_id,omitempty"`
QID *int `json:"qid,omitempty"`
UUID *string `json:"uuid,omitempty"`
}
// Get returns QIDs of the current QRadar installation.
func (c *QIDService) Get(ctx context.Context, fields, filter string, from, to int) ([]QID, error) {
req, err := c.client.requestHelp(http.MethodGet, qidAPIPrefix, fields, filter, from, to, nil, nil)
if err != nil {
return nil, err
}
var result []QID
_, err = c.client.Do(ctx, req, &result)
if err != nil {
return nil, err
}
return result, nil
}
// GetByID returns QID of the current QRadar installation by ID.
func (c *QIDService) GetByID(ctx context.Context, fields string, id int) (*QID, error) {
req, err := c.client.requestHelp(http.MethodGet, qidAPIPrefix, fields, "", 0, 0, &id, nil)
if err != nil {
return nil, err
}
var result QID
_, err = c.client.Do(ctx, req, &result)
if err != nil {
return nil, err
}
return &result, nil
}
// GetByQID returns QID of the current QRadar installation by QID.
func (c *QIDService) GetByQID(ctx context.Context, fields string, qid int) (*QID, error) {
req, err := c.client.requestHelp(http.MethodGet, qidAPIPrefix, fields, fmt.Sprintf("qid=%d", qid), 0, 0, nil, nil)
if err != nil {
return nil, err
}
var result []QID
_, err = c.client.Do(ctx, req, &result)
if err != nil {
return nil, err
}
if len(result) == 0 {
return nil, nil
}
if len(result) > 1 {
return nil, fmt.Errorf("found more elements than expected - %d", len(result))
}
return &result[0], nil
}
// Create creates QID in QRadar installation.
func (c *QIDService) Create(ctx context.Context, fields string, data interface{}) (*QID, error) {
req, err := c.client.requestHelp(http.MethodPost, qidAPIPrefix, fields, "", 0, 0, nil, data)
if err != nil {
return nil, err
}
var result QID
_, err = c.client.Do(ctx, req, &result)
if err != nil {
return nil, err
}
return &result, nil
}
// UpdateByID updates QID record in QRadar installation bu ID.
func (c *QIDService) UpdateByID(ctx context.Context, fields string, id int, data interface{}) (*QID, error) {
req, err := c.client.requestHelp(http.MethodPost, qidAPIPrefix, fields, "", 0, 0, &id, data)
if err != nil {
return nil, err
}
var result QID
_, err = c.client.Do(ctx, req, &result)
if err != nil {
return nil, err
}
return &result, nil
}
// GetByName returns QID of the current QRadar installation by name.
// If there are more than one QID that the same, this will returm the one with the least QID number
func (c *QIDService) GetByName(ctx context.Context, fields string, name string) (*QID, error) {
req, err := c.client.requestHelp(http.MethodGet, qidAPIPrefix, fields, fmt.Sprintf("name=\"%s\"", name), 0, 0, nil, nil)
if err != nil {
return nil, err
}
var result []QID
_, err = c.client.Do(ctx, req, &result)
if err != nil {
return nil, err
}
if len(result) == 0 {
return nil, nil
}
if len(result) > 1 {
return getQIDWithLeastQID(result), nil
}
return &result[0], nil
}
func getQIDWithLeastQID(blobs []QID) *QID {
res := 0
min := *blobs[0].QID
for i, blob := range blobs {
if blob.QID != nil && *blob.QID < min {
min = *blob.QID
res = i
}
}
return &blobs[res]
}