|
1 | 1 | package dtrack |
2 | 2 |
|
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + |
| 8 | + "github.com/google/uuid" |
| 9 | +) |
| 10 | + |
3 | 11 | type Tag struct { |
4 | 12 | Name string `json:"name"` |
5 | 13 | } |
| 14 | + |
| 15 | +type TagService struct { |
| 16 | + client *Client |
| 17 | +} |
| 18 | + |
| 19 | +type TagListResponseItem struct { |
| 20 | + Name string `json:"name,omitempty"` |
| 21 | + ProjectCount int64 `json:"projectCount,omitempty"` |
| 22 | + PolicyCount int64 `json:"policyCount,omitempty"` |
| 23 | + NotificationRuleCount int64 `json:"notificationRuleCount,omitempty"` |
| 24 | +} |
| 25 | + |
| 26 | +type TaggedProjectListResponseItem struct { |
| 27 | + UUID uuid.UUID `json:"uuid,omitempty"` |
| 28 | + Name string `json:"name,omitempty"` |
| 29 | + Version string `json:"version,omitempty"` |
| 30 | +} |
| 31 | + |
| 32 | +type TaggedPolicyListResponseItem struct { |
| 33 | + UUID uuid.UUID `json:"uuid,omitempty"` |
| 34 | + Name string `json:"name,omitempty"` |
| 35 | +} |
| 36 | + |
| 37 | +func (ts TagService) Create(ctx context.Context, names []string) (err error) { |
| 38 | + err = ts.client.assertServerVersionAtLeast("4.13.0") |
| 39 | + if err != nil { |
| 40 | + return |
| 41 | + } |
| 42 | + |
| 43 | + req, err := ts.client.newRequest(ctx, http.MethodPut, "/api/v1/tag", withBody(names)) |
| 44 | + if err != nil { |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + _, err = ts.client.doRequest(req, nil) |
| 49 | + return |
| 50 | +} |
| 51 | + |
| 52 | +func (ts TagService) GetAll(ctx context.Context, po PageOptions, so SortOptions) (p Page[TagListResponseItem], err error) { |
| 53 | + err = ts.client.assertServerVersionAtLeast("4.12.0") |
| 54 | + if err != nil { |
| 55 | + return |
| 56 | + } |
| 57 | + |
| 58 | + req, err := ts.client.newRequest(ctx, http.MethodGet, "/api/v1/tag", withPageOptions(po), withSortOptions(so)) |
| 59 | + if err != nil { |
| 60 | + return |
| 61 | + } |
| 62 | + |
| 63 | + res, err := ts.client.doRequest(req, &p.Items) |
| 64 | + if err != nil { |
| 65 | + return |
| 66 | + } |
| 67 | + |
| 68 | + p.TotalCount = res.TotalCount |
| 69 | + return |
| 70 | +} |
| 71 | + |
| 72 | +func (ts TagService) Delete(ctx context.Context, names []string) (err error) { |
| 73 | + err = ts.client.assertServerVersionAtLeast("4.12.0") |
| 74 | + if err != nil { |
| 75 | + return |
| 76 | + } |
| 77 | + |
| 78 | + req, err := ts.client.newRequest(ctx, http.MethodDelete, "/api/v1/tag", withBody(names)) |
| 79 | + if err != nil { |
| 80 | + return |
| 81 | + } |
| 82 | + |
| 83 | + _, err = ts.client.doRequest(req, nil) |
| 84 | + return |
| 85 | +} |
| 86 | + |
| 87 | +func (ts TagService) TagProjects(ctx context.Context, tag string, projects []uuid.UUID) (err error) { |
| 88 | + err = ts.client.assertServerVersionAtLeast("4.12.0") |
| 89 | + if err != nil { |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + req, err := ts.client.newRequest(ctx, http.MethodPost, fmt.Sprintf("/api/v1/tag/%s/project", tag), withBody(projects)) |
| 94 | + if err != nil { |
| 95 | + return |
| 96 | + } |
| 97 | + _, err = ts.client.doRequest(req, nil) |
| 98 | + return |
| 99 | +} |
| 100 | + |
| 101 | +func (ts TagService) UntagProjects(ctx context.Context, tag string, projects []uuid.UUID) (err error) { |
| 102 | + err = ts.client.assertServerVersionAtLeast("4.12.0") |
| 103 | + if err != nil { |
| 104 | + return |
| 105 | + } |
| 106 | + |
| 107 | + req, err := ts.client.newRequest(ctx, http.MethodDelete, fmt.Sprintf("/api/v1/tag/%s/project", tag), withBody(projects)) |
| 108 | + if err != nil { |
| 109 | + return |
| 110 | + } |
| 111 | + _, err = ts.client.doRequest(req, nil) |
| 112 | + return |
| 113 | +} |
| 114 | + |
| 115 | +func (ts TagService) GetProjects(ctx context.Context, tag string, po PageOptions, so SortOptions) (p Page[TaggedProjectListResponseItem], err error) { |
| 116 | + err = ts.client.assertServerVersionAtLeast("4.12.0") |
| 117 | + if err != nil { |
| 118 | + return |
| 119 | + } |
| 120 | + |
| 121 | + req, err := ts.client.newRequest(ctx, http.MethodGet, fmt.Sprintf("/api/v1/tag/%s/project", tag), withPageOptions(po), withSortOptions(so)) |
| 122 | + if err != nil { |
| 123 | + return |
| 124 | + } |
| 125 | + |
| 126 | + res, err := ts.client.doRequest(req, &p.Items) |
| 127 | + if err != nil { |
| 128 | + return |
| 129 | + } |
| 130 | + |
| 131 | + p.TotalCount = res.TotalCount |
| 132 | + return |
| 133 | +} |
| 134 | + |
| 135 | +func (ts TagService) TagPolicies(ctx context.Context, tag string, policies []uuid.UUID) (err error) { |
| 136 | + err = ts.client.assertServerVersionAtLeast("4.12.0") |
| 137 | + if err != nil { |
| 138 | + return |
| 139 | + } |
| 140 | + |
| 141 | + req, err := ts.client.newRequest(ctx, http.MethodPost, fmt.Sprintf("/api/v1/tag/%s/policy", tag), withBody(policies)) |
| 142 | + if err != nil { |
| 143 | + return |
| 144 | + } |
| 145 | + _, err = ts.client.doRequest(req, nil) |
| 146 | + return |
| 147 | +} |
| 148 | + |
| 149 | +func (ts TagService) UntagPolicies(ctx context.Context, tag string, policies []uuid.UUID) (err error) { |
| 150 | + err = ts.client.assertServerVersionAtLeast("4.12.0") |
| 151 | + if err != nil { |
| 152 | + return |
| 153 | + } |
| 154 | + |
| 155 | + req, err := ts.client.newRequest(ctx, http.MethodDelete, fmt.Sprintf("/api/v1/tag/%s/policy", tag), withBody(policies)) |
| 156 | + if err != nil { |
| 157 | + return |
| 158 | + } |
| 159 | + _, err = ts.client.doRequest(req, nil) |
| 160 | + return |
| 161 | +} |
| 162 | + |
| 163 | +func (ts TagService) GetPolicies(ctx context.Context, tag string, po PageOptions, so SortOptions) (p Page[TaggedPolicyListResponseItem], err error) { |
| 164 | + err = ts.client.assertServerVersionAtLeast("4.12.0") |
| 165 | + if err != nil { |
| 166 | + return |
| 167 | + } |
| 168 | + |
| 169 | + req, err := ts.client.newRequest(ctx, http.MethodGet, fmt.Sprintf("/api/v1/tag/%s/policy", tag), withPageOptions(po), withSortOptions(so)) |
| 170 | + if err != nil { |
| 171 | + return |
| 172 | + } |
| 173 | + |
| 174 | + res, err := ts.client.doRequest(req, &p.Items) |
| 175 | + if err != nil { |
| 176 | + return |
| 177 | + } |
| 178 | + |
| 179 | + p.TotalCount = res.TotalCount |
| 180 | + return |
| 181 | +} |
| 182 | + |
| 183 | +func (ts TagService) TagNotificationRules(ctx context.Context, tag string, rules []uuid.UUID) (err error) { |
| 184 | + err = ts.client.assertServerVersionAtLeast("4.12.0") |
| 185 | + if err != nil { |
| 186 | + return |
| 187 | + } |
| 188 | + |
| 189 | + req, err := ts.client.newRequest(ctx, http.MethodPost, fmt.Sprintf("/api/v1/tag/%s/notificationRule", tag), withBody(rules)) |
| 190 | + if err != nil { |
| 191 | + return |
| 192 | + } |
| 193 | + _, err = ts.client.doRequest(req, nil) |
| 194 | + return |
| 195 | +} |
| 196 | + |
| 197 | +func (ts TagService) UntagNotificationRules(ctx context.Context, tag string, rules []uuid.UUID) (err error) { |
| 198 | + err = ts.client.assertServerVersionAtLeast("4.12.0") |
| 199 | + if err != nil { |
| 200 | + return |
| 201 | + } |
| 202 | + |
| 203 | + req, err := ts.client.newRequest(ctx, http.MethodDelete, fmt.Sprintf("/api/v1/tag/%s/notificationRule", tag), withBody(rules)) |
| 204 | + if err != nil { |
| 205 | + return |
| 206 | + } |
| 207 | + _, err = ts.client.doRequest(req, nil) |
| 208 | + return |
| 209 | +} |
| 210 | + |
| 211 | +func (ts TagService) GetNotificationRules(ctx context.Context, tag string, po PageOptions, so SortOptions) (p Page[TaggedPolicyListResponseItem], err error) { |
| 212 | + err = ts.client.assertServerVersionAtLeast("4.12.0") |
| 213 | + if err != nil { |
| 214 | + return |
| 215 | + } |
| 216 | + |
| 217 | + req, err := ts.client.newRequest(ctx, http.MethodGet, fmt.Sprintf("/api/v1/tag/%s/notificationRule", tag), withPageOptions(po), withSortOptions(so)) |
| 218 | + if err != nil { |
| 219 | + return |
| 220 | + } |
| 221 | + |
| 222 | + res, err := ts.client.doRequest(req, &p.Items) |
| 223 | + if err != nil { |
| 224 | + return |
| 225 | + } |
| 226 | + |
| 227 | + p.TotalCount = res.TotalCount |
| 228 | + return |
| 229 | +} |
| 230 | + |
| 231 | +func (ts TagService) GetTagsForPolicy(ctx context.Context, policy uuid.UUID, po PageOptions, so SortOptions) (p Page[Tag], err error) { |
| 232 | + var req *http.Request |
| 233 | + if ts.client.isServerVersionAtLeast("4.12.0") { |
| 234 | + req, err = ts.client.newRequest(ctx, http.MethodGet, fmt.Sprintf("/api/v1/tag/policy/%s", policy), withPageOptions(po), withSortOptions(so)) |
| 235 | + } else { |
| 236 | + err = ts.client.assertServerVersionAtLeast("4.6.0") |
| 237 | + if err != nil { |
| 238 | + return |
| 239 | + } |
| 240 | + |
| 241 | + req, err = ts.client.newRequest(ctx, http.MethodGet, fmt.Sprintf("/api/v1/tag/%s", policy), withPageOptions(po), withSortOptions(so)) |
| 242 | + } |
| 243 | + if err != nil { |
| 244 | + return |
| 245 | + } |
| 246 | + |
| 247 | + res, err := ts.client.doRequest(req, &p.Items) |
| 248 | + if err != nil { |
| 249 | + return |
| 250 | + } |
| 251 | + |
| 252 | + p.TotalCount = res.TotalCount |
| 253 | + return |
| 254 | + |
| 255 | +} |
0 commit comments