Skip to content

Commit

Permalink
snat rules refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
anton-sidelnikov committed Aug 22, 2024
1 parent db3e93b commit c1eee59
Show file tree
Hide file tree
Showing 9 changed files with 208 additions and 129 deletions.
15 changes: 12 additions & 3 deletions acceptance/openstack/networking/v2/extensions/snatrules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,27 @@ func TestSnatRuleLifeCycle(t *testing.T) {
FloatingIPID: elasticIp.ID,
SourceType: 0,
}
snatRule, err := snatrules.Create(client, createOpts).Extract()
snatRule, err := snatrules.Create(client, createOpts)
th.AssertNoErr(t, err)
t.Logf("Created SNAT rule: %s", snatRule.ID)

defer func() {
t.Logf("Attempting to delete SNAT rule: %s", snatRule.ID)
err = snatrules.Delete(client, snatRule.ID).ExtractErr()
err = snatrules.Delete(client, snatRule.ID)
th.AssertNoErr(t, err)
t.Logf("Deleted SNAT rule: %s", snatRule.ID)
}()

newSnatRule, err := snatrules.Get(client, snatRule.ID).Extract()
t.Logf("Attempting to Obtain SNAT rule: %s", snatRule.ID)
newSnatRule, err := snatrules.Get(client, snatRule.ID)
th.AssertNoErr(t, err)
th.AssertEquals(t, createOpts.NatGatewayID, newSnatRule.NatGatewayID)

t.Logf("Attempting to Obtain SNAT rules in NAT Gateway: %s", natGateway.ID)
listnatRules, err := snatrules.List(client, snatrules.ListOpts{
NatGatewayId: natGateway.ID,
})
th.AssertNoErr(t, err)
th.AssertEquals(t, len(listnatRules), 1)
th.AssertEquals(t, listnatRules[0].NatGatewayID, natGateway.ID)
}
33 changes: 26 additions & 7 deletions openstack/networking/v2/extensions/dnatrules/Create.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,32 @@ import (
// CreateOpts contains all the values needed to create a new dnat rule
// resource.
type CreateOpts struct {
NatGatewayID string `json:"nat_gateway_id" required:"true"`
PortID string `json:"port_id,omitempty"`
PrivateIp string `json:"private_ip,omitempty"`
InternalServicePort *int `json:"internal_service_port" required:"true"`
FloatingIpID string `json:"floating_ip_id" required:"true"`
ExternalServicePort *int `json:"external_service_port" required:"true"`
Protocol string `json:"protocol" required:"true"`
// Specifies the NAT gateway ID.
NatGatewayID string `json:"nat_gateway_id" required:"true"`
// Specifies the port ID of the cloud server (ECS or BMS).
// Either this parameter or private_ip must be specified.
// When the DNAT rule is used in the VPC scenario, use this parameter.
PortID string `json:"port_id,omitempty"`
// Specifies the private IP address, for example, the IP address of an on-premises network connected by a Direct Connect connection.
// This parameter and port_id are alternative.
// When the DNAT rule is used in the Direct Connect scenario, use this parameter.
PrivateIp string `json:"private_ip,omitempty"`
// Specifies the port number used by the cloud server (ECS or BMS) to provide services for external systems.
// In VPC DNAT scenarios, this parameter indicates the protocol port number of the NIC of
// the cloud server (ECS or BMS) in the DNAT rule. In Direct Connect DNAT scenarios,
// this parameter indicates the protocol port number of your private IP address in the DNAT rule.
// The value ranges from 0 to 65535.
InternalServicePort *int `json:"internal_service_port" required:"true"`
// Specifies the EIP ID.
FloatingIpID string `json:"floating_ip_id" required:"true"`
// Specifies the port for providing services for external systems.
// The value ranges from 0 to 65535.
ExternalServicePort *int `json:"external_service_port" required:"true"`
// Specifies the protocol. TCP, UDP, and ANY are supported.
// The protocol number of TCP, UDP, and ANY are 6, 17, and 0, respectively.
Protocol string `json:"protocol" required:"true"`
// Provides supplementary information about the DNAT rule.
Description string `json:"description,omitempty"`
}

// Create will create a new Waf Certificate on the values in CreateOpts.
Expand Down
82 changes: 82 additions & 0 deletions openstack/networking/v2/extensions/snatrules/Create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package snatrules

import (
"github.com/opentelekomcloud/gophertelekomcloud"
"github.com/opentelekomcloud/gophertelekomcloud/internal/build"
"github.com/opentelekomcloud/gophertelekomcloud/internal/extract"
)

type CreateOpts struct {
// Specifies the NAT gateway ID.
NatGatewayID string `json:"nat_gateway_id" required:"true"`
// Specifies the network ID used by the SNAT rule. This parameter and cidr are alternative.
NetworkID string `json:"network_id,omitempty"`
// Specifies the EIP ID. Use commas (,) to separate multiple IDs.
// The maximum length of the ID is 4,096 bytes.
// Constraints: The number of EIP IDs cannot exceed 20.
FloatingIPID string `json:"floating_ip_id" required:"true"`
// Specifies CIDR, which can be in the format of a network segment or a host IP address.
// This parameter and network_id are alternative.
// If source_type is set to 0, cidr must be a subset of the VPC subnet.
// If source_type is set to 1, cidr must be a CIDR block of your on-premises network connected to the VPC through Direct Connect or Cloud Connect.
Cidr string `json:"cidr,omitempty"`
// 0: Either network_id or cidr can be specified in a VPC.
// 1: Only cidr can be specified over a Direct Connect connection.
// If no value is entered, the default value 0 (VPC) is used.
SourceType int `json:"source_type,omitempty"`
// Provides supplementary information about the SNAT rule.
Description string `json:"description,omitempty"`
}

func Create(client *golangsdk.ServiceClient, opts CreateOpts) (*SnatRule, error) {
b, err := build.RequestBody(opts, "snat_rule")
if err != nil {
return nil, err
}

// POST /v2.0/snat_rules
raw, err := client.Post(client.ServiceURL("snat_rules"), b,
nil, &golangsdk.RequestOpts{
OkCodes: []int{201},
})
if err != nil {
return nil, err
}

var res SnatRule
err = extract.IntoStructPtr(raw.Body, &res, "snat_rule")
return &res, err
}

type SnatRule struct {
// Specifies the SNAT rule ID.
ID string `json:"id"`
// Specifies the NAT gateway ID.
NatGatewayID string `json:"nat_gateway_id"`
// Specifies the network ID used by the SNAT rule.
NetworkID string `json:"network_id"`
// Specifies the project ID.
TenantID string `json:"tenant_id"`
// Specifies the EIP ID. Use commas (,) to separate multiple IDs.
FloatingIPID string `json:"floating_ip_id"`
// Specifies the EIP. Use commas (,) to separate multiple EIPs.
FloatingIPAddress string `json:"floating_ip_address"`
// Specifies the status of the SNAT rule.
Status string `json:"status"`
// Specifies the unfrozen or frozen state.
// Specifies whether the SNAT rule is enabled or disabled.
// The state can be:
// true: The SNAT rule is enabled.
// false: The SNAT rule is disabled.
AdminStateUp bool `json:"admin_state_up"`
// Specifies a subset of the VPC subnet CIDR block or a CIDR block of Direct Connect connection.
Cidr string `json:"cidr"`
// 0: Either network_id or cidr can be specified in a VPC.
// 1: Only cidr can be specified over a Direct Connect connection.
// If no value is entered, the default value 0 (VPC) is used.
SourceType interface{} `json:"source_type"`
// Specifies when the SNAT rule is created (UTC time).
// Its value rounds to 6 decimal places for seconds.
// The format is yyyy-mm-dd hh:mm:ss.
CreatedAt string `json:"created_at"`
}
13 changes: 13 additions & 0 deletions openstack/networking/v2/extensions/snatrules/Delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package snatrules

import (
"github.com/opentelekomcloud/gophertelekomcloud"
)

func Delete(client *golangsdk.ServiceClient, id string) (err error) {
// DELETE /v2.0/snat_rules/{snat_rule_id}
_, err = client.Delete(client.ServiceURL("snat_rules", id), &golangsdk.RequestOpts{
OkCodes: []int{204},
})
return
}
17 changes: 17 additions & 0 deletions openstack/networking/v2/extensions/snatrules/Get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package snatrules

import (
"github.com/opentelekomcloud/gophertelekomcloud"
"github.com/opentelekomcloud/gophertelekomcloud/internal/extract"
)

func Get(client *golangsdk.ServiceClient, id string) (*SnatRule, error) {
// GET /v2.0/snat_rules/{snat_rule_id}
raw, err := client.Get(client.ServiceURL("snat_rules", id), nil, nil)
if err != nil {
return nil, err
}

var res SnatRule
return &res, extract.IntoStructPtr(raw.Body, &res, "snat_rule")
}
58 changes: 58 additions & 0 deletions openstack/networking/v2/extensions/snatrules/List.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package snatrules

import (
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
"github.com/opentelekomcloud/gophertelekomcloud/internal/extract"
)

func List(client *golangsdk.ServiceClient, opts ListOpts) ([]SnatRule, error) {
url, err := golangsdk.NewURLBuilder().WithEndpoints("snat_rules").WithQueryParams(&opts).Build()
if err != nil {
return nil, err
}

// GET /v2.0/snat_rules
raw, err := client.Get(client.ServiceURL(url.String()), nil, nil)
if err != nil {
return nil, err
}

var res []SnatRule
err = extract.IntoSlicePtr(raw.Body, &res, "snat_rules")
return res, err
}

type ListOpts struct {
// Specifies the SNAT rule ID.
Id string `q:"id,omitempty"`
// Specifies the number of records returned on each page.
Limit int `q:"limit,omitempty"`
// Specifies the project ID.
ProjectId string `q:"tenant_id,omitempty"`
// Specifies the NAT gateway ID.
NatGatewayId string `q:"nat_gateway_id,omitempty"`
// Specifies the network ID used by the SNAT rule.
NetworkId string `q:"network_id,omitempty"`
// Specifies a subset of the VPC subnet CIDR block or a CIDR block of Direct Connect connection.
Cidr string `q:"cidr,omitempty"`
// 0: Either network_id or cidr can be specified in a VPC.
// 1: Only cidr can be specified over a Direct Connect connection.
// If no value is entered, the default value 0 (VPC) is used.
SourceType string `q:"source_type,omitempty"`
// Specifies the EIP ID.
FloatingIpId string `q:"floating_ip_id,omitempty"`
// Specifies the EIP address.
FloatingIpAddress string `q:"floating_ip_address,omitempty"`
// Provides supplementary information about the SNAT rule.
Description string `q:"description,omitempty"`
// Specifies the status of the SNAT rule.
Status string `q:"status,omitempty"`
// Specifies whether the SNAT rule is enabled or disabled.
// The value can be:
// true: The SNAT rule is enabled.
// false: The SNAT rule is disabled.
AdminStateUp bool `q:"admin_state_up,omitempty"`
// Specifies when the SNAT rule was created (UTC time).
// Its value rounds to 6 decimal places for seconds. The format is yyyy-mm-dd hh:mm:ss.
CreatedAt bool `q:"created_at,omitempty"`
}
53 changes: 0 additions & 53 deletions openstack/networking/v2/extensions/snatrules/requests.go

This file was deleted.

53 changes: 0 additions & 53 deletions openstack/networking/v2/extensions/snatrules/results.go

This file was deleted.

13 changes: 0 additions & 13 deletions openstack/networking/v2/extensions/snatrules/urls.go

This file was deleted.

0 comments on commit c1eee59

Please sign in to comment.