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

     1  package snats
     2  
     3  import (
     4  	"github.com/chnsz/golangsdk"
     5  )
     6  
     7  // CreateOpts is the structure that used to create a new SNAT rule.
     8  type CreateOpts struct {
     9  	// The ID of the gateway to which the SNAT rule belongs.
    10  	GatewayId string `json:"nat_gateway_id" required:"true"`
    11  	//  IDs of floating IPs connected by SNAT rules (separated by commas).
    12  	FloatingIpId string `json:"floating_ip_id,omitempty"`
    13  	// The IDs (separated by commas) of global EIPs connected by SNAT rule.
    14  	GlobalEipId string `json:"global_eip_id,omitempty"`
    15  	// The description of the SNAT rule.
    16  	Description string `json:"description,omitempty"`
    17  	// The network IDs of subnet connected by SNAT rule (VPC side).
    18  	NetworkId string `json:"network_id,omitempty"`
    19  	// The CIDR block connected by SNAT rule (DC side).
    20  	Cidr string `json:"cidr,omitempty"`
    21  	// The resource type of the SNAT rule.
    22  	// The valid values are as follows:
    23  	// + 0: VPC side.
    24  	// + 1: DC side.
    25  	SourceType int `json:"source_type,omitempty"`
    26  }
    27  
    28  var requestOpts = golangsdk.RequestOpts{
    29  	MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
    30  }
    31  
    32  // Create is a method used to create an SNAT rule using given parameters.
    33  func Create(c *golangsdk.ServiceClient, opts CreateOpts) (*Rule, error) {
    34  	b, err := golangsdk.BuildRequestBody(opts, "snat_rule")
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  
    39  	var r createResp
    40  	_, err = c.Post(rootURL(c), b, &r, &golangsdk.RequestOpts{
    41  		MoreHeaders: requestOpts.MoreHeaders,
    42  	})
    43  	return &r.Rule, err
    44  }
    45  
    46  // Get is a method used to obtain the detail of the SNAT rule by its ID.
    47  func Get(c *golangsdk.ServiceClient, ruleId string) (*Rule, error) {
    48  	var r queryResp
    49  	_, err := c.Get(resourceURL(c, ruleId), &r, &golangsdk.RequestOpts{
    50  		MoreHeaders: requestOpts.MoreHeaders,
    51  	})
    52  	return &r.Rule, err
    53  }
    54  
    55  // CreateOpts is the structure that used to update the configuration of the SNAT rule.
    56  type UpdateOpts struct {
    57  	// The ID of the gateway to which the SNAT rule belongs.
    58  	GatewayId string `json:"nat_gateway_id" required:"true"`
    59  	// The floating IP addresses (separated by commas) connected by SNAT rule.
    60  	FloatingIpAddress string `json:"public_ip_address,omitempty"`
    61  	// The IDs (separated by commas) of global EIPs connected by SNAT rule.
    62  	GlobalEipId string `json:"global_eip_id,omitempty"`
    63  	// The description of the SNAT rule.
    64  	Description *string `json:"description,omitempty"`
    65  }
    66  
    67  // Update is a method used to modify an existing SNAT rule using given parameters.
    68  func Update(c *golangsdk.ServiceClient, ruleId string, opts UpdateOpts) (*Rule, error) {
    69  	b, err := golangsdk.BuildRequestBody(opts, "snat_rule")
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  
    74  	var r updateResp
    75  	_, err = c.Put(resourceURL(c, ruleId), b, &r, &golangsdk.RequestOpts{
    76  		MoreHeaders: requestOpts.MoreHeaders,
    77  	})
    78  	return &r.Rule, err
    79  }
    80  
    81  // Delete is a method used to delete an existing SNAT rule.
    82  func Delete(c *golangsdk.ServiceClient, gatewayId, ruleId string) error {
    83  	_, err := c.Delete(deleteURL(c, gatewayId, ruleId), &golangsdk.RequestOpts{
    84  		MoreHeaders: requestOpts.MoreHeaders,
    85  	})
    86  	return err
    87  }