terraform/vendor/github.com/joyent/triton-go/images.go

205 lines
6.0 KiB
Go
Raw Normal View History

provider/triton: Move to joyent/triton-go (#13225) * provider/triton: Move to joyent/triton-go This commit moves the Triton provider to the new joyent/triton-go library from gosdc. This has a number of advantages - not least that requests can be signed using an SSH agent without having to keep unencrypted key material in memory. Schema has been maintained for all resources, and several tests have been added and acceptance tests repaired - in some cases by fixing bugs in the underlying resources. After applying this patch, all acceptance tests pass: ``` go generate $(go list ./... | grep -v /terraform/vendor/) 2017/03/30 13:48:33 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/triton -v -timeout 120m === RUN TestProvider --- PASS: TestProvider (0.00s) === RUN TestProvider_impl --- PASS: TestProvider_impl (0.00s) === RUN TestAccTritonFabric_basic --- PASS: TestAccTritonFabric_basic (15.11s) === RUN TestAccTritonFirewallRule_basic --- PASS: TestAccTritonFirewallRule_basic (1.48s) === RUN TestAccTritonFirewallRule_update --- PASS: TestAccTritonFirewallRule_update (1.55s) === RUN TestAccTritonFirewallRule_enable --- PASS: TestAccTritonFirewallRule_enable (1.52s) === RUN TestAccTritonKey_basic --- PASS: TestAccTritonKey_basic (11.76s) === RUN TestAccTritonKey_noKeyName --- PASS: TestAccTritonKey_noKeyName (11.20s) === RUN TestAccTritonMachine_basic --- PASS: TestAccTritonMachine_basic (82.19s) === RUN TestAccTritonMachine_dns --- PASS: TestAccTritonMachine_dns (173.36s) === RUN TestAccTritonMachine_nic --- PASS: TestAccTritonMachine_nic (167.82s) === RUN TestAccTritonMachine_addNIC --- PASS: TestAccTritonMachine_addNIC (192.11s) === RUN TestAccTritonMachine_firewall --- PASS: TestAccTritonMachine_firewall (188.53s) === RUN TestAccTritonMachine_metadata --- PASS: TestAccTritonMachine_metadata (614.57s) === RUN TestAccTritonVLAN_basic --- PASS: TestAccTritonVLAN_basic (0.93s) === RUN TestAccTritonVLAN_update --- PASS: TestAccTritonVLAN_update (1.50s) PASS ok github.com/hashicorp/terraform/builtin/providers/triton 1463.621s ``` * provider/triton: Update docs for provider config * deps: Vendor github.com/joyent/triton-go/... * deps: Remove github.com/joyent/gosdc
2017-03-31 00:25:27 +02:00
package triton
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"time"
"github.com/hashicorp/errwrap"
)
type ImagesClient struct {
*Client
}
// Images returns a c used for accessing functions pertaining to
// Images functionality in the Triton API.
func (c *Client) Images() *ImagesClient {
return &ImagesClient{c}
}
type ImageFile struct {
Compression string `json:"compression"`
SHA1 string `json:"sha1"`
Size int64 `json:"size"`
}
type Image struct {
ID string `json:"id"`
Name string `json:"name"`
OS string `json:"os"`
Description string `json:"description"`
Version string `json:"version"`
Type string `json:"type"`
Requirements map[string]interface{} `json:"requirements"`
Homepage string `json:"homepage"`
Files []*ImageFile `json:"files"`
PublishedAt time.Time `json:"published_at"`
Owner string `json:"owner"`
Public bool `json:"public"`
State string `json:"state"`
Tags map[string]string `json:"tags"`
EULA string `json:"eula"`
ACL []string `json:"acl"`
Error TritonError `json:"error"`
}
type ListImagesInput struct{}
func (client *ImagesClient) ListImages(*ListImagesInput) ([]*Image, error) {
respReader, err := client.executeRequest(http.MethodGet, "/my/images", nil)
if respReader != nil {
defer respReader.Close()
}
if err != nil {
return nil, errwrap.Wrapf("Error executing ListImages request: {{err}}", err)
}
var result []*Image
decoder := json.NewDecoder(respReader)
if err = decoder.Decode(&result); err != nil {
return nil, errwrap.Wrapf("Error decoding ListImages response: {{err}}", err)
}
return result, nil
}
type GetImageInput struct {
ImageID string
}
func (client *ImagesClient) GetImage(input *GetImageInput) (*Image, error) {
path := fmt.Sprintf("/%s/images/%s", client.accountName, input.ImageID)
respReader, err := client.executeRequest(http.MethodGet, path, nil)
if respReader != nil {
defer respReader.Close()
}
if err != nil {
return nil, errwrap.Wrapf("Error executing GetImage request: {{err}}", err)
}
var result *Image
decoder := json.NewDecoder(respReader)
if err = decoder.Decode(&result); err != nil {
return nil, errwrap.Wrapf("Error decoding GetImage response: {{err}}", err)
}
return result, nil
}
type DeleteImageInput struct {
ImageID string
}
func (client *ImagesClient) DeleteImage(input *DeleteImageInput) error {
path := fmt.Sprintf("/%s/images/%s", client.accountName, input.ImageID)
respReader, err := client.executeRequest(http.MethodDelete, path, nil)
if respReader != nil {
defer respReader.Close()
}
if err != nil {
return errwrap.Wrapf("Error executing DeleteKey request: {{err}}", err)
}
return nil
}
type ExportImageInput struct {
ImageID string
MantaPath string
}
type MantaLocation struct {
MantaURL string `json:"manta_url"`
ImagePath string `json:"image_path"`
ManifestPath string `json:"manifest_path"`
}
func (client *ImagesClient) ExportImage(input *ExportImageInput) (*MantaLocation, error) {
path := fmt.Sprintf("/%s/images/%s", client.accountName, input.ImageID)
query := &url.Values{}
query.Set("action", "export")
query.Set("manta_path", input.MantaPath)
respReader, err := client.executeRequestURIParams(http.MethodGet, path, nil, query)
if respReader != nil {
defer respReader.Close()
}
if err != nil {
return nil, errwrap.Wrapf("Error executing GetImage request: {{err}}", err)
}
var result *MantaLocation
decoder := json.NewDecoder(respReader)
if err = decoder.Decode(&result); err != nil {
return nil, errwrap.Wrapf("Error decoding GetImage response: {{err}}", err)
}
return result, nil
}
type CreateImageFromMachineInput struct {
MachineID string `json:"machine"`
Name string `json:"name"`
Version string `json:"version,omitempty"`
Description string `json:"description,omitempty"`
HomePage string `json:"homepage,omitempty"`
EULA string `json:"eula,omitempty"`
ACL []string `json:"acl,omitempty"`
tags map[string]string `json:"tags,omitempty"`
}
func (client *ImagesClient) CreateImageFromMachine(input *CreateImageFromMachineInput) (*Image, error) {
path := fmt.Sprintf("/%s/images", client.accountName)
respReader, err := client.executeRequest(http.MethodPost, path, input)
if respReader != nil {
defer respReader.Close()
}
if err != nil {
return nil, errwrap.Wrapf("Error executing CreateImageFromMachine request: {{err}}", err)
}
var result *Image
decoder := json.NewDecoder(respReader)
if err = decoder.Decode(&result); err != nil {
return nil, errwrap.Wrapf("Error decoding CreateImageFromMachine response: {{err}}", err)
}
return result, nil
}
type UpdateImageInput struct {
ImageID string `json:"-"`
Name string `json:"name"`
Version string `json:"version,omitempty"`
Description string `json:"description,omitempty"`
HomePage string `json:"homepage,omitempty"`
EULA string `json:"eula,omitempty"`
ACL []string `json:"acl,omitempty"`
tags map[string]string `json:"tags,omitempty"`
}
func (client *ImagesClient) UpdateImage(input *UpdateImageInput) (*Image, error) {
path := fmt.Sprintf("/%s/images/%s", client.accountName, input.ImageID)
query := &url.Values{}
query.Set("action", "update")
respReader, err := client.executeRequestURIParams(http.MethodPost, path, input, query)
if respReader != nil {
defer respReader.Close()
}
if err != nil {
return nil, errwrap.Wrapf("Error executing UpdateImage request: {{err}}", err)
}
var result *Image
decoder := json.NewDecoder(respReader)
if err = decoder.Decode(&result); err != nil {
return nil, errwrap.Wrapf("Error decoding UpdateImage response: {{err}}", err)
}
return result, nil
}