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

     1  package vpcs
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/vnpaycloud-console/gophercloud/v2"
     7  	"github.com/vnpaycloud-console/gophercloud/v2/pagination"
     8  )
     9  
    10  type ListOptsBuilder interface {
    11  	ToVPCListQuery() (string, error)
    12  }
    13  
    14  // ListOpts allows the filtering of VPCs based on their properties.
    15  type ListOpts struct {
    16  	Name      string `q:"name"`
    17  	CIDR      string `q:"cidr"`
    18  	ID        string `q:"id"`
    19  	ProjectID string `q:"project_id"`
    20  	Limit     int    `q:"limit"`
    21  	Status    string `q:"status"`
    22  }
    23  
    24  // ToVPCListQuery formats a ListOpts into a query string.
    25  func (opts ListOpts) ToVPCListQuery() (string, error) {
    26  	q, err := gophercloud.BuildQueryString(opts)
    27  	return q.String(), err
    28  }
    29  
    30  // List returns a Pager which allows you to iterate over a collection of VPCs.
    31  // It accepts a ListOpts struct, which allows you to filter and sort the
    32  // returned collection for greater efficiency.
    33  func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    34  	url := listURL(c)
    35  	if opts != nil {
    36  		query, err := opts.ToVPCListQuery()
    37  		if err != nil {
    38  			return pagination.Pager{Err: err}
    39  		}
    40  		url += query
    41  	}
    42  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
    43  		return VPCPage{pagination.LinkedPageBase{PageResult: r}}
    44  	})
    45  }
    46  
    47  // Get retrieves a specific VPC based on its unique ID.
    48  func Get(ctx context.Context, c *gophercloud.ServiceClient, id string) (r GetResult) {
    49  	resp, err := c.Get(ctx, getURL(c, id), &r.Body, nil)
    50  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    51  	return
    52  }
    53  
    54  // CreateOptsBuilder allows extensions to add additional parameters to the
    55  // Create request.
    56  type CreateOptsBuilder interface {
    57  	ToVPCCreateMap() (map[string]any, error)
    58  }
    59  
    60  // CreateOpts represents options used to create a VPC.
    61  type CreateOpts struct {
    62  	Name        string `json:"name,omitempty"`
    63  	Description string `json:"description,omitempty"`
    64  	CIDR        string `json:"cidr,omitempty"`
    65  }
    66  
    67  // ToVPCCreateMap formats a CreateOpts struct into a request body.
    68  func (opts CreateOpts) ToVPCCreateMap() (map[string]any, error) {
    69  	return gophercloud.BuildRequestBody(opts, "vpc")
    70  }
    71  
    72  // Create accepts a CreateOpts struct and creates a new VPC using the values
    73  // provided. If the call is successful, a VPC will be returned in the
    74  // CreateResult struct.
    75  func Create(ctx context.Context, c *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    76  	b, err := opts.ToVPCCreateMap()
    77  	if err != nil {
    78  		r.Err = err
    79  		return
    80  	}
    81  
    82  	resp, err := c.Post(ctx, createURL(c), b, &r.Body, nil)
    83  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    84  	return
    85  }
    86  
    87  // UpdateOptsBuilder allows extensions to add additional parameters to the
    88  // Update request.
    89  type UpdateOptsBuilder interface {
    90  	ToVPCUpdateMap() (map[string]any, error)
    91  }
    92  
    93  // UpdateOpts represents options used to update a VPC.
    94  type UpdateOpts struct {
    95  	Name        string `json:"name,omitempty"`
    96  	Description string `json:"description,omitempty"`
    97  	EnableSNAT  *bool  `json:"enable_snat,omitempty"`
    98  }
    99  
   100  // ToVPCUpdateMap formats a UpdateOpts struct into a request body.
   101  func (opts UpdateOpts) ToVPCUpdateMap() (map[string]any, error) {
   102  	return gophercloud.BuildRequestBody(opts, "vpc")
   103  }
   104  
   105  // Update accepts a UpdateOpts struct and updates an existing VPC using the
   106  // values provided. If the call is successful, a VPC will be returned in the
   107  // UpdateResult struct.
   108  func Update(ctx context.Context, c *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   109  	b, err := opts.ToVPCUpdateMap()
   110  	if err != nil {
   111  		r.Err = err
   112  		return
   113  	}
   114  
   115  	h, err := gophercloud.BuildHeaders(opts)
   116  	if err != nil {
   117  		r.Err = err
   118  		return
   119  	}
   120  
   121  	resp, err := c.Put(ctx, updateURL(c, id), b, &r.Body, &gophercloud.RequestOpts{
   122  		MoreHeaders: h,
   123  		OkCodes:     []int{200, 201},
   124  	})
   125  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   126  	return
   127  }
   128  
   129  // Delete removes a VPC based on its unique ID. A successful response indicates
   130  // that the VPC has been deleted.
   131  func Delete(ctx context.Context, c *gophercloud.ServiceClient, id string) (r DeleteResult) {
   132  	resp, err := c.Delete(ctx, deleteURL(c, id), nil)
   133  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   134  	return
   135  }