|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "net/url" |
| 10 | +) |
| 11 | + |
| 12 | +const CONTENT_TYPE_LOCATION = "application/vnd.meshcloud.api.meshlocation.v1-preview.hal+json" |
| 13 | + |
| 14 | +type MeshLocation struct { |
| 15 | + ApiVersion string `json:"apiVersion" tfsdk:"api_version"` |
| 16 | + Kind string `json:"kind" tfsdk:"kind"` |
| 17 | + Metadata MeshLocationMetadata `json:"metadata" tfsdk:"metadata"` |
| 18 | + Spec MeshLocationSpec `json:"spec" tfsdk:"spec"` |
| 19 | +} |
| 20 | + |
| 21 | +type MeshLocationMetadata struct { |
| 22 | + Name string `json:"name" tfsdk:"name"` |
| 23 | + CreatedOn string `json:"createdOn" tfsdk:"created_on"` |
| 24 | +} |
| 25 | + |
| 26 | +type MeshLocationSpec struct { |
| 27 | + DisplayName string `json:"displayName" tfsdk:"display_name"` |
| 28 | +} |
| 29 | + |
| 30 | +type MeshLocationCreate struct { |
| 31 | + ApiVersion string `json:"apiVersion" tfsdk:"api_version"` |
| 32 | + Metadata MeshLocationCreateMetadata `json:"metadata" tfsdk:"metadata"` |
| 33 | + Spec MeshLocationSpec `json:"spec" tfsdk:"spec"` |
| 34 | +} |
| 35 | + |
| 36 | +type MeshLocationCreateMetadata struct { |
| 37 | + Name string `json:"name" tfsdk:"name"` |
| 38 | +} |
| 39 | + |
| 40 | +func (c *MeshStackProviderClient) urlForLocation(identifier string) *url.URL { |
| 41 | + return c.endpoints.Locations.JoinPath(identifier) |
| 42 | +} |
| 43 | + |
| 44 | +func (c *MeshStackProviderClient) ReadLocation(identifier string) (*MeshLocation, error) { |
| 45 | + targetUrl := c.urlForLocation(identifier) |
| 46 | + |
| 47 | + req, err := http.NewRequest("GET", targetUrl.String(), nil) |
| 48 | + if err != nil { |
| 49 | + return nil, err |
| 50 | + } |
| 51 | + req.Header.Set("Accept", CONTENT_TYPE_LOCATION) |
| 52 | + |
| 53 | + res, err := c.doAuthenticatedRequest(req) |
| 54 | + if err != nil { |
| 55 | + return nil, err |
| 56 | + } |
| 57 | + |
| 58 | + defer func() { |
| 59 | + _ = res.Body.Close() |
| 60 | + }() |
| 61 | + |
| 62 | + if res.StatusCode == http.StatusNotFound { |
| 63 | + return nil, nil |
| 64 | + } |
| 65 | + |
| 66 | + data, err := io.ReadAll(res.Body) |
| 67 | + if err != nil { |
| 68 | + return nil, err |
| 69 | + } |
| 70 | + |
| 71 | + if !isSuccessHTTPStatus(res) { |
| 72 | + return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data) |
| 73 | + } |
| 74 | + |
| 75 | + var location MeshLocation |
| 76 | + err = json.Unmarshal(data, &location) |
| 77 | + if err != nil { |
| 78 | + return nil, err |
| 79 | + } |
| 80 | + |
| 81 | + return &location, nil |
| 82 | +} |
| 83 | + |
| 84 | +func (c *MeshStackProviderClient) CreateLocation(location *MeshLocationCreate) (*MeshLocation, error) { |
| 85 | + payload, err := json.Marshal(location) |
| 86 | + if err != nil { |
| 87 | + return nil, err |
| 88 | + } |
| 89 | + |
| 90 | + req, err := http.NewRequest("POST", c.endpoints.Locations.String(), bytes.NewBuffer(payload)) |
| 91 | + if err != nil { |
| 92 | + return nil, err |
| 93 | + } |
| 94 | + req.Header.Set("Content-Type", CONTENT_TYPE_LOCATION) |
| 95 | + req.Header.Set("Accept", CONTENT_TYPE_LOCATION) |
| 96 | + |
| 97 | + res, err := c.doAuthenticatedRequest(req) |
| 98 | + if err != nil { |
| 99 | + return nil, err |
| 100 | + } |
| 101 | + |
| 102 | + defer func() { |
| 103 | + _ = res.Body.Close() |
| 104 | + }() |
| 105 | + |
| 106 | + data, err := io.ReadAll(res.Body) |
| 107 | + if err != nil { |
| 108 | + return nil, err |
| 109 | + } |
| 110 | + |
| 111 | + if !isSuccessHTTPStatus(res) { |
| 112 | + return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data) |
| 113 | + } |
| 114 | + |
| 115 | + var createdLocation MeshLocation |
| 116 | + err = json.Unmarshal(data, &createdLocation) |
| 117 | + if err != nil { |
| 118 | + return nil, err |
| 119 | + } |
| 120 | + |
| 121 | + return &createdLocation, nil |
| 122 | +} |
| 123 | + |
| 124 | +func (c *MeshStackProviderClient) UpdateLocation(identifier string, location *MeshLocationCreate) (*MeshLocation, error) { |
| 125 | + targetUrl := c.urlForLocation(identifier) |
| 126 | + |
| 127 | + payload, err := json.Marshal(location) |
| 128 | + if err != nil { |
| 129 | + return nil, err |
| 130 | + } |
| 131 | + |
| 132 | + req, err := http.NewRequest("PUT", targetUrl.String(), bytes.NewBuffer(payload)) |
| 133 | + if err != nil { |
| 134 | + return nil, err |
| 135 | + } |
| 136 | + req.Header.Set("Content-Type", CONTENT_TYPE_LOCATION) |
| 137 | + req.Header.Set("Accept", CONTENT_TYPE_LOCATION) |
| 138 | + |
| 139 | + res, err := c.doAuthenticatedRequest(req) |
| 140 | + if err != nil { |
| 141 | + return nil, err |
| 142 | + } |
| 143 | + |
| 144 | + defer func() { |
| 145 | + _ = res.Body.Close() |
| 146 | + }() |
| 147 | + |
| 148 | + data, err := io.ReadAll(res.Body) |
| 149 | + if err != nil { |
| 150 | + return nil, err |
| 151 | + } |
| 152 | + |
| 153 | + if !isSuccessHTTPStatus(res) { |
| 154 | + return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data) |
| 155 | + } |
| 156 | + |
| 157 | + var updatedLocation MeshLocation |
| 158 | + err = json.Unmarshal(data, &updatedLocation) |
| 159 | + if err != nil { |
| 160 | + return nil, err |
| 161 | + } |
| 162 | + |
| 163 | + return &updatedLocation, nil |
| 164 | +} |
| 165 | + |
| 166 | +func (c *MeshStackProviderClient) DeleteLocation(identifier string) error { |
| 167 | + targetUrl := c.urlForLocation(identifier) |
| 168 | + return c.deleteMeshObject(*targetUrl, 204) |
| 169 | +} |
0 commit comments