Skip to content

Instance Snapshots #240

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
118 changes: 118 additions & 0 deletions fake_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2022,3 +2022,121 @@ func (c *FakeClient) UnassignIP(id, region string) (*SimpleResponse, error) {
Result: "success",
}, nil
}

// InstanceSnapshots represents a list of instance snapshots in the fake client
type InstanceSnapshots struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this plural?

It doesn't look like a list of instance snapshots.

ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
IncludedVolumes []string `json:"included_volumes"`
Status InstanceSnapshotStatus `json:"status"`
CreatedAt time.Time `json:"created_at"`
}

// CreateInstanceSnapshot implemented in a fake way for automated tests
func (c *FakeClient) CreateInstanceSnapshot(instanceID string, config *CreateInstanceSnapshotConfig) (*InstanceSnapshot, error) {
snapshot := InstanceSnapshot{
ID: c.generateID(),
Name: config.Name,
Description: config.Description,
IncludedVolumes: []string{},
Status: InstanceSnapshotStatus{
State: "pending",
Message: "Creating snapshot",
},
CreatedAt: time.Now(),
}

if config.IncludeVolumes {
// Add some fake volume IDs
snapshot.IncludedVolumes = []string{"vol-1", "vol-2"}
snapshot.Status.Volumes = []InstanceSnapshotVolume{
{ID: "vol-1", State: "pending"},
{ID: "vol-2", State: "pending"},
}
}

return &snapshot, nil
}

// GetInstanceSnapshot implemented in a fake way for automated tests
func (c *FakeClient) GetInstanceSnapshot(instanceID, snapshotID string) (*InstanceSnapshot, error) {
// Return a fake completed snapshot
return &InstanceSnapshot{
ID: snapshotID,
Name: "test-snapshot",
Description: "Test snapshot",
IncludedVolumes: []string{"vol-1", "vol-2"},
Status: InstanceSnapshotStatus{
State: "completed",
Message: "Snapshot completed successfully",
Volumes: []InstanceSnapshotVolume{
{ID: "vol-1", State: "completed"},
{ID: "vol-2", State: "completed"},
},
},
CreatedAt: time.Now(),
}, nil
}

// ListInstanceSnapshots implemented in a fake way for automated tests
func (c *FakeClient) ListInstanceSnapshots(instanceID string) ([]InstanceSnapshot, error) {
// Return a list of fake snapshots
return []InstanceSnapshot{
{
ID: "snapshot-1",
Name: "test-snapshot-1",
Description: "Test snapshot 1",
IncludedVolumes: []string{"vol-1"},
Status: InstanceSnapshotStatus{
State: "completed",
Message: "Snapshot completed successfully",
Volumes: []InstanceSnapshotVolume{
{ID: "vol-1", State: "completed"},
},
},
CreatedAt: time.Now(),
},
{
ID: "snapshot-2",
Name: "test-snapshot-2",
Description: "Test snapshot 2",
IncludedVolumes: []string{"vol-2"},
Status: InstanceSnapshotStatus{
State: "completed",
Message: "Snapshot completed successfully",
Volumes: []InstanceSnapshotVolume{
{ID: "vol-2", State: "completed"},
},
},
CreatedAt: time.Now(),
},
}, nil
}

// UpdateInstanceSnapshot implemented in a fake way for automated tests
func (c *FakeClient) UpdateInstanceSnapshot(instanceID, snapshotID string, config *UpdateInstanceSnapshotConfig) (*InstanceSnapshot, error) {
// Return a fake updated snapshot
return &InstanceSnapshot{
ID: snapshotID,
Name: config.Name,
Description: config.Description,
IncludedVolumes: []string{"vol-1", "vol-2"},
Status: InstanceSnapshotStatus{
State: "completed",
Message: "Snapshot completed successfully",
Volumes: []InstanceSnapshotVolume{
{ID: "vol-1", State: "completed"},
{ID: "vol-2", State: "completed"},
},
},
CreatedAt: time.Now(),
}, nil
}

// DeleteInstanceSnapshot implemented in a fake way for automated tests
func (c *FakeClient) DeleteInstanceSnapshot(instanceID, snapshotID string) (*SimpleResponse, error) {
return &SimpleResponse{
Result: "success",
}, nil
}
119 changes: 119 additions & 0 deletions instance_snapshot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package civogo

import (
"bytes"
"encoding/json"
"fmt"
"time"
)

// InstanceSnapshot represents a snapshot of an instance
type InstanceSnapshot struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
IncludedVolumes []string `json:"included_volumes"`
Status InstanceSnapshotStatus `json:"status"`
CreatedAt time.Time `json:"created_at"`
}

// InstanceSnapshotStatus represents the status of an instance snapshot
type InstanceSnapshotStatus struct {
State string `json:"state"`
Message string `json:"message"`
Volumes []InstanceSnapshotVolume `json:"volumes,omitempty"`
}

// InstanceSnapshotVolume represents the status of a volume in a snapshot
type InstanceSnapshotVolume struct {
ID string `json:"id"`
State string `json:"state"`
}

// CreateInstanceSnapshotConfig represents the configuration for creating a new instance snapshot
type CreateInstanceSnapshotConfig struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
IncludeVolumes bool `json:"include_volumes"`
}

// UpdateInstanceSnapshotConfig represents the configuration for updating an instance snapshot
type UpdateInstanceSnapshotConfig struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
}

// CreateInstanceSnapshot creates a new snapshot of an instance
func (c *Client) CreateInstanceSnapshot(instanceID string, config *CreateInstanceSnapshotConfig) (*InstanceSnapshot, error) {
body, err := c.SendPostRequest(fmt.Sprintf("/v2/instances/%s/snapshots", instanceID), config)
if err != nil {
return nil, decodeError(err)
}

var snapshot InstanceSnapshot
if err := json.NewDecoder(bytes.NewReader(body)).Decode(&snapshot); err != nil {
return nil, err
}

return &snapshot, nil
}

// GetInstanceSnapshot retrieves snapshot of a specific instance
func (c *Client) GetInstanceSnapshot(instanceID, snapshotID string) (*InstanceSnapshot, error) {
resp, err := c.SendGetRequest(fmt.Sprintf("/v2/instances/%s/snapshots/%s", instanceID, snapshotID))
if err != nil {
return nil, decodeError(err)
}

var snapshot InstanceSnapshot
if err := json.NewDecoder(bytes.NewReader(resp)).Decode(&snapshot); err != nil {
return nil, err
}

return &snapshot, nil
}

// ListInstanceSnapshots retrieves all snapshots for a specific instance
func (c *Client) ListInstanceSnapshots(instanceID string) ([]InstanceSnapshot, error) {
resp, err := c.SendGetRequest(fmt.Sprintf("/v2/instances/%s/snapshots", instanceID))
if err != nil {
return nil, decodeError(err)
}

var snapshots []InstanceSnapshot
if err := json.NewDecoder(bytes.NewReader(resp)).Decode(&snapshots); err != nil {
return nil, err
}

return snapshots, nil
}

// UpdateInstanceSnapshot updates an existing instance snapshot
func (c *Client) UpdateInstanceSnapshot(instanceID, snapshotID string, config *UpdateInstanceSnapshotConfig) (*InstanceSnapshot, error) {
body, err := c.SendPutRequest(fmt.Sprintf("/v2/instances/%s/snapshots/%s", instanceID, snapshotID), config)
if err != nil {
return nil, decodeError(err)
}

var snapshot InstanceSnapshot
if err := json.NewDecoder(bytes.NewReader(body)).Decode(&snapshot); err != nil {
return nil, err
}

return &snapshot, nil
}

// DeleteInstanceSnapshot deletes an instance snapshot by its ID
func (c *Client) DeleteInstanceSnapshot(instanceID, snapshotID string) (*SimpleResponse, error) {
resp, err := c.SendDeleteRequest(fmt.Sprintf("/v2/instances/%s/snapshots/%s", instanceID, snapshotID))
if err != nil {
return nil, decodeError(err)
}

var result SimpleResponse
if err := json.NewDecoder(bytes.NewReader(resp)).Decode(&result); err != nil {
return nil, err
}

return &result, nil
}
Loading
Loading