2018-01-16 12:48:25 -05:00
|
|
|
//
|
|
|
|
// Copyright (c) 2018, Joyent, Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
//
|
|
|
|
|
2017-10-31 11:02:15 -04:00
|
|
|
package compute
|
2017-04-26 15:12:57 -04:00
|
|
|
|
|
|
|
import (
|
2017-05-05 10:00:09 -04:00
|
|
|
"context"
|
2017-04-26 15:12:57 -04:00
|
|
|
"encoding/json"
|
2020-05-11 15:26:01 -04:00
|
|
|
"fmt"
|
2017-04-26 15:12:57 -04:00
|
|
|
"net/http"
|
2020-05-11 15:26:01 -04:00
|
|
|
"net/url"
|
2018-01-16 12:48:25 -05:00
|
|
|
"path"
|
2017-04-26 15:12:57 -04:00
|
|
|
|
2017-10-31 11:02:15 -04:00
|
|
|
"github.com/joyent/triton-go/client"
|
2018-01-16 12:48:25 -05:00
|
|
|
"github.com/pkg/errors"
|
2017-04-26 15:12:57 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type PackagesClient struct {
|
2017-10-31 11:02:15 -04:00
|
|
|
client *client.Client
|
2017-04-26 15:12:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type Package struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Memory int64 `json:"memory"`
|
|
|
|
Disk int64 `json:"disk"`
|
|
|
|
Swap int64 `json:"swap"`
|
|
|
|
LWPs int64 `json:"lwps"`
|
|
|
|
VCPUs int64 `json:"vcpus"`
|
|
|
|
Version string `json:"version"`
|
|
|
|
Group string `json:"group"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
Default bool `json:"default"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ListPackagesInput struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Memory int64 `json:"memory"`
|
|
|
|
Disk int64 `json:"disk"`
|
|
|
|
Swap int64 `json:"swap"`
|
|
|
|
LWPs int64 `json:"lwps"`
|
|
|
|
VCPUs int64 `json:"vcpus"`
|
|
|
|
Version string `json:"version"`
|
|
|
|
Group string `json:"group"`
|
|
|
|
}
|
|
|
|
|
2017-10-31 11:02:15 -04:00
|
|
|
func (c *PackagesClient) List(ctx context.Context, input *ListPackagesInput) ([]*Package, error) {
|
2018-01-16 12:48:25 -05:00
|
|
|
fullPath := path.Join("/", c.client.AccountName, "packages")
|
2020-05-11 15:26:01 -04:00
|
|
|
|
|
|
|
query := &url.Values{}
|
|
|
|
if input.Name != "" {
|
|
|
|
query.Set("name", input.Name)
|
|
|
|
}
|
|
|
|
if input.Memory != 0 {
|
|
|
|
query.Set("memory", fmt.Sprintf("%d", input.Memory))
|
|
|
|
}
|
|
|
|
if input.Disk != 0 {
|
|
|
|
query.Set("disk", fmt.Sprintf("%d", input.Disk))
|
|
|
|
}
|
|
|
|
if input.Swap != 0 {
|
|
|
|
query.Set("swap", fmt.Sprintf("%d", input.Swap))
|
|
|
|
}
|
|
|
|
if input.LWPs != 0 {
|
|
|
|
query.Set("lwps", fmt.Sprintf("%d", input.LWPs))
|
|
|
|
}
|
|
|
|
if input.VCPUs != 0 {
|
|
|
|
query.Set("vcpus", fmt.Sprintf("%d", input.VCPUs))
|
|
|
|
}
|
|
|
|
if input.Version != "" {
|
|
|
|
query.Set("version", input.Version)
|
|
|
|
}
|
|
|
|
if input.Group != "" {
|
|
|
|
query.Set("group", input.Group)
|
|
|
|
}
|
|
|
|
|
2017-10-31 11:02:15 -04:00
|
|
|
reqInputs := client.RequestInput{
|
|
|
|
Method: http.MethodGet,
|
2018-01-16 12:48:25 -05:00
|
|
|
Path: fullPath,
|
2020-05-11 15:26:01 -04:00
|
|
|
Query: query,
|
2017-10-31 11:02:15 -04:00
|
|
|
}
|
|
|
|
respReader, err := c.client.ExecuteRequest(ctx, reqInputs)
|
2017-04-26 15:12:57 -04:00
|
|
|
if respReader != nil {
|
|
|
|
defer respReader.Close()
|
|
|
|
}
|
|
|
|
if err != nil {
|
2018-01-16 12:48:25 -05:00
|
|
|
return nil, errors.Wrap(err, "unable to list packages")
|
2017-04-26 15:12:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var result []*Package
|
|
|
|
decoder := json.NewDecoder(respReader)
|
|
|
|
if err = decoder.Decode(&result); err != nil {
|
2018-01-16 12:48:25 -05:00
|
|
|
return nil, errors.Wrap(err, "unable to decode list packages response")
|
2017-04-26 15:12:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type GetPackageInput struct {
|
|
|
|
ID string
|
|
|
|
}
|
|
|
|
|
2017-10-31 11:02:15 -04:00
|
|
|
func (c *PackagesClient) Get(ctx context.Context, input *GetPackageInput) (*Package, error) {
|
2018-01-16 12:48:25 -05:00
|
|
|
fullPath := path.Join("/", c.client.AccountName, "packages", input.ID)
|
2017-10-31 11:02:15 -04:00
|
|
|
reqInputs := client.RequestInput{
|
|
|
|
Method: http.MethodGet,
|
2018-01-16 12:48:25 -05:00
|
|
|
Path: fullPath,
|
2017-10-31 11:02:15 -04:00
|
|
|
}
|
|
|
|
respReader, err := c.client.ExecuteRequest(ctx, reqInputs)
|
2017-04-26 15:12:57 -04:00
|
|
|
if respReader != nil {
|
|
|
|
defer respReader.Close()
|
|
|
|
}
|
|
|
|
if err != nil {
|
2018-01-16 12:48:25 -05:00
|
|
|
return nil, errors.Wrap(err, "unable to get package")
|
2017-04-26 15:12:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var result *Package
|
|
|
|
decoder := json.NewDecoder(respReader)
|
|
|
|
if err = decoder.Decode(&result); err != nil {
|
2018-01-16 12:48:25 -05:00
|
|
|
return nil, errors.Wrap(err, "unable to decode get package response")
|
2017-04-26 15:12:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|