atlas cloud token warn

This commit is contained in:
Matthew Hooker 2017-06-07 10:40:31 -07:00
parent 950861460e
commit dde40d54c6
No known key found for this signature in database
GPG Key ID: 7B5F933D9CE8C6A1
2 changed files with 63 additions and 3 deletions

View File

@ -7,6 +7,7 @@ package vagrantcloud
import ( import (
"fmt" "fmt"
"log" "log"
"os"
"strings" "strings"
"github.com/hashicorp/packer/common" "github.com/hashicorp/packer/common"
@ -40,9 +41,10 @@ type boxDownloadUrlTemplate struct {
} }
type PostProcessor struct { type PostProcessor struct {
config Config config Config
client *VagrantCloudClient client *VagrantCloudClient
runner multistep.Runner runner multistep.Runner
warnAtlasToken bool
} }
func (p *PostProcessor) Configure(raws ...interface{}) error { func (p *PostProcessor) Configure(raws ...interface{}) error {
@ -64,6 +66,17 @@ func (p *PostProcessor) Configure(raws ...interface{}) error {
p.config.VagrantCloudUrl = VAGRANT_CLOUD_URL p.config.VagrantCloudUrl = VAGRANT_CLOUD_URL
} }
if p.config.AccessToken == "" {
envToken := os.Getenv("VAGRANT_CLOUD_TOKEN")
if envToken == "" {
envToken = os.Getenv("ATLAS_TOKEN")
if envToken != "" {
p.warnAtlasToken = true
}
}
p.config.AccessToken = envToken
}
// Accumulate any errors // Accumulate any errors
errs := new(packer.MultiError) errs := new(packer.MultiError)
@ -101,6 +114,10 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac
"Unknown files in artifact from vagrant post-processor: %s", artifact.Files()) "Unknown files in artifact from vagrant post-processor: %s", artifact.Files())
} }
if p.warnAtlasToken {
ui.Message("Warning: Using Vagrant Cloud token found in ATLAS_TOKEN. Please make sure it is correct, or set VAGRANT_CLOUD_TOKEN")
}
// create the HTTP client // create the HTTP client
p.client = VagrantCloudClient{}.New(p.config.VagrantCloudUrl, p.config.AccessToken) p.client = VagrantCloudClient{}.New(p.config.VagrantCloudUrl, p.config.AccessToken)

View File

@ -2,6 +2,7 @@ package vagrantcloud
import ( import (
"bytes" "bytes"
"os"
"testing" "testing"
"github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer"
@ -24,6 +25,48 @@ func testBadConfig() map[string]interface{} {
} }
} }
func TestPostProcessor_Configure_fromVagrantEnv(t *testing.T) {
var p PostProcessor
config := testGoodConfig()
config["access_token"] = ""
os.Setenv("VAGRANT_CLOUD_TOKEN", "bar")
defer func() {
os.Setenv("VAGRANT_CLOUD_TOKEN", "")
}()
if err := p.Configure(config); err != nil {
t.Fatalf("err: %s", err)
}
if p.config.AccessToken != "bar" {
t.Fatalf("Expected to get token from VAGRANT_CLOUD_TOKEN env var. Got '%s' instead",
p.config.AccessToken)
}
}
func TestPostProcessor_Configure_fromAtlasEnv(t *testing.T) {
var p PostProcessor
config := testGoodConfig()
config["access_token"] = ""
os.Setenv("ATLAS_TOKEN", "foo")
defer func() {
os.Setenv("ATLAS_TOKEN", "")
}()
if err := p.Configure(config); err != nil {
t.Fatalf("err: %s", err)
}
if p.config.AccessToken != "foo" {
t.Fatalf("Expected to get token from ATLAS_TOKEN env var. Got '%s' instead",
p.config.AccessToken)
}
if !p.warnAtlasToken {
t.Fatal("Expected warn flag to be set when getting token from atlas env var.")
}
}
func TestPostProcessor_Configure_Good(t *testing.T) { func TestPostProcessor_Configure_Good(t *testing.T) {
var p PostProcessor var p PostProcessor
if err := p.Configure(testGoodConfig()); err != nil { if err := p.Configure(testGoodConfig()); err != nil {