1//
2// Copyright (c) 2018, Joyent, Inc. All rights reserved.
3//
4// This Source Code Form is subject to the terms of the Mozilla Public
5// License, v. 2.0. If a copy of the MPL was not distributed with this
6// file, You can obtain one at http://mozilla.org/MPL/2.0/.
7//
8
9package compute
10
11import (
12	"context"
13	"encoding/json"
14	"fmt"
15	"net/http"
16	"net/url"
17	"path"
18
19	"github.com/joyent/triton-go/client"
20	"github.com/pkg/errors"
21)
22
23type PackagesClient struct {
24	client *client.Client
25}
26
27type Package struct {
28	ID          string `json:"id"`
29	Name        string `json:"name"`
30	Memory      int64  `json:"memory"`
31	Disk        int64  `json:"disk"`
32	Swap        int64  `json:"swap"`
33	LWPs        int64  `json:"lwps"`
34	VCPUs       int64  `json:"vcpus"`
35	Version     string `json:"version"`
36	Group       string `json:"group"`
37	Description string `json:"description"`
38	Default     bool   `json:"default"`
39}
40
41type ListPackagesInput struct {
42	Name    string `json:"name"`
43	Memory  int64  `json:"memory"`
44	Disk    int64  `json:"disk"`
45	Swap    int64  `json:"swap"`
46	LWPs    int64  `json:"lwps"`
47	VCPUs   int64  `json:"vcpus"`
48	Version string `json:"version"`
49	Group   string `json:"group"`
50}
51
52func (c *PackagesClient) List(ctx context.Context, input *ListPackagesInput) ([]*Package, error) {
53	fullPath := path.Join("/", c.client.AccountName, "packages")
54
55	query := &url.Values{}
56	if input.Name != "" {
57		query.Set("name", input.Name)
58	}
59	if input.Memory != 0 {
60		query.Set("memory", fmt.Sprintf("%d", input.Memory))
61	}
62	if input.Disk != 0 {
63		query.Set("disk", fmt.Sprintf("%d", input.Disk))
64	}
65	if input.Swap != 0 {
66		query.Set("swap", fmt.Sprintf("%d", input.Swap))
67	}
68	if input.LWPs != 0 {
69		query.Set("lwps", fmt.Sprintf("%d", input.LWPs))
70	}
71	if input.VCPUs != 0 {
72		query.Set("vcpus", fmt.Sprintf("%d", input.VCPUs))
73	}
74	if input.Version != "" {
75		query.Set("version", input.Version)
76	}
77	if input.Group != "" {
78		query.Set("group", input.Group)
79	}
80
81	reqInputs := client.RequestInput{
82		Method: http.MethodGet,
83		Path:   fullPath,
84		Query:  query,
85	}
86	respReader, err := c.client.ExecuteRequest(ctx, reqInputs)
87	if respReader != nil {
88		defer respReader.Close()
89	}
90	if err != nil {
91		return nil, errors.Wrap(err, "unable to list packages")
92	}
93
94	var result []*Package
95	decoder := json.NewDecoder(respReader)
96	if err = decoder.Decode(&result); err != nil {
97		return nil, errors.Wrap(err, "unable to decode list packages response")
98	}
99
100	return result, nil
101}
102
103type GetPackageInput struct {
104	ID string
105}
106
107func (c *PackagesClient) Get(ctx context.Context, input *GetPackageInput) (*Package, error) {
108	fullPath := path.Join("/", c.client.AccountName, "packages", input.ID)
109	reqInputs := client.RequestInput{
110		Method: http.MethodGet,
111		Path:   fullPath,
112	}
113	respReader, err := c.client.ExecuteRequest(ctx, reqInputs)
114	if respReader != nil {
115		defer respReader.Close()
116	}
117	if err != nil {
118		return nil, errors.Wrap(err, "unable to get package")
119	}
120
121	var result *Package
122	decoder := json.NewDecoder(respReader)
123	if err = decoder.Decode(&result); err != nil {
124		return nil, errors.Wrap(err, "unable to decode get package response")
125	}
126
127	return result, nil
128}
129