/usr/share/gocode/src/github.com/hashicorp/atlas-go/v1/application.go is in golang-github-hashicorp-atlas-go-dev 0.0~git20170808.8261ea0-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | package atlas
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
)
// appWrapper is the API wrapper since the server wraps the resulting object.
type appWrapper struct {
Application *App `json:"application"`
}
// App represents a single instance of an application on the Atlas server.
type App struct {
// User is the namespace (username or organization) under which the
// Atlas application resides
User string `json:"username"`
// Name is the name of the application
Name string `json:"name"`
}
// Slug returns the slug format for this App (User/Name)
func (a *App) Slug() string {
return fmt.Sprintf("%s/%s", a.User, a.Name)
}
// App gets the App by the given user space and name. In the event the App is
// not found (404), or for any other non-200 responses, an error is returned.
func (c *Client) App(user, name string) (*App, error) {
log.Printf("[INFO] getting application %s/%s", user, name)
endpoint := fmt.Sprintf("/api/v1/vagrant/applications/%s/%s", user, name)
request, err := c.Request("GET", endpoint, nil)
if err != nil {
return nil, err
}
response, err := checkResp(c.HTTPClient.Do(request))
if err != nil {
return nil, err
}
var app App
if err := decodeJSON(response, &app); err != nil {
return nil, err
}
return &app, nil
}
// CreateApp creates a new App under the given user with the given name. If the
// App is created successfully, it is returned. If the server returns any
// errors, an error is returned.
func (c *Client) CreateApp(user, name string) (*App, error) {
log.Printf("[INFO] creating application %s/%s", user, name)
body, err := json.Marshal(&appWrapper{&App{
User: user,
Name: name,
}})
if err != nil {
return nil, err
}
endpoint := "/api/v1/vagrant/applications"
request, err := c.Request("POST", endpoint, &RequestOptions{
Body: bytes.NewReader(body),
Headers: map[string]string{
"Content-Type": "application/json",
},
})
if err != nil {
return nil, err
}
response, err := checkResp(c.HTTPClient.Do(request))
if err != nil {
return nil, err
}
var app App
if err := decodeJSON(response, &app); err != nil {
return nil, err
}
return &app, nil
}
// appVersion represents a specific version of an App in Atlas. It is actually
// an upload container/wrapper.
type appVersion struct {
UploadPath string `json:"upload_path"`
Token string `json:"token"`
Version uint64 `json:"version"`
}
// appMetadataWrapper is a wrapper around a map the prefixes the json key with
// "metadata" when marshalled to format requests to the API properly.
type appMetadataWrapper struct {
Metadata map[string]interface{} `json:"metadata,omitempty"`
}
// UploadApp creates and uploads a new version for the App. If the server does not
// find the application, an error is returned. If the server does not accept the
// data, an error is returned.
//
// It is the responsibility of the caller to create a properly-formed data
// object; this method blindly passes along the contents of the io.Reader.
func (c *Client) UploadApp(app *App, metadata map[string]interface{},
data io.Reader, size int64) (uint64, error) {
log.Printf("[INFO] uploading application %s (%d bytes) with metadata %q",
app.Slug(), size, metadata)
endpoint := fmt.Sprintf("/api/v1/vagrant/applications/%s/%s/versions",
app.User, app.Name)
// If metadata was given, setup the RequestOptions to pass in the metadata
// with the request.
var ro *RequestOptions
if metadata != nil {
// wrap the struct into the correct JSON format
wrapper := struct {
Application *appMetadataWrapper `json:"application"`
}{
&appMetadataWrapper{metadata},
}
m, err := json.Marshal(wrapper)
if err != nil {
return 0, err
}
// Create the request options.
ro = &RequestOptions{
Body: bytes.NewReader(m),
BodyLength: int64(len(m)),
}
}
request, err := c.Request("POST", endpoint, ro)
if err != nil {
return 0, err
}
response, err := checkResp(c.HTTPClient.Do(request))
if err != nil {
return 0, err
}
var av appVersion
if err := decodeJSON(response, &av); err != nil {
return 0, err
}
if err := c.putFile(av.UploadPath, data, size); err != nil {
return 0, err
}
return av.Version, nil
}
|