github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/vbs/v2/backups/results.go (about) 1 package backups 2 3 import ( 4 "encoding/json" 5 "time" 6 7 "github.com/opentelekomcloud/gophertelekomcloud" 8 "github.com/opentelekomcloud/gophertelekomcloud/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 108 if err := r.ExtractInto(&s); err != nil { 109 return nil, err 110 } 111 112 // Description test is some strange JSON 113 var desc struct { 114 Description string `json:"DESC"` 115 } 116 err := json.Unmarshal([]byte(s.Backup.Description), &desc) 117 if err == nil { 118 s.Backup.Description = desc.Description 119 } 120 121 return s.Backup, nil 122 } 123 124 // ExtractBackupRestore is a function that accepts a result and extracts a backup 125 func (r commonResult) ExtractBackupRestore() (*BackupRestoreInfo, error) { 126 var s struct { 127 Restore *BackupRestoreInfo `json:"restore"` 128 } 129 err := r.ExtractInto(&s) 130 return s.Restore, err 131 } 132 133 // CreateResult represents the result of a create operation. Call its Extract 134 // method to interpret it as a Backup. 135 type CreateResult struct { 136 commonResult 137 } 138 139 // GetResult represents the result of a get operation. Call its Extract 140 // method to interpret it as a Backup. 141 type GetResult struct { 142 commonResult 143 } 144 145 // DeleteResult represents the result of a delete operation. Call its ExtractErr 146 // method to determine if the request succeeded or failed. 147 type DeleteResult struct { 148 golangsdk.ErrResult 149 } 150 151 // UnmarshalJSON overrides the default, to convert the JSON API response into our Backup struct 152 func (r *Backup) UnmarshalJSON(b []byte) error { 153 type tmp Backup 154 var s struct { 155 tmp 156 CreatedAt golangsdk.JSONRFC3339MilliNoZ `json:"created_at"` 157 UpdatedAt golangsdk.JSONRFC3339MilliNoZ `json:"updated_at"` 158 DataTimeStamp golangsdk.JSONRFC3339MilliNoZ `json:"data_timestamp"` 159 } 160 err := json.Unmarshal(b, &s) 161 if err != nil { 162 return err 163 } 164 165 *r = Backup(s.tmp) 166 167 r.CreatedAt = time.Time(s.CreatedAt) 168 r.UpdatedAt = time.Time(s.UpdatedAt) 169 r.DataTimeStamp = time.Time(s.DataTimeStamp) 170 171 return nil 172 }