github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/csbs/v1/backup/results.go (about)

     1  package backup
     2  
     3  import (
     4  	"encoding/json"
     5  	"strconv"
     6  	"time"
     7  
     8  	"github.com/huaweicloud/golangsdk"
     9  	"github.com/huaweicloud/golangsdk/pagination"
    10  )
    11  
    12  type Checkpoint struct {
    13  	Status         string         `json:"status"`
    14  	CreatedAt      time.Time      `json:"-"`
    15  	Id             string         `json:"id"`
    16  	ResourceGraph  string         `json:"resource_graph"`
    17  	ProjectId      string         `json:"project_id"`
    18  	ProtectionPlan ProtectionPlan `json:"protection_plan"`
    19  }
    20  
    21  type ProtectionPlan struct {
    22  	Id              string           `json:"id"`
    23  	Name            string           `json:"name"`
    24  	BackupResources []BackupResource `json:"resources"`
    25  }
    26  
    27  type BackupResource struct {
    28  	ID        string `json:"id"`
    29  	Type      string `json:"type"`
    30  	Name      string `json:"name"`
    31  	ExtraInfo string `json:"-"`
    32  }
    33  
    34  type ResourceCapability struct {
    35  	Result       bool   `json:"result"`
    36  	ResourceType string `json:"resource_type"`
    37  	ErrorCode    string `json:"error_code"`
    38  	ErrorMsg     string `json:"error_msg"`
    39  	ResourceId   string `json:"resource_id"`
    40  }
    41  
    42  // UnmarshalJSON helps to unmarshal Checkpoint fields into needed values.
    43  func (r *Checkpoint) UnmarshalJSON(b []byte) error {
    44  	type tmp Checkpoint
    45  	var s struct {
    46  		tmp
    47  		CreatedAt golangsdk.JSONRFC3339MilliNoZ `json:"created_at"`
    48  	}
    49  	err := json.Unmarshal(b, &s)
    50  	if err != nil {
    51  		return err
    52  	}
    53  	*r = Checkpoint(s.tmp)
    54  
    55  	r.CreatedAt = time.Time(s.CreatedAt)
    56  
    57  	return err
    58  }
    59  
    60  // UnmarshalJSON helps to unmarshal BackupResource fields into needed values.
    61  func (r *BackupResource) UnmarshalJSON(b []byte) error {
    62  	type tmp BackupResource
    63  	var s struct {
    64  		tmp
    65  		ExtraInfo interface{} `json:"extra_info"`
    66  	}
    67  	err := json.Unmarshal(b, &s)
    68  	if err != nil {
    69  		return err
    70  	}
    71  
    72  	*r = BackupResource(s.tmp)
    73  
    74  	switch t := s.ExtraInfo.(type) {
    75  	case float64:
    76  		r.ID = strconv.FormatFloat(t, 'f', -1, 64)
    77  	case string:
    78  		r.ID = t
    79  	}
    80  
    81  	return err
    82  }
    83  
    84  func (r commonResult) ExtractQueryResponse() ([]ResourceCapability, error) {
    85  	var s struct {
    86  		ResourcesCaps []ResourceCapability `json:"protectable"`
    87  	}
    88  	err := r.ExtractInto(&s)
    89  	return s.ResourcesCaps, err
    90  }
    91  
    92  type Backup struct {
    93  	CheckpointId string        `json:"checkpoint_id"`
    94  	CreatedAt    time.Time     `json:"-"`
    95  	ExtendInfo   ExtendInfo    `json:"extend_info"`
    96  	Id           string        `json:"id"`
    97  	Name         string        `json:"name"`
    98  	ResourceId   string        `json:"resource_id"`
    99  	Status       string        `json:"status"`
   100  	UpdatedAt    time.Time     `json:"-"`
   101  	VMMetadata   VMMetadata    `json:"backup_data"`
   102  	Description  string        `json:"description"`
   103  	Tags         []ResourceTag `json:"tags"`
   104  	ResourceType string        `json:"resource_type"`
   105  }
   106  
   107  type ExtendInfo struct {
   108  	AutoTrigger          bool           `json:"auto_trigger"`
   109  	AverageSpeed         int            `json:"average_speed"`
   110  	CopyFrom             string         `json:"copy_from"`
   111  	CopyStatus           string         `json:"copy_status"`
   112  	FailCode             FailCode       `json:"fail_code"`
   113  	FailOp               string         `json:"fail_op"`
   114  	FailReason           string         `json:"fail_reason"`
   115  	ImageType            string         `json:"image_type"`
   116  	Incremental          bool           `json:"incremental"`
   117  	Progress             int            `json:"progress"`
   118  	ResourceAz           string         `json:"resource_az"`
   119  	ResourceName         string         `json:"resource_name"`
   120  	ResourceType         string         `json:"resource_type"`
   121  	Size                 int            `json:"size"`
   122  	SpaceSavingRatio     int            `json:"space_saving_ratio"`
   123  	VolumeBackups        []VolumeBackup `json:"volume_backups"`
   124  	FinishedAt           time.Time      `json:"-"`
   125  	TaskId               string         `json:"taskid"`
   126  	HypervisorType       string         `json:"hypervisor_type"`
   127  	SupportedRestoreMode string         `json:"supported_restore_mode"`
   128  	Supportlld           bool           `json:"support_lld"`
   129  }
   130  
   131  type VMMetadata struct {
   132  	RegionName       string `json:"__openstack_region_name"`
   133  	CloudServiceType string `json:"cloudservicetype"`
   134  	Disk             int    `json:"disk"`
   135  	ImageType        string `json:"imagetype"`
   136  	Ram              int    `json:"ram"`
   137  	Vcpus            int    `json:"vcpus"`
   138  	Eip              string `json:"eip"`
   139  	PrivateIp        string `json:"private_ip"`
   140  }
   141  
   142  type FailCode struct {
   143  	Code        string `json:"Code"`
   144  	Description string `json:"Description"`
   145  }
   146  
   147  type VolumeBackup struct {
   148  	AverageSpeed     int    `json:"average_speed"`
   149  	Bootable         bool   `json:"bootable"`
   150  	Id               string `json:"id"`
   151  	ImageType        string `json:"image_type"`
   152  	Incremental      bool   `json:"incremental"`
   153  	SnapshotID       string `json:"snapshot_id"`
   154  	Name             string `json:"name"`
   155  	Size             int    `json:"size"`
   156  	SourceVolumeId   string `json:"source_volume_id"`
   157  	SourceVolumeSize int    `json:"source_volume_size"`
   158  	SpaceSavingRatio int    `json:"space_saving_ratio"`
   159  	Status           string `json:"status"`
   160  	SourceVolumeName string `json:"source_volume_name"`
   161  }
   162  
   163  // UnmarshalJSON helps to unmarshal Backup fields into needed values.
   164  func (r *Backup) UnmarshalJSON(b []byte) error {
   165  	type tmp Backup
   166  	var s struct {
   167  		tmp
   168  		CreatedAt golangsdk.JSONRFC3339MilliNoZ `json:"created_at"`
   169  		UpdatedAt golangsdk.JSONRFC3339MilliNoZ `json:"updated_at"`
   170  	}
   171  	err := json.Unmarshal(b, &s)
   172  	if err != nil {
   173  		return err
   174  	}
   175  	*r = Backup(s.tmp)
   176  
   177  	r.CreatedAt = time.Time(s.CreatedAt)
   178  	r.UpdatedAt = time.Time(s.UpdatedAt)
   179  
   180  	return err
   181  }
   182  
   183  // UnmarshalJSON helps to unmarshal ExtendInfo fields into needed values.
   184  func (r *ExtendInfo) UnmarshalJSON(b []byte) error {
   185  	type tmp ExtendInfo
   186  	var s struct {
   187  		tmp
   188  		FinishedAt golangsdk.JSONRFC3339MilliNoZ `json:"finished_at"`
   189  	}
   190  	err := json.Unmarshal(b, &s)
   191  	if err != nil {
   192  		return err
   193  	}
   194  	*r = ExtendInfo(s.tmp)
   195  
   196  	r.FinishedAt = time.Time(s.FinishedAt)
   197  
   198  	return err
   199  }
   200  
   201  // Extract will get the checkpoint object from the commonResult
   202  func (r commonResult) Extract() (*Checkpoint, error) {
   203  	var s struct {
   204  		Checkpoint *Checkpoint `json:"checkpoint"`
   205  	}
   206  
   207  	err := r.ExtractInto(&s)
   208  	return s.Checkpoint, err
   209  }
   210  
   211  // ExtractBackup will get the backup object from the commonResult
   212  func (r commonResult) ExtractBackup() (*Backup, error) {
   213  	var s struct {
   214  		Backup *Backup `json:"checkpoint_item"`
   215  	}
   216  
   217  	err := r.ExtractInto(&s)
   218  	return s.Backup, err
   219  }
   220  
   221  // BackupPage is the page returned by a pager when traversing over a
   222  // collection of backups.
   223  type BackupPage struct {
   224  	pagination.LinkedPageBase
   225  }
   226  
   227  // NextPageURL is invoked when a paginated collection of backups has reached
   228  // the end of a page and the pager seeks to traverse over a new one. In order
   229  // to do this, it needs to construct the next page's URL.
   230  func (r BackupPage) NextPageURL() (string, error) {
   231  	var s struct {
   232  		Links []golangsdk.Link `json:"checkpoint_items_links"`
   233  	}
   234  	err := r.ExtractInto(&s)
   235  	if err != nil {
   236  		return "", err
   237  	}
   238  	return golangsdk.ExtractNextURL(s.Links)
   239  }
   240  
   241  // IsEmpty checks whether a BackupPage struct is empty.
   242  func (r BackupPage) IsEmpty() (bool, error) {
   243  	is, err := ExtractBackups(r)
   244  	return len(is) == 0, err
   245  }
   246  
   247  // ExtractBackups accepts a Page struct, specifically a BackupPage struct,
   248  // and extracts the elements into a slice of Backup structs. In other words,
   249  // a generic collection is mapped into a relevant slice.
   250  func ExtractBackups(r pagination.Page) ([]Backup, error) {
   251  	var s struct {
   252  		Backups []Backup `json:"checkpoint_items"`
   253  	}
   254  	err := (r.(BackupPage)).ExtractInto(&s)
   255  	return s.Backups, err
   256  }
   257  
   258  type commonResult struct {
   259  	golangsdk.Result
   260  }
   261  
   262  type CreateResult struct {
   263  	commonResult
   264  }
   265  
   266  type DeleteResult struct {
   267  	commonResult
   268  }
   269  
   270  type GetResult struct {
   271  	commonResult
   272  }
   273  
   274  type QueryResult struct {
   275  	commonResult
   276  }