From 5da380baedb784675f8c0071399ddf76c076a8f2 Mon Sep 17 00:00:00 2001 From: jasminSPC Date: Thu, 13 Oct 2016 00:41:04 +0200 Subject: [PATCH] Error handling Minor typo fixes --- builder/profitbricks/step_create_server.go | 39 +++++++++++++------ builder/profitbricks/step_take_snapshot.go | 10 ++++- .../source/docs/builders/profitbricks.html.md | 4 +- 3 files changed, 39 insertions(+), 14 deletions(-) diff --git a/builder/profitbricks/step_create_server.go b/builder/profitbricks/step_create_server.go index 102149a05..2e83fd6c8 100644 --- a/builder/profitbricks/step_create_server.go +++ b/builder/profitbricks/step_create_server.go @@ -63,8 +63,16 @@ func (s *stepCreateServer) Run(state multistep.StateBag) multistep.StepAction { datacenter = profitbricks.CompositeCreateDatacenter(datacenter) if datacenter.StatusCode > 299 { - ui.Error(parseErrorMessage(datacenter.Response)) - return multistep.ActionHalt + if datacenter.StatusCode > 299 { + var restError RestError + json.Unmarshal([]byte(datacenter.Response), &restError) + if ( len(restError.Messages) > 0) { + ui.Error(restError.Messages[0].Message) + } else { + ui.Error(datacenter.Response) + } + return multistep.ActionHalt + } } s.waitTillProvisioned(datacenter.Headers.Get("Location"), *c) @@ -116,16 +124,15 @@ func (s *stepCreateServer) Cleanup(state multistep.StateBag) { ui.Say("Removing Virtual Data Center...") profitbricks.SetAuth(c.PBUsername, c.PBPassword) - dcId := state.Get("datacenter_id").(string) - - resp := profitbricks.DeleteDatacenter(dcId) - - s.checkForErrors(resp) - err := s.waitTillProvisioned(resp.Headers.Get("Location"), *c) - if err != nil { - ui.Error(fmt.Sprintf( - "Error deleting Virtual Data Center. Please destroy it manually: %s", err)) + if dcId, ok := state.GetOk("datacenter_id"); ok { + resp := profitbricks.DeleteDatacenter(dcId.(string)) + s.checkForErrors(resp) + err := s.waitTillProvisioned(resp.Headers.Get("Location"), *c) + if err != nil { + ui.Error(fmt.Sprintf( + "Error deleting Virtual Data Center. Please destroy it manually: %s", err)) + } } } @@ -161,6 +168,16 @@ func (d *stepCreateServer) checkForErrors(instance profitbricks.Resp) error { return nil } +type RestError struct { + HttpStatus int `json:"httpStatus,omitempty"` + Messages []Message`json:"messages,omitempty"` +} + +type Message struct { + ErrorCode string `json:"errorCode,omitempty"` + Message string`json:"message,omitempty"` +} + func (d *stepCreateServer) getImageId(imageName string, c *Config) string { d.setPB(c.PBUsername, c.PBPassword, c.PBUrl) diff --git a/builder/profitbricks/step_take_snapshot.go b/builder/profitbricks/step_take_snapshot.go index 3997745c5..a3280e048 100644 --- a/builder/profitbricks/step_take_snapshot.go +++ b/builder/profitbricks/step_take_snapshot.go @@ -7,6 +7,7 @@ import ( "github.com/mitchellh/packer/packer" "github.com/profitbricks/profitbricks-sdk-go" "time" + "encoding/json" ) type stepTakeSnapshot struct{} @@ -27,7 +28,14 @@ func (s *stepTakeSnapshot) Run(state multistep.StateBag) multistep.StepAction { state.Put("snapshotname", c.SnapshotName) if snapshot.StatusCode > 299 { - ui.Say(fmt.Sprintf("Error occured %s", snapshot.Response)) + var restError RestError + json.Unmarshal([]byte(snapshot.Response), &restError) + if ( len(restError.Messages) > 0) { + ui.Error(restError.Messages[0].Message) + } else { + ui.Error(snapshot.Response) + } + return multistep.ActionHalt } diff --git a/website/source/docs/builders/profitbricks.html.md b/website/source/docs/builders/profitbricks.html.md index 4d8549c18..5df07ad34 100644 --- a/website/source/docs/builders/profitbricks.html.md +++ b/website/source/docs/builders/profitbricks.html.md @@ -25,7 +25,7 @@ builder. - `image` (string) - ProfitBricks volume image. Only Linux public images are supported. Defaults to "Ubuntu-16.04". To obtain full list of available images you can use [ProfitBricks CLI](https://github.com/profitbricks/profitbricks-cli#image). -- `password` (string) - ProfitBrucks password. This can be specified via environment variable `PROFITBRICKS_PASSWORD', if provided. The value definded in the config has precedence over environemnt variable. +- `password` (string) - ProfitBricks password. This can be specified via environment variable `PROFITBRICKS_PASSWORD', if provided. The value definded in the config has precedence over environemnt variable. - `username` (string) - ProfitBricks username. This can be specified via environment variable `PROFITBRICKS_USERNAME', if provided. The value definded in the config has precedence over environemnt variable. @@ -34,7 +34,7 @@ builder. - `cores` (integer) - Amount of CPU cores to use for this build. Defaults to "4". -- `disk_size` (string) - Amount of disk space for this image. Defaults to "50" +- `disk_size` (string) - Amount of disk space for this image in GB. Defaults to "50" - `disk_type` (string) - Type of disk to use for this image. Defaults to "HDD".