github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/networking/v2/extensions/hw_snatrules/requests.go (about)

     1  package hw_snatrules
     2  
     3  import (
     4  	"github.com/chnsz/golangsdk"
     5  )
     6  
     7  // CreateOptsBuilder is an interface must satisfy to be used as Create
     8  // options.
     9  type CreateOptsBuilder interface {
    10  	ToSnatRuleCreateMap() (map[string]interface{}, error)
    11  }
    12  
    13  // CreateOpts contains all the values needed to create a new snat rule
    14  // resource.
    15  type CreateOpts struct {
    16  	NatGatewayID string `json:"nat_gateway_id" required:"true"`
    17  	FloatingIPID string `json:"floating_ip_id" required:"true"`
    18  	Description  string `json:"description,omitempty"`
    19  	NetworkID    string `json:"network_id,omitempty"`
    20  	Cidr         string `json:"cidr,omitempty"`
    21  	SourceType   int    `json:"source_type,omitempty"`
    22  }
    23  
    24  // ToSnatRuleCreateMap allows CreateOpts to satisfy the CreateOptsBuilder
    25  // interface
    26  func (opts CreateOpts) ToSnatRuleCreateMap() (map[string]interface{}, error) {
    27  	return golangsdk.BuildRequestBody(opts, "snat_rule")
    28  }
    29  
    30  // Create is a method by which can create a new snat rule
    31  func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    32  	b, err := opts.ToSnatRuleCreateMap()
    33  	if err != nil {
    34  		r.Err = err
    35  		return
    36  	}
    37  	_, r.Err = c.Post(rootURL(c), b, &r.Body, nil)
    38  	return
    39  }
    40  
    41  // UpdateOptsBuilder is an interface must satisfy to be used as Update
    42  // options.
    43  type UpdateOptsBuilder interface {
    44  	ToSnatRuleUpdateMap() (map[string]interface{}, error)
    45  }
    46  
    47  // UpdateOpts contains all the values needed to update a snat rule
    48  // resource.
    49  type UpdateOpts struct {
    50  	NatGatewayID      string  `json:"nat_gateway_id" required:"true"`
    51  	FloatingIPAddress string  `json:"public_ip_address,omitempty"`
    52  	Description       *string `json:"description,omitempty"`
    53  }
    54  
    55  // ToSnatRuleUpdateMap allows UpdateOpts to satisfy the UpdateOptsBuilder
    56  // interface
    57  func (opts UpdateOpts) ToSnatRuleUpdateMap() (map[string]interface{}, error) {
    58  	return golangsdk.BuildRequestBody(opts, "snat_rule")
    59  }
    60  
    61  // Update is a method by which can update a snat rule
    62  func Update(c *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
    63  	b, err := opts.ToSnatRuleUpdateMap()
    64  	if err != nil {
    65  		r.Err = err
    66  		return
    67  	}
    68  	_, r.Err = c.Put(resourceURL(c, id), b, &r.Body, nil)
    69  	return
    70  }
    71  
    72  // Get is a method by which can get the detailed information of the specified
    73  // snat rule.
    74  func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
    75  	_, r.Err = c.Get(resourceURL(c, id), &r.Body, nil)
    76  	return
    77  }
    78  
    79  // Delete is a method by which can be able to delete a snat rule
    80  func Delete(c *golangsdk.ServiceClient, id, natGatewayID string) (r DeleteResult) {
    81  	_, r.Err = c.Delete(resourceURLDelete(c, id, natGatewayID), nil)
    82  	return
    83  }