github.com/gophercloud/gophercloud@v1.11.0/openstack/networking/v2/extensions/qos/policies/results.go (about)

     1  package policies
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/gophercloud/gophercloud"
     7  	"github.com/gophercloud/gophercloud/pagination"
     8  )
     9  
    10  // QoSPolicyExt represents additional resource attributes available with the QoS extension.
    11  type QoSPolicyExt struct {
    12  	// QoSPolicyID represents an associated QoS policy.
    13  	QoSPolicyID string `json:"qos_policy_id"`
    14  }
    15  
    16  type commonResult struct {
    17  	gophercloud.Result
    18  }
    19  
    20  // GetResult represents the result of a get operation. Call its Extract
    21  // method to interpret it as a QoS policy.
    22  type GetResult struct {
    23  	commonResult
    24  }
    25  
    26  // CreateResult represents the result of a Create operation. Call its Extract
    27  // method to interpret it as a QoS policy.
    28  type CreateResult struct {
    29  	commonResult
    30  }
    31  
    32  // UpdateResult represents the result of a Create operation. Call its Extract
    33  // method to interpret it as a QoS policy.
    34  type UpdateResult struct {
    35  	commonResult
    36  }
    37  
    38  // DeleteResult represents the result of a delete operation. Call its
    39  // ExtractErr method to determine if the request succeeded or failed.
    40  type DeleteResult struct {
    41  	gophercloud.ErrResult
    42  }
    43  
    44  // Extract is a function that accepts a result and extracts a QoS policy resource.
    45  func (r commonResult) Extract() (*Policy, error) {
    46  	var s struct {
    47  		Policy *Policy `json:"policy"`
    48  	}
    49  	err := r.ExtractInto(&s)
    50  	return s.Policy, err
    51  }
    52  
    53  // Policy represents a QoS policy.
    54  type Policy struct {
    55  	// ID is the id of the policy.
    56  	ID string `json:"id"`
    57  
    58  	// Name is the human-readable name of the policy.
    59  	Name string `json:"name"`
    60  
    61  	// TenantID is the id of the Identity project.
    62  	TenantID string `json:"tenant_id"`
    63  
    64  	// ProjectID is the id of the Identity project.
    65  	ProjectID string `json:"project_id"`
    66  
    67  	// CreatedAt is the time at which the policy has been created.
    68  	CreatedAt time.Time `json:"created_at"`
    69  
    70  	// UpdatedAt is the time at which the policy has been created.
    71  	UpdatedAt time.Time `json:"updated_at"`
    72  
    73  	// IsDefault indicates if the policy is default policy or not.
    74  	IsDefault bool `json:"is_default"`
    75  
    76  	// Description is thehuman-readable description for the resource.
    77  	Description string `json:"description"`
    78  
    79  	// Shared indicates whether this policy is shared across all projects.
    80  	Shared bool `json:"shared"`
    81  
    82  	// RevisionNumber represents revision number of the policy.
    83  	RevisionNumber int `json:"revision_number"`
    84  
    85  	// Rules represents QoS rules of the policy.
    86  	Rules []map[string]interface{} `json:"rules"`
    87  
    88  	// Tags optionally set via extensions/attributestags
    89  	Tags []string `json:"tags"`
    90  }
    91  
    92  // PolicyPage stores a single page of Policies from a List() API call.
    93  type PolicyPage struct {
    94  	pagination.LinkedPageBase
    95  }
    96  
    97  // NextPageURL is invoked when a paginated collection of policies has reached
    98  // the end of a page and the pager seeks to traverse over a new one.
    99  // In order to do this, it needs to construct the next page's URL.
   100  func (r PolicyPage) NextPageURL() (string, error) {
   101  	var s struct {
   102  		Links []gophercloud.Link `json:"policies_links"`
   103  	}
   104  	err := r.ExtractInto(&s)
   105  	if err != nil {
   106  		return "", err
   107  	}
   108  	return gophercloud.ExtractNextURL(s.Links)
   109  }
   110  
   111  // IsEmpty checks whether a PolicyPage is empty.
   112  func (r PolicyPage) IsEmpty() (bool, error) {
   113  	if r.StatusCode == 204 {
   114  		return true, nil
   115  	}
   116  
   117  	is, err := ExtractPolicies(r)
   118  	return len(is) == 0, err
   119  }
   120  
   121  // ExtractPolicies accepts a PolicyPage, and extracts the elements into a slice of Policies.
   122  func ExtractPolicies(r pagination.Page) ([]Policy, error) {
   123  	var s []Policy
   124  	err := ExtractPolicysInto(r, &s)
   125  	return s, err
   126  }
   127  
   128  // ExtractPoliciesInto extracts the elements into a slice of RBAC Policy structs.
   129  func ExtractPolicysInto(r pagination.Page, v interface{}) error {
   130  	return r.(PolicyPage).Result.ExtractIntoSlicePtr(v, "policies")
   131  }