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

     1  package trunks
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/vnpaycloud-console/gophercloud/v2"
     7  	"github.com/vnpaycloud-console/gophercloud/v2/pagination"
     8  )
     9  
    10  // CreateOptsBuilder allows extensions to add additional parameters to the
    11  // Create request.
    12  type CreateOptsBuilder interface {
    13  	ToTrunkCreateMap() (map[string]any, error)
    14  }
    15  
    16  // CreateOpts represents the attributes used when creating a new trunk.
    17  type CreateOpts struct {
    18  	TenantID     string    `json:"tenant_id,omitempty"`
    19  	ProjectID    string    `json:"project_id,omitempty"`
    20  	PortID       string    `json:"port_id" required:"true"`
    21  	Name         string    `json:"name,omitempty"`
    22  	Description  string    `json:"description,omitempty"`
    23  	AdminStateUp *bool     `json:"admin_state_up,omitempty"`
    24  	Subports     []Subport `json:"sub_ports"`
    25  }
    26  
    27  // ToTrunkCreateMap builds a request body from CreateOpts.
    28  func (opts CreateOpts) ToTrunkCreateMap() (map[string]any, error) {
    29  	if opts.Subports == nil {
    30  		opts.Subports = []Subport{}
    31  	}
    32  	return gophercloud.BuildRequestBody(opts, "trunk")
    33  }
    34  
    35  func Create(ctx context.Context, c *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    36  	body, err := opts.ToTrunkCreateMap()
    37  	if err != nil {
    38  		r.Err = err
    39  		return
    40  	}
    41  
    42  	resp, err := c.Post(ctx, createURL(c), body, &r.Body, nil)
    43  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    44  	return
    45  }
    46  
    47  // Delete accepts a unique ID and deletes the trunk associated with it.
    48  func Delete(ctx context.Context, c *gophercloud.ServiceClient, id string) (r DeleteResult) {
    49  	resp, err := c.Delete(ctx, deleteURL(c, id), nil)
    50  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    51  	return
    52  }
    53  
    54  // ListOptsBuilder allows extensions to add additional parameters to the
    55  // List request.
    56  type ListOptsBuilder interface {
    57  	ToTrunkListQuery() (string, error)
    58  }
    59  
    60  // ListOpts allows the filtering and sorting of paginated collections through
    61  // the API. Filtering is achieved by passing in struct field values that map to
    62  // the trunk attributes you want to see returned. SortKey allows you to sort
    63  // by a particular trunk attribute. SortDir sets the direction, and is either
    64  // `asc' or `desc'. Marker and Limit are used for pagination.
    65  type ListOpts struct {
    66  	AdminStateUp   *bool  `q:"admin_state_up"`
    67  	Description    string `q:"description"`
    68  	ID             string `q:"id"`
    69  	Name           string `q:"name"`
    70  	PortID         string `q:"port_id"`
    71  	RevisionNumber string `q:"revision_number"`
    72  	Status         string `q:"status"`
    73  	TenantID       string `q:"tenant_id"`
    74  	ProjectID      string `q:"project_id"`
    75  	SortDir        string `q:"sort_dir"`
    76  	SortKey        string `q:"sort_key"`
    77  	Tags           string `q:"tags"`
    78  	TagsAny        string `q:"tags-any"`
    79  	NotTags        string `q:"not-tags"`
    80  	NotTagsAny     string `q:"not-tags-any"`
    81  }
    82  
    83  // ToTrunkListQuery formats a ListOpts into a query string.
    84  func (opts ListOpts) ToTrunkListQuery() (string, error) {
    85  	q, err := gophercloud.BuildQueryString(opts)
    86  	return q.String(), err
    87  }
    88  
    89  // List returns a Pager which allows you to iterate over a collection of
    90  // trunks. It accepts a ListOpts struct, which allows you to filter and sort
    91  // the returned collection for greater efficiency.
    92  //
    93  // Default policy settings return only those trunks that are owned by the tenant
    94  // who submits the request, unless the request is submitted by a user with
    95  // administrative rights.
    96  func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    97  	url := listURL(c)
    98  	if opts != nil {
    99  		query, err := opts.ToTrunkListQuery()
   100  		if err != nil {
   101  			return pagination.Pager{Err: err}
   102  		}
   103  		url += query
   104  	}
   105  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
   106  		return TrunkPage{pagination.LinkedPageBase{PageResult: r}}
   107  	})
   108  }
   109  
   110  // Get retrieves a specific trunk based on its unique ID.
   111  func Get(ctx context.Context, c *gophercloud.ServiceClient, id string) (r GetResult) {
   112  	resp, err := c.Get(ctx, getURL(c, id), &r.Body, nil)
   113  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   114  	return
   115  }
   116  
   117  type UpdateOptsBuilder interface {
   118  	ToTrunkUpdateMap() (map[string]any, error)
   119  }
   120  
   121  type UpdateOpts struct {
   122  	AdminStateUp *bool   `json:"admin_state_up,omitempty"`
   123  	Name         *string `json:"name,omitempty"`
   124  	Description  *string `json:"description,omitempty"`
   125  }
   126  
   127  func (opts UpdateOpts) ToTrunkUpdateMap() (map[string]any, error) {
   128  	return gophercloud.BuildRequestBody(opts, "trunk")
   129  }
   130  
   131  func Update(ctx context.Context, c *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   132  	body, err := opts.ToTrunkUpdateMap()
   133  	if err != nil {
   134  		r.Err = err
   135  		return
   136  	}
   137  	resp, err := c.Put(ctx, updateURL(c, id), body, &r.Body, &gophercloud.RequestOpts{
   138  		OkCodes: []int{200},
   139  	})
   140  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   141  	return
   142  }
   143  
   144  func GetSubports(ctx context.Context, c *gophercloud.ServiceClient, id string) (r GetSubportsResult) {
   145  	resp, err := c.Get(ctx, getSubportsURL(c, id), &r.Body, &gophercloud.RequestOpts{
   146  		OkCodes: []int{200},
   147  	})
   148  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   149  	return
   150  }
   151  
   152  type AddSubportsOpts struct {
   153  	Subports []Subport `json:"sub_ports" required:"true"`
   154  }
   155  
   156  type AddSubportsOptsBuilder interface {
   157  	ToTrunkAddSubportsMap() (map[string]any, error)
   158  }
   159  
   160  func (opts AddSubportsOpts) ToTrunkAddSubportsMap() (map[string]any, error) {
   161  	return gophercloud.BuildRequestBody(opts, "")
   162  }
   163  
   164  func AddSubports(ctx context.Context, c *gophercloud.ServiceClient, id string, opts AddSubportsOptsBuilder) (r UpdateSubportsResult) {
   165  	body, err := opts.ToTrunkAddSubportsMap()
   166  	if err != nil {
   167  		r.Err = err
   168  		return
   169  	}
   170  	resp, err := c.Put(ctx, addSubportsURL(c, id), body, &r.Body, &gophercloud.RequestOpts{
   171  		OkCodes: []int{200},
   172  	})
   173  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   174  	return
   175  }
   176  
   177  type RemoveSubport struct {
   178  	PortID string `json:"port_id" required:"true"`
   179  }
   180  
   181  type RemoveSubportsOpts struct {
   182  	Subports []RemoveSubport `json:"sub_ports"`
   183  }
   184  
   185  type RemoveSubportsOptsBuilder interface {
   186  	ToTrunkRemoveSubportsMap() (map[string]any, error)
   187  }
   188  
   189  func (opts RemoveSubportsOpts) ToTrunkRemoveSubportsMap() (map[string]any, error) {
   190  	return gophercloud.BuildRequestBody(opts, "")
   191  }
   192  
   193  func RemoveSubports(ctx context.Context, c *gophercloud.ServiceClient, id string, opts RemoveSubportsOptsBuilder) (r UpdateSubportsResult) {
   194  	body, err := opts.ToTrunkRemoveSubportsMap()
   195  	if err != nil {
   196  		r.Err = err
   197  		return
   198  	}
   199  	resp, err := c.Put(ctx, removeSubportsURL(c, id), body, &r.Body, &gophercloud.RequestOpts{
   200  		OkCodes: []int{200},
   201  	})
   202  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   203  	return
   204  }