Skip to content

Commit

Permalink
Merge pull request #19 from loic-roux-404/feat-enhancements
Browse files Browse the repository at this point in the history
feat: image wait for download, reupload if changed + existing instance import
  • Loading branch information
ngotzmann authored Sep 8, 2023
2 parents 3d7d813 + 94b2f5d commit 1283d91
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
10 changes: 10 additions & 0 deletions contabo/handleErrors.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,13 @@ func MultipleDataObjectsError(
Detail: "The API response for a specific object contained multiple objects.",
})
}

func HandleDownloadErrors(
diags diag.Diagnostics,
) diag.Diagnostics {
return append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Download error, check the url availability and retry",
Detail: "Download error, check the url availability and retry",
})
}
45 changes: 45 additions & 0 deletions contabo/resource_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import (
uuid "github.com/satori/go.uuid"
)

const (
DOWNLOADING string = "downloading"
ERROR string = "error"
)

func resourceImage() *schema.Resource {
return &schema.Resource{
Description: "In order to provide a custom image, please specify an URL from which the image can be downloaded directly. A custom image must be in either `.iso` or `.qcow2` format. Other formats will be rejected. Please note that downloading can take a while depending on network speed resp. bandwidth and size of image. You can check the status by retrieving information about the image via a GET request. Download will be rejected if you have exceeded your limits.",
Expand Down Expand Up @@ -45,6 +50,7 @@ func resourceImage() *schema.Resource {
Type: schema.TypeString,
Required: true,
Description: "URL from which the image has been downloaded.",
ForceNew: true,
},
"uploaded_size_mb": {
Type: schema.TypeInt,
Expand Down Expand Up @@ -147,6 +153,13 @@ func resourceImageRead(ctx context.Context, d *schema.ResourceData, m interface{
XRequestId(uuid.NewV4().String()).
Execute()

image, diag := pollImageDownloaded(diags, client, ctx, imageId)

if err != nil || image == nil {
diags = append(diags, diag...)
return AddImageToData(res.Data[0], d, diags)
}

if err != nil {
return HandleResponseErrors(diags, httpResp)
} else if len(res.Data) != 1 {
Expand Down Expand Up @@ -258,3 +271,35 @@ func AddImageToData(

return diags
}

func pollImageDownloaded(
diags diag.Diagnostics,
client *openapi.APIClient,
ctx context.Context,
imageId string,
) (*openapi.ImageResponse, diag.Diagnostics) {
res, httpResp, err := client.ImagesApi.
RetrieveImage(ctx, imageId).
XRequestId(uuid.NewV4().String()).
Execute()

if err != nil {
return nil, HandleResponseErrors(diags, httpResp)
} else if len(res.Data) != 1 {
return nil, MultipleDataObjectsError(diags)
}

status := res.Data[0].Status

if status == ERROR {
return nil, HandleDownloadErrors(diags)
}

if status == DOWNLOADING {
time.Sleep(time.Second)
return pollImageDownloaded(diags, client, ctx, imageId)
}

return &res.Data[0], nil
}

15 changes: 15 additions & 0 deletions contabo/resource_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ func resourceInstance() *schema.Resource {
Type: schema.TypeString,
Computed: true,
Description: "The identifier of the compute instance. Use it to manage it!",

},
"existing_instance_id": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: "The identifier of the existing compute instance. (override id)",
},
"last_updated": {
Type: schema.TypeString,
Expand Down Expand Up @@ -262,6 +269,14 @@ func resourceInstanceCreate(ctx context.Context, d *schema.ResourceData, m inter
var diags diag.Diagnostics
client := m.(*openapi.APIClient)

extstingId := d.Get("existing_instance_id").(string)

if extstingId != "" {
d.SetId(extstingId)

return resourceInstanceUpdate(ctx, d, m)
}

createInstanceRequest := openapi.NewCreateInstanceRequestWithDefaults()

displayName := d.Get("display_name").(string)
Expand Down

0 comments on commit 1283d91

Please sign in to comment.