|
| 1 | +package v2 |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | +) |
| 8 | + |
| 9 | +const ( |
| 10 | + CreateListPath = "%s/api/secure/falco/lists" |
| 11 | + GetListPath = "%s/api/secure/falco/lists/%d" |
| 12 | + UpdateListPath = "%s/api/secure/falco/lists/%d" |
| 13 | + DeleteListPath = "%s/api/secure/falco/lists/%d" |
| 14 | +) |
| 15 | + |
| 16 | +type ListInterface interface { |
| 17 | + CreateList(ctx context.Context, list List) (List, error) |
| 18 | + GetListByID(ctx context.Context, id int) (List, error) |
| 19 | + UpdateList(ctx context.Context, list List) (List, error) |
| 20 | + DeleteList(ctx context.Context, id int) error |
| 21 | +} |
| 22 | + |
| 23 | +func (client *Client) CreateList(ctx context.Context, list List) (List, error) { |
| 24 | + payload, err := Marshal[List](list) |
| 25 | + if err != nil { |
| 26 | + return List{}, err |
| 27 | + } |
| 28 | + |
| 29 | + response, err := client.requester.Request(ctx, http.MethodPost, client.CreateListURL(), payload) |
| 30 | + if err != nil { |
| 31 | + return List{}, err |
| 32 | + } |
| 33 | + defer response.Body.Close() |
| 34 | + |
| 35 | + if response.StatusCode != http.StatusOK && response.StatusCode != http.StatusCreated { |
| 36 | + return List{}, client.ErrorFromResponse(response) |
| 37 | + } |
| 38 | + |
| 39 | + return Unmarshal[List](response.Body) |
| 40 | +} |
| 41 | + |
| 42 | +func (client *Client) GetListByID(ctx context.Context, id int) (List, error) { |
| 43 | + response, err := client.requester.Request(ctx, http.MethodGet, client.GetListURL(id), nil) |
| 44 | + if err != nil { |
| 45 | + return List{}, err |
| 46 | + } |
| 47 | + defer response.Body.Close() |
| 48 | + |
| 49 | + if response.StatusCode != http.StatusOK { |
| 50 | + return List{}, client.ErrorFromResponse(response) |
| 51 | + } |
| 52 | + |
| 53 | + list, err := Unmarshal[List](response.Body) |
| 54 | + if err != nil { |
| 55 | + return List{}, err |
| 56 | + } |
| 57 | + |
| 58 | + if list.Version == 0 { |
| 59 | + return List{}, fmt.Errorf("list with ID: %d does not exists", id) |
| 60 | + } |
| 61 | + |
| 62 | + return list, nil |
| 63 | +} |
| 64 | + |
| 65 | +func (client *Client) UpdateList(ctx context.Context, list List) (List, error) { |
| 66 | + payload, err := Marshal[List](list) |
| 67 | + if err != nil { |
| 68 | + return List{}, err |
| 69 | + } |
| 70 | + |
| 71 | + response, err := client.requester.Request(ctx, http.MethodPut, client.UpdateListURL(list.ID), payload) |
| 72 | + if err != nil { |
| 73 | + return List{}, err |
| 74 | + } |
| 75 | + defer response.Body.Close() |
| 76 | + |
| 77 | + if response.StatusCode != http.StatusOK { |
| 78 | + return List{}, client.ErrorFromResponse(response) |
| 79 | + } |
| 80 | + |
| 81 | + return Unmarshal[List](response.Body) |
| 82 | +} |
| 83 | + |
| 84 | +func (client *Client) DeleteList(ctx context.Context, id int) error { |
| 85 | + response, err := client.requester.Request(ctx, http.MethodDelete, client.DeleteListURL(id), nil) |
| 86 | + if err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + defer response.Body.Close() |
| 90 | + |
| 91 | + if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK { |
| 92 | + return client.ErrorFromResponse(response) |
| 93 | + } |
| 94 | + |
| 95 | + return nil |
| 96 | +} |
| 97 | + |
| 98 | +func (client *Client) CreateListURL() string { |
| 99 | + return fmt.Sprintf(CreateListPath, client.config.url) |
| 100 | +} |
| 101 | + |
| 102 | +func (client *Client) GetListURL(id int) string { |
| 103 | + return fmt.Sprintf(GetListPath, client.config.url, id) |
| 104 | +} |
| 105 | + |
| 106 | +func (client *Client) UpdateListURL(id int) string { |
| 107 | + return fmt.Sprintf(UpdateListPath, client.config.url, id) |
| 108 | +} |
| 109 | + |
| 110 | +func (client *Client) DeleteListURL(id int) string { |
| 111 | + return fmt.Sprintf(DeleteListPath, client.config.url, id) |
| 112 | +} |
0 commit comments