CreateApplication creates a new application for the authenticated user.
(accessToken, region, name string)
| 303 | |
| 304 | // CreateApplication creates a new application for the authenticated user. |
| 305 | func (c *Client) CreateApplication(accessToken, region, name string) (*Application, error) { |
| 306 | payload := CreateApplicationRequest{RegionCode: region, Name: name} |
| 307 | body, err := json.Marshal(payload) |
| 308 | if err != nil { |
| 309 | return nil, err |
| 310 | } |
| 311 | |
| 312 | req, err := http.NewRequest(http.MethodPost, c.APIURL+"/1/applications", bytes.NewReader(body)) |
| 313 | if err != nil { |
| 314 | return nil, err |
| 315 | } |
| 316 | c.setAPIHeaders(req, accessToken) |
| 317 | req.Header.Set("Content-Type", "application/json") |
| 318 | |
| 319 | resp, err := c.client.Do(req) |
| 320 | if err != nil { |
| 321 | return nil, fmt.Errorf("create application request failed: %w", err) |
| 322 | } |
| 323 | defer resp.Body.Close() |
| 324 | |
| 325 | if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated { |
| 326 | respBody, _ := io.ReadAll(resp.Body) |
| 327 | respStr := string(respBody) |
| 328 | |
| 329 | if strings.Contains(strings.ToLower(respStr), "cluster") && |
| 330 | strings.Contains(strings.ToLower(respStr), "not available") || |
| 331 | strings.Contains(strings.ToLower(respStr), "no cluster") { |
| 332 | return nil, &ErrClusterUnavailable{ |
| 333 | Region: region, |
| 334 | Message: fmt.Sprintf("no cluster available in region %q", region), |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | return nil, fmt.Errorf( |
| 339 | "create application failed with status %d: %s", |
| 340 | resp.StatusCode, |
| 341 | respStr, |
| 342 | ) |
| 343 | } |
| 344 | |
| 345 | var singleResp SingleApplicationResponse |
| 346 | if err := json.NewDecoder(resp.Body).Decode(&singleResp); err != nil { |
| 347 | return nil, fmt.Errorf("failed to parse application response: %w", err) |
| 348 | } |
| 349 | |
| 350 | app := singleResp.Data.toApplication() |
| 351 | return &app, nil |
| 352 | } |
| 353 | |
| 354 | // UpdateApplication renames an existing application. |
| 355 | func (c *Client) UpdateApplication(accessToken, appID, name string) (*Application, error) { |