github.com/gophercloud/gophercloud@v1.11.0/openstack/blockstorage/v3/qos/results.go (about)

     1  package qos
     2  
     3  import (
     4  	"github.com/gophercloud/gophercloud"
     5  	"github.com/gophercloud/gophercloud/pagination"
     6  )
     7  
     8  // QoS contains all the information associated with an OpenStack QoS specification.
     9  type QoS struct {
    10  	// Name is the name of the QoS.
    11  	Name string `json:"name"`
    12  	// Unique identifier for the QoS.
    13  	ID string `json:"id"`
    14  	// Consumer of QoS
    15  	Consumer string `json:"consumer"`
    16  	// Arbitrary key-value pairs defined by the user.
    17  	Specs map[string]string `json:"specs"`
    18  }
    19  
    20  type commonResult struct {
    21  	gophercloud.Result
    22  }
    23  
    24  // Extract will get the QoS object out of the commonResult object.
    25  func (r commonResult) Extract() (*QoS, error) {
    26  	var s QoS
    27  	err := r.ExtractInto(&s)
    28  	return &s, err
    29  }
    30  
    31  // ExtractInto converts our response data into a QoS struct
    32  func (r commonResult) ExtractInto(qos interface{}) error {
    33  	return r.Result.ExtractIntoStructPtr(qos, "qos_specs")
    34  }
    35  
    36  // CreateResult contains the response body and error from a Create request.
    37  type CreateResult struct {
    38  	commonResult
    39  }
    40  
    41  // DeleteResult contains the response body and error from a Delete request.
    42  type DeleteResult struct {
    43  	gophercloud.ErrResult
    44  }
    45  
    46  type QoSPage struct {
    47  	pagination.LinkedPageBase
    48  }
    49  
    50  // IsEmpty determines if a QoSPage contains any results.
    51  func (page QoSPage) IsEmpty() (bool, error) {
    52  	if page.StatusCode == 204 {
    53  		return true, nil
    54  	}
    55  
    56  	qos, err := ExtractQoS(page)
    57  	return len(qos) == 0, err
    58  }
    59  
    60  // NextPageURL uses the response's embedded link reference to navigate to the
    61  // next page of results.
    62  func (page QoSPage) NextPageURL() (string, error) {
    63  	var s struct {
    64  		Links []gophercloud.Link `json:"qos_specs_links"`
    65  	}
    66  	err := page.ExtractInto(&s)
    67  	if err != nil {
    68  		return "", err
    69  	}
    70  	return gophercloud.ExtractNextURL(s.Links)
    71  }
    72  
    73  // ExtractQoS provides access to the list of qos in a page acquired
    74  // from the List operation.
    75  func ExtractQoS(r pagination.Page) ([]QoS, error) {
    76  	var s struct {
    77  		QoSs []QoS `json:"qos_specs"`
    78  	}
    79  	err := (r.(QoSPage)).ExtractInto(&s)
    80  	return s.QoSs, err
    81  }
    82  
    83  // GetResult is the response of a Get operations. Call its Extract method to
    84  // interpret it as a Flavor.
    85  type GetResult struct {
    86  	commonResult
    87  }
    88  
    89  // Extract interprets any updateResult as qosSpecs, if possible.
    90  func (r updateResult) Extract() (map[string]string, error) {
    91  	var s struct {
    92  		QosSpecs map[string]string `json:"qos_specs"`
    93  	}
    94  	err := r.ExtractInto(&s)
    95  	return s.QosSpecs, err
    96  }
    97  
    98  // updateResult contains the result of a call for (potentially) multiple
    99  // key-value pairs. Call its Extract method to interpret it as a
   100  // map[string]interface.
   101  type updateResult struct {
   102  	gophercloud.Result
   103  }
   104  
   105  // AssociateResult contains the response body and error from a Associate request.
   106  type AssociateResult struct {
   107  	gophercloud.ErrResult
   108  }
   109  
   110  // DisassociateResult contains the response body and error from a Disassociate request.
   111  type DisassociateResult struct {
   112  	gophercloud.ErrResult
   113  }
   114  
   115  // DisassociateAllResult contains the response body and error from a DisassociateAll request.
   116  type DisassociateAllResult struct {
   117  	gophercloud.ErrResult
   118  }
   119  
   120  // QoS contains all the information associated with an OpenStack QoS specification.
   121  type QosAssociation struct {
   122  	// Name is the name of the associated resource
   123  	Name string `json:"name"`
   124  	// Unique identifier of the associated resources
   125  	ID string `json:"id"`
   126  	// AssociationType of the QoS Association
   127  	AssociationType string `json:"association_type"`
   128  }
   129  
   130  // AssociationPage contains a single page of all Associations of a QoS
   131  type AssociationPage struct {
   132  	pagination.SinglePageBase
   133  }
   134  
   135  // IsEmpty indicates whether an Association page is empty.
   136  func (page AssociationPage) IsEmpty() (bool, error) {
   137  	if page.StatusCode == 204 {
   138  		return true, nil
   139  	}
   140  
   141  	v, err := ExtractAssociations(page)
   142  	return len(v) == 0, err
   143  }
   144  
   145  // ExtractAssociations interprets a page of results as a slice of QosAssociations
   146  func ExtractAssociations(r pagination.Page) ([]QosAssociation, error) {
   147  	var s struct {
   148  		QosAssociations []QosAssociation `json:"qos_associations"`
   149  	}
   150  	err := (r.(AssociationPage)).ExtractInto(&s)
   151  	return s.QosAssociations, err
   152  }