-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathposture_zones.go
85 lines (68 loc) · 2.22 KB
/
posture_zones.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
package v2
import (
"context"
"fmt"
"net/http"
)
const (
PostureZonesPath = "%s/api/cspm/v1/policy/zones"
PostureZonePath = "%s/api/cspm/v1/policy/zones/%d"
)
type PostureZoneInterface interface {
Base
CreateOrUpdatePostureZone(ctx context.Context, z *PostureZoneRequest) (*PostureZone, string, error)
GetPostureZone(ctx context.Context, id int) (*PostureZone, error)
DeletePostureZone(ctx context.Context, id int) error
}
func (client *Client) CreateOrUpdatePostureZone(ctx context.Context, r *PostureZoneRequest) (*PostureZone, string, error) {
if r.ID == "" {
r.ID = "0"
}
payload, err := Marshal(r)
if err != nil {
return nil, "", err
}
response, err := client.requester.Request(ctx, http.MethodPost, client.createPostureZoneURL(), payload)
if err != nil {
return nil, "", err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK && response.StatusCode != http.StatusCreated && response.StatusCode != http.StatusAccepted {
errStatus, err := client.ErrorAndStatusFromResponse(response)
return nil, errStatus, err
}
wrapper, err := Unmarshal[PostureZoneResponse](response.Body)
if err != nil {
return nil, "", err
}
return &wrapper.Data, "", nil
}
func (client *Client) GetPostureZone(ctx context.Context, id int) (*PostureZone, error) {
response, err := client.requester.Request(ctx, http.MethodGet, client.getPostureZoneURL(id), nil)
if err != nil {
return nil, err
}
defer response.Body.Close()
wrapper, err := Unmarshal[PostureZoneResponse](response.Body)
if err != nil {
return nil, err
}
return &wrapper.Data, nil
}
func (client *Client) DeletePostureZone(ctx context.Context, id int) error {
response, err := client.requester.Request(ctx, http.MethodDelete, client.getPostureZoneURL(id), nil)
if err != nil {
return err
}
defer response.Body.Close()
if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK && response.StatusCode != http.StatusNotFound {
return client.ErrorFromResponse(response)
}
return nil
}
func (client *Client) createPostureZoneURL() string {
return fmt.Sprintf(PostureZonesPath, client.config.url)
}
func (client *Client) getPostureZoneURL(id int) string {
return fmt.Sprintf(PostureZonePath, client.config.url, id)
}