github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/networking/v2/extensions/qos/policies/requests.go (about)

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