Skip to content

Commit

Permalink
virtual interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
anton-sidelnikov committed Sep 20, 2024
1 parent d9d1196 commit aa90dd7
Show file tree
Hide file tree
Showing 9 changed files with 492 additions and 77 deletions.
30 changes: 20 additions & 10 deletions acceptance/openstack/dcaas/v3/virtual_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/opentelekomcloud/gophertelekomcloud/openstack/common/pointerto"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/common/tags"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/dcaas/v3/virtual-gateway"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/networking/v1/vpcs"
th "github.com/opentelekomcloud/gophertelekomcloud/testhelper"
)

Expand All @@ -27,19 +28,24 @@ func TestVirtualGatewayListing(t *testing.T) {
}

func TestVirtualGatewayLifecycle(t *testing.T) {
if os.Getenv("OS_VPC_ID") == "" {
vpcID := os.Getenv("OS_VPC_ID")
if vpcID == "" {
t.Skip("OS_VPC_ID necessary for this test")
}
client, err := clients.NewDCaaSV3Client()
th.AssertNoErr(t, err)

clientNet, err := clients.NewNetworkV1Client()
th.AssertNoErr(t, err)
vpc, err := vpcs.Get(clientNet, vpcID).Extract()
th.AssertNoErr(t, err)
// Create a virtual gateway
name := strings.ToLower(tools.RandomString("test-virtual-gateway", 5))
name := strings.ToLower(tools.RandomString("acc-virtual-gateway-v3-", 5))
createOpts := virtual_gateway.CreateOpts{
Name: name,
VpcId: os.Getenv("OS_VPC_ID"),
Description: "test-virtual-gateway-v3",
LocalEpGroup: []string{""},
VpcId: vpcID,
Description: "acc-virtual-gateway-v3",
LocalEpGroup: []string{vpc.CIDR},
Tags: []tags.ResourceTag{
{
Key: "TestKey",
Expand All @@ -51,19 +57,23 @@ func TestVirtualGatewayLifecycle(t *testing.T) {
created, err := virtual_gateway.Create(client, createOpts)
th.AssertNoErr(t, err)

_, err = virtual_gateway.Get(client, created.ID)
vgw, err := virtual_gateway.Get(client, created.ID)
th.AssertNoErr(t, err)
th.AssertEquals(t, name, vgw.Name)

nameUpdated := strings.ToLower(tools.RandomString("acc-virtual-gateway-v3-up", 5))
updateOpts := virtual_gateway.UpdateOpts{
Name: tools.RandomString(name, 3),
Description: pointerto.String("test-virtual-gateway-v3-updated"),
Name: nameUpdated,
Description: pointerto.String("acc-virtual-gateway-v3-updated"),
}
_ = virtual_gateway.Update(client, created.ID, updateOpts)
updated, err := virtual_gateway.Update(client, created.ID, updateOpts)
th.AssertNoErr(t, err)
th.AssertEquals(t, nameUpdated, updated.Name)

opts := virtual_gateway.ListOpts{}
_, err = virtual_gateway.List(client, opts)
gateways, err := virtual_gateway.List(client, opts)
th.AssertNoErr(t, err)
th.AssertEquals(t, 1, len(gateways))

t.Cleanup(func() {
err = virtual_gateway.Delete(client, created.ID)
Expand Down
121 changes: 121 additions & 0 deletions acceptance/openstack/dcaas/v3/virtual_interface_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package v3

import (
"os"
"strings"
"testing"

"github.com/opentelekomcloud/gophertelekomcloud/acceptance/clients"
"github.com/opentelekomcloud/gophertelekomcloud/acceptance/tools"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/common/pointerto"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/common/tags"
direct_connect "github.com/opentelekomcloud/gophertelekomcloud/openstack/dcaas/v2/direct-connect"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/dcaas/v3/virtual-gateway"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/dcaas/v3/virtual-interface"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/networking/v1/vpcs"
th "github.com/opentelekomcloud/gophertelekomcloud/testhelper"
)

func TestVirtualInterfaceListing(t *testing.T) {
client, err := clients.NewDCaaSV3Client()
th.AssertNoErr(t, err)

// List virtual interfaces
opts := virtual_interface.ListOpts{}
viList, err := virtual_interface.List(client, opts)
th.AssertNoErr(t, err)

for _, vi := range viList {
tools.PrintResource(t, vi)
}
}

func TestVirtualInterfaceLifecycle(t *testing.T) {
if os.Getenv("RUN_DCAAS_VIRTUAL_INTERFACE") == "" {
t.Skip("DIRECT_CONNECT_ID necessary for this test or run it only in test_terraform")
}
vpcID := os.Getenv("OS_VPC_ID")
if vpcID == "" {
t.Skip("OS_VPC_ID necessary for this test")
}
clientV2, err := clients.NewDCaaSV2Client()
th.AssertNoErr(t, err)
clientV3, err := clients.NewDCaaSV3Client()
th.AssertNoErr(t, err)

name := strings.ToLower(tools.RandomString("acc-direct-connect", 5))
createOpts := direct_connect.CreateOpts{
Name: name,
PortType: "1G",
Bandwidth: 100,
Location: "Biere",
Provider: "OTC",
}

dc, err := direct_connect.Create(clientV2, createOpts)
th.AssertNoErr(t, err)

clientNet, err := clients.NewNetworkV1Client()
th.AssertNoErr(t, err)
vpc, err := vpcs.Get(clientNet, vpcID).Extract()
th.AssertNoErr(t, err)

// Create a virtual gateway
nameVgw := strings.ToLower(tools.RandomString("acc-virtual-gateway-v3-", 5))
vgOpts := virtual_gateway.CreateOpts{
Name: nameVgw,
VpcId: vpcID,
Description: "acc-virtual-gateway-v3",
LocalEpGroup: []string{vpc.CIDR},
Tags: []tags.ResourceTag{
{
Key: "TestKey",
Value: "TestValue",
},
},
}

vg, err := virtual_gateway.Create(clientV3, vgOpts)
th.AssertNoErr(t, err)

t.Cleanup(func() {
err = virtual_gateway.Delete(clientV3, vg.ID)
th.AssertNoErr(t, err)
})

// Create a virtual interface
nameVi := tools.RandomString("acc-virtual-interface-", 5)
viOpts := virtual_interface.CreateOpts{
Name: nameVi,
DirectConnectID: dc.ID,
VgwId: vg.ID,
Type: "private",
ServiceType: "vpc",
VLAN: 100,
Bandwidth: 5,
LocalGatewayV4IP: "16.16.16.1/30",
RemoteGatewayV4IP: "16.16.16.2/30",
RouteMode: "static",
RemoteEpGroup: []string{vpc.CIDR},
}

created, err := virtual_interface.Create(clientV3, viOpts)
th.AssertNoErr(t, err)

vi, err := virtual_interface.Get(clientV3, created.ID)
th.AssertNoErr(t, err)
th.AssertEquals(t, vi.RemoteEpGroup[0], vpc.CIDR)

updated, err := virtual_interface.Update(clientV3, created.ID, virtual_interface.UpdateOpts{
Name: name + "-updated",
Description: pointerto.String("New description"),
})
th.AssertNoErr(t, err)
th.AssertEquals(t, name+"-updated", updated.Name)

// Cleanup
t.Cleanup(func() {
err = virtual_interface.Delete(clientV3, created.ID)
th.AssertNoErr(t, err)
})
}
17 changes: 11 additions & 6 deletions openstack/dcaas/v3/virtual-gateway/Update.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package virtual_gateway
import (
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
"github.com/opentelekomcloud/gophertelekomcloud/internal/build"
"github.com/opentelekomcloud/gophertelekomcloud/internal/extract"
)

type UpdateOpts struct {
// The list of IPv4 subnets from the virtual gateway to access cloud services, which is usually the CIDR block of
// the VPC.
LocalEpGroup []string `json:"local_ep_group" required:"true"`
LocalEpGroup []string `json:"local_ep_group,omitempty"`
// The list of IPv6 subnets from the virtual gateway to access cloud services, which is usually the CIDR block of
// the VPC.
LocalEpGroupIpv6 []string `json:"local_ep_group_ipv6,omitempty"`
Expand All @@ -24,15 +25,19 @@ type UpdateOpts struct {
Description *string `json:"description,omitempty"`
}

func Update(c *golangsdk.ServiceClient, id string, opts UpdateOpts) (err error) {
func Update(c *golangsdk.ServiceClient, id string, opts UpdateOpts) (*VirtualGateway, error) {
b, err := build.RequestBody(opts, "virtual_gateway")
if err != nil {
return
return nil, err
}

_, err = c.Put(c.ServiceURL("dcaas", "virtual-gateways", id), b, nil, &golangsdk.RequestOpts{
raw, err := c.Put(c.ServiceURL("dcaas", "virtual-gateways", id), b, nil, &golangsdk.RequestOpts{
OkCodes: []int{200, 202},
})
if err != nil {
return nil, err
}

return
var res VirtualGateway
err = extract.IntoStructPtr(raw.Body, &res, "virtual_gateway")
return &res, err
}
68 changes: 52 additions & 16 deletions openstack/dcaas/v3/virtual-interface/Create.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,58 @@ import (
)

type CreateOpts struct {
TenantID string `json:"tenant_id,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
DirectConnectID string `json:"direct_connect_id" required:"true"`
VgwID string `json:"vgw_id" required:"true"`
Type string `json:"type" required:"true"`
ServiceType string `json:"service_type" required:"true"`
VLAN int `json:"vlan" required:"true"`
Bandwidth int `json:"bandwidth" required:"true"`
LocalGatewayV4IP string `json:"local_gateway_v4_ip" required:"true"`
RemoteGatewayV4IP string `json:"remote_gateway_v4_ip" required:"true"`
RouteMode string `json:"route_mode" required:"true"`
BGPASN int `json:"bgp_asn,omitempty"`
BGPMD5 string `json:"bgp_md5,omitempty"`
RemoteEPGroupID string `json:"remote_ep_group_id" required:"true"`
AdminStateUp bool `json:"admin_state_up,omitempty"`
// Specifies the project ID of another tenant, which is used to create virtual interfaces across tenants.
ResourceTenantID string `json:"resource_tenant_id,omitempty"`
// Specifies the virtual interface name.
Name string `json:"name,omitempty"`
// Provides supplementary information about the virtual interface.
Description string `json:"description,omitempty"`
// Specifies the ID of the connection associated with the virtual interface.
// When creating a virtual interface, you need to specify direct_connect_id or lag_id.
// This parameter is mandatory when LAG is not supported at the site.
DirectConnectID string `json:"direct_connect_id,omitempty"`
// Specifies the type of the virtual interface. The value is private.
Type string `json:"type" required:"true"`
// Specifies the type of the access gateway. You do not need to set this parameter.
ServiceType string `json:"service_type,omitempty"`
// Specifies the customer VLAN to be connected.
// If you select a hosted connection, the VLAN must be the same as that of the hosted connection.
VLAN int `json:"vlan" required:"true"`
// Specifies the virtual interface bandwidth.
Bandwidth int `json:"bandwidth" required:"true"`
// Specifies the IPv4 interface address of the gateway used on the cloud.
// This parameter is mandatory if address_family is set to an IPv4 address.
LocalGatewayV4IP string `json:"local_gateway_v4_ip,omitempty"`
// Specifies the IPv4 interface address of the gateway on the on-premises network.
// This parameter is mandatory if address_family is set to an IPv4 address.
RemoteGatewayV4IP string `json:"remote_gateway_v4_ip,omitempty"`
// Specifies the address family of the virtual interface.
// The value can be IPv4 or IPv6.
AddressFamily string `json:"address_family,omitempty"`
// Specifies the IPv6 interface address of the gateway used on the cloud.
// This parameter is mandatory if address_family is set to an IPv6 address.
LocalGatewayV6IP string `json:"local_gateway_v6_ip,omitempty"`
// Specifies the IPv6 interface address of the gateway on the on-premises network.
// This parameter is mandatory if address_family is set to an IPv6 address.
RemoteGatewayV6IP string `json:"remote_gateway_v6_ip,omitempty"`
// Specifies the ID of the virtual gateway connected by the virtual interface.
VgwId string `json:"vgw_id,omitempty" required:"true"`
// Specifies the routing mode. The value can be static or bgp.
RouteMode string `json:"route_mode" required:"true"`
// Specifies the ASN of the BGP peer on the customer side.
BGPASN int `json:"bgp_asn,omitempty"`
// Specifies the MD5 password of the BGP peer.
BGPMD5 string `json:"bgp_md5,omitempty"`
// Specifies the remote subnet list, which records the CIDR blocks used in the on-premises data center.
RemoteEpGroup []string `json:"remote_ep_group" required:"true"`
// Specifies the subnets that access Internet services through a connection.
ServiceEpGroup []string `json:"service_ep_group,omitempty"`
// Specifies whether to enable BFD.
EnableBfd bool `json:"enable_bfd,omitempty"`
// Specifies whether to enable NQA.
EnableNqa bool `json:"enable_nqa,omitempty"`
// Specifies the ID of the LAG associated with the virtual interface.
LagId string `json:"lag_id,omitempty"`
}

func Create(c *golangsdk.ServiceClient, opts CreateOpts) (*VirtualInterface, error) {
Expand Down
49 changes: 49 additions & 0 deletions openstack/dcaas/v3/virtual-interface/CreatePeer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package virtual_interface

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

type CreatePeerOpts struct {
// Specifies the name of the virtual interface peer.
Name string `json:"name" required:"true"`
// Provides supplementary information about the virtual interface peer.
Description string `json:"description,omitempty"`
// Specifies the gateway address of the virtual interface peer used on the cloud.
LocalGatewayIP string `json:"local_gateway_ip" required:"true"`
// Specifies the IPv4 interface address of the gateway on the on-premises network.
// This parameter is mandatory if address_family is set to an IPv4 address.
RemoteGatewayIP string `json:"remote_gateway_ip" required:"true"`
// Specifies the address family of the virtual interface.
// The value can be IPv4 or IPv6.
AddressFamily string `json:"address_family" required:"true"`
// Specifies the routing mode. The value can be static or bgp.
RouteMode string `json:"route_mode" required:"true"`
// Specifies the ASN of the BGP peer on the customer side.
BGPASN int `json:"bgp_asn,omitempty"`
// Specifies the MD5 password of the BGP peer.
BGPMD5 string `json:"bgp_md5,omitempty"`
// Specifies the remote subnet list, which records the CIDR blocks used in the on-premises data center.
RemoteEpGroup []string `json:"remote_ep_group,omitempty"`
// Specifies the ID of the virtual interface corresponding to the virtual interface peer.
VifId string `json:"vif_id" required:"true"`
}

func CreatePeer(c *golangsdk.ServiceClient, opts CreatePeerOpts) (*VifPeer, error) {
b, err := build.RequestBody(opts, "vif_peer")
if err != nil {
return nil, err
}
raw, err := c.Post(c.ServiceURL("dcaas", "vif-peers"), b, nil, &golangsdk.RequestOpts{
OkCodes: []int{201},
})
if err != nil {
return nil, err
}

var res VifPeer
err = extract.IntoStructPtr(raw.Body, &res, "vif_peer")
return &res, err
}
10 changes: 10 additions & 0 deletions openstack/dcaas/v3/virtual-interface/DeletePeer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package virtual_interface

import golangsdk "github.com/opentelekomcloud/gophertelekomcloud"

func DeletePeer(c *golangsdk.ServiceClient, id string) (err error) {
_, err = c.Delete(c.ServiceURL("dcaas", "vif-peers", id), &golangsdk.RequestOpts{
OkCodes: []int{200, 201, 204},
})
return
}
Loading

0 comments on commit aa90dd7

Please sign in to comment.