mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-10-24 12:32:55 +08:00
149 lines
3.9 KiB
Go
149 lines
3.9 KiB
Go
package openstack
|
|
|
|
import (
|
|
"yunion.io/x/jsonutils"
|
|
"yunion.io/x/onecloud/pkg/cloudprovider"
|
|
"yunion.io/x/onecloud/pkg/compute/models"
|
|
)
|
|
|
|
const (
|
|
SNAPSHOT_STATUS_CREATING = "creating" //The snapshot is being created.
|
|
SNAPSHOT_STATUS_AVAILABLE = "available" //The snapshot is ready to use.
|
|
SNAPSHOT_STATUS_BACKING_UP = "backing-up" //The snapshot is being backed up.
|
|
SNAPSHOT_STATUS_DELETING = "deleting" //The snapshot is being deleted.
|
|
SNAPSHOT_STATUS_ERROR = "error" //A snapshot creation error occurred.
|
|
SNAPSHOT_STATUS_DELETED = "deleted" //The snapshot has been deleted.
|
|
SNAPSHOT_STATUS_UNMANAGING = "unmanaging" //The snapshot is being unmanaged.
|
|
SNAPSHOT_STATUS_RESTORING = "restoring" //The snapshot is being restored to a volume.
|
|
SNAPSHOT_STATUS_ERROR_DELETING = "error_deleting" //A snapshot deletion error occurred.
|
|
)
|
|
|
|
type SSnapshot struct {
|
|
region *SRegion
|
|
|
|
ID string
|
|
VolumeID string
|
|
|
|
Status string
|
|
Metadata Metadata
|
|
|
|
Progress string `json:"os-extended-snapshot-attributes:progress"`
|
|
Name string
|
|
UserID string
|
|
ProjectID string `json:"os-extended-snapshot-attributes:project_id"`
|
|
//CreatedAt time.Time
|
|
Size int32
|
|
|
|
Description string
|
|
//UpdatedAt time.Time
|
|
}
|
|
|
|
func (region *SRegion) GetISnapshotById(snapshotId string) (cloudprovider.ICloudSnapshot, error) {
|
|
_, resp, err := region.CinderGet("/snapshots/"+snapshotId, "", nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
snapshot := SSnapshot{}
|
|
if err := resp.Unmarshal(&snapshot, "snapshot"); err != nil {
|
|
return nil, err
|
|
}
|
|
return &snapshot, nil
|
|
}
|
|
|
|
func (snapshot *SSnapshot) GetStatus() string {
|
|
switch snapshot.Status {
|
|
case SNAPSHOT_STATUS_CREATING:
|
|
return models.SNAPSHOT_CREATING
|
|
case SNAPSHOT_STATUS_AVAILABLE:
|
|
return models.SNAPSHOT_READY
|
|
case SNAPSHOT_STATUS_BACKING_UP:
|
|
return models.SNAPSHOT_ROLLBACKING
|
|
case SNAPSHOT_STATUS_DELETED, SNAPSHOT_STATUS_DELETING:
|
|
return models.SNAPSHOT_DELETING
|
|
default:
|
|
return models.SNAPSHOT_UNKNOWN
|
|
}
|
|
}
|
|
|
|
func (snapshot *SSnapshot) IsEmulated() bool {
|
|
return false
|
|
}
|
|
|
|
func (snapshot *SSnapshot) Refresh() error {
|
|
_snapshot, err := snapshot.region.GetISnapshotById(snapshot.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return jsonutils.Update(snapshot, _snapshot)
|
|
}
|
|
|
|
func (region *SRegion) GetSnapshots(diskId string) ([]cloudprovider.ICloudSnapshot, error) {
|
|
_, resp, err := region.CinderGet("/snapshots/detail", "", nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
snapshots := []SSnapshot{}
|
|
if err := resp.Unmarshal(&snapshots, "snapshots"); err != nil {
|
|
return nil, err
|
|
}
|
|
iSnapshots := []cloudprovider.ICloudSnapshot{}
|
|
for i := 0; i < len(snapshots); i++ {
|
|
if len(diskId) == 0 || snapshots[i].VolumeID == diskId {
|
|
snapshots[i].region = region
|
|
iSnapshots = append(iSnapshots, &snapshots[i])
|
|
}
|
|
}
|
|
return iSnapshots, nil
|
|
}
|
|
|
|
func (region *SRegion) GetISnapshots() ([]cloudprovider.ICloudSnapshot, error) {
|
|
return region.GetSnapshots("")
|
|
}
|
|
|
|
func (snapshot *SSnapshot) GetMetadata() *jsonutils.JSONDict {
|
|
return nil
|
|
}
|
|
|
|
func (snapshot *SSnapshot) GetSize() int32 {
|
|
return snapshot.Size
|
|
}
|
|
|
|
func (snapshot *SSnapshot) GetDiskId() string {
|
|
return snapshot.VolumeID
|
|
}
|
|
|
|
func (snapshot *SSnapshot) GetId() string {
|
|
return snapshot.ID
|
|
}
|
|
|
|
func (snapshot *SSnapshot) GetGlobalId() string {
|
|
return snapshot.ID
|
|
}
|
|
|
|
func (snapshot *SSnapshot) GetName() string {
|
|
return snapshot.Name
|
|
}
|
|
|
|
func (snapshot *SSnapshot) Delete() error {
|
|
return snapshot.region.DeleteSnapshot(snapshot.ID)
|
|
}
|
|
|
|
func (snapshot *SSnapshot) GetDiskType() string {
|
|
if len(snapshot.VolumeID) > 0 {
|
|
if disk, err := snapshot.region.GetDisk(snapshot.VolumeID); err == nil {
|
|
if disk.Bootable {
|
|
return models.DISK_TYPE_SYS
|
|
}
|
|
}
|
|
}
|
|
return models.DISK_TYPE_DATA
|
|
}
|
|
|
|
func (snapshot *SRegion) DeleteSnapshot(snapshotId string) error {
|
|
return cloudprovider.ErrNotImplemented
|
|
}
|
|
|
|
func (snapshot *SRegion) CreateSnapshot(diskId, name, desc string) (string, error) {
|
|
return "", cloudprovider.ErrNotImplemented
|
|
}
|