@@ -389,22 +389,6 @@ func ResourceInstanceServerCreate(ctx context.Context, d *schema.ResourceData, m
389389 commercialType := d .Get ("type" ).(string )
390390
391391 imageUUID := locality .ExpandID (d .Get ("image" ))
392- if imageUUID != "" && ! scwvalidation .IsUUID (imageUUID ) {
393- // Replace dashes with underscores ubuntu-focal -> ubuntu_focal
394- imageLabel := formatImageLabel (imageUUID )
395-
396- marketPlaceAPI := marketplace .NewAPI (meta .ExtractScwClient (m ))
397- image , err := marketPlaceAPI .GetLocalImageByLabel (& marketplace.GetLocalImageByLabelRequest {
398- CommercialType : commercialType ,
399- Zone : zone ,
400- ImageLabel : imageLabel ,
401- Type : volumeTypeToMarketplaceFilter (d .Get ("root_volume.0.volume_type" )),
402- })
403- if err != nil {
404- return diag .FromErr (fmt .Errorf ("could not get image '%s': %s" , zonal .NewID (zone , imageLabel ), err ))
405- }
406- imageUUID = image .ID
407- }
408392
409393 req := & instanceSDK.CreateServerRequest {
410394 Zone : zone ,
@@ -417,10 +401,6 @@ func ResourceInstanceServerCreate(ctx context.Context, d *schema.ResourceData, m
417401 RoutedIPEnabled : types .ExpandBoolPtr (types .GetBool (d , "routed_ip_enabled" )),
418402 }
419403
420- if imageUUID != "" {
421- req .Image = scw .StringPtr (imageUUID )
422- }
423-
424404 enableIPv6 , ok := d .GetOk ("enable_ipv6" )
425405 if ok {
426406 req .EnableIPv6 = scw .BoolPtr (enableIPv6 .(bool )) //nolint:staticcheck
@@ -473,6 +453,27 @@ func ResourceInstanceServerCreate(ctx context.Context, d *schema.ResourceData, m
473453 return diag .FromErr (err )
474454 }
475455
456+ if imageUUID != "" && ! scwvalidation .IsUUID (imageUUID ) {
457+ // Replace dashes with underscores ubuntu-focal -> ubuntu_focal
458+ imageLabel := formatImageLabel (imageUUID )
459+
460+ marketPlaceAPI := marketplace .NewAPI (meta .ExtractScwClient (m ))
461+ image , err := marketPlaceAPI .GetLocalImageByLabel (& marketplace.GetLocalImageByLabelRequest {
462+ CommercialType : commercialType ,
463+ Zone : zone ,
464+ ImageLabel : imageLabel ,
465+ Type : volumeTypeToMarketplaceFilter (req .Volumes ["0" ].VolumeType ),
466+ })
467+ if err != nil {
468+ return diag .FromErr (fmt .Errorf ("could not get image '%s': %s" , zonal .NewID (zone , imageLabel ), err ))
469+ }
470+ imageUUID = image .ID
471+ }
472+
473+ if imageUUID != "" {
474+ req .Image = scw .StringPtr (imageUUID )
475+ }
476+
476477 res , err := api .CreateServer (req , scw .WithContext (ctx ))
477478 if err != nil {
478479 return diag .FromErr (err )
@@ -1431,7 +1432,7 @@ func instanceServerVolumesUpdate(ctx context.Context, d *schema.ResourceData, ap
14311432
14321433 // local volumes can only be added when the server is stopped
14331434 if volumeHasChange && ! serverIsStopped && volume .IsLocal () && volume .IsAttached () {
1434- return nil , errors .New ("instanceSDK must be stopped to change local volumes" )
1435+ return nil , errors .New ("instance must be stopped to change local volumes" )
14351436 }
14361437 volumes [strconv .Itoa (i + 1 )] = volume .VolumeTemplate ()
14371438 }
0 commit comments