yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/jdcloud/snapshot.go (about)

     1  // Copyright 2019 Yunion
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package jdcloud
    16  
    17  import (
    18  	"fmt"
    19  
    20  	commodels "github.com/jdcloud-api/jdcloud-sdk-go/services/common/models"
    21  	"github.com/jdcloud-api/jdcloud-sdk-go/services/disk/apis"
    22  	"github.com/jdcloud-api/jdcloud-sdk-go/services/disk/client"
    23  	"github.com/jdcloud-api/jdcloud-sdk-go/services/disk/models"
    24  
    25  	"yunion.io/x/log"
    26  
    27  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    28  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    29  	"yunion.io/x/cloudmux/pkg/multicloud"
    30  )
    31  
    32  type SSnapshot struct {
    33  	multicloud.SResourceBase
    34  	JdcloudTags
    35  	region *SRegion
    36  
    37  	models.Snapshot
    38  	disk *SDisk
    39  }
    40  
    41  func (s *SSnapshot) GetId() string {
    42  	return s.SnapshotId
    43  }
    44  
    45  func (s *SSnapshot) GetName() string {
    46  	return s.Name
    47  }
    48  
    49  func (s *SSnapshot) GetStatus() string {
    50  	switch s.Status {
    51  	case "creating":
    52  		return api.SNAPSHOT_CREATING
    53  	case "available", "in-use":
    54  		return api.SNAPSHOT_READY
    55  	case "deleting":
    56  		return api.SNAPSHOT_DELETING
    57  	case "error_create":
    58  		return api.SNAPSHOT_FAILED
    59  	case "error_delete":
    60  		return api.SNAPSHOT_DELETE_FAILED
    61  	default:
    62  		return api.SNAPSHOT_UNKNOWN
    63  	}
    64  }
    65  
    66  func (s *SSnapshot) GetSizeMb() int32 {
    67  	return int32(s.SnapshotSizeGB) * 1024
    68  }
    69  
    70  func (s *SSnapshot) GetDiskId() string {
    71  	return s.DiskId
    72  }
    73  
    74  func (s *SSnapshot) getDisk() (*SDisk, error) {
    75  	if s.disk != nil {
    76  		return s.disk, nil
    77  	}
    78  	disk, err := s.region.GetDiskById(s.DiskId)
    79  	if err != nil {
    80  		return disk, err
    81  	}
    82  	s.disk = disk
    83  	return s.disk, nil
    84  }
    85  
    86  func (s *SSnapshot) GetDiskType() string {
    87  	disk, err := s.getDisk()
    88  	if err != nil {
    89  		log.Errorf("unable to get disk %s", s.DiskId)
    90  		return ""
    91  	}
    92  	return disk.GetDiskType()
    93  }
    94  
    95  func (s *SSnapshot) Refresh() error {
    96  	return nil
    97  }
    98  
    99  func (s *SSnapshot) GetGlobalId() string {
   100  	return s.GetId()
   101  }
   102  
   103  func (s *SSnapshot) IsEmulated() bool {
   104  	return false
   105  }
   106  
   107  func (s *SSnapshot) Delete() error {
   108  	return cloudprovider.ErrNotImplemented
   109  }
   110  
   111  func (s *SSnapshot) GetProjectId() string {
   112  	return ""
   113  }
   114  
   115  func (r *SRegion) GetSnapshots(diskId string, pageNumber, pageSize int) ([]SSnapshot, int, error) {
   116  	filters := []commodels.Filter{}
   117  	if diskId != "" {
   118  		filters = append(filters, commodels.Filter{
   119  			Name:   "diskId",
   120  			Values: []string{diskId},
   121  		})
   122  	}
   123  	req := apis.NewDescribeSnapshotsRequestWithAllParams(r.ID, &pageNumber, &pageSize, nil, filters)
   124  	client := client.NewDiskClient(r.getCredential())
   125  	client.Logger = Logger{debug: r.client.debug}
   126  	resp, err := client.DescribeSnapshots(req)
   127  	if err != nil {
   128  		return nil, 0, err
   129  	}
   130  	if resp.Error.Code >= 400 {
   131  		return nil, 0, fmt.Errorf(resp.Error.Message)
   132  	}
   133  	snapshots := make([]SSnapshot, len(resp.Result.Snapshots))
   134  	for i := range resp.Result.Snapshots {
   135  		snapshots = append(snapshots, SSnapshot{
   136  			region:   r,
   137  			Snapshot: resp.Result.Snapshots[i],
   138  		})
   139  	}
   140  	return snapshots, resp.Result.TotalCount, nil
   141  }
   142  
   143  func (r *SRegion) GetSnapshotById(id string) (*SSnapshot, error) {
   144  	req := apis.NewDescribeSnapshotRequest(r.ID, id)
   145  	client := client.NewDiskClient(r.getCredential())
   146  	client.Logger = Logger{debug: r.client.debug}
   147  	resp, err := client.DescribeSnapshot(req)
   148  	if err != nil {
   149  		return nil, err
   150  	}
   151  	if resp.Error.Code >= 400 {
   152  		return nil, fmt.Errorf(resp.Error.Message)
   153  	}
   154  	snapshot := SSnapshot{
   155  		region:   r,
   156  		Snapshot: resp.Result.Snapshot,
   157  	}
   158  	return &snapshot, nil
   159  }