Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(instance_server): volume template creation #2640

Merged
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
21 changes: 12 additions & 9 deletions internal/services/instance/helpers_instance_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,37 +29,42 @@ type UnknownVolume struct {
ServerID *string
Boot *bool

// IsBlockVolume is true if volume is managed by block API
IsBlockVolume bool

InstanceVolumeType instance.VolumeVolumeType
}

// VolumeTemplate returns a template to be used for servers requests.
func (volume *UnknownVolume) VolumeTemplate() *instance.VolumeServerTemplate {
template := &instance.VolumeServerTemplate{}

if volume.ID != "" {
template.ID = &volume.ID
if !volume.IsBlockVolume() {
template.Name = &volume.Name
}
} else {
template.VolumeType = volume.InstanceVolumeType
template.Size = volume.Size
}

if volume.Boot != nil {
template.Boot = volume.Boot
}

if volume.IsBlockVolume {
if volume.IsBlockVolume() {
template.VolumeType = volume.InstanceVolumeType
} else {
template.Name = &volume.Name
}

return template
}

// IsLocal returns true if the volume is a local volume
func (volume *UnknownVolume) IsLocal() bool {
return !volume.IsBlockVolume && volume.InstanceVolumeType == instance.VolumeVolumeTypeLSSD
return !volume.IsBlockVolume() && volume.InstanceVolumeType == instance.VolumeVolumeTypeLSSD
}

// IsBlockVolume is true if volume is managed by block API
func (volume *UnknownVolume) IsBlockVolume() bool {
return volume.InstanceVolumeType == instance.VolumeVolumeTypeSbsVolume
}

// IsAttached returns true if the volume is attached to a server
Expand All @@ -83,7 +88,6 @@ func (api *BlockAndInstanceAPI) GetUnknownVolume(req *GetUnknownVolumeRequest, o
ID: getVolumeResponse.Volume.ID,
Name: getVolumeResponse.Volume.Name,
Size: &getVolumeResponse.Volume.Size,
IsBlockVolume: false,
InstanceVolumeType: getVolumeResponse.Volume.VolumeType,
}
if getVolumeResponse.Volume.Server != nil {
Expand All @@ -106,7 +110,6 @@ func (api *BlockAndInstanceAPI) GetUnknownVolume(req *GetUnknownVolumeRequest, o
ID: blockVolume.ID,
Name: blockVolume.Name,
Size: &blockVolume.Size,
IsBlockVolume: true,
InstanceVolumeType: instance.VolumeVolumeTypeSbsVolume,
}
for _, ref := range blockVolume.References {
Expand Down
77 changes: 77 additions & 0 deletions internal/services/instance/helpers_instance_block_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package instance_test

import (
"testing"

instanceSDK "github.com/scaleway/scaleway-sdk-go/api/instance/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/instance"
"github.com/stretchr/testify/assert"
)

func TestUnknownVolume_VolumeTemplate(t *testing.T) {
type fields struct {
Zone scw.Zone
ID string
Name string
Size *scw.Size
ServerID *string
Boot *bool
IsBlockVolume bool
InstanceVolumeType instanceSDK.VolumeVolumeType
}
tests := []struct {
name string
volume *instance.UnknownVolume
want *instanceSDK.VolumeServerTemplate
}{
{
name: "Root Volume",
volume: &instance.UnknownVolume{
Name: "",
ID: "",
InstanceVolumeType: instanceSDK.VolumeVolumeTypeLSSD,
Size: scw.SizePtr(20000000000),
Boot: scw.BoolPtr(false),
},
want: &instanceSDK.VolumeServerTemplate{
Boot: scw.BoolPtr(false),
Size: scw.SizePtr(20000000000),
VolumeType: instanceSDK.VolumeVolumeTypeLSSD,
},
},
{
name: "Root Volume from ID",
volume: &instance.UnknownVolume{
Name: "tf-vol-stoic-johnson",
ID: "25152794-d15a-4dd5-abfc-b19ec276aa20",
InstanceVolumeType: instanceSDK.VolumeVolumeTypeLSSD,
Size: scw.SizePtr(20000000000),
Boot: scw.BoolPtr(true),
},
want: &instanceSDK.VolumeServerTemplate{
ID: scw.StringPtr("25152794-d15a-4dd5-abfc-b19ec276aa20"),
Boot: scw.BoolPtr(true),
Name: scw.StringPtr("tf-vol-stoic-johnson"),
},
},
{
name: "Additional Volume sbs",
volume: &instance.UnknownVolume{
Name: "tf-volume-elegant-minsky",
ID: "cc380989-b71b-47f0-829f-062e329f4097",
InstanceVolumeType: instanceSDK.VolumeVolumeTypeSbsVolume,
Size: scw.SizePtr(10000000000),
},
want: &instanceSDK.VolumeServerTemplate{
ID: scw.StringPtr("cc380989-b71b-47f0-829f-062e329f4097"),
VolumeType: instanceSDK.VolumeVolumeTypeSbsVolume,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, tt.volume.VolumeTemplate())
})
}
}
Loading