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

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