Skip to content
Merged
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
32 changes: 30 additions & 2 deletions v1/providers/shadeform/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package v1

import (
"context"
"encoding/json"
"fmt"
"io"
"strings"
Expand All @@ -21,6 +22,15 @@ const (
instanceNameSeparator = "_"
)

const (
OutOfStockErrorCode = "OUT_OF_STOCK"
)

type DefaultErrorResponse struct {
ErrorCode string `json:"error_code"`
Error string `json:"error"`
}

func (c *ShadeformClient) CreateInstance(ctx context.Context, attrs v1.CreateInstanceAttrs) (*v1.Instance, error) { //nolint:gocyclo,funlen // ok
authCtx := c.makeAuthContext(ctx)

Expand Down Expand Up @@ -110,8 +120,26 @@ func (c *ShadeformClient) CreateInstance(ctx context.Context, attrs v1.CreateIns
defer func() { _ = httpResp.Body.Close() }()
}
if err != nil {
httpMessage, _ := io.ReadAll(httpResp.Body)
return nil, errors.WrapAndTrace(fmt.Errorf("failed to create instance: %w, %s", err, string(httpMessage)))
if httpResp.StatusCode == 409 {
// Shadeform provides more details on 409 errors in the response body
httpMessage, _ := io.ReadAll(httpResp.Body)
jsonStr := string(httpMessage)

var errorResponse DefaultErrorResponse
err = json.Unmarshal([]byte(jsonStr), &errorResponse)
if err != nil {
return nil, errors.WrapAndTrace(fmt.Errorf("failed to create instance: %w, %s", err, string(httpMessage)))
}

if errorResponse.ErrorCode == OutOfStockErrorCode {
return nil, v1.ErrInsufficientResources
} else {
return nil, errors.WrapAndTrace(fmt.Errorf("failed to create instance: %w, %s", err, string(httpMessage)))
}
} else {
httpMessage, _ := io.ReadAll(httpResp.Body)
return nil, errors.WrapAndTrace(fmt.Errorf("failed to create instance: %w, %s", err, string(httpMessage)))
}
}

if resp == nil {
Expand Down
57 changes: 55 additions & 2 deletions v1/providers/shadeform/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import (
"context"
"github.com/brevdev/cloud/internal/validation"

Check failure on line 5 in v1/providers/shadeform/validation_test.go

View workflow job for this annotation

GitHub Actions / Test and Lint

File is not properly formatted (gofumpt)
openapi "github.com/brevdev/cloud/v1/providers/shadeform/gen/shadeform"
"os"
"testing"
"time"

Check failure on line 10 in v1/providers/shadeform/validation_test.go

View workflow job for this annotation

GitHub Actions / Test and Lint

File is not properly formatted (gofumpt)
"github.com/brevdev/cloud/internal/ssh"
"github.com/brevdev/cloud/internal/validation"
v1 "github.com/brevdev/cloud/v1"
openapi "github.com/brevdev/cloud/v1/providers/shadeform/gen/shadeform"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -118,6 +118,59 @@
})
}

func TestOutOfStockError(t *testing.T) {
checkSkip(t)
apiKey := getAPIKey()

ctx, cancel := context.WithTimeout(context.Background(), 15*time.Minute)
t.Cleanup(cancel)

client := NewShadeformClient("validation-test", apiKey)
client.WithConfiguration(Configuration{})

_, err := client.CreateInstance(ctx, v1.CreateInstanceAttrs{
RefID: uuid.New().String(),
InstanceType: "datacrunch_H200x8",
Location: "abc123", // Put a region that is not valid
PublicKey: ssh.GetTestPublicKey(),
Name: "test_name",
FirewallRules: v1.FirewallRules{
EgressRules: []v1.FirewallRule{
{
ID: "test-rule1",
FromPort: 80,
ToPort: 8080,
IPRanges: []string{"127.0.0.1", "10.0.0.0/24"},
},
{
ID: "test-rule2",
FromPort: 5432,
ToPort: 5432,
IPRanges: []string{"127.0.0.1", "10.0.0.0/24"},
},
},
IngressRules: []v1.FirewallRule{
{
ID: "test-rule3",
FromPort: 80,
ToPort: 8080,
IPRanges: []string{"127.0.0.1", "10.0.0.0/24"},
},
{
ID: "test-rule4",
FromPort: 5432,
ToPort: 5432,
IPRanges: []string{"127.0.0.1", "10.0.0.0/24"},
},
},
},
})
if err == nil {
t.Fatalf("ValidateCreateInstance failed: Should have resulted in an insufficientResourcesError")
}
require.True(t, err.Error() == v1.ErrInsufficientResources.Error(), "Error must be ErrInsufficientResources")
}

func checkSkip(t *testing.T) {
apiKey := getAPIKey()
isValidationTest := os.Getenv("VALIDATION_TEST")
Expand Down
Loading