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

     1  package services
     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  	ToServiceCreateMap() (map[string]interface{}, error)
    12  }
    13  
    14  // CreateOpts contains all the values needed to create a new VPN service
    15  type CreateOpts struct {
    16  	// TenantID specifies a tenant to own the VPN service. The caller must have
    17  	// an admin role in order to set this. Otherwise, this field is left unset
    18  	// and the caller will be the owner.
    19  	TenantID string `json:"tenant_id,omitempty"`
    20  
    21  	// SubnetID is the ID of the subnet.
    22  	SubnetID string `json:"subnet_id,omitempty"`
    23  
    24  	// RouterID is the ID of the router.
    25  	RouterID string `json:"router_id" required:"true"`
    26  
    27  	// Description is the human readable description of the service.
    28  	Description string `json:"description,omitempty"`
    29  
    30  	// AdminStateUp is the administrative state of the resource, which is up (true) or down (false).
    31  	AdminStateUp *bool `json:"admin_state_up"`
    32  
    33  	// Name is the human readable name of the service.
    34  	Name string `json:"name,omitempty"`
    35  
    36  	// The ID of the flavor.
    37  	FlavorID string `json:"flavor_id,omitempty"`
    38  }
    39  
    40  // ToServiceCreateMap casts a CreateOpts struct to a map.
    41  func (opts CreateOpts) ToServiceCreateMap() (map[string]interface{}, error) {
    42  	return gophercloud.BuildRequestBody(opts, "vpnservice")
    43  }
    44  
    45  // Create accepts a CreateOpts struct and uses the values to create a new
    46  // VPN service.
    47  func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    48  	b, err := opts.ToServiceCreateMap()
    49  	if err != nil {
    50  		r.Err = err
    51  		return
    52  	}
    53  	resp, err := c.Post(rootURL(c), b, &r.Body, nil)
    54  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    55  	return
    56  }
    57  
    58  // Delete will permanently delete a particular VPN service based on its
    59  // unique ID.
    60  func Delete(c *gophercloud.ServiceClient, id string) (r DeleteResult) {
    61  	resp, err := c.Delete(resourceURL(c, id), nil)
    62  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    63  	return
    64  }
    65  
    66  // UpdateOptsBuilder allows extensions to add additional parameters to the
    67  // Update request.
    68  type UpdateOptsBuilder interface {
    69  	ToServiceUpdateMap() (map[string]interface{}, error)
    70  }
    71  
    72  // UpdateOpts contains the values used when updating a VPN service
    73  type UpdateOpts struct {
    74  	// Name is the human readable name of the service.
    75  	Name *string `json:"name,omitempty"`
    76  
    77  	// Description is the human readable description of the service.
    78  	Description *string `json:"description,omitempty"`
    79  
    80  	// AdminStateUp is the administrative state of the resource, which is up (true) or down (false).
    81  	AdminStateUp *bool `json:"admin_state_up,omitempty"`
    82  }
    83  
    84  // ToServiceUpdateMap casts aa UodateOpts struct to a map.
    85  func (opts UpdateOpts) ToServiceUpdateMap() (map[string]interface{}, error) {
    86  	return gophercloud.BuildRequestBody(opts, "vpnservice")
    87  }
    88  
    89  // Update allows VPN services to be updated.
    90  func Update(c *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
    91  	b, err := opts.ToServiceUpdateMap()
    92  	if err != nil {
    93  		r.Err = err
    94  		return
    95  	}
    96  	resp, err := c.Put(resourceURL(c, id), b, &r.Body, &gophercloud.RequestOpts{
    97  		OkCodes: []int{200},
    98  	})
    99  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   100  	return
   101  }
   102  
   103  // ListOptsBuilder allows extensions to add additional parameters to the
   104  // List request.
   105  type ListOptsBuilder interface {
   106  	ToServiceListQuery() (string, error)
   107  }
   108  
   109  // ListOpts allows the filtering and sorting of paginated collections through
   110  // the API. Filtering is achieved by passing in struct field values that map to
   111  // the VPN service attributes you want to see returned.
   112  type ListOpts struct {
   113  	TenantID     string `q:"tenant_id"`
   114  	Name         string `q:"name"`
   115  	Description  string `q:"description"`
   116  	AdminStateUp *bool  `q:"admin_state_up"`
   117  	Status       string `q:"status"`
   118  	SubnetID     string `q:"subnet_id"`
   119  	RouterID     string `q:"router_id"`
   120  	ProjectID    string `q:"project_id"`
   121  	ExternalV6IP string `q:"external_v6_ip"`
   122  	ExternalV4IP string `q:"external_v4_ip"`
   123  	FlavorID     string `q:"flavor_id"`
   124  }
   125  
   126  // ToServiceListQuery formats a ListOpts into a query string.
   127  func (opts ListOpts) ToServiceListQuery() (string, error) {
   128  	q, err := gophercloud.BuildQueryString(opts)
   129  	return q.String(), err
   130  }
   131  
   132  // List returns a Pager which allows you to iterate over a collection of
   133  // VPN services. It accepts a ListOpts struct, which allows you to filter
   134  // and sort the returned collection for greater efficiency.
   135  func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
   136  	url := rootURL(c)
   137  	if opts != nil {
   138  		query, err := opts.ToServiceListQuery()
   139  		if err != nil {
   140  			return pagination.Pager{Err: err}
   141  		}
   142  		url += query
   143  	}
   144  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
   145  		return ServicePage{pagination.LinkedPageBase{PageResult: r}}
   146  	})
   147  }
   148  
   149  // Get retrieves a particular VPN service based on its unique ID.
   150  func Get(c *gophercloud.ServiceClient, id string) (r GetResult) {
   151  	resp, err := c.Get(resourceURL(c, id), &r.Body, nil)
   152  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   153  	return
   154  }