From 14171e5a50aa2243c23bb4f4e51a567c687c2d4f Mon Sep 17 00:00:00 2001 From: Jordi Donadeu Date: Mon, 30 Oct 2023 12:17:17 +0000 Subject: [PATCH] Code improvements --- configuration.go | 2 +- vps/blockstorage.go | 26 ++++++++++++++++++-------- vps/blockstorage_test.go | 30 +++++++++++++++--------------- 3 files changed, 34 insertions(+), 24 deletions(-) diff --git a/configuration.go b/configuration.go index 0a080b5..dddded4 100644 --- a/configuration.go +++ b/configuration.go @@ -9,7 +9,7 @@ import ( ) const ( - libraryVersion = "6.22.0" + libraryVersion = "6.22.1" defaultBasePath = "https://api.transip.nl/v6" userAgent = "go-client-gotransip/" + libraryVersion ) diff --git a/vps/blockstorage.go b/vps/blockstorage.go index 3537831..1e6d3ce 100644 --- a/vps/blockstorage.go +++ b/vps/blockstorage.go @@ -14,23 +14,33 @@ import ( // getting information, ordering, upgrading, attaching/detaching it to a vps type BlockStorageRepository repository.RestRepository +// BlockStorageType is one of the following strings +// 'big-storage', 'fast-storage' +type BlockStorageType string + +// Definition of all the possible block storage types +const ( + BigStorageType BlockStorageType = "big-storage" + FastStorageType BlockStorageType = "fast-storage" +) + // BlockStorageOrder struct which is used to construct a new order request for a blockstorage type BlockStorageOrder struct { // The type of the block storage. It can be big-storage or fast-storage. - Type string `json:"type"` + ProductType BlockStorageType `json:"type"` // The size of the block storage in KB. // Big storages: The minimum size is 2 TiB and storage can be extended with up to maximum of 40 TiB. Make sure to // use a multiple of 2 TiB. Note that 2 TiB equals 2147483648 KiB. // Fast storages: The minimum size is 10 GiB and storage can be extended with up to maximum of 10000 GiB. Make sure // to use a multiple of 10 GiB. Note that 10 GiB equals 10485760 KiB. Size int `json:"size"` - // Whether to order offsite backups, omit this to use current value + // Whether to order offsite backups OffsiteBackups bool `json:"offsiteBackups"` // The name of the availabilityZone where the BlockStorage should be created. This parameter can not be used in conjunction with vpsName // If a vpsName is provided as well as an availabilityZone, the zone of the vps is leading AvailabilityZone string `json:"availabilityZone,omitempty"` // The name of the VPS to attach the block storage to - VpsName string `json:"vpsName"` + VpsName string `json:"vpsName,omitempty"` // Description that the block storage should have after ordering Description string `json:"description,omitempty"` } @@ -56,13 +66,13 @@ type BlockStorage struct { // Name that can be set by customer Description string `json:"description"` // Disk size of the block storage in kB - DiskSize int64 `json:"diskSize,omitempty"` + Size int64 `json:"size,omitempty"` // Whether a blockstorage has backups OffsiteBackups bool `json:"offsiteBackups"` // The VPS that the block storage is attached to VpsName string `json:"vpsName"` // Status of the block storage can be 'active', 'attaching' or 'detachting' - Status BlockStorageStatus `json:"status,omitempty"` + Status BlockStorageStatus `json:"status"` // Serial of the block storage. This is a unique identifier that is visible by the vps it has been attached to. On // linux servers it is visible using udevadm info /dev/vdb where it will be the value of ID_SERIAL. A symlink will // also be created in /dev/disk-by-id/ containing the serial. This is useful if you want to map a disk inside a VPS @@ -73,7 +83,7 @@ type BlockStorage struct { // The availability zone the blockstorage is located in AvailabilityZone string `json:"availabilityZone,omitempty"` // The type of the block storage. It can be big-storage or fast-storage. - ProductType string `json:"productType"` + ProductType string `json:"BlockStorageType"` } // BlockStorageBackup struct for a BlockStorageBackup @@ -83,7 +93,7 @@ type BlockStorageBackup struct { // Status of the block storage backup ('active', 'creating', 'reverting', 'deleting', 'pendingDeletion', 'syncing', 'moving') Status BackupStatus `json:"status,omitempty"` // The backup disk size in kB - DiskSize int64 `json:"diskSize"` + Size int64 `json:"size"` // Date of the block storage backup DateTimeCreate rest.Time `json:"dateTimeCreate,omitempty"` // The name of the availability zone the backup is in @@ -150,7 +160,7 @@ func (r *BlockStorageRepository) Upgrade(blockStorageName string, size int, offs // - One Block Storages can only be attached to one VPS at a time; // - One VPS can have a maximum of 10 blockstorages attached; // - Set the vpsName property to the VPS name to attach to for attaching Block Storage; -// - Set the vpsName property to null to detach the Block Storage from the currently attached VPS. +// - Set the vpsName property to an empty string to detach the Block Storage from the currently attached VPS. func (r *BlockStorageRepository) Update(blockStorage BlockStorage) error { requestBody := blockStorageWrapper{BlockStorage: blockStorage} restRequest := rest.Request{Endpoint: fmt.Sprintf("/block-storages/%s", blockStorage.Name), Body: &requestBody} diff --git a/vps/blockstorage_test.go b/vps/blockstorage_test.go index c9197a7..280cfc7 100644 --- a/vps/blockstorage_test.go +++ b/vps/blockstorage_test.go @@ -13,7 +13,7 @@ import ( ) func TestBlockStorageRepository_GetBlockStorages(t *testing.T) { - const apiResponse = `{ "blockStorages": [ { "name": "example-blockstorage", "description": "Block storage description", "diskSize": 2147483648, "offsiteBackups": true, "vpsName": "example-vps", "status": "active", "isLocked": false, "availabilityZone": "ams0", "serial": "e7e12b3c7c6602973ac7" } ] } ` + const apiResponse = `{ "blockStorages": [ { "name": "example-blockstorage", "description": "Block storage description", "size": 2147483648, "offsiteBackups": true, "vpsName": "example-vps", "status": "active", "isLocked": false, "availabilityZone": "ams0", "serial": "e7e12b3c7c6602973ac7" } ] } ` server := testutil.MockServer{T: t, ExpectedURL: "/block-storages", ExpectedMethod: "GET", StatusCode: 200, Response: apiResponse} client, tearDown := server.GetClient() defer tearDown() @@ -25,7 +25,7 @@ func TestBlockStorageRepository_GetBlockStorages(t *testing.T) { assert.Equal(t, "example-blockstorage", all[0].Name) assert.Equal(t, "Block storage description", all[0].Description) - assert.EqualValues(t, 2147483648, all[0].DiskSize) + assert.EqualValues(t, 2147483648, all[0].Size) assert.Equal(t, true, all[0].OffsiteBackups) assert.Equal(t, "example-vps", all[0].VpsName) assert.EqualValues(t, "active", all[0].Status) @@ -35,7 +35,7 @@ func TestBlockStorageRepository_GetBlockStorages(t *testing.T) { } func TestBlockStorageRepository_GetSelection(t *testing.T) { - const apiResponse = `{ "blockStorages": [ { "name": "example-blockstorage", "description": "Block storage description", "diskSize": 2147483648, "offsiteBackups": true, "vpsName": "example-vps", "status": "active", "isLocked": false, "availabilityZone": "ams0", "serial": "e7e12b3c7c6602973ac7"} ] } ` + const apiResponse = `{ "blockStorages": [ { "name": "example-blockstorage", "description": "Block storage description", "size": 2147483648, "offsiteBackups": true, "vpsName": "example-vps", "status": "active", "isLocked": false, "availabilityZone": "ams0", "serial": "e7e12b3c7c6602973ac7"} ] } ` server := testutil.MockServer{T: t, ExpectedURL: "/block-storages?page=1&pageSize=25", ExpectedMethod: "GET", StatusCode: 200, Response: apiResponse} client, tearDown := server.GetClient() defer tearDown() @@ -47,7 +47,7 @@ func TestBlockStorageRepository_GetSelection(t *testing.T) { assert.Equal(t, "example-blockstorage", all[0].Name) assert.Equal(t, "Block storage description", all[0].Description) - assert.EqualValues(t, 2147483648, all[0].DiskSize) + assert.EqualValues(t, 2147483648, all[0].Size) assert.Equal(t, true, all[0].OffsiteBackups) assert.Equal(t, "example-vps", all[0].VpsName) assert.EqualValues(t, "active", all[0].Status) @@ -57,7 +57,7 @@ func TestBlockStorageRepository_GetSelection(t *testing.T) { } func TestBlockStorageRepository_GetBlockStorageByName(t *testing.T) { - const apiResponse = `{ "blockStorage": { "name": "example-blockstorage", "description": "Block storage description", "diskSize": 2147483648, "offsiteBackups": true, "vpsName": "example-vps", "status": "active", "isLocked": false, "availabilityZone": "ams0", "serial": "e7e12b3c7c6602973ac7" } } ` + const apiResponse = `{ "blockStorage": { "name": "example-blockstorage", "description": "Block storage description", "size": 2147483648, "offsiteBackups": true, "vpsName": "example-vps", "status": "active", "isLocked": false, "availabilityZone": "ams0", "serial": "e7e12b3c7c6602973ac7" } } ` server := testutil.MockServer{T: t, ExpectedURL: "/block-storages/example-blockstorage", ExpectedMethod: "GET", StatusCode: 200, Response: apiResponse} client, tearDown := server.GetClient() defer tearDown() @@ -67,7 +67,7 @@ func TestBlockStorageRepository_GetBlockStorageByName(t *testing.T) { require.NoError(t, err) assert.Equal(t, "example-blockstorage", blockstorage.Name) assert.Equal(t, "Block storage description", blockstorage.Description) - assert.EqualValues(t, 2147483648, blockstorage.DiskSize) + assert.EqualValues(t, 2147483648, blockstorage.Size) assert.Equal(t, true, blockstorage.OffsiteBackups) assert.Equal(t, "example-vps", blockstorage.VpsName) assert.EqualValues(t, "active", blockstorage.Status) @@ -83,7 +83,7 @@ func TestBlockStorageRepository_OrderBlockStorage(t *testing.T) { defer tearDown() repo := BlockStorageRepository{Client: *client} - order := BlockStorageOrder{Type: "fast-storage", Size: 8, OffsiteBackups: true, AvailabilityZone: "ams0", VpsName: "example-vps", Description: "test-description"} + order := BlockStorageOrder{ProductType: "fast-storage", Size: 8, OffsiteBackups: true, AvailabilityZone: "ams0", VpsName: "example-vps", Description: "test-description"} err := repo.Order(order) require.NoError(t, err) @@ -102,7 +102,7 @@ func TestBlockStorageRepository_UpgradeBlockStorage(t *testing.T) { } func TestBlockStorageRepository_UpdateBlockStorage(t *testing.T) { - const expectedRequest = `{"blockStorage":{"name":"example-blockstorage","description":"Block storage description","diskSize":2147483648,"offsiteBackups":true,"vpsName":"example-vps","status":"active","serial":"e7e12b3c7c6602973ac7","isLocked":false,"availabilityZone":"ams0","productType":"fast-storage"}}` + const expectedRequest = `{"blockStorage":{"name":"example-blockstorage","description":"Block storage description","size":2147483648,"offsiteBackups":true,"vpsName":"example-vps","status":"active","serial":"e7e12b3c7c6602973ac7","isLocked":false,"availabilityZone":"ams0","BlockStorageType":"fast-storage"}}` server := testutil.MockServer{T: t, ExpectedURL: "/block-storages/example-blockstorage", ExpectedMethod: "PUT", StatusCode: 204, ExpectedRequest: expectedRequest} client, tearDown := server.GetClient() defer tearDown() @@ -111,7 +111,7 @@ func TestBlockStorageRepository_UpdateBlockStorage(t *testing.T) { blockStorage := BlockStorage{ Name: "example-blockstorage", Description: "Block storage description", - DiskSize: 2147483648, + Size: 2147483648, OffsiteBackups: true, VpsName: "example-vps", Status: BlockStorageStatusActive, @@ -126,7 +126,7 @@ func TestBlockStorageRepository_UpdateBlockStorage(t *testing.T) { } func TestBlockStorageRepository_DetachVpsFromBlockStorage(t *testing.T) { - const expectedRequest = `{"blockStorage":{"name":"example-blockstorage","description":"Block storage description","diskSize":2147483648,"offsiteBackups":true,"vpsName":"example-vps","status":"active","serial":"e7e12b3c7c6602973ac7","isLocked":false,"availabilityZone":"ams0","productType":"fast-storage"}}` + const expectedRequest = `{"blockStorage":{"name":"example-blockstorage","description":"Block storage description","size":2147483648,"offsiteBackups":true,"vpsName":"example-vps","status":"active","serial":"e7e12b3c7c6602973ac7","isLocked":false,"availabilityZone":"ams0","BlockStorageType":"fast-storage"}}` server := testutil.MockServer{T: t, ExpectedURL: "/block-storages/example-blockstorage", ExpectedMethod: "PUT", StatusCode: 204, ExpectedRequest: expectedRequest} client, tearDown := server.GetClient() defer tearDown() @@ -135,7 +135,7 @@ func TestBlockStorageRepository_DetachVpsFromBlockStorage(t *testing.T) { blockStorage := BlockStorage{ Name: "example-blockstorage", Description: "Block storage description", - DiskSize: 2147483648, + Size: 2147483648, OffsiteBackups: true, Status: "active", IsLocked: false, @@ -148,7 +148,7 @@ func TestBlockStorageRepository_DetachVpsFromBlockStorage(t *testing.T) { } func TestBlockStorageRepository_AttachVpsToBlockStorage(t *testing.T) { - const expectedRequest = `{"blockStorage":{"name":"example-blockstorage","description":"Block storage description","diskSize":2147483648,"offsiteBackups":true,"vpsName":"","status":"active","serial":"e7e12b3c7c6602973ac7","isLocked":false,"availabilityZone":"ams0","productType":"fast-storage"}}` + const expectedRequest = `{"blockStorage":{"name":"example-blockstorage","description":"Block storage description","size":2147483648,"offsiteBackups":true,"vpsName":"","status":"active","serial":"e7e12b3c7c6602973ac7","isLocked":false,"availabilityZone":"ams0","BlockStorageType":"fast-storage"}}` server := testutil.MockServer{T: t, ExpectedURL: "/block-storages/example-blockstorage", ExpectedMethod: "PUT", StatusCode: 204, ExpectedRequest: expectedRequest} client, tearDown := server.GetClient() defer tearDown() @@ -157,7 +157,7 @@ func TestBlockStorageRepository_AttachVpsToBlockStorage(t *testing.T) { blockStorage := BlockStorage{ Name: "example-blockstorage", Description: "Block storage description", - DiskSize: 2147483648, + Size: 2147483648, OffsiteBackups: true, VpsName: "example-vps", Status: "active", @@ -182,7 +182,7 @@ func TestBlockStorageRepository_CancelBlockStorage(t *testing.T) { } func TestBlockStorageRepository_GetBlockStorageBackups(t *testing.T) { - const apiResponse = `{ "backups": [ { "id": 1583, "status": "active", "diskSize": 4294967296, "dateTimeCreate": "2019-12-31 09:13:55", "availabilityZone": "ams0" } ] }` + const apiResponse = `{ "backups": [ { "id": 1583, "status": "active", "size": 4294967296, "dateTimeCreate": "2019-12-31 09:13:55", "availabilityZone": "ams0" } ] }` server := testutil.MockServer{T: t, ExpectedURL: "/block-storages/example-blockstorage/backups", ExpectedMethod: "GET", StatusCode: 200, Response: apiResponse} client, tearDown := server.GetClient() defer tearDown() @@ -194,7 +194,7 @@ func TestBlockStorageRepository_GetBlockStorageBackups(t *testing.T) { assert.EqualValues(t, 1583, all[0].ID) assert.EqualValues(t, "active", all[0].Status) - assert.EqualValues(t, 4294967296, all[0].DiskSize) + assert.EqualValues(t, 4294967296, all[0].Size) assert.Equal(t, "2019-12-31 09:13:55", all[0].DateTimeCreate.Format("2006-01-02 15:04:05")) assert.Equal(t, "ams0", all[0].AvailabilityZone) }