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

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