|
| 1 | +package v2 |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | +) |
| 8 | + |
| 9 | +const ( |
| 10 | + CreateMacroPath = "%s/api/secure/falco/macros" |
| 11 | + GetMacroByIDPath = "%s/api/secure/falco/macros/%d" |
| 12 | + UpdateMacroPath = "%s/api/secure/falco/macros/%d" |
| 13 | + DeleteMacroPath = "%s/api/secure/falco/macros/%d" |
| 14 | +) |
| 15 | + |
| 16 | +type MacroInterface interface { |
| 17 | + CreateMacro(ctx context.Context, macro Macro) (Macro, error) |
| 18 | + GetMacroByID(ctx context.Context, id int) (Macro, error) |
| 19 | + UpdateMacro(ctx context.Context, macro Macro) (Macro, error) |
| 20 | + DeleteMacro(ctx context.Context, id int) error |
| 21 | +} |
| 22 | + |
| 23 | +func (client *Client) CreateMacro(ctx context.Context, macro Macro) (Macro, error) { |
| 24 | + payload, err := Marshal(macro) |
| 25 | + if err != nil { |
| 26 | + return Macro{}, err |
| 27 | + } |
| 28 | + |
| 29 | + response, err := client.requester.Request(ctx, http.MethodPost, client.CreateMacroURL(), payload) |
| 30 | + if err != nil { |
| 31 | + return Macro{}, err |
| 32 | + } |
| 33 | + defer response.Body.Close() |
| 34 | + |
| 35 | + if response.StatusCode != http.StatusOK && response.StatusCode != http.StatusCreated { |
| 36 | + return Macro{}, client.ErrorFromResponse(response) |
| 37 | + } |
| 38 | + |
| 39 | + return Unmarshal[Macro](response.Body) |
| 40 | +} |
| 41 | + |
| 42 | +func (client *Client) GetMacroByID(ctx context.Context, id int) (Macro, error) { |
| 43 | + response, err := client.requester.Request(ctx, http.MethodGet, client.GetMacroByIDURL(id), nil) |
| 44 | + if err != nil { |
| 45 | + return Macro{}, err |
| 46 | + } |
| 47 | + defer response.Body.Close() |
| 48 | + |
| 49 | + if response.StatusCode != http.StatusOK { |
| 50 | + return Macro{}, client.ErrorFromResponse(response) |
| 51 | + } |
| 52 | + |
| 53 | + macro, err := Unmarshal[Macro](response.Body) |
| 54 | + if err != nil { |
| 55 | + return Macro{}, err |
| 56 | + } |
| 57 | + |
| 58 | + if macro.Version == 0 { |
| 59 | + return Macro{}, fmt.Errorf("macro with ID: %d does not exists", id) |
| 60 | + } |
| 61 | + |
| 62 | + return macro, nil |
| 63 | +} |
| 64 | + |
| 65 | +func (client *Client) UpdateMacro(ctx context.Context, macro Macro) (Macro, error) { |
| 66 | + payload, err := Marshal(macro) |
| 67 | + if err != nil { |
| 68 | + return Macro{}, err |
| 69 | + } |
| 70 | + |
| 71 | + response, err := client.requester.Request(ctx, http.MethodPut, client.UpdateMacroURL(macro.ID), payload) |
| 72 | + if err != nil { |
| 73 | + return Macro{}, err |
| 74 | + } |
| 75 | + defer response.Body.Close() |
| 76 | + |
| 77 | + if response.StatusCode != http.StatusOK { |
| 78 | + return Macro{}, client.ErrorFromResponse(response) |
| 79 | + } |
| 80 | + |
| 81 | + return Unmarshal[Macro](response.Body) |
| 82 | +} |
| 83 | + |
| 84 | +func (client *Client) DeleteMacro(ctx context.Context, id int) error { |
| 85 | + response, err := client.requester.Request(ctx, http.MethodDelete, client.DeleteMacroURL(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 && response.StatusCode != http.StatusNotFound { |
| 92 | + return client.ErrorFromResponse(response) |
| 93 | + } |
| 94 | + return nil |
| 95 | +} |
| 96 | + |
| 97 | +func (client *Client) CreateMacroURL() string { |
| 98 | + return fmt.Sprintf(CreateMacroPath, client.config.url) |
| 99 | +} |
| 100 | + |
| 101 | +func (client *Client) GetMacroByIDURL(id int) string { |
| 102 | + return fmt.Sprintf(GetMacroByIDPath, client.config.url, id) |
| 103 | +} |
| 104 | + |
| 105 | +func (client *Client) UpdateMacroURL(id int) string { |
| 106 | + return fmt.Sprintf(UpdateMacroPath, client.config.url, id) |
| 107 | +} |
| 108 | + |
| 109 | +func (client *Client) DeleteMacroURL(id int) string { |
| 110 | + return fmt.Sprintf(DeleteMacroPath, client.config.url, id) |
| 111 | +} |
0 commit comments