//nolint:golint package dcxAutohost import ( "fmt" "io/ioutil" "net/http" "strings" "github.com/pkg/errors" ) // FinalizeCurrent will create a job in in the DCX queue to configure and enable the // switch-ports that are connected to the given device's reservation; it's used to configure // switch ports prior to migrating. // // see: https://pages.github.rackspace.com/dcx/gateway/#/devices/finalizePost func (a *Client) FinalizeCurrent(device string) (string, error) { uri := fmt.Sprintf("/devices/%s/finalize_switch_ports", device) req, err := http.NewRequest(http.MethodPost, a.path(uri), nil) if err != nil { return "", err } req.Header.Set("X-Auth-Token", a.token) req.Header.Set("Accept-Encoding", "application/json") req.Header.Set("Content-Type", "application/json") res, err := http.DefaultClient.Do(req) if res != nil { defer func() { if defErr := res.Body.Close(); defErr != nil { err = errors.Wrap(err, defErr.Error()) } }() } if err != nil { return "", err } body, err := ioutil.ReadAll(res.Body) if err != nil { return "", err } if res.StatusCode > http.StatusNoContent { return "", DCXError(res.Status, body) } return strings.Replace(res.Header.Get("Location"), "/jobs/", "", -1), err } // FinalizeReservation will create a job in in the DCX queue to configure and enable the // switch-ports that are connected to the given device's reservation; it's used to configure // switch ports prior to migrating. // // see: https://pages.github.rackspace.com/dcx/gateway/#/devices/finalizeReservationPost func (a *Client) FinalizeReservation(device string) (string, error) { uri := fmt.Sprintf("/devices/%s/finalize_reservation_switch_ports", device) req, err := http.NewRequest(http.MethodPost, a.path(uri), nil) if err != nil { return "", err } req.Header.Set("X-Auth-Token", a.token) req.Header.Set("Accept-Encoding", "application/json") req.Header.Set("Content-Type", "application/json") res, err := http.DefaultClient.Do(req) if res != nil { defer func() { if defErr := res.Body.Close(); defErr != nil { err = errors.Wrap(err, defErr.Error()) } }() } if err != nil { return "", err } body, err := ioutil.ReadAll(res.Body) if err != nil { return "", err } if res.StatusCode > http.StatusNoContent { return "", DCXError(res.Status, body) } return strings.Replace(res.Header.Get("Location"), "/jobs/", "", -1), err }