fixed logic error and fixed suggestion

This commit is contained in:
Luke Farnell 2017-05-16 17:05:40 -04:00 committed by Matthew Hooker
parent 078c139ef1
commit 3afd77a5e4
No known key found for this signature in database
GPG Key ID: 7B5F933D9CE8C6A1
2 changed files with 42 additions and 12 deletions

View File

@ -35,6 +35,16 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
return nil, nil return nil, nil
} }
func contains(slice []string, item string) bool {
set := make(map[string]struct{}, len(slice))
for _, s := range slice {
set[s] = struct{}{}
}
_, ok := set[item]
return ok
}
func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) { func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
client := godo.NewClient(oauth2.NewClient(oauth2.NoContext, &apiTokenSource{ client := godo.NewClient(oauth2.NewClient(oauth2.NoContext, &apiTokenSource{
AccessToken: b.config.APIToken, AccessToken: b.config.APIToken,
@ -54,15 +64,34 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
} }
regions, _, err := client.Regions.List(context.TODO(), opt) regions, _, err := client.Regions.List(context.TODO(), opt)
if err != nil { if err != nil {
return nil, fmt.Errorf("DigitalOcean: Unable to get regions, %s.", err) return nil, fmt.Errorf("DigitalOcean: Unable to get regions, %s", err)
} }
for _, snapShotRegion := range b.config.SnapshotRegions {
for _, region := range regions { var dcs []string
if snapShotRegion == region.Slug { for _, val := range regions {
dcs = append(dcs, val.Slug)
}
regionSet := make(map[string]struct{})
regionsMap := make([]string, 0, len(b.config.SnapshotRegions))
regionSet[b.config.Region] = struct{}{}
for _, region := range b.config.SnapshotRegions {
// If we already saw the region, then don't look again
if _, ok := regionSet[region]; ok {
continue
}
// Mark that we saw the region
regionSet[region] = struct{}{}
regionsMap = append(regionsMap, region)
}
for _, val := range regionsMap {
if contains(dcs, val) {
continue continue
} else { } else {
return nil, fmt.Errorf("DigitalOcean: Invalid region, %s.", snapShotRegion) return nil, fmt.Errorf("DigitalOcean: Invalid region, %s", val)
}
} }
} }
} }

View File

@ -19,6 +19,7 @@ func (s *stepSnapshot) Run(state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
c := state.Get("config").(Config) c := state.Get("config").(Config)
dropletId := state.Get("droplet_id").(int) dropletId := state.Get("droplet_id").(int)
var snapshotRegions []string
ui.Say(fmt.Sprintf("Creating snapshot: %v", c.SnapshotName)) ui.Say(fmt.Sprintf("Creating snapshot: %v", c.SnapshotName))
action, _, err := client.DropletActions.Snapshot(context.TODO(), dropletId, c.SnapshotName) action, _, err := client.DropletActions.Snapshot(context.TODO(), dropletId, c.SnapshotName)
@ -75,12 +76,12 @@ func (s *stepSnapshot) Run(state multistep.StateBag) multistep.StepAction {
regions = append(regions, region) regions = append(regions, region)
} }
c.SnapshotRegions = regions snapshotRegions = regions
for transfer := range c.SnapshotRegions { for transfer := range snapshotRegions {
transferRequest := &godo.ActionRequest{ transferRequest := &godo.ActionRequest{
"type": "transfer", "type": "transfer",
"region": c.SnapshotRegions[transfer], "region": snapshotRegions[transfer],
} }
imageTransfer, _, err := client.ImageActions.Transfer(context.TODO(), images[0].ID, transferRequest) imageTransfer, _, err := client.ImageActions.Transfer(context.TODO(), images[0].ID, transferRequest)
if err != nil { if err != nil {
@ -114,7 +115,7 @@ func (s *stepSnapshot) Run(state multistep.StateBag) multistep.StepAction {
log.Printf("Snapshot image ID: %d", imageId) log.Printf("Snapshot image ID: %d", imageId)
state.Put("snapshot_image_id", imageId) state.Put("snapshot_image_id", imageId)
state.Put("snapshot_name", c.SnapshotName) state.Put("snapshot_name", c.SnapshotName)
state.Put("regions", c.SnapshotRegions) state.Put("regions", snapshotRegions)
return multistep.ActionContinue return multistep.ActionContinue
} }