|
| 1 | +package secure |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | +) |
| 9 | + |
| 10 | +func (client *sysdigSecureClient) scanningPoliciesURL() string { |
| 11 | + return fmt.Sprintf("%s/api/scanning/v1/policies", client.URL) |
| 12 | +} |
| 13 | + |
| 14 | +func (client *sysdigSecureClient) scanningPolicyAssignmentURL() string { |
| 15 | + return fmt.Sprintf("%s/api/scanning/v1/mappings?bundleId=default", client.URL) |
| 16 | +} |
| 17 | + |
| 18 | +func (client *sysdigSecureClient) scanningPolicyURL(scanningPolicyId string) string { |
| 19 | + return fmt.Sprintf("%s/api/scanning/v1/policies/%s", client.URL, scanningPolicyId) |
| 20 | +} |
| 21 | + |
| 22 | +// Scanning Policies |
| 23 | + |
| 24 | +func (client *sysdigSecureClient) CreateScanningPolicy(ctx context.Context, scanningPolicyRequest ScanningPolicy) (scanningPolicy ScanningPolicy, err error) { |
| 25 | + response, err := client.doSysdigSecureRequest(ctx, http.MethodPost, client.scanningPoliciesURL(), scanningPolicyRequest.ToJSON()) |
| 26 | + if err != nil { |
| 27 | + return |
| 28 | + } |
| 29 | + defer response.Body.Close() |
| 30 | + |
| 31 | + if response.StatusCode != http.StatusOK { |
| 32 | + err = errorFromResponse(response) |
| 33 | + return |
| 34 | + } |
| 35 | + |
| 36 | + body, err := io.ReadAll(response.Body) |
| 37 | + if err != nil { |
| 38 | + return |
| 39 | + } |
| 40 | + |
| 41 | + return ScanningPolicyFromJSON(body), nil |
| 42 | +} |
| 43 | + |
| 44 | +func (client *sysdigSecureClient) GetScanningPolicyById(ctx context.Context, scanningPolicyID string) (scanningPolicy ScanningPolicy, err error) { |
| 45 | + response, err := client.doSysdigSecureRequest(ctx, http.MethodGet, client.scanningPolicyURL(scanningPolicyID), nil) |
| 46 | + if err != nil { |
| 47 | + return |
| 48 | + } |
| 49 | + defer response.Body.Close() |
| 50 | + |
| 51 | + if response.StatusCode != http.StatusOK { |
| 52 | + return ScanningPolicy{}, errorFromResponse(response) |
| 53 | + } |
| 54 | + |
| 55 | + body, err := io.ReadAll(response.Body) |
| 56 | + if err != nil { |
| 57 | + return |
| 58 | + } |
| 59 | + return ScanningPolicyFromJSON(body), nil |
| 60 | +} |
| 61 | + |
| 62 | +func (client *sysdigSecureClient) UpdateScanningPolicyById(ctx context.Context, scanningPolicyRequest ScanningPolicy) (scanningPolicy ScanningPolicy, err error) { |
| 63 | + response, err := client.doSysdigSecureRequest(ctx, http.MethodPut, client.scanningPolicyURL(scanningPolicyRequest.ID), scanningPolicyRequest.ToJSON()) |
| 64 | + if err != nil { |
| 65 | + return |
| 66 | + } |
| 67 | + defer response.Body.Close() |
| 68 | + |
| 69 | + if response.StatusCode != http.StatusOK { |
| 70 | + return ScanningPolicy{}, errorFromResponse(response) |
| 71 | + } |
| 72 | + |
| 73 | + body, err := io.ReadAll(response.Body) |
| 74 | + if err != nil { |
| 75 | + return |
| 76 | + } |
| 77 | + return ScanningPolicyFromJSON(body), nil |
| 78 | +} |
| 79 | + |
| 80 | +func (client *sysdigSecureClient) DeleteScanningPolicyById(ctx context.Context, scanningPolicyID string) error { |
| 81 | + response, err := client.doSysdigSecureRequest(ctx, http.MethodDelete, client.scanningPolicyURL(scanningPolicyID), nil) |
| 82 | + if err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + defer response.Body.Close() |
| 86 | + |
| 87 | + if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK { |
| 88 | + return errorFromResponse(response) |
| 89 | + } |
| 90 | + |
| 91 | + return err |
| 92 | +} |
| 93 | + |
| 94 | +// Scanning Policy Assignments |
| 95 | + |
| 96 | +func (client *sysdigSecureClient) CreateScanningPolicyAssignmentList(ctx context.Context, scanningPolicyAssignmentRequest ScanningPolicyAssignmentList) (scanningPolicyAssignmentList ScanningPolicyAssignmentList, err error) { |
| 97 | + response, err := client.doSysdigSecureRequest(ctx, http.MethodPut, client.scanningPolicyAssignmentURL(), scanningPolicyAssignmentRequest.ToJSON()) |
| 98 | + if err != nil { |
| 99 | + return |
| 100 | + } |
| 101 | + defer response.Body.Close() |
| 102 | + |
| 103 | + if response.StatusCode != http.StatusOK { |
| 104 | + err = errorFromResponse(response) |
| 105 | + return |
| 106 | + } |
| 107 | + |
| 108 | + body, err := io.ReadAll(response.Body) |
| 109 | + if err != nil { |
| 110 | + return |
| 111 | + } |
| 112 | + |
| 113 | + return ScanningPolicyAssignmentFromJSON(body), nil |
| 114 | +} |
| 115 | + |
| 116 | +func (client *sysdigSecureClient) DeleteScanningPolicyAssignmentList(ctx context.Context, scanningPolicyAssignmentList ScanningPolicyAssignmentList) error { |
| 117 | + response, err := client.doSysdigSecureRequest(ctx, http.MethodPut, client.scanningPolicyAssignmentURL(), scanningPolicyAssignmentList.ToJSON()) |
| 118 | + if err != nil { |
| 119 | + return err |
| 120 | + } |
| 121 | + defer response.Body.Close() |
| 122 | + |
| 123 | + if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK { |
| 124 | + return errorFromResponse(response) |
| 125 | + } |
| 126 | + |
| 127 | + return err |
| 128 | +} |
| 129 | + |
| 130 | +func (client *sysdigSecureClient) GetScanningPolicyAssignmentList(ctx context.Context) (scanningPolicyAssignmentList ScanningPolicyAssignmentList, err error) { |
| 131 | + response, err := client.doSysdigSecureRequest(ctx, http.MethodGet, client.scanningPolicyAssignmentURL(), nil) |
| 132 | + if err != nil { |
| 133 | + return |
| 134 | + } |
| 135 | + defer response.Body.Close() |
| 136 | + |
| 137 | + if response.StatusCode != http.StatusOK { |
| 138 | + return ScanningPolicyAssignmentList{}, errorFromResponse(response) |
| 139 | + } |
| 140 | + |
| 141 | + body, err := io.ReadAll(response.Body) |
| 142 | + if err != nil { |
| 143 | + return |
| 144 | + } |
| 145 | + return ScanningPolicyAssignmentFromJSON(body), nil |
| 146 | +} |
0 commit comments