1package gsclient
2
3import (
4	"context"
5	"errors"
6	"net/http"
7	"path"
8)
9
10//ServerIsoImageRelationList JSON struct of a list of relations between a server and ISO-Images
11type ServerIsoImageRelationList struct {
12	//Array of relations between a server and ISO-Images
13	List []ServerIsoImageRelationProperties `json:"isoimage_relations"`
14}
15
16//ServerIsoImageRelation JSON struct of a single relation between a server and an ISO-Image
17type ServerIsoImageRelation struct {
18	//Properties of a relation between a server and an ISO-Image
19	Properties ServerIsoImageRelationProperties `json:"isoimage_relation"`
20}
21
22//ServerIsoImageRelationProperties JSON struct of properties of a relation between a server and an ISO-Image
23type ServerIsoImageRelationProperties struct {
24	//The UUID of an object is always unique, and refers to a specific object.
25	ObjectUUID string `json:"object_uuid"`
26
27	//The human-readable name of the object. It supports the full UTF-8 charset, with a maximum of 64 characters.
28	ObjectName string `json:"object_name"`
29
30	//Whether the ISO-Image is private or not.
31	Private bool `json:"private"`
32
33	//Defines the date and time the object was initially created.
34	CreateTime GSTime `json:"create_time"`
35
36	//Whether the server boots from this iso image or not.
37	Bootdevice bool `json:"bootdevice"`
38}
39
40//ServerIsoImageRelationCreateRequest JSON struct of a request for creating a relation between a server and an ISO-Image
41type ServerIsoImageRelationCreateRequest struct {
42	//The UUID of the ISO-image you are requesting.
43	ObjectUUID string `json:"object_uuid"`
44}
45
46//ServerIsoImageRelationUpdateRequest JSON struct of a request for updating a relation between a server and an ISO-Image
47type ServerIsoImageRelationUpdateRequest struct {
48	//Whether the server boots from this ISO-image or not.
49	BootDevice bool   `json:"bootdevice"`
50	Name       string `json:"name"`
51}
52
53//GetServerIsoImageList gets a list of a specific server's ISO images
54//
55//See: https://gridscale.io/en//api-documentation/index.html#operation/getServerLinkedIsoimages
56func (c *Client) GetServerIsoImageList(ctx context.Context, id string) ([]ServerIsoImageRelationProperties, error) {
57	if !isValidUUID(id) {
58		return nil, errors.New("'id' is invalid")
59	}
60	r := gsRequest{
61		uri:                 path.Join(apiServerBase, id, "isoimages"),
62		method:              http.MethodGet,
63		skipCheckingRequest: true,
64	}
65	var response ServerIsoImageRelationList
66	err := r.execute(ctx, *c, &response)
67	return response.List, err
68}
69
70//GetServerIsoImage gets an ISO image of a specific server
71//
72//See: https://gridscale.io/en//api-documentation/index.html#operation/getServerLinkedIsoimage
73func (c *Client) GetServerIsoImage(ctx context.Context, serverID, isoImageID string) (ServerIsoImageRelationProperties, error) {
74	if !isValidUUID(serverID) || !isValidUUID(isoImageID) {
75		return ServerIsoImageRelationProperties{}, errors.New("'id' is invalid")
76	}
77	r := gsRequest{
78		uri:                 path.Join(apiServerBase, serverID, "isoimages", isoImageID),
79		method:              http.MethodGet,
80		skipCheckingRequest: true,
81	}
82	var response ServerIsoImageRelation
83	err := r.execute(ctx, *c, &response)
84	return response.Properties, err
85}
86
87//UpdateServerIsoImage updates a link between a storage and an ISO image
88//
89//See: https://gridscale.io/en//api-documentation/index.html#operation/updateServerLinkedIsoimage
90func (c *Client) UpdateServerIsoImage(ctx context.Context, serverID, isoImageID string, body ServerIsoImageRelationUpdateRequest) error {
91	if !isValidUUID(serverID) || !isValidUUID(isoImageID) {
92		return errors.New("'serverID' or 'isoImageID' is invalid")
93	}
94	r := gsRequest{
95		uri:    path.Join(apiServerBase, serverID, "isoimages", isoImageID),
96		method: http.MethodPatch,
97		body:   body,
98	}
99	return r.execute(ctx, *c, nil)
100}
101
102//CreateServerIsoImage creates a link between a server and an ISO image
103//
104//See: https://gridscale.io/en//api-documentation/index.html#operation/linkIsoimageToServer
105func (c *Client) CreateServerIsoImage(ctx context.Context, id string, body ServerIsoImageRelationCreateRequest) error {
106	if !isValidUUID(id) || !isValidUUID(body.ObjectUUID) {
107		return errors.New("'serverID' or 'isoImageID' is invalid")
108	}
109	r := gsRequest{
110		uri:    path.Join(apiServerBase, id, "isoimages"),
111		method: http.MethodPost,
112		body:   body,
113	}
114	return r.execute(ctx, *c, nil)
115}
116
117//DeleteServerIsoImage deletes a link between an ISO image and a server
118//
119//See: https://gridscale.io/en//api-documentation/index.html#operation/unlinkIsoimageFromServer
120func (c *Client) DeleteServerIsoImage(ctx context.Context, serverID, isoImageID string) error {
121	if !isValidUUID(serverID) || !isValidUUID(isoImageID) {
122		return errors.New("'serverID' or 'isoImageID' is invalid")
123	}
124	r := gsRequest{
125		uri:    path.Join(apiServerBase, serverID, "isoimages", isoImageID),
126		method: http.MethodDelete,
127	}
128	return r.execute(ctx, *c, nil)
129}
130
131//LinkIsoImage attaches an ISO image to a server
132func (c *Client) LinkIsoImage(ctx context.Context, serverID string, isoimageID string) error {
133	body := ServerIsoImageRelationCreateRequest{
134		ObjectUUID: isoimageID,
135	}
136	return c.CreateServerIsoImage(ctx, serverID, body)
137}
138
139//UnlinkIsoImage removes the link between an ISO image and a server
140func (c *Client) UnlinkIsoImage(ctx context.Context, serverID string, isoimageID string) error {
141	return c.DeleteServerIsoImage(ctx, serverID, isoimageID)
142}
143