1package gsclient
2
3import (
4	"context"
5	"errors"
6	"net/http"
7	"path"
8)
9
10//LocationList JSON struct of a list of locations
11type LocationList struct {
12	//Array of locations
13	List map[string]LocationProperties `json:"locations"`
14}
15
16//Location JSON struct of a single location
17type Location struct {
18	//Properties of a location
19	Properties LocationProperties `json:"location"`
20}
21
22//LocationProperties JSON struct of properties of a location
23type LocationProperties struct {
24	//Uses IATA airport code, which works as a location identifier.
25	Iata string `json:"iata"`
26
27	//Status indicates the status of the object.
28	Status string `json:"status"`
29
30	//List of labels.
31	Labels []string `json:"labels"`
32
33	//The human-readable name of the location. It supports the full UTF-8 charset, with a maximum of 64 characters.
34	Name string `json:"name"`
35
36	//The UUID of an object is always unique, and refers to a specific object.
37	ObjectUUID string `json:"object_uuid"`
38
39	//The human-readable name of the location. It supports the full UTF-8 charset, with a maximum of 64 characters.
40	Country string `json:"country"`
41}
42
43//GetLocationList gets a list of available locations]
44//
45//See: https://gridscale.io/en//api-documentation/index.html#operation/getLocations
46func (c *Client) GetLocationList(ctx context.Context) ([]Location, error) {
47	r := gsRequest{
48		uri:                 apiLocationBase,
49		method:              http.MethodGet,
50		skipCheckingRequest: true,
51	}
52	var response LocationList
53	var locations []Location
54	err := r.execute(ctx, *c, &response)
55	for _, properties := range response.List {
56		locations = append(locations, Location{Properties: properties})
57	}
58	return locations, err
59}
60
61//GetLocation gets a specific location
62//
63//See: https://gridscale.io/en//api-documentation/index.html#operation/getLocation
64func (c *Client) GetLocation(ctx context.Context, id string) (Location, error) {
65	if !isValidUUID(id) {
66		return Location{}, errors.New("'id' is invalid")
67	}
68	r := gsRequest{
69		uri:                 path.Join(apiLocationBase, id),
70		method:              http.MethodGet,
71		skipCheckingRequest: true,
72	}
73	var location Location
74	err := r.execute(ctx, *c, &location)
75	return location, err
76}
77