diff --git a/pkg/multicloud/ecloud/disk.go b/pkg/multicloud/ecloud/disk.go index d6e2fb0688..fd331f0f27 100644 --- a/pkg/multicloud/ecloud/disk.go +++ b/pkg/multicloud/ecloud/disk.go @@ -62,6 +62,7 @@ type SDisk struct { type SDiskManualAttr struct { IsVirtual bool TempalteId string + ServerId string } func (d *SDisk) GetBillingType() string { @@ -202,15 +203,38 @@ func (s *SDisk) CreateISnapshot(ctx context.Context, name string, desc string) ( } func (s *SDisk) GetISnapshot(id string) (cloudprovider.ICloudSnapshot, error) { - return nil, cloudprovider.ErrNotImplemented + parentId, isSystem := s.ID, false + if s.ManualAttr.IsVirtual { + parentId, isSystem = s.ManualAttr.ServerId, true + } + snapshots, err := s.storage.zone.region.GetSnapshots(id, parentId, isSystem) + if err != nil { + return nil, err + } + if len(snapshots) == 0 { + return nil, cloudprovider.ErrNotFound + } + return &snapshots[0], nil } func (s *SDisk) GetISnapshots() ([]cloudprovider.ICloudSnapshot, error) { - return nil, cloudprovider.ErrNotImplemented + parentId, isSystem := s.ID, false + if s.ManualAttr.IsVirtual { + parentId, isSystem = s.ManualAttr.ServerId, true + } + snapshots, err := s.storage.zone.region.GetSnapshots("", parentId, isSystem) + if err != nil { + return nil, err + } + isnapshots := make([]cloudprovider.ICloudSnapshot, len(snapshots)) + for i := range snapshots { + isnapshots[i] = &snapshots[i] + } + return isnapshots, nil } func (s *SDisk) GetExtSnapshotPolicyIds() ([]string, error) { - return []string{}, cloudprovider.ErrNotImplemented + return []string{}, nil } func (s *SDisk) Resize(ctx context.Context, newSizeMB int64) error { diff --git a/pkg/multicloud/ecloud/instance.go b/pkg/multicloud/ecloud/instance.go index c8ea95438a..bf9311bbab 100644 --- a/pkg/multicloud/ecloud/instance.go +++ b/pkg/multicloud/ecloud/instance.go @@ -364,6 +364,7 @@ func (in *SInstance) fetchSysDisk() { ManualAttr: SDiskManualAttr{ IsVirtual: true, TempalteId: in.ImageRef, + ServerId: in.Id, }, SCreateTime: in.SCreateTime, SZoneRegionBase: in.SZoneRegionBase, diff --git a/pkg/multicloud/ecloud/snapshot.go b/pkg/multicloud/ecloud/snapshot.go new file mode 100644 index 0000000000..cb71bc27de --- /dev/null +++ b/pkg/multicloud/ecloud/snapshot.go @@ -0,0 +1,110 @@ +package ecloud + +import ( + "context" + "strings" + + api "yunion.io/x/onecloud/pkg/apis/compute" + "yunion.io/x/onecloud/pkg/cloudprovider" + "yunion.io/x/onecloud/pkg/multicloud" +) + +type SSnapshot struct { + multicloud.SResourceBase + region *SRegion + SCreateTime + + BackupType string + CreateBy string + Description string + EcType string + Id string + Name string + Size int + VolumeId string + VolumeType string + Status string + IsSystem bool + SystemDisk string +} + +func (s *SSnapshot) GetId() string { + return s.Id +} + +func (s *SSnapshot) GetName() string { + return s.Name +} + +func (s *SSnapshot) GetStatus() string { + switch strings.ToLower(s.Status) { + case "available", "in_use", "active": + return api.SNAPSHOT_READY + case "creating", "attaching", "backing_up", "saving", "queued": + return api.SNAPSHOT_CREATING + case "deleting": + return api.SNAPSHOT_DELETING + case "error_deleting": + return api.SNAPSHOT_DELETE_FAILED + case "error": + return api.SNAPSHOT_FAILED + default: + return api.SNAPSHOT_UNKNOWN + } +} + +func (s *SSnapshot) GetSizeMb() int32 { + return int32(s.Size) +} + +func (s *SSnapshot) GetDiskId() string { + if s.IsSystem { + return s.SystemDisk + } + return s.VolumeId +} + +func (s *SSnapshot) GetDiskType() string { + if s.IsSystem { + return api.DISK_TYPE_SYS + } + return api.DISK_TYPE_DATA +} + +func (s *SSnapshot) GetGlobalId() string { + return s.Id +} + +func (s *SSnapshot) Delete() error { + return cloudprovider.ErrNotImplemented +} + +func (s *SSnapshot) GetProjectId() string { + return "" +} + +func (s *SRegion) GetSnapshots(snapshotId string, parentId string, isSystem bool) ([]SSnapshot, error) { + var apiRequest *SApiRequest + query := map[string]string{} + if len(snapshotId) > 0 { + query["backupId"] = snapshotId + } + if isSystem { + if len(parentId) > 0 { + query["serverId"] = parentId + } + apiRequest = NewApiRequest(s.ID, "/api/v2/vmBackup", query, nil) + } else { + if len(parentId) > 0 { + query["volumeId"] = parentId + } + apiRequest = NewApiRequest(s.ID, "/api/v2/volume/volumebackup", query, nil) + } + request := NewNovaRequest(apiRequest) + snapshots := make([]SSnapshot, 0) + err := s.client.doList(context.Background(), request, &snapshots) + if err != nil { + return nil, err + } + return snapshots, nil +}