github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v2/extensions/natgateways/requests.go (about)

     1  package natgateways
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  	"github.com/huaweicloud/golangsdk/pagination"
     6  )
     7  
     8  // CreateOptsBuilder is an interface must satisfy to be used as Create
     9  // options.
    10  type CreateOptsBuilder interface {
    11  	ToNatGatewayCreateMap() (map[string]interface{}, error)
    12  }
    13  
    14  // CreateOpts contains all the values needed to create a new nat gateway
    15  // resource.
    16  type CreateOpts struct {
    17  	Name                string `json:"name" required:"true"`
    18  	Description         string `json:"description,omitempty"`
    19  	Spec                string `json:"spec" required:"true"`
    20  	RouterID            string `json:"router_id" required:"true"`
    21  	InternalNetworkID   string `json:"internal_network_id" required:"true"`
    22  	TenantID            string `json:"tenant_id,omitempty"`
    23  	EnterpriseProjectID string `json:"enterprise_project_id,omitempty"`
    24  }
    25  
    26  type ListOpts struct {
    27  	Limit               int    `q:"limit"`
    28  	ID                  string `q:"id"`
    29  	Name                string `q:"name"`
    30  	TenantId            string `q:"tenant_id"`
    31  	Description         string `q:"description"`
    32  	Spec                string `q:"spec"`
    33  	RouterID            string `q:"router_id"`
    34  	InternalNetworkID   string `q:"internal_network_id"`
    35  	Status              string `q:"status"`
    36  	AdminStateUp        *bool  `q:"admin_state_up"`
    37  	CreatedAt           string `q:"created_at"`
    38  	EnterpriseProjectID string `q:"enterprise_project_id"`
    39  }
    40  
    41  // ToNatGatewayCreateMap allows CreateOpts to satisfy the CreateOptsBuilder
    42  // interface
    43  func (opts CreateOpts) ToNatGatewayCreateMap() (map[string]interface{}, error) {
    44  	return golangsdk.BuildRequestBody(opts, "nat_gateway")
    45  }
    46  
    47  // Create is a method by which can create a new nat gateway
    48  func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    49  	b, err := opts.ToNatGatewayCreateMap()
    50  	if err != nil {
    51  		r.Err = err
    52  		return
    53  	}
    54  	_, r.Err = c.Post(rootURL(c), b, &r.Body, &golangsdk.RequestOpts{
    55  		OkCodes: []int{201},
    56  	})
    57  	return
    58  }
    59  
    60  // Get is a method by which can get the detailed information of the specified
    61  // nat gateway.
    62  func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
    63  	_, r.Err = c.Get(resourceURL(c, id), &r.Body, nil)
    64  	return
    65  }
    66  
    67  // Delete is a method by which can be able to delete a nat gateway
    68  func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) {
    69  	_, r.Err = c.Delete(resourceURL(c, id), nil)
    70  	return
    71  }
    72  
    73  // UpdateOptsBuilder is the interface type must satisfy to be used as Update
    74  // options.
    75  type UpdateOptsBuilder interface {
    76  	ToNatGatewayUpdateMap() (map[string]interface{}, error)
    77  }
    78  
    79  //UpdateOpts is a struct which represents the request body of update method
    80  type UpdateOpts struct {
    81  	Name        string `json:"name,omitempty"`
    82  	Description string `json:"description,omitempty"`
    83  	Spec        string `json:"spec,omitempty"`
    84  }
    85  
    86  func (opts UpdateOpts) ToNatGatewayUpdateMap() (map[string]interface{}, error) {
    87  	return golangsdk.BuildRequestBody(opts, "nat_gateway")
    88  }
    89  
    90  func (opts ListOpts) ToNatGatewayListQuery() (string, error) {
    91  	q, err := golangsdk.BuildQueryString(opts)
    92  	return q.String(), err
    93  }
    94  
    95  type ListOptsBuilder interface {
    96  	ToNatGatewayListQuery() (string, error)
    97  }
    98  
    99  //Update allows nat gateway resources to be updated.
   100  func Update(c *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   101  	b, err := opts.ToNatGatewayUpdateMap()
   102  	if err != nil {
   103  		r.Err = err
   104  		return
   105  	}
   106  	_, r.Err = c.Put(resourceURL(c, id), b, &r.Body, &golangsdk.RequestOpts{
   107  		OkCodes: []int{200},
   108  	})
   109  	return
   110  }
   111  
   112  func List(c *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
   113  	url := rootURL(c)
   114  	if opts != nil {
   115  		query, err := opts.ToNatGatewayListQuery()
   116  		if err != nil {
   117  			return pagination.Pager{Err: err}
   118  		}
   119  		url += query
   120  	}
   121  
   122  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
   123  		return NatGatewayPage{pagination.LinkedPageBase{PageResult: r}}
   124  	})
   125  }