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

     1  package dnats
     2  
     3  import "github.com/chnsz/golangsdk"
     4  
     5  // CreateOpts is the structure used to create a new private DNAT rule.
     6  type CreateOpts struct {
     7  	// The ID of the gateway to which the private DNAT rule belongs.
     8  	GatewayId string `json:"gateway_id" required:"true"`
     9  	// The ID of the transit IP for private NAT.
    10  	TransitIpId string `json:"transit_ip_id" required:"true"`
    11  	// The description of the DNAT rule, which contain maximum of `255` characters, and angle brackets (< and >) are
    12  	// not allowed.
    13  	Description string `json:"description,omitempty"`
    14  	// The network interface ID of the transit IP for private NAT.
    15  	NetworkInterfaceId string `json:"network_interface_id,omitempty"`
    16  	// The protocol type of the private DNAT rule.
    17  	// The valid values (and the related protocol numbers) are 'UDP/udp (6)', 'TCP/tcp' (17) and 'ANY/any (0)'.
    18  	Protocol string `json:"protocol,omitempty"`
    19  	// The private IP address of the backend instance.
    20  	PrivateIpAddress string `json:"private_ip_address,omitempty"`
    21  	// The port of the backend instance.
    22  	InternalServicePort string `json:"internal_service_port,omitempty"`
    23  	// The port of the transit IP.
    24  	TransitServicePort string `json:"transit_service_port,omitempty"`
    25  }
    26  
    27  var requestOpts = golangsdk.RequestOpts{
    28  	MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
    29  }
    30  
    31  // Create is a method used to create a private DNAT rule using given parameters.
    32  func Create(c *golangsdk.ServiceClient, opts CreateOpts) (*Rule, error) {
    33  	b, err := golangsdk.BuildRequestBody(opts, "dnat_rule")
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  
    38  	var r createResp
    39  	_, err = c.Post(rootURL(c), b, &r, &golangsdk.RequestOpts{
    40  		MoreHeaders: requestOpts.MoreHeaders,
    41  	})
    42  	return &r.Rule, err
    43  }
    44  
    45  // Get is a method used to obtain the private DNAT rule detail by its ID.
    46  func Get(c *golangsdk.ServiceClient, ruleId string) (*Rule, error) {
    47  	var r queryResp
    48  	_, err := c.Get(resourceURL(c, ruleId), &r, &golangsdk.RequestOpts{
    49  		MoreHeaders: requestOpts.MoreHeaders,
    50  	})
    51  	return &r.Rule, err
    52  }
    53  
    54  // UpdateOpts is the structure used to modify an existing private DNAT rule.
    55  type UpdateOpts struct {
    56  	// The ID of the transit IP for private NAT.
    57  	TransitIpId string `json:"transit_ip_id,omitempty"`
    58  	// The description of the DNAT rule, which contain maximum of `255` characters, and angle brackets (< and >) are
    59  	// not allowed.
    60  	Description *string `json:"description,omitempty"`
    61  	// The network interface ID of the transit IP for private NAT.
    62  	NetworkInterfaceId string `json:"network_interface_id,omitempty"`
    63  	// The protocol type. The valid values (and the related protocol numbers) are 'UDP/udp (6)', 'TCP/tcp' (17) and
    64  	// 'ANY/any (0)'.
    65  	Protocol string `json:"protocol,omitempty"`
    66  	// The private IP address of the backend instance.
    67  	PrivateIpAddress string `json:"private_ip_address,omitempty"`
    68  	// The port of the backend instance.
    69  	InternalServicePort string `json:"internal_service_port,omitempty"`
    70  	// The port of the transit IP.
    71  	TransitServicePort string `json:"transit_service_port,omitempty"`
    72  }
    73  
    74  // Update is a method used to modify an existing DNAT rule using given parameters.
    75  func Update(c *golangsdk.ServiceClient, ruleId string, opts UpdateOpts) (*Rule, error) {
    76  	b, err := golangsdk.BuildRequestBody(opts, "dnat_rule")
    77  	if err != nil {
    78  		return nil, err
    79  	}
    80  
    81  	var r updateResp
    82  	_, err = c.Put(resourceURL(c, ruleId), b, &r, &golangsdk.RequestOpts{
    83  		MoreHeaders: requestOpts.MoreHeaders,
    84  	})
    85  	return &r.Rule, err
    86  }
    87  
    88  // Delete is a method to remove the specified DNAT rule using its ID.
    89  func Delete(c *golangsdk.ServiceClient, ruleId string) error {
    90  	_, err := c.Delete(resourceURL(c, ruleId), &golangsdk.RequestOpts{
    91  		MoreHeaders: requestOpts.MoreHeaders,
    92  	})
    93  	return err
    94  }