Merge pull request #4089 from mitchellh/replace3076

command/push: Fix variable pushes to Atlas
This commit is contained in:
Matthew Hooker 2016-11-15 12:24:51 -08:00 committed by GitHub
commit c122a178a9
5 changed files with 62 additions and 20 deletions

View File

@ -42,14 +42,14 @@ func (c *PushCommand) Run(args []string) int {
var name string var name string
var create bool var create bool
f := c.Meta.FlagSet("push", FlagSetVars) flags := c.Meta.FlagSet("push", FlagSetVars)
f.Usage = func() { c.Ui.Error(c.Help()) } flags.Usage = func() { c.Ui.Error(c.Help()) }
f.StringVar(&token, "token", "", "token") flags.StringVar(&token, "token", "", "token")
f.StringVar(&message, "m", "", "message") flags.StringVar(&message, "m", "", "message")
f.StringVar(&message, "message", "", "message") flags.StringVar(&message, "message", "", "message")
f.StringVar(&name, "name", "", "name") flags.StringVar(&name, "name", "", "name")
f.BoolVar(&create, "create", false, "create (deprecated)") flags.BoolVar(&create, "create", false, "create (deprecated)")
if err := f.Parse(args); err != nil { if err := flags.Parse(args); err != nil {
return 1 return 1
} }
@ -57,9 +57,9 @@ func (c *PushCommand) Run(args []string) int {
c.Ui.Say("[DEPRECATED] -m/-message is deprecated and will be removed in a future Packer release") c.Ui.Say("[DEPRECATED] -m/-message is deprecated and will be removed in a future Packer release")
} }
args = f.Args() args = flags.Args()
if len(args) != 1 { if len(args) != 1 {
f.Usage() flags.Usage()
return 1 return 1
} }
@ -188,6 +188,9 @@ func (c *PushCommand) Run(args []string) int {
uploadOpts.Builds[b.Name] = info uploadOpts.Builds[b.Name] = info
} }
// Collect the variables from CLI args and any var files
uploadOpts.Vars = core.Context().UserVariables
// Add the upload metadata // Add the upload metadata
metadata := make(map[string]interface{}) metadata := make(map[string]interface{})
if message != "" { if message != "" {
@ -320,6 +323,17 @@ func (c *PushCommand) upload(
Name: bc.Name, Name: bc.Name,
Builds: make([]atlas.BuildConfigBuild, 0, len(opts.Builds)), Builds: make([]atlas.BuildConfigBuild, 0, len(opts.Builds)),
} }
// Build the BuildVars struct
buildVars := atlas.BuildVars{}
for k, v := range opts.Vars {
buildVars = append(buildVars, atlas.BuildVar{
Key: k,
Value: v,
})
}
for name, info := range opts.Builds { for name, info := range opts.Builds {
version.Builds = append(version.Builds, atlas.BuildConfigBuild{ version.Builds = append(version.Builds, atlas.BuildConfigBuild{
Name: name, Name: name,
@ -331,7 +345,7 @@ func (c *PushCommand) upload(
// Start the upload // Start the upload
doneCh, errCh := make(chan struct{}), make(chan error) doneCh, errCh := make(chan struct{}), make(chan error)
go func() { go func() {
err := c.client.UploadBuildConfigVersion(&version, opts.Metadata, r, r.Size) err := c.client.UploadBuildConfigVersion(&version, opts.Metadata, buildVars, r, r.Size)
if err != nil { if err != nil {
errCh <- err errCh <- err
return return
@ -348,6 +362,7 @@ type uploadOpts struct {
Slug string Slug string
Builds map[string]*uploadBuildInfo Builds map[string]*uploadBuildInfo
Metadata map[string]interface{} Metadata map[string]interface{}
Vars map[string]string
} }
type uploadBuildInfo struct { type uploadBuildInfo struct {

View File

@ -205,15 +205,28 @@ func TestPush_vars(t *testing.T) {
args := []string{ args := []string{
"-var", "name=foo/bar", "-var", "name=foo/bar",
"-var", "one=two",
"-var-file", filepath.Join(testFixture("push-vars"), "vars.json"),
"-var", "overridden=yes",
filepath.Join(testFixture("push-vars"), "template.json"), filepath.Join(testFixture("push-vars"), "template.json"),
} }
if code := c.Run(args); code != 0 { if code := c.Run(args); code != 0 {
fatalCommand(t, c.Meta) fatalCommand(t, c.Meta)
} }
expected := "foo/bar" if actualOpts.Slug != "foo/bar" {
if actualOpts.Slug != expected { t.Fatalf("bad slug: %s", actualOpts.Slug)
t.Fatalf("bad: %#v", actualOpts.Slug) }
expected := map[string]string{
"bar": "baz",
"name": "foo/bar",
"null": "",
"one": "two",
"overridden": "yes",
}
if !reflect.DeepEqual(actualOpts.Vars, expected) {
t.Fatalf("bad vars: got %#v\n expected %#v\n", actualOpts.Vars, expected)
} }
} }

View File

@ -0,0 +1,5 @@
{
"null": null,
"bar": "baz",
"overridden": "no"
}

View File

@ -13,6 +13,13 @@ type bcWrapper struct {
BuildConfig *BuildConfig `json:"build_configuration"` BuildConfig *BuildConfig `json:"build_configuration"`
} }
// Atlas expects a list of key/value vars
type BuildVar struct {
Key string `json:"key"`
Value string `json:"value"`
}
type BuildVars []BuildVar
// BuildConfig represents a Packer build configuration. // BuildConfig represents a Packer build configuration.
type BuildConfig struct { type BuildConfig struct {
// User is the namespace under which the build config lives // User is the namespace under which the build config lives
@ -126,7 +133,7 @@ func (c *Client) CreateBuildConfig(user, name string) (*BuildConfig, error) {
// //
// Actual API: "Create Build Config Version" // Actual API: "Create Build Config Version"
func (c *Client) UploadBuildConfigVersion(v *BuildConfigVersion, metadata map[string]interface{}, func (c *Client) UploadBuildConfigVersion(v *BuildConfigVersion, metadata map[string]interface{},
data io.Reader, size int64) error { vars BuildVars, data io.Reader, size int64) error {
log.Printf("[INFO] uploading build configuration version %s (%d bytes), with metadata %q", log.Printf("[INFO] uploading build configuration version %s (%d bytes), with metadata %q",
v.Slug(), size, metadata) v.Slug(), size, metadata)
@ -137,6 +144,7 @@ func (c *Client) UploadBuildConfigVersion(v *BuildConfigVersion, metadata map[st
var bodyData bcCreateWrapper var bodyData bcCreateWrapper
bodyData.Version.Builds = v.Builds bodyData.Version.Builds = v.Builds
bodyData.Version.Metadata = metadata bodyData.Version.Metadata = metadata
bodyData.Version.Vars = vars
body, err := json.Marshal(bodyData) body, err := json.Marshal(bodyData)
if err != nil { if err != nil {
return err return err
@ -179,5 +187,6 @@ type bcCreateWrapper struct {
Version struct { Version struct {
Metadata map[string]interface{} `json:"metadata,omitempty"` Metadata map[string]interface{} `json:"metadata,omitempty"`
Builds []BuildConfigBuild `json:"builds"` Builds []BuildConfigBuild `json:"builds"`
Vars BuildVars `json:"packer_vars,omitempty"`
} `json:"version"` } `json:"version"`
} }

10
vendor/vendor.json vendored
View File

@ -342,15 +342,15 @@
"checksumSHA1": "FUiF2WLrih0JdHsUTMMDz3DRokw=", "checksumSHA1": "FUiF2WLrih0JdHsUTMMDz3DRokw=",
"comment": "20141209094003-92-g95fa852", "comment": "20141209094003-92-g95fa852",
"path": "github.com/hashicorp/atlas-go/archive", "path": "github.com/hashicorp/atlas-go/archive",
"revision": "a32da833807becb5b150e125c859e01b707e74ca", "revision": "1792bd8de119ba49b17fd8d3c3c1f488ec613e62",
"revisionTime": "2016-10-12T21:43:57Z" "revisionTime": "2016-11-07T20:49:10Z"
}, },
{ {
"checksumSHA1": "aD7uHoVmfg2T9mpnVZ5dWe6rGtY=", "checksumSHA1": "lrfddRS4/LDKnF0sAbyZ59eUSjo=",
"comment": "20141209094003-92-g95fa852", "comment": "20141209094003-92-g95fa852",
"path": "github.com/hashicorp/atlas-go/v1", "path": "github.com/hashicorp/atlas-go/v1",
"revision": "a32da833807becb5b150e125c859e01b707e74ca", "revision": "1792bd8de119ba49b17fd8d3c3c1f488ec613e62",
"revisionTime": "2016-10-12T21:43:57Z" "revisionTime": "2016-11-07T20:49:10Z"
}, },
{ {
"checksumSHA1": "cdOCt0Yb+hdErz8NAQqayxPmRsY=", "checksumSHA1": "cdOCt0Yb+hdErz8NAQqayxPmRsY=",