Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion cmd/instances/cancel.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"contabo.com/cli/cntb/client"
contaboCmd "contabo.com/cli/cntb/cmd"
"contabo.com/cli/cntb/cmd/util"
"contabo.com/cli/cntb/openapi"
"contabo.com/cli/cntb/outputFormatter"
uuid "github.com/satori/go.uuid"
log "github.com/sirupsen/logrus"
Expand All @@ -21,9 +22,14 @@ var instanceCancelCmd = &cobra.Command{
Long: `Your are free to cancel a previously created instances at any time.`,
Example: `cntb cancel instance 12345`,
Run: func(cmd *cobra.Command, args []string) {
// Create the cancel request body
cancelRequest := openapi.NewCancelInstanceRequest()

resp, httpResp, err := client.ApiClient().InstancesApi.
CancelInstance(context.Background(), cancelInstanceId).
XRequestId(uuid.NewV4().String()).Execute()
XRequestId(uuid.NewV4().String()).
CancelInstanceRequest(*cancelRequest).
Execute()

util.HandleErrors(err, httpResp, "while canceling the instance")

Expand Down
12 changes: 9 additions & 3 deletions cmd/objectStorage/cancel.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"contabo.com/cli/cntb/client"
contaboCmd "contabo.com/cli/cntb/cmd"
"contabo.com/cli/cntb/cmd/util"
"contabo.com/cli/cntb/openapi"
"contabo.com/cli/cntb/outputFormatter"
uuid "github.com/satori/go.uuid"
log "github.com/sirupsen/logrus"
Expand All @@ -19,9 +20,14 @@ var objectStorageCancelCmd = &cobra.Command{
Long: "Your are free to cancel a previously created object storage at any time.",
Example: "cntb cancel objectStorage 1f771979-1c0f-44ab-ab5b-2c3752731b45",
Run: func(cmd *cobra.Command, args []string) {
// Create the cancel request body
cancelRequest := openapi.NewCancelObjectStorageRequest()

resp, httpResp, err := client.ApiClient().ObjectStoragesApi.
CancelObjectStorage(context.Background(), cancelbjectStorageId).
XRequestId(uuid.NewV4().String()).Execute()
CancelObjectStorage(context.Background(), cancelObjectStorageId).
XRequestId(uuid.NewV4().String()).
CancelObjectStorageRequest(*cancelRequest).
Execute()

util.HandleErrors(err, httpResp, "while canceling the object storage")

Expand Down Expand Up @@ -53,7 +59,7 @@ var objectStorageCancelCmd = &cobra.Command{
log.Fatal("Too many positional arguments.")
}

cancelbjectStorageId = args[0]
cancelObjectStorageId = args[0]

return nil
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/objectStorage/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ var (

// cancel
var (
cancelbjectStorageId string
cancelObjectStorageId string
)

// stats
Expand Down
21 changes: 14 additions & 7 deletions cmd/util/handleErrors.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,21 @@ func HandleErrors(
) {
if err != nil {
apiError := jmap{}
responseBody, _ := ioutil.ReadAll(httpResp.Body)
err := json.Unmarshal(responseBody, &apiError)
if err != nil {
log.Error(fmt.Sprintf("Error while parsing response 500 - Internal Server Error, retry or contact support\n"))
log.Debug(fmt.Sprintf("Error while parsing response: %v", err))
// Check if httpResp is not nil before accessing its Body
if httpResp != nil && httpResp.Body != nil {
responseBody, _ := ioutil.ReadAll(httpResp.Body)
err := json.Unmarshal(responseBody, &apiError)
if err != nil {
log.Error(fmt.Sprintf("Error while parsing response 500 - Internal Server Error, retry or contact support\n"))
log.Debug(fmt.Sprintf("Error while parsing response: %v", err))
} else {
log.Error(fmt.Sprintf("Error %v: %v - %v\n", info, apiError["statusCode"], apiError["message"]))
log.Debug(fmt.Sprintf("Full response: %v\n", httpResp))
}
} else {
log.Error(fmt.Sprintf("Error %v: %v - %v\n", info, apiError["statusCode"], apiError["message"]))
log.Debug(fmt.Sprintf("Full response: %v\n", httpResp))
// Handle case where httpResp is nil (e.g., network error)
log.Error(fmt.Sprintf("Error %v: %v\n", info, err))
log.Debug(fmt.Sprintf("No HTTP response available, error: %v", err))
}
log.Fatal("Aborting, due to errors")
}
Expand Down