Skip to content

Commit 585c8ff

Browse files
committed
Merge branch 'feat/add-lb-pn-resource' of github.com:yfodil/terraform-provider-scaleway into feat/add-lb-pn-resource
2 parents f488284 + 47d269a commit 585c8ff

25 files changed

+5702
-2388
lines changed

docs/resources/edge_services_dns_stage.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ In addition to all arguments above, the following attributes are exported:
3333

3434
- `id` - The ID of the DNS stage (UUID format).
3535
- `type` - The type of the stage.
36+
- `default_fqdn` - The Default Fully Qualified Domain Name attached to the stage.
3637
- `created_at` - The date and time of the creation of the DNS stage.
3738
- `updated_at` - The date and time of the last update of the DNS stage.
3839

docs/resources/instance_server.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ To retrieve more information by label please use: ```scw marketplace image get l
236236
~> **Important:** When updating `placement_group_id` the `state` must be set to `stopped`, otherwise it will fail.
237237

238238
- `root_volume` - (Optional) Root [volume](https://www.scaleway.com/en/developers/api/instance/#path-volume-types-list-volume-types) attached to the server on creation.
239+
- `name` - (Optional) Name of the root volume.
239240
- `volume_id` - (Optional) The volume ID of the root volume of the server, allows you to create server with an existing volume. If empty, will be computed to a created volume ID.
240241
- `size_in_gb` - (Required) Size of the root volume in gigabytes.
241242
To find the right size use [this endpoint](https://www.scaleway.com/en/developers/api/instance/#path-instances-list-all-instances) and

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ require (
3030
github.com/nats-io/jwt/v2 v2.8.0
3131
github.com/nats-io/nats.go v1.46.1
3232
github.com/robfig/cron/v3 v3.0.1
33-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.35.0.20251015050748-12aafea99911
33+
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.35.0.20251017125838-3eb0f2adaa94
3434
github.com/stretchr/testify v1.11.1
3535
golang.org/x/crypto v0.43.0
3636
golang.org/x/sync v0.17.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,8 +457,8 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR
457457
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
458458
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
459459
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
460-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.35.0.20251015050748-12aafea99911 h1:puwRtGGoGw9Rw3qlB7ltimV2+uugkalN08DyVEL1VoE=
461-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.35.0.20251015050748-12aafea99911/go.mod h1:SVm1Zk6UpZtqZN6KtEQpjC+v+Lir4tyVfhQTU19q3PA=
460+
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.35.0.20251017125838-3eb0f2adaa94 h1:39j+3zweuYVml1Ozahl7RVb0AtpaiQtRztjq4W3GvNM=
461+
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.35.0.20251017125838-3eb0f2adaa94/go.mod h1:SVm1Zk6UpZtqZN6KtEQpjC+v+Lir4tyVfhQTU19q3PA=
462462
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN3Uc8sB6B/s6Z4t2xvBgU1htSHuq8=
463463
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4=
464464
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=

internal/meta/extractors.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@ func getKeyInRawConfigMap(rawConfig map[string]cty.Value, key string, ty cty.Typ
137137
case value.Type().IsListType():
138138
// If it's a list and the second element of the key is an index, we look for the value in the list at the given index
139139
if index, err := strconv.Atoi(keys[1]); err == nil {
140+
if value.Length().Equals(cty.NumberIntVal(0)).True() {
141+
// If the list is empty, then the value is not set
142+
return nil, false
143+
}
144+
140145
return getKeyInRawConfigMap(value.AsValueSlice()[index].AsValueMap(), strings.Join(keys[2:], ""), ty)
141146
}
142147
// If it's a list and the second element of the key is '#', we look for the value in the list's first element

internal/services/edgeservices/dns_stage.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ func ResourceDNSStage() *schema.Resource {
6363
Computed: true,
6464
Description: "The type of the stage",
6565
},
66+
"default_fqdn": {
67+
Type: schema.TypeString,
68+
Computed: true,
69+
Description: "Default Fully Qualified Domain Name attached to the stage",
70+
},
6671
"created_at": {
6772
Type: schema.TypeString,
6873
Computed: true,
@@ -120,6 +125,7 @@ func ResourceDNSStageRead(ctx context.Context, d *schema.ResourceData, m any) di
120125
_ = d.Set("created_at", types.FlattenTime(dnsStage.CreatedAt))
121126
_ = d.Set("updated_at", types.FlattenTime(dnsStage.UpdatedAt))
122127
_ = d.Set("type", dnsStage.Type.String())
128+
_ = d.Set("default_fqdn", dnsStage.DefaultFqdn)
123129

124130
oldFQDNs := d.Get("fqdns").([]any)
125131
oldFQDNsSet := make(map[string]bool)

internal/services/instance/server.go

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ func ResourceServer() *schema.Resource {
126126
"name": {
127127
Type: schema.TypeString,
128128
Computed: true,
129+
Optional: true,
129130
Description: "Name of the root volume",
130131
},
131132
"size_in_gb": {
@@ -519,7 +520,7 @@ func ResourceInstanceServerCreate(ctx context.Context, d *schema.ResourceData, m
519520
}
520521

521522
////
522-
// Configure Block Volume
523+
// Configure Volumes
523524
////
524525
var diags diag.Diagnostics
525526

@@ -530,6 +531,11 @@ func ResourceInstanceServerCreate(ctx context.Context, d *schema.ResourceData, m
530531
}
531532
}
532533

534+
err = renameRootVolumeIfNeeded(d, api, zone, res.Server.Volumes)
535+
if err != nil {
536+
return diag.FromErr(err)
537+
}
538+
533539
////
534540
// Set user data
535541
////
@@ -639,10 +645,6 @@ func ResourceInstanceServerCreate(ctx context.Context, d *schema.ResourceData, m
639645
return append(diags, ResourceInstanceServerRead(ctx, d, m)...)
640646
}
641647

642-
func errorCheck(err error, message string) bool {
643-
return strings.Contains(err.Error(), message)
644-
}
645-
646648
//gocyclo:ignore
647649
func ResourceInstanceServerRead(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
648650
api, zone, id, err := instancehelpers.InstanceAndBlockAPIWithZoneAndID(m, d.Id())
@@ -701,6 +703,9 @@ func ResourceInstanceServerRead(ctx context.Context, d *schema.ResourceData, m a
701703
_ = d.Set("placement_group_policy_respected", server.PlacementGroup.PolicyRespected)
702704
}
703705

706+
////
707+
// Read server's public IPs
708+
////
704709
if ipID, hasIPID := d.GetOk("ip_id"); hasIPID {
705710
publicIP := FindIPInList(ipID.(string), server.PublicIPs)
706711
if publicIP != nil && !publicIP.Dynamic {
@@ -733,52 +738,28 @@ func ResourceInstanceServerRead(ctx context.Context, d *schema.ResourceData, m a
733738
_ = d.Set("admin_password_encryption_ssh_key_id", server.AdminPasswordEncryptionSSHKeyID)
734739
}
735740

736-
var additionalVolumesIDs []string
741+
////
742+
// Read server's volumes
743+
////
744+
rootVolume := make(map[string]any, 1)
745+
additionalVolumesIDs := make([]string, 0, len(server.Volumes)-1)
737746

738747
for i, serverVolume := range sortVolumeServer(server.Volumes) {
739748
if i == 0 {
740-
rootVolume := map[string]any{}
741-
742-
vs, ok := d.Get("root_volume").([]map[string]any)
743-
if ok && len(vs) > 0 {
744-
rootVolume = vs[0]
745-
}
746-
747-
vol, err := api.GetUnknownVolume(&instancehelpers.GetUnknownVolumeRequest{
748-
VolumeID: serverVolume.ID,
749-
Zone: server.Zone,
750-
})
749+
rootVolume, err = flattenServerVolume(api, serverVolume, zone)
751750
if err != nil {
752-
return diag.FromErr(fmt.Errorf("failed to read instance volume %s: %w", serverVolume.ID, err))
753-
}
754-
755-
rootVolume["volume_id"] = zonal.NewID(zone, vol.ID).String()
756-
if vol.Size != nil {
757-
rootVolume["size_in_gb"] = int(uint64(*vol.Size) / gb)
758-
} else if serverVolume.Size != nil {
759-
rootVolume["size_in_gb"] = int(uint64(*serverVolume.Size) / gb)
760-
}
761-
762-
if vol.IsBlockVolume() {
763-
rootVolume["sbs_iops"] = types.FlattenUint32Ptr(vol.Iops)
751+
return diag.FromErr(err)
764752
}
765753

766754
_, rootVolumeAttributeSet := d.GetOk("root_volume") // Related to https://github.com/hashicorp/terraform-plugin-sdk/issues/142
767755
rootVolume["delete_on_termination"] = d.Get("root_volume.0.delete_on_termination").(bool) || !rootVolumeAttributeSet
768-
rootVolume["volume_type"] = serverVolume.VolumeType
769-
rootVolume["boot"] = serverVolume.Boot
770-
rootVolume["name"] = serverVolume.Name
771-
772-
_ = d.Set("root_volume", []map[string]any{rootVolume})
773756
} else {
774757
additionalVolumesIDs = append(additionalVolumesIDs, zonal.NewID(zone, serverVolume.ID).String())
775758
}
776759
}
777760

761+
_ = d.Set("root_volume", []map[string]any{rootVolume})
778762
_ = d.Set("additional_volume_ids", additionalVolumesIDs)
779-
if len(additionalVolumesIDs) > 0 {
780-
_ = d.Set("additional_volume_ids", additionalVolumesIDs)
781-
}
782763

783764
////
784765
// Read server user data
@@ -1165,7 +1146,7 @@ func ResourceInstanceServerUpdate(ctx context.Context, d *schema.ResourceData, m
11651146
}
11661147
}
11671148

1168-
_, err = waitForServer(ctx, api.API, zone, id, d.Timeout(schema.TimeoutUpdate))
1149+
server, err = waitForServer(ctx, api.API, zone, id, d.Timeout(schema.TimeoutUpdate))
11691150
if err != nil {
11701151
return diag.FromErr(err)
11711152
}
@@ -1181,6 +1162,11 @@ func ResourceInstanceServerUpdate(ctx context.Context, d *schema.ResourceData, m
11811162
warnings = append(warnings, ResourceInstanceServerUpdateRootVolumeIOPS(ctx, api, zone, id, types.ExpandUint32Ptr(d.Get("root_volume.0.sbs_iops")))...)
11821163
}
11831164

1165+
err = renameRootVolumeIfNeeded(d, api, zone, server.Volumes)
1166+
if err != nil {
1167+
return diag.FromErr(err)
1168+
}
1169+
11841170
return append(warnings, ResourceInstanceServerRead(ctx, d, m)...)
11851171
}
11861172

@@ -1707,3 +1693,28 @@ func GetEndOfServiceDate(ctx context.Context, client *scw.Client, zone scw.Zone,
17071693

17081694
return "", fmt.Errorf("could not find product catalog entry for %q in %s", commercialType, zone)
17091695
}
1696+
1697+
func renameRootVolumeIfNeeded(d *schema.ResourceData, api *instancehelpers.BlockAndInstanceAPI, zone scw.Zone, volumes map[string]*instanceSDK.VolumeServer) error {
1698+
if volumes == nil || volumes["0"] == nil || volumes["0"].Name != nil {
1699+
return nil
1700+
}
1701+
1702+
if rootVolumeName, setByUser := meta.GetRawConfigForKey(d, "root_volume.0.name", cty.String); setByUser {
1703+
if *volumes["0"].Name != rootVolumeName {
1704+
_, err := api.UpdateVolume(&instanceSDK.UpdateVolumeRequest{
1705+
Zone: zone,
1706+
VolumeID: volumes["0"].ID,
1707+
Name: scw.StringPtr(rootVolumeName.(string)),
1708+
})
1709+
if err != nil {
1710+
return err
1711+
}
1712+
}
1713+
}
1714+
1715+
return nil
1716+
}
1717+
1718+
func errorCheck(err error, message string) bool {
1719+
return strings.Contains(err.Error(), message)
1720+
}

0 commit comments

Comments
 (0)