github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/vbs/v2/backups/results.go (about)

     1  package backups
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	"github.com/huaweicloud/golangsdk"
     8  	"github.com/huaweicloud/golangsdk/pagination"
     9  )
    10  
    11  type Backup struct {
    12  	//Backup ID
    13  	Id string `json:"id"`
    14  	//Backup name
    15  	Name string `json:"name"`
    16  	//Backup URL
    17  	Links []golangsdk.Link `json:"links"`
    18  	//Backup status
    19  	Status string `json:"status"`
    20  	//Backup description
    21  	Description string `json:"description"`
    22  	//AvailabilityZone where the backup resides
    23  	AvailabilityZone string `json:"availability_zone"`
    24  	//Source volume ID of the backup
    25  	VolumeId string `json:"volume_id"`
    26  	//Cause of the backup failure
    27  	FailReason string `json:"fail_reason"`
    28  	//Backup size
    29  	Size int `json:"size"`
    30  	//Number of objects on OBS for the disk data
    31  	ObjectCount int `json:"object_count"`
    32  	//Container of the backup
    33  	Container string `json:"container"`
    34  	//Backup creation time
    35  	CreatedAt time.Time `json:"-"`
    36  	//ID of the tenant to which the backup belongs
    37  	TenantId string `json:"os-bak-tenant-attr:tenant_id"`
    38  	//Backup metadata
    39  	ServiceMetadata string `json:"service_metadata"`
    40  	//Time when the backup was updated
    41  	UpdatedAt time.Time `json:"-"`
    42  	//Current time
    43  	DataTimeStamp time.Time `json:"-"`
    44  	//Whether a dependent backup exists
    45  	DependentBackups bool `json:"has_dependent_backups"`
    46  	//ID of the snapshot associated with the backup
    47  	SnapshotId string `json:"snapshot_id"`
    48  	//Whether the backup is an incremental backup
    49  	Incremental bool `json:"is_incremental"`
    50  }
    51  
    52  type BackupRestoreInfo struct {
    53  	//Backup ID
    54  	BackupId string `json:"backup_id"`
    55  	//Volume ID
    56  	VolumeId string `json:"volume_id"`
    57  	//Volume name
    58  	VolumeName string `json:"volume_name"`
    59  }
    60  
    61  // BackupPage is the page returned by a pager when traversing over a
    62  // collection of Backups.
    63  type BackupPage struct {
    64  	pagination.LinkedPageBase
    65  }
    66  
    67  // NextPageURL is invoked when a paginated collection of Backups has reached
    68  // the end of a page and the pager seeks to traverse over a new one. In order
    69  // to do this, it needs to construct the next page's URL.
    70  func (r BackupPage) NextPageURL() (string, error) {
    71  	var s struct {
    72  		Links []golangsdk.Link `json:"backups_links"`
    73  	}
    74  	err := r.ExtractInto(&s)
    75  	if err != nil {
    76  		return "", err
    77  	}
    78  	return golangsdk.ExtractNextURL(s.Links)
    79  }
    80  
    81  // IsEmpty checks whether a BackupPage struct is empty.
    82  func (r BackupPage) IsEmpty() (bool, error) {
    83  	is, err := ExtractBackups(r)
    84  	return len(is) == 0, err
    85  }
    86  
    87  // ExtractBackups accepts a Page struct, specifically a BackupPage struct,
    88  // and extracts the elements into a slice of Backups struct. In other words,
    89  // a generic collection is mapped into a relevant slice.
    90  func ExtractBackups(r pagination.Page) ([]Backup, error) {
    91  	var s struct {
    92  		Backups []Backup `json:"backups"`
    93  	}
    94  	err := (r.(BackupPage)).ExtractInto(&s)
    95  	return s.Backups, err
    96  }
    97  
    98  type commonResult struct {
    99  	golangsdk.Result
   100  }
   101  
   102  // Extract is a function that accepts a result and extracts a backup.
   103  func (r commonResult) Extract() (*Backup, error) {
   104  	var s struct {
   105  		Backup *Backup `json:"backup"`
   106  	}
   107  	err := r.ExtractInto(&s)
   108  	return s.Backup, err
   109  }
   110  
   111  // ExtractBackupRestore is a function that accepts a result and extracts a backup
   112  func (r commonResult) ExtractBackupRestore() (*BackupRestoreInfo, error) {
   113  	var s struct {
   114  		Restore *BackupRestoreInfo `json:"restore"`
   115  	}
   116  	err := r.ExtractInto(&s)
   117  	return s.Restore, err
   118  }
   119  
   120  // CreateResult represents the result of a create operation. Call its Extract
   121  // method to interpret it as a Backup.
   122  type CreateResult struct {
   123  	commonResult
   124  }
   125  
   126  // GetResult represents the result of a get operation. Call its Extract
   127  // method to interpret it as a Backup.
   128  type GetResult struct {
   129  	commonResult
   130  }
   131  
   132  // DeleteResult represents the result of a delete operation. Call its ExtractErr
   133  // method to determine if the request succeeded or failed.
   134  type DeleteResult struct {
   135  	golangsdk.ErrResult
   136  }
   137  
   138  // UnmarshalJSON overrides the default, to convert the JSON API response into our Backup struct
   139  func (r *Backup) UnmarshalJSON(b []byte) error {
   140  	type tmp Backup
   141  	var s struct {
   142  		tmp
   143  		CreatedAt     golangsdk.JSONRFC3339MilliNoZ `json:"created_at"`
   144  		UpdatedAt     golangsdk.JSONRFC3339MilliNoZ `json:"updated_at"`
   145  		DataTimeStamp golangsdk.JSONRFC3339MilliNoZ `json:"data_timestamp"`
   146  	}
   147  	err := json.Unmarshal(b, &s)
   148  	if err != nil {
   149  		return err
   150  	}
   151  
   152  	*r = Backup(s.tmp)
   153  
   154  	r.CreatedAt = time.Time(s.CreatedAt)
   155  	r.UpdatedAt = time.Time(s.UpdatedAt)
   156  	r.DataTimeStamp = time.Time(s.DataTimeStamp)
   157  
   158  	return nil
   159  }