diff --git a/fake_client.go b/fake_client.go index bdfcb5e..7d4b0e0 100644 --- a/fake_client.go +++ b/fake_client.go @@ -22,6 +22,7 @@ type FakeClient struct { IP []IP Networks []Network Volumes []Volume + VolumeSnapshots []VolumeSnapshot SSHKeys []SSHKey Webhooks []Webhook DiskImage []DiskImage @@ -165,6 +166,15 @@ type Clienter interface { DetachVolume(id string) (*SimpleResponse, error) DeleteVolume(id string) (*SimpleResponse, error) + // VolumeSnapshot + GetVolumeSnapshotByVolumeID(volumeID, snapshotID string) (*VolumeSnapshot, error) + ListVolumeSnapshotsByVolumeID(volumeID string) ([]VolumeSnapshot, error) + CreateVolumeSnapshot(volumeID string, config *VolumeSnapshotConfig) (*VolumeSnapshot, error) + DeleteVolumeAndAllSnapshot(volumeID string) (*SimpleResponse, error) + ListVolumeSnapshots() ([]VolumeSnapshot, error) + GetVolumeSnapshot(id string) (*VolumeSnapshot, error) + DeleteVolumeSnapshot(id string) (*SimpleResponse, error) + // Webhooks CreateWebhook(r *WebhookConfig) (*Webhook, error) ListWebhooks() ([]Webhook, error) @@ -1330,6 +1340,93 @@ func (c *FakeClient) DeleteVolume(id string) (*SimpleResponse, error) { return &SimpleResponse{Result: "failed"}, nil } +// GetVolumeSnapshotByVolumeID implemented in a fake way for automated tests +func (c *FakeClient) GetVolumeSnapshotByVolumeID(volumeID, snapshotID string) (*VolumeSnapshot, error) { + for _, snapshot := range c.VolumeSnapshots { + if snapshot.VolumeID == volumeID && snapshot.SnapshotID == snapshotID { + return &snapshot, nil + } + } + + err := fmt.Errorf("unable to find volume snapshot %s, zero matches", snapshotID) + return nil, ZeroMatchesError.wrap(err) +} + +// ListVolumeSnapshotsByVolumeID implemented in a fake way for automated tests +func (c *FakeClient) ListVolumeSnapshotsByVolumeID(volumeID string) ([]VolumeSnapshot, error) { + snapshots := make([]VolumeSnapshot, 0) + for _, snapshot := range c.VolumeSnapshots { + if snapshot.VolumeID == volumeID { + snapshots = append(snapshots, snapshot) + } + } + + return snapshots, nil +} + +// CreateVolumeSnapshot implemented in a fake way for automated tests +func (c *FakeClient) CreateVolumeSnapshot(volumeID string, config *VolumeSnapshotConfig) (*VolumeSnapshot, error) { + snapshot := VolumeSnapshot{ + SnapshotID: c.generateID(), + Name: config.Name, + VolumeID: volumeID, + State: "Ready", + } + c.VolumeSnapshots = append(c.VolumeSnapshots, snapshot) + + return &snapshot, nil +} + +// DeleteVolumeAndAllSnapshot implemented in a fake way for automated tests +func (c *FakeClient) DeleteVolumeAndAllSnapshot(volumeID string) (*SimpleResponse, error) { + for i, volume := range c.Volumes { + if volume.ID == volumeID { + c.Volumes[len(c.Volumes)-1], c.Volumes[i] = c.Volumes[i], c.Volumes[len(c.Volumes)-1] + c.Volumes = c.Volumes[:len(c.Volumes)-1] + break + } + } + + for i := 0; i < len(c.VolumeSnapshots); i++ { + if c.VolumeSnapshots[i].VolumeID == volumeID { + c.VolumeSnapshots = append(c.VolumeSnapshots[:i], c.VolumeSnapshots[i+1:]...) + i-- + } + } + + return &SimpleResponse{Result: "success"}, nil +} + +// ListVolumeSnapshots implemented in a fake way for automated tests +func (c *FakeClient) ListVolumeSnapshots() ([]VolumeSnapshot, error) { + return c.VolumeSnapshots, nil +} + +// GetVolumeSnapshot implemented in a fake way for automated tests +func (c *FakeClient) GetVolumeSnapshot(snapshotID string) (*VolumeSnapshot, error) { + for _, snapshot := range c.VolumeSnapshots { + if snapshot.SnapshotID == snapshotID { + return &snapshot, nil + } + } + + err := fmt.Errorf("unable to find volume snapshot %s, zero matches", snapshotID) + return nil, ZeroMatchesError.wrap(err) +} + +// DeleteVolumeSnapshot implemented in a fake way for automated tests +func (c *FakeClient) DeleteVolumeSnapshot(snapshotID string) (*SimpleResponse, error) { + for i, snapshot := range c.VolumeSnapshots { + if snapshot.SnapshotID == snapshotID { + c.VolumeSnapshots[len(c.VolumeSnapshots)-1], c.VolumeSnapshots[i] = c.VolumeSnapshots[i], c.VolumeSnapshots[len(c.VolumeSnapshots)-1] + c.VolumeSnapshots = c.VolumeSnapshots[:len(c.VolumeSnapshots)-1] + return &SimpleResponse{Result: "success"}, nil + } + } + + return &SimpleResponse{Result: "failed"}, nil +} + // CreateWebhook implemented in a fake way for automated tests func (c *FakeClient) CreateWebhook(r *WebhookConfig) (*Webhook, error) { webhook := Webhook{ diff --git a/volume.go b/volume.go index b913249..ce01f62 100644 --- a/volume.go +++ b/volume.go @@ -245,16 +245,16 @@ func (c *Client) DeleteVolume(id string) (*SimpleResponse, error) { } // GetVolumeSnapshotByVolumeID retrieves a specific volume snapshot by volume ID and snapshot ID -func (c *Client) GetVolumeSnapshotByVolumeID(volumeID, snapshotID string) (VolumeSnapshot, error) { +func (c *Client) GetVolumeSnapshotByVolumeID(volumeID, snapshotID string) (*VolumeSnapshot, error) { resp, err := c.SendGetRequest(fmt.Sprintf("/v2/volumes/%s/snapshots/%s", volumeID, snapshotID)) if err != nil { - return VolumeSnapshot{}, decodeError(err) + return nil, decodeError(err) } var volumeSnapshot = VolumeSnapshot{} if err := json.NewDecoder(bytes.NewReader(resp)).Decode(&volumeSnapshot); err != nil { - return VolumeSnapshot{}, err + return nil, err } - return volumeSnapshot, nil + return &volumeSnapshot, nil } // ListVolumeSnapshotsByVolumeID returns all snapshots for a specific volume by volume ID diff --git a/volume_snapshot.go b/volume_snapshot.go index cc95d86..98ce249 100644 --- a/volume_snapshot.go +++ b/volume_snapshot.go @@ -42,16 +42,16 @@ func (c *Client) ListVolumeSnapshots() ([]VolumeSnapshot, error) { } // GetVolumeSnapshot finds a volume by the full ID -func (c *Client) GetVolumeSnapshot(id string) (VolumeSnapshot, error) { +func (c *Client) GetVolumeSnapshot(id string) (*VolumeSnapshot, error) { resp, err := c.SendGetRequest(fmt.Sprintf("/v2/snapshots/%s?resource_type=volume", id)) if err != nil { - return VolumeSnapshot{}, decodeError(err) + return nil, decodeError(err) } var volumeSnapshot = VolumeSnapshot{} if err := json.NewDecoder(bytes.NewReader(resp)).Decode(&volumeSnapshot); err != nil { - return VolumeSnapshot{}, err + return nil, err } - return volumeSnapshot, nil + return &volumeSnapshot, nil } // DeleteVolumeSnapshot deletes a volume snapshot diff --git a/volume_snapshot_test.go b/volume_snapshot_test.go index c425801..a954748 100644 --- a/volume_snapshot_test.go +++ b/volume_snapshot_test.go @@ -15,7 +15,7 @@ func TestListVolumeSnapshots(t *testing.T) { "source_volume_name": "test-volume", "instance_id": "ins1234", "restore_size": 20, - "state": "available", + "state": "Ready", "creation_time": "2020-01-01T00:00:00Z" }]`, }) @@ -37,7 +37,7 @@ func TestListVolumeSnapshots(t *testing.T) { SourceVolumeName: "test-volume", InstanceID: "ins1234", RestoreSize: 20, - State: "available", + State: "Ready", CreationTime: "2020-01-01T00:00:00Z", }, } @@ -57,7 +57,7 @@ func TestGetVolumeSnapshot(t *testing.T) { "source_volume_name": "test-volume", "instance_id": "ins1234", "restore_size": 20, - "state": "available", + "state": "Ready", "creation_time": "2020-01-01T00:00:00Z" }`, }) @@ -69,7 +69,7 @@ func TestGetVolumeSnapshot(t *testing.T) { return } - expected := VolumeSnapshot{ + expected := &VolumeSnapshot{ Name: "test-snapshot", SnapshotID: "snapshot-uuid", SnapshotDescription: "snapshot for testing", @@ -77,7 +77,7 @@ func TestGetVolumeSnapshot(t *testing.T) { SourceVolumeName: "test-volume", InstanceID: "ins1234", RestoreSize: 20, - State: "available", + State: "Ready", CreationTime: "2020-01-01T00:00:00Z", } diff --git a/volume_test.go b/volume_test.go index 2a6ed59..e2190b6 100644 --- a/volume_test.go +++ b/volume_test.go @@ -318,7 +318,7 @@ func TestGetVolumeSnapshotByVolumeID(t *testing.T) { return } - expected := VolumeSnapshot{ + expected := &VolumeSnapshot{ SnapshotID: "12345", Name: "test-snapshot", SnapshotDescription: "snapshot for testing",