github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/vpcep/v1/endpoints/requests.go (about)

     1  package endpoints
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  	"github.com/huaweicloud/golangsdk/openstack/common/tags"
     6  )
     7  
     8  // CreateOptsBuilder allows extensions to add parameters to the
     9  // Create request.
    10  type CreateOptsBuilder interface {
    11  	ToEndpointCreateMap() (map[string]interface{}, error)
    12  }
    13  
    14  // CreateOpts contains the options for create a VPC endpoint
    15  // This object is passed to Create().
    16  type CreateOpts struct {
    17  	// Specifies the ID of the VPC endpoint service
    18  	ServiceID string `json:"endpoint_service_id" required:"true"`
    19  	// Specifies the ID of the VPC where the VPC endpoint is to be created
    20  	VpcID string `json:"vpc_id" required:"true"`
    21  
    22  	// Specifies the network ID of the subnet created in the VPC specified by vpc_id
    23  	// The parameter is mandatory to create an interface VPC endpoint
    24  	SubnetID string `json:"subnet_id,omitempty"`
    25  	// Specifies the IP address for accessing the associated VPC endpoint service
    26  	PortIP string `json:"port_ip,omitempty"`
    27  	// Specifies whether to create a private domain name
    28  	EnableDNS *bool `json:"enable_dns,omitempty"`
    29  	// Specifies whether to enable access control
    30  	EnableWhitelist *bool `json:"enable_whitelist,omitempty"`
    31  	// Specifies the whitelist for controlling access to the VPC endpoint
    32  	Whitelist []string `json:"whitelist,omitempty"`
    33  	// Specifies the IDs of route tables
    34  	RouteTables []string `json:"routeTables,omitempty"`
    35  	// Specifies the resource tags in key/value format
    36  	Tags []tags.ResourceTag `json:"tags,omitempty"`
    37  }
    38  
    39  // ToEndpointCreateMap assembles a request body based on the contents of a CreateOpts.
    40  func (opts CreateOpts) ToEndpointCreateMap() (map[string]interface{}, error) {
    41  	return golangsdk.BuildRequestBody(opts, "")
    42  }
    43  
    44  // Create accepts a CreateOpts struct and uses the values to create a new
    45  // VPC endpoint
    46  func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    47  	b, err := opts.ToEndpointCreateMap()
    48  	if err != nil {
    49  		r.Err = err
    50  		return
    51  	}
    52  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}}
    53  	_, r.Err = c.Post(rootURL(c), b, &r.Body, reqOpt)
    54  	return
    55  }
    56  
    57  // Get retrieves a particular VPC endpoint based on its unique ID
    58  func Get(c *golangsdk.ServiceClient, endpointID string) (r GetResult) {
    59  	_, r.Err = c.Get(resourceURL(c, endpointID), &r.Body, nil)
    60  	return
    61  }
    62  
    63  // Delete will permanently delete a particular VPC endpoint based on its unique ID
    64  func Delete(c *golangsdk.ServiceClient, endpointID string) (r DeleteResult) {
    65  	_, r.Err = c.Delete(resourceURL(c, endpointID), nil)
    66  	return
    67  }
    68  
    69  // ListOptsBuilder allows extensions to add parameters to the
    70  // List request.
    71  type ListOptsBuilder interface {
    72  	ToEndpointListQuery() (string, error)
    73  }
    74  
    75  // ListOpts allows the filtering of list data using given parameters.
    76  type ListOpts struct {
    77  	ServiceName string `q:"endpoint_service_name"`
    78  	VPCID       string `q:"vpc_id"`
    79  	ID          string `q:"id"`
    80  }
    81  
    82  // ToEndpointListQuery formats a ListOpts into a query string.
    83  func (opts ListOpts) ToEndpointListQuery() (string, error) {
    84  	q, err := golangsdk.BuildQueryString(opts)
    85  	return q.String(), err
    86  }
    87  
    88  // List makes a request against the API to list VPC endpoints.
    89  func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) ([]Endpoint, error) {
    90  	var r ListResult
    91  	url := rootURL(client)
    92  	if opts != nil {
    93  		query, err := opts.ToEndpointListQuery()
    94  		if err != nil {
    95  			return nil, err
    96  		}
    97  		url += query
    98  	}
    99  	_, r.Err = client.Get(url, &r.Body, nil)
   100  	if r.Err != nil {
   101  		return nil, r.Err
   102  	}
   103  
   104  	allEndpoints, err := r.ExtractEndpoints()
   105  	if err != nil {
   106  		return nil, err
   107  	}
   108  
   109  	return allEndpoints, nil
   110  }