github.com/gophercloud/gophercloud@v1.11.0/openstack/blockstorage/extensions/volumetransfers/results.go (about)

     1  package volumetransfers
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	"github.com/gophercloud/gophercloud"
     8  	"github.com/gophercloud/gophercloud/pagination"
     9  )
    10  
    11  // Transfer represents a Volume Transfer record
    12  type Transfer struct {
    13  	ID        string              `json:"id"`
    14  	AuthKey   string              `json:"auth_key"`
    15  	Name      string              `json:"name"`
    16  	VolumeID  string              `json:"volume_id"`
    17  	CreatedAt time.Time           `json:"-"`
    18  	Links     []map[string]string `json:"links"`
    19  }
    20  
    21  // UnmarshalJSON is our unmarshalling helper
    22  func (r *Transfer) UnmarshalJSON(b []byte) error {
    23  	type tmp Transfer
    24  	var s struct {
    25  		tmp
    26  		CreatedAt gophercloud.JSONRFC3339MilliNoZ `json:"created_at"`
    27  	}
    28  	err := json.Unmarshal(b, &s)
    29  	if err != nil {
    30  		return err
    31  	}
    32  	*r = Transfer(s.tmp)
    33  
    34  	r.CreatedAt = time.Time(s.CreatedAt)
    35  
    36  	return err
    37  }
    38  
    39  type commonResult struct {
    40  	gophercloud.Result
    41  }
    42  
    43  // Extract will get the Transfer object out of the commonResult object.
    44  func (r commonResult) Extract() (*Transfer, error) {
    45  	var s Transfer
    46  	err := r.ExtractInto(&s)
    47  	return &s, err
    48  }
    49  
    50  // ExtractInto converts our response data into a transfer struct
    51  func (r commonResult) ExtractInto(v interface{}) error {
    52  	return r.Result.ExtractIntoStructPtr(v, "transfer")
    53  }
    54  
    55  // CreateResult contains the response body and error from a Create request.
    56  type CreateResult struct {
    57  	commonResult
    58  }
    59  
    60  // GetResult contains the response body and error from a Get request.
    61  type GetResult struct {
    62  	commonResult
    63  }
    64  
    65  // DeleteResult contains the response body and error from a Delete request.
    66  type DeleteResult struct {
    67  	gophercloud.ErrResult
    68  }
    69  
    70  // ExtractTransfers extracts and returns Transfers. It is used while iterating over a transfers.List call.
    71  func ExtractTransfers(r pagination.Page) ([]Transfer, error) {
    72  	var s []Transfer
    73  	err := ExtractTransfersInto(r, &s)
    74  	return s, err
    75  }
    76  
    77  // ExtractTransfersInto similar to ExtractInto but operates on a `list` of transfers
    78  func ExtractTransfersInto(r pagination.Page, v interface{}) error {
    79  	return r.(TransferPage).Result.ExtractIntoSlicePtr(v, "transfers")
    80  }
    81  
    82  // TransferPage is a pagination.pager that is returned from a call to the List function.
    83  type TransferPage struct {
    84  	pagination.LinkedPageBase
    85  }
    86  
    87  // IsEmpty returns true if a ListResult contains no Transfers.
    88  func (r TransferPage) IsEmpty() (bool, error) {
    89  	if r.StatusCode == 204 {
    90  		return true, nil
    91  	}
    92  
    93  	transfers, err := ExtractTransfers(r)
    94  	return len(transfers) == 0, err
    95  }
    96  
    97  func (page TransferPage) NextPageURL() (string, error) {
    98  	var s struct {
    99  		Links []gophercloud.Link `json:"transfers_links"`
   100  	}
   101  	err := page.ExtractInto(&s)
   102  	if err != nil {
   103  		return "", err
   104  	}
   105  	return gophercloud.ExtractNextURL(s.Links)
   106  }