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

     1  package policies
     2  
     3  import (
     4  	"github.com/gophercloud/gophercloud"
     5  	"github.com/gophercloud/gophercloud/openstack/networking/v2/networks"
     6  	"github.com/gophercloud/gophercloud/openstack/networking/v2/ports"
     7  	"github.com/gophercloud/gophercloud/pagination"
     8  )
     9  
    10  // PortCreateOptsExt adds QoS options to the base ports.CreateOpts.
    11  type PortCreateOptsExt struct {
    12  	ports.CreateOptsBuilder
    13  
    14  	// QoSPolicyID represents an associated QoS policy.
    15  	QoSPolicyID string `json:"qos_policy_id,omitempty"`
    16  }
    17  
    18  // ToPortCreateMap casts a CreateOpts struct to a map.
    19  func (opts PortCreateOptsExt) ToPortCreateMap() (map[string]interface{}, error) {
    20  	base, err := opts.CreateOptsBuilder.ToPortCreateMap()
    21  	if err != nil {
    22  		return nil, err
    23  	}
    24  
    25  	port := base["port"].(map[string]interface{})
    26  
    27  	if opts.QoSPolicyID != "" {
    28  		port["qos_policy_id"] = opts.QoSPolicyID
    29  	}
    30  
    31  	return base, nil
    32  }
    33  
    34  // PortUpdateOptsExt adds QoS options to the base ports.UpdateOpts.
    35  type PortUpdateOptsExt struct {
    36  	ports.UpdateOptsBuilder
    37  
    38  	// QoSPolicyID represents an associated QoS policy.
    39  	// Setting it to a pointer of an empty string will remove associated QoS policy from port.
    40  	QoSPolicyID *string `json:"qos_policy_id,omitempty"`
    41  }
    42  
    43  // ToPortUpdateMap casts a UpdateOpts struct to a map.
    44  func (opts PortUpdateOptsExt) ToPortUpdateMap() (map[string]interface{}, error) {
    45  	base, err := opts.UpdateOptsBuilder.ToPortUpdateMap()
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  
    50  	port := base["port"].(map[string]interface{})
    51  
    52  	if opts.QoSPolicyID != nil {
    53  		qosPolicyID := *opts.QoSPolicyID
    54  		if qosPolicyID != "" {
    55  			port["qos_policy_id"] = qosPolicyID
    56  		} else {
    57  			port["qos_policy_id"] = nil
    58  		}
    59  	}
    60  
    61  	return base, nil
    62  }
    63  
    64  // NetworkCreateOptsExt adds QoS options to the base networks.CreateOpts.
    65  type NetworkCreateOptsExt struct {
    66  	networks.CreateOptsBuilder
    67  
    68  	// QoSPolicyID represents an associated QoS policy.
    69  	QoSPolicyID string `json:"qos_policy_id,omitempty"`
    70  }
    71  
    72  // ToNetworkCreateMap casts a CreateOpts struct to a map.
    73  func (opts NetworkCreateOptsExt) ToNetworkCreateMap() (map[string]interface{}, error) {
    74  	base, err := opts.CreateOptsBuilder.ToNetworkCreateMap()
    75  	if err != nil {
    76  		return nil, err
    77  	}
    78  
    79  	network := base["network"].(map[string]interface{})
    80  
    81  	if opts.QoSPolicyID != "" {
    82  		network["qos_policy_id"] = opts.QoSPolicyID
    83  	}
    84  
    85  	return base, nil
    86  }
    87  
    88  // NetworkUpdateOptsExt adds QoS options to the base networks.UpdateOpts.
    89  type NetworkUpdateOptsExt struct {
    90  	networks.UpdateOptsBuilder
    91  
    92  	// QoSPolicyID represents an associated QoS policy.
    93  	// Setting it to a pointer of an empty string will remove associated QoS policy from network.
    94  	QoSPolicyID *string `json:"qos_policy_id,omitempty"`
    95  }
    96  
    97  // ToNetworkUpdateMap casts a UpdateOpts struct to a map.
    98  func (opts NetworkUpdateOptsExt) ToNetworkUpdateMap() (map[string]interface{}, error) {
    99  	base, err := opts.UpdateOptsBuilder.ToNetworkUpdateMap()
   100  	if err != nil {
   101  		return nil, err
   102  	}
   103  
   104  	network := base["network"].(map[string]interface{})
   105  
   106  	if opts.QoSPolicyID != nil {
   107  		qosPolicyID := *opts.QoSPolicyID
   108  		if qosPolicyID != "" {
   109  			network["qos_policy_id"] = qosPolicyID
   110  		} else {
   111  			network["qos_policy_id"] = nil
   112  		}
   113  	}
   114  
   115  	return base, nil
   116  }
   117  
   118  // PolicyListOptsBuilder allows extensions to add additional parameters to the List request.
   119  type PolicyListOptsBuilder interface {
   120  	ToPolicyListQuery() (string, error)
   121  }
   122  
   123  // ListOpts allows the filtering and sorting of paginated collections through
   124  // the Neutron API. Filtering is achieved by passing in struct field values
   125  // that map to the Policy attributes you want to see returned.
   126  // SortKey allows you to sort by a particular Policy attribute.
   127  // SortDir sets the direction, and is either `asc' or `desc'.
   128  // Marker and Limit are used for the pagination.
   129  type ListOpts struct {
   130  	ID             string `q:"id"`
   131  	TenantID       string `q:"tenant_id"`
   132  	ProjectID      string `q:"project_id"`
   133  	Name           string `q:"name"`
   134  	Description    string `q:"description"`
   135  	RevisionNumber *int   `q:"revision_number"`
   136  	IsDefault      *bool  `q:"is_default"`
   137  	Shared         *bool  `q:"shared"`
   138  	Limit          int    `q:"limit"`
   139  	Marker         string `q:"marker"`
   140  	SortKey        string `q:"sort_key"`
   141  	SortDir        string `q:"sort_dir"`
   142  	Tags           string `q:"tags"`
   143  	TagsAny        string `q:"tags-any"`
   144  	NotTags        string `q:"not-tags"`
   145  	NotTagsAny     string `q:"not-tags-any"`
   146  }
   147  
   148  // ToPolicyListQuery formats a ListOpts into a query string.
   149  func (opts ListOpts) ToPolicyListQuery() (string, error) {
   150  	q, err := gophercloud.BuildQueryString(opts)
   151  	return q.String(), err
   152  }
   153  
   154  // List returns a Pager which allows you to iterate over a collection of
   155  // Policy. It accepts a ListOpts struct, which allows you to filter and sort
   156  // the returned collection for greater efficiency.
   157  func List(c *gophercloud.ServiceClient, opts PolicyListOptsBuilder) pagination.Pager {
   158  	url := listURL(c)
   159  	if opts != nil {
   160  		query, err := opts.ToPolicyListQuery()
   161  		if err != nil {
   162  			return pagination.Pager{Err: err}
   163  		}
   164  		url += query
   165  	}
   166  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
   167  		return PolicyPage{pagination.LinkedPageBase{PageResult: r}}
   168  
   169  	})
   170  }
   171  
   172  // Get retrieves a specific QoS policy based on its ID.
   173  func Get(c *gophercloud.ServiceClient, id string) (r GetResult) {
   174  	resp, err := c.Get(getURL(c, id), &r.Body, nil)
   175  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   176  	return
   177  }
   178  
   179  // CreateOptsBuilder allows to add additional parameters to the
   180  // Create request.
   181  type CreateOptsBuilder interface {
   182  	ToPolicyCreateMap() (map[string]interface{}, error)
   183  }
   184  
   185  // CreateOpts specifies parameters of a new QoS policy.
   186  type CreateOpts struct {
   187  	// Name is the human-readable name of the QoS policy.
   188  	Name string `json:"name"`
   189  
   190  	// TenantID is the id of the Identity project.
   191  	TenantID string `json:"tenant_id,omitempty"`
   192  
   193  	// ProjectID is the id of the Identity project.
   194  	ProjectID string `json:"project_id,omitempty"`
   195  
   196  	// Shared indicates whether this QoS policy is shared across all projects.
   197  	Shared bool `json:"shared,omitempty"`
   198  
   199  	// Description is the human-readable description for the QoS policy.
   200  	Description string `json:"description,omitempty"`
   201  
   202  	// IsDefault indicates if this QoS policy is default policy or not.
   203  	IsDefault bool `json:"is_default,omitempty"`
   204  }
   205  
   206  // ToPolicyCreateMap constructs a request body from CreateOpts.
   207  func (opts CreateOpts) ToPolicyCreateMap() (map[string]interface{}, error) {
   208  	return gophercloud.BuildRequestBody(opts, "policy")
   209  }
   210  
   211  // Create requests the creation of a new QoS policy on the server.
   212  func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
   213  	b, err := opts.ToPolicyCreateMap()
   214  	if err != nil {
   215  		r.Err = err
   216  		return
   217  	}
   218  	resp, err := client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{
   219  		OkCodes: []int{201},
   220  	})
   221  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   222  	return
   223  }
   224  
   225  // UpdateOptsBuilder allows extensions to add additional parameters to the
   226  // Update request.
   227  type UpdateOptsBuilder interface {
   228  	ToPolicyUpdateMap() (map[string]interface{}, error)
   229  }
   230  
   231  // UpdateOpts represents options used to update a QoS policy.
   232  type UpdateOpts struct {
   233  	// Name is the human-readable name of the QoS policy.
   234  	Name string `json:"name,omitempty"`
   235  
   236  	// Shared indicates whether this QoS policy is shared across all projects.
   237  	Shared *bool `json:"shared,omitempty"`
   238  
   239  	// Description is the human-readable description for the QoS policy.
   240  	Description *string `json:"description,omitempty"`
   241  
   242  	// IsDefault indicates if this QoS policy is default policy or not.
   243  	IsDefault *bool `json:"is_default,omitempty"`
   244  }
   245  
   246  // ToPolicyUpdateMap builds a request body from UpdateOpts.
   247  func (opts UpdateOpts) ToPolicyUpdateMap() (map[string]interface{}, error) {
   248  	return gophercloud.BuildRequestBody(opts, "policy")
   249  }
   250  
   251  // Update accepts a UpdateOpts struct and updates an existing policy using the
   252  // values provided.
   253  func Update(c *gophercloud.ServiceClient, policyID string, opts UpdateOptsBuilder) (r UpdateResult) {
   254  	b, err := opts.ToPolicyUpdateMap()
   255  	if err != nil {
   256  		r.Err = err
   257  		return
   258  	}
   259  	resp, err := c.Put(updateURL(c, policyID), b, &r.Body, &gophercloud.RequestOpts{
   260  		OkCodes: []int{200},
   261  	})
   262  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   263  	return
   264  }
   265  
   266  // Delete accepts a unique ID and deletes the QoS policy associated with it.
   267  func Delete(c *gophercloud.ServiceClient, id string) (r DeleteResult) {
   268  	resp, err := c.Delete(deleteURL(c, id), nil)
   269  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   270  	return
   271  }