Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 118 additions & 3 deletions api/block/v1alpha1/block_sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,9 @@ const (
// Snapshot was deleted.
SnapshotStatusDeleted = SnapshotStatus("deleted")
// Snapshot attached to one or more references.
SnapshotStatusInUse = SnapshotStatus("in_use")
SnapshotStatusLocked = SnapshotStatus("locked")
SnapshotStatusInUse = SnapshotStatus("in_use")
SnapshotStatusLocked = SnapshotStatus("locked")
SnapshotStatusExporting = SnapshotStatus("exporting")
)

func (enum SnapshotStatus) String() string {
Expand Down Expand Up @@ -546,6 +547,21 @@ type DeleteVolumeRequest struct {
VolumeID string `json:"-"`
}

// ExportSnapshotToObjectStorageRequest: export snapshot to object storage request.
type ExportSnapshotToObjectStorageRequest struct {
// Zone: zone to target. If none is passed will use default zone from the config.
Zone scw.Zone `json:"-"`

// SnapshotID: UUID of the snapshot.
SnapshotID string `json:"-"`

// Bucket: scaleway Object Storage bucket where the object is stored.
Bucket string `json:"bucket"`

// Key: the object key inside the given bucket.
Key string `json:"key"`
}

// GetSnapshotRequest: get snapshot request.
type GetSnapshotRequest struct {
// Zone: zone to target. If none is passed will use default zone from the config.
Expand All @@ -564,6 +580,30 @@ type GetVolumeRequest struct {
VolumeID string `json:"-"`
}

// ImportSnapshotFromObjectStorageRequest: import snapshot from object storage request.
type ImportSnapshotFromObjectStorageRequest struct {
// Zone: zone to target. If none is passed will use default zone from the config.
Zone scw.Zone `json:"-"`

// Bucket: scaleway Object Storage bucket where the object is stored.
Bucket string `json:"bucket"`

// Key: the object key inside the given bucket.
Key string `json:"key"`

// Name: name of the snapshot.
Name string `json:"name"`

// ProjectID: UUID of the Project to which the volume and the snapshot belong.
ProjectID string `json:"project_id"`

// Tags: list of tags assigned to the snapshot.
Tags []string `json:"tags"`

// Size: size of the snapshot.
Size *scw.Size `json:"size,omitempty"`
}

// ImportSnapshotFromS3Request: import snapshot from s3 request.
type ImportSnapshotFromS3Request struct {
// Zone: zone to target. If none is passed will use default zone from the config.
Expand Down Expand Up @@ -1117,7 +1157,7 @@ func (s *API) CreateSnapshot(req *CreateSnapshotRequest, opts ...scw.RequestOpti
return &resp, nil
}

// ImportSnapshotFromS3: The bucket must contain a QCOW2 image.
// Deprecated: ImportSnapshotFromS3: The bucket must contain a QCOW2 image.
// The bucket can be imported into any Availability Zone as long as it is in the same region as the bucket.
func (s *API) ImportSnapshotFromS3(req *ImportSnapshotFromS3Request, opts ...scw.RequestOption) (*Snapshot, error) {
var err error
Expand Down Expand Up @@ -1155,6 +1195,81 @@ func (s *API) ImportSnapshotFromS3(req *ImportSnapshotFromS3Request, opts ...scw
return &resp, nil
}

// ImportSnapshotFromObjectStorage: The bucket must contain a QCOW2 image.
// The bucket can be imported into any Availability Zone as long as it is in the same region as the bucket.
func (s *API) ImportSnapshotFromObjectStorage(req *ImportSnapshotFromObjectStorageRequest, opts ...scw.RequestOption) (*Snapshot, error) {
var err error

if req.Zone == "" {
defaultZone, _ := s.client.GetDefaultZone()
req.Zone = defaultZone
}

if req.ProjectID == "" {
defaultProjectID, _ := s.client.GetDefaultProjectID()
req.ProjectID = defaultProjectID
}

if fmt.Sprint(req.Zone) == "" {
return nil, errors.New("field Zone cannot be empty in request")
}

scwReq := &scw.ScalewayRequest{
Method: "POST",
Path: "/block/v1alpha1/zones/" + fmt.Sprint(req.Zone) + "/snapshots/import-from-object-storage",
}

err = scwReq.SetBody(req)
if err != nil {
return nil, err
}

var resp Snapshot

err = s.client.Do(scwReq, &resp, opts...)
if err != nil {
return nil, err
}
return &resp, nil
}

// ExportSnapshotToObjectStorage: The snapshot is exported in QCOW2 format.
// The snapshot must not be in transient state.
func (s *API) ExportSnapshotToObjectStorage(req *ExportSnapshotToObjectStorageRequest, opts ...scw.RequestOption) (*Snapshot, error) {
var err error

if req.Zone == "" {
defaultZone, _ := s.client.GetDefaultZone()
req.Zone = defaultZone
}

if fmt.Sprint(req.Zone) == "" {
return nil, errors.New("field Zone cannot be empty in request")
}

if fmt.Sprint(req.SnapshotID) == "" {
return nil, errors.New("field SnapshotID cannot be empty in request")
}

scwReq := &scw.ScalewayRequest{
Method: "POST",
Path: "/block/v1alpha1/zones/" + fmt.Sprint(req.Zone) + "/snapshots/" + fmt.Sprint(req.SnapshotID) + "/export-to-object-storage",
}

err = scwReq.SetBody(req)
if err != nil {
return nil, err
}

var resp Snapshot

err = s.client.Do(scwReq, &resp, opts...)
if err != nil {
return nil, err
}
return &resp, nil
}

// DeleteSnapshot: You must specify the `snapshot_id` of the snapshot you want to delete. The snapshot must not be in use.
func (s *API) DeleteSnapshot(req *DeleteSnapshotRequest, opts ...scw.RequestOption) error {
var err error
Expand Down