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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
package atlas
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
)
// bcWrapper is the API wrapper since the server wraps the resulting object.
type bcWrapper struct {
BuildConfig *BuildConfig `json:"build_configuration"`
}
// Atlas expects a list of key/value vars
type BuildVar struct {
Key string `json:"key"`
Value string `json:"value"`
Sensitive bool `json:"sensitive"`
}
type BuildVars []BuildVar
// BuildConfig represents a Packer build configuration.
type BuildConfig struct {
// User is the namespace under which the build config lives
User string `json:"username"`
// Name is the actual name of the build config, unique in the scope
// of the username.
Name string `json:"name"`
}
// Slug returns the slug format for this BuildConfig (User/Name)
func (b *BuildConfig) Slug() string {
return fmt.Sprintf("%s/%s", b.User, b.Name)
}
// BuildConfigVersion represents a single uploaded (or uploadable) version
// of a build configuration.
type BuildConfigVersion struct {
// The fields below are the username/name combo to uniquely identify
// a build config.
User string `json:"username"`
Name string `json:"name"`
// Builds is the list of builds that this version supports.
Builds []BuildConfigBuild
}
// Slug returns the slug format for this BuildConfigVersion (User/Name)
func (bv *BuildConfigVersion) Slug() string {
return fmt.Sprintf("%s/%s", bv.User, bv.Name)
}
// BuildConfigBuild is a single build that is present in an uploaded
// build configuration.
type BuildConfigBuild struct {
// Name is a unique name for this build
Name string `json:"name"`
// Type is the type of builder that this build needs to run on,
// such as "amazon-ebs" or "qemu".
Type string `json:"type"`
// Artifact is true if this build results in one or more artifacts
// being sent to Atlas
Artifact bool `json:"artifact"`
}
// BuildConfig gets a single build configuration by user and name.
func (c *Client) BuildConfig(user, name string) (*BuildConfig, error) {
log.Printf("[INFO] getting build configuration %s/%s", user, name)
endpoint := fmt.Sprintf("/api/v1/packer/build-configurations/%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 bc BuildConfig
if err := decodeJSON(response, &bc); err != nil {
return nil, err
}
return &bc, nil
}
// CreateBuildConfig creates a new build configuration.
func (c *Client) CreateBuildConfig(user, name string) (*BuildConfig, error) {
log.Printf("[INFO] creating build configuration %s/%s", user, name)
endpoint := "/api/v1/packer/build-configurations"
body, err := json.Marshal(&bcWrapper{
BuildConfig: &BuildConfig{
User: user,
Name: name,
},
})
if err != nil {
return nil, err
}
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 bc BuildConfig
if err := decodeJSON(response, &bc); err != nil {
return nil, err
}
return &bc, nil
}
// UploadBuildConfigVersion creates a single build configuration version
// and uploads the template associated with it.
//
// Actual API: "Create Build Config Version"
func (c *Client) UploadBuildConfigVersion(v *BuildConfigVersion, metadata map[string]interface{},
vars BuildVars, data io.Reader, size int64) error {
log.Printf("[INFO] uploading build configuration version %s (%d bytes), with metadata %q",
v.Slug(), size, metadata)
endpoint := fmt.Sprintf("/api/v1/packer/build-configurations/%s/%s/versions",
v.User, v.Name)
var bodyData bcCreateWrapper
bodyData.Version.Builds = v.Builds
bodyData.Version.Metadata = metadata
bodyData.Version.Vars = vars
body, err := json.Marshal(bodyData)
if err != nil {
return err
}
request, err := c.Request("POST", endpoint, &RequestOptions{
Body: bytes.NewReader(body),
Headers: map[string]string{
"Content-Type": "application/json",
},
})
if err != nil {
return err
}
response, err := checkResp(c.HTTPClient.Do(request))
if err != nil {
return err
}
var bv bcCreate
if err := decodeJSON(response, &bv); err != nil {
return err
}
if err := c.putFile(bv.UploadPath, data, size); err != nil {
return err
}
return nil
}
// bcCreate is the struct returned when creating a build configuration.
type bcCreate struct {
UploadPath string `json:"upload_path"`
}
// bcCreateWrapper is the wrapper for creating a build config.
type bcCreateWrapper struct {
Version struct {
Metadata map[string]interface{} `json:"metadata,omitempty"`
Builds []BuildConfigBuild `json:"builds"`
Vars BuildVars `json:"packer_vars,omitempty"`
} `json:"version"`
}
|