From 238b0405abe943a263c583559211016427c71bfb Mon Sep 17 00:00:00 2001 From: Toni Kangas Date: Fri, 19 Jan 2024 17:31:51 +0200 Subject: [PATCH] feat(storage): add support for encryption --- upcloud/request/server.go | 9 +++++---- upcloud/request/storage.go | 8 +++++--- upcloud/request/storage_test.go | 10 ++++++---- upcloud/server_test.go | 2 ++ upcloud/storage.go | 10 ++++++---- upcloud/storage_test.go | 21 ++++++++++++--------- 6 files changed, 36 insertions(+), 24 deletions(-) diff --git a/upcloud/request/server.go b/upcloud/request/server.go index e0b83a45..52b0c683 100644 --- a/upcloud/request/server.go +++ b/upcloud/request/server.go @@ -74,10 +74,11 @@ func (s CreateServerIPAddressSlice) MarshalJSON() ([]byte, error) { // CreateServerStorageDevice represents a storage device for a CreateServerRequest type CreateServerStorageDevice struct { - Action string `json:"action"` - Address string `json:"address,omitempty"` - Storage string `json:"storage"` - Title string `json:"title,omitempty"` + Action string `json:"action"` + Address string `json:"address,omitempty"` + Encrypted upcloud.Boolean `json:"encrypted,omitempty"` + Storage string `json:"storage"` + Title string `json:"title,omitempty"` // Storage size in gigabytes Size int `json:"size,omitempty"` Tier string `json:"tier,omitempty"` diff --git a/upcloud/request/storage.go b/upcloud/request/storage.go index 901c4da2..861c1d9d 100644 --- a/upcloud/request/storage.go +++ b/upcloud/request/storage.go @@ -66,6 +66,7 @@ func (r *GetStorageDetailsRequest) RequestURL() string { // CreateStorageRequest represents a request to create a storage device type CreateStorageRequest struct { Size int `json:"size,string"` + Encrypted upcloud.Boolean `json:"encrypted,omitempty"` Tier string `json:"tier,omitempty"` Title string `json:"title,omitempty"` Zone string `json:"zone"` @@ -186,9 +187,10 @@ func (r *DeleteStorageRequest) RequestURL() string { type CloneStorageRequest struct { UUID string `json:"-"` - Zone string `json:"zone"` - Tier string `json:"tier,omitempty"` - Title string `json:"title"` + Encrypted upcloud.Boolean `json:"encrypted,omitempty"` + Zone string `json:"zone"` + Tier string `json:"tier,omitempty"` + Title string `json:"title"` } // RequestURL implements the Request interface diff --git a/upcloud/request/storage_test.go b/upcloud/request/storage_test.go index 3def41fc..61a7aa35 100644 --- a/upcloud/request/storage_test.go +++ b/upcloud/request/storage_test.go @@ -59,10 +59,11 @@ func TestGetStorageDetailsRequest(t *testing.T) { // TestCreateStorageRequest tests that CreateStorageRequest objects behave correctly func TestCreateStorageRequest(t *testing.T) { request := CreateStorageRequest{ - Tier: upcloud.StorageTierMaxIOPS, - Title: "Test storage", - Size: 10, - Zone: "fi-hel2", + Encrypted: upcloud.FromBool(true), + Tier: upcloud.StorageTierMaxIOPS, + Title: "Test storage", + Size: 10, + Zone: "fi-hel2", BackupRule: &upcloud.BackupRule{ Interval: upcloud.BackupRuleIntervalDaily, Time: "0430", @@ -73,6 +74,7 @@ func TestCreateStorageRequest(t *testing.T) { expectedJSON := ` { "storage": { + "encrypted": "yes", "size": "10", "tier": "maxiops", "title": "Test storage", diff --git a/upcloud/server_test.go b/upcloud/server_test.go index 8de9362a..f84b2d51 100644 --- a/upcloud/server_test.go +++ b/upcloud/server_test.go @@ -215,6 +215,7 @@ func TestUnmarshalServerDetails(t *testing.T) { "storage_device": [ { "address": "virtio:0", + "storage_encrypted": "yes", "part_of_plan" : "yes", "storage": "012580a1-32a1-466e-a323-689ca16f2d43", "storage_size": 20, @@ -249,6 +250,7 @@ func TestUnmarshalServerDetails(t *testing.T) { err := json.Unmarshal([]byte(originalJSON), &serverDetails) assert.Nil(t, err) + assert.Equal(t, true, serverDetails.StorageDevices[0].Encrypted.Bool()) assert.Equal(t, "cdrom,disk", serverDetails.BootOrder) assert.Equal(t, "on", serverDetails.Firewall) assert.Len(t, serverDetails.IPAddresses, 3) diff --git a/upcloud/storage.go b/upcloud/storage.go index 3663f868..8a4ee3a6 100644 --- a/upcloud/storage.go +++ b/upcloud/storage.go @@ -74,8 +74,9 @@ func (s *Storages) UnmarshalJSON(b []byte) error { // Storage represents a storage device type Storage struct { - Access string `json:"access"` - License float64 `json:"license"` + Access string `json:"access"` + Encrypted Boolean `json:"encrypted"` + License float64 `json:"license"` // TODO: Convert to boolean PartOfPlan string `json:"part_of_plan"` Size int `json:"size"` @@ -148,7 +149,8 @@ type BackupRule struct { // ServerStorageDevice represents a storage device in the context of server requests or server details type ServerStorageDevice struct { - Address string `json:"address"` + Address string `json:"address"` + Encrypted Boolean `json:"storage_encrypted"` // TODO: Convert to boolean PartOfPlan string `json:"part_of_plan"` UUID string `json:"storage"` @@ -159,7 +161,7 @@ type ServerStorageDevice struct { BootDisk int `json:"boot_disk,string"` } -// StorageImportDetails represents the details of an ongoing or completed storge import operation. +// StorageImportDetails represents the details of an ongoing or completed storage import operation. type StorageImportDetails struct { ClientContentLength int `json:"client_content_length"` ClientContentType string `json:"client_content_type"` diff --git a/upcloud/storage_test.go b/upcloud/storage_test.go index 1b085003..ec563dbe 100644 --- a/upcloud/storage_test.go +++ b/upcloud/storage_test.go @@ -16,6 +16,7 @@ func TestUnmarshalStorage(t *testing.T) { "storage": [ { "access": "private", + "encrypted": "yes", "license": 0, "size": 10, "state": "online", @@ -27,6 +28,7 @@ func TestUnmarshalStorage(t *testing.T) { }, { "access" : "private", + "encrypted": "no", "created" : "2019-09-17T14:34:43Z", "license" : 0, "origin" : "01eff7ad-168e-413e-83b0-054f6a28fa23", @@ -66,15 +68,16 @@ func TestUnmarshalStorage(t *testing.T) { testData := []Storage{ { - Access: StorageAccessPrivate, - License: 0.0, - Size: 10, - State: StorageStateOnline, - Tier: StorageTierHDD, - Title: "Operating system disk", - Type: StorageTypeNormal, - UUID: "01eff7ad-168e-413e-83b0-054f6a28fa23", - Zone: "uk-lon1", + Access: StorageAccessPrivate, + Encrypted: FromBool(true), + License: 0.0, + Size: 10, + State: StorageStateOnline, + Tier: StorageTierHDD, + Title: "Operating system disk", + Type: StorageTypeNormal, + UUID: "01eff7ad-168e-413e-83b0-054f6a28fa23", + Zone: "uk-lon1", }, { Access: StorageAccessPrivate,