|
| 1 | +package v2 |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | +) |
| 8 | + |
| 9 | +const ( |
| 10 | + PlatformZonesPath = "%s/platform/v1/zones" |
| 11 | + PlatformZonePath = "%s/platform/v1/zones/%d" |
| 12 | +) |
| 13 | + |
| 14 | +type ZoneInterface interface { |
| 15 | + Base |
| 16 | + GetZones(ctx context.Context) ([]Zone, error) |
| 17 | + GetZoneById(ctx context.Context, id int) (*Zone, error) |
| 18 | + CreateZone(ctx context.Context, zone *ZoneRequest) (*Zone, error) |
| 19 | + UpdateZone(ctx context.Context, zone *ZoneRequest) (*Zone, error) |
| 20 | + DeleteZone(ctx context.Context, id int) error |
| 21 | +} |
| 22 | + |
| 23 | +func (client *Client) GetZones(ctx context.Context) ([]Zone, error) { |
| 24 | + response, err := client.requester.Request(ctx, http.MethodGet, client.getZonesURL(), nil) |
| 25 | + if err != nil { |
| 26 | + return nil, err |
| 27 | + } |
| 28 | + defer response.Body.Close() |
| 29 | + |
| 30 | + wrapper, err := Unmarshal[ZonesWrapper](response.Body) |
| 31 | + if err != nil { |
| 32 | + return nil, err |
| 33 | + } |
| 34 | + |
| 35 | + return wrapper.Zones, nil |
| 36 | +} |
| 37 | + |
| 38 | +func (client *Client) GetZoneById(ctx context.Context, id int) (*Zone, error) { |
| 39 | + response, err := client.requester.Request(ctx, http.MethodGet, client.getZoneURL(id), nil) |
| 40 | + if err != nil { |
| 41 | + return nil, err |
| 42 | + } |
| 43 | + defer response.Body.Close() |
| 44 | + |
| 45 | + zone, err := Unmarshal[Zone](response.Body) |
| 46 | + if err != nil { |
| 47 | + return nil, err |
| 48 | + } |
| 49 | + |
| 50 | + return &zone, nil |
| 51 | +} |
| 52 | + |
| 53 | +func (client *Client) CreateZone(ctx context.Context, zone *ZoneRequest) (*Zone, error) { |
| 54 | + payload, err := Marshal(zone) |
| 55 | + if err != nil { |
| 56 | + return nil, err |
| 57 | + } |
| 58 | + |
| 59 | + response, err := client.requester.Request(ctx, http.MethodPost, client.getZonesURL(), payload) |
| 60 | + if err != nil { |
| 61 | + return nil, err |
| 62 | + } |
| 63 | + defer response.Body.Close() |
| 64 | + |
| 65 | + if response.StatusCode != http.StatusOK && response.StatusCode != http.StatusCreated { |
| 66 | + return nil, client.ErrorFromResponse(response) |
| 67 | + } |
| 68 | + |
| 69 | + createdZone, err := Unmarshal[Zone](response.Body) |
| 70 | + if err != nil { |
| 71 | + return nil, err |
| 72 | + } |
| 73 | + |
| 74 | + return &createdZone, nil |
| 75 | +} |
| 76 | + |
| 77 | +func (client *Client) UpdateZone(ctx context.Context, zone *ZoneRequest) (*Zone, error) { |
| 78 | + payload, err := Marshal(zone) |
| 79 | + if err != nil { |
| 80 | + return nil, err |
| 81 | + } |
| 82 | + |
| 83 | + response, err := client.requester.Request(ctx, http.MethodPut, client.getZoneURL(zone.ID), payload) |
| 84 | + if err != nil { |
| 85 | + return nil, err |
| 86 | + } |
| 87 | + defer response.Body.Close() |
| 88 | + |
| 89 | + if response.StatusCode != http.StatusOK && response.StatusCode != http.StatusCreated { |
| 90 | + return nil, client.ErrorFromResponse(response) |
| 91 | + } |
| 92 | + |
| 93 | + updatedZone, err := Unmarshal[Zone](response.Body) |
| 94 | + if err != nil { |
| 95 | + return nil, err |
| 96 | + } |
| 97 | + |
| 98 | + return &updatedZone, nil |
| 99 | +} |
| 100 | + |
| 101 | +func (client *Client) DeleteZone(ctx context.Context, id int) error { |
| 102 | + response, err := client.requester.Request(ctx, http.MethodDelete, client.getZoneURL(id), nil) |
| 103 | + if err != nil { |
| 104 | + return err |
| 105 | + } |
| 106 | + defer response.Body.Close() |
| 107 | + |
| 108 | + if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK && response.StatusCode != http.StatusNotFound { |
| 109 | + return client.ErrorFromResponse(response) |
| 110 | + } |
| 111 | + |
| 112 | + return nil |
| 113 | +} |
| 114 | + |
| 115 | +func (client *Client) getZonesURL() string { |
| 116 | + return fmt.Sprintf(PlatformZonesPath, client.config.url) |
| 117 | +} |
| 118 | + |
| 119 | +func (client *Client) getZoneURL(id int) string { |
| 120 | + return fmt.Sprintf(PlatformZonePath, client.config.url, id) |
| 121 | +} |
0 commit comments