Error handling

Minor typo fixes
This commit is contained in:
jasminSPC 2016-10-13 00:41:04 +02:00
parent dca286bf38
commit 5da380baed
3 changed files with 39 additions and 14 deletions

View File

@ -63,9 +63,17 @@ func (s *stepCreateServer) Run(state multistep.StateBag) multistep.StepAction {
datacenter = profitbricks.CompositeCreateDatacenter(datacenter)
if datacenter.StatusCode > 299 {
ui.Error(parseErrorMessage(datacenter.Response))
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)
state.Put("datacenter_id", datacenter.Id)
@ -116,18 +124,17 @@ 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)
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))
}
}
}
func (d *stepCreateServer) waitTillProvisioned(path string, config Config) error {
d.setPB(config.PBUsername, config.PBPassword, config.PBUrl)
@ -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)

View File

@ -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
}

View File

@ -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".