github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/internal/acceptance/openstack/networking/v2/extensions/dns/dns.go (about) 1 package dns 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/vnpaycloud-console/gophercloud/v2" 8 "github.com/vnpaycloud-console/gophercloud/v2/internal/acceptance/tools" 9 "github.com/vnpaycloud-console/gophercloud/v2/openstack/networking/v2/extensions/dns" 10 "github.com/vnpaycloud-console/gophercloud/v2/openstack/networking/v2/extensions/layer3/floatingips" 11 "github.com/vnpaycloud-console/gophercloud/v2/openstack/networking/v2/networks" 12 "github.com/vnpaycloud-console/gophercloud/v2/openstack/networking/v2/ports" 13 th "github.com/vnpaycloud-console/gophercloud/v2/testhelper" 14 ) 15 16 // PortWithDNSExt represents a port with the DNS fields 17 type PortWithDNSExt struct { 18 ports.Port 19 dns.PortDNSExt 20 } 21 22 // FloatingIPWithDNSExt represents a floating IP with the DNS fields 23 type FloatingIPWithDNSExt struct { 24 floatingips.FloatingIP 25 dns.FloatingIPDNSExt 26 } 27 28 // NetworkWithDNSExt represents a network with the DNS fields 29 type NetworkWithDNSExt struct { 30 networks.Network 31 dns.NetworkDNSExt 32 } 33 34 // CreatePortDNS will create a port with a DNS name on the specified subnet. An 35 // error will be returned if the port could not be created. 36 func CreatePortDNS(t *testing.T, client *gophercloud.ServiceClient, networkID, subnetID, dnsName string) (*PortWithDNSExt, error) { 37 portName := tools.RandomString("TESTACC-", 8) 38 portDescription := tools.RandomString("TESTACC-PORT-DESC-", 8) 39 iFalse := true 40 41 t.Logf("Attempting to create port: %s", portName) 42 43 portCreateOpts := ports.CreateOpts{ 44 NetworkID: networkID, 45 Name: portName, 46 Description: portDescription, 47 AdminStateUp: &iFalse, 48 FixedIPs: []ports.IP{{SubnetID: subnetID}}, 49 } 50 51 createOpts := dns.PortCreateOptsExt{ 52 CreateOptsBuilder: portCreateOpts, 53 DNSName: dnsName, 54 } 55 56 var port PortWithDNSExt 57 58 err := ports.Create(context.TODO(), client, createOpts).ExtractInto(&port) 59 if err != nil { 60 return &port, err 61 } 62 63 t.Logf("Successfully created port: %s", portName) 64 65 th.AssertEquals(t, port.Name, portName) 66 th.AssertEquals(t, port.Description, portDescription) 67 th.AssertEquals(t, port.DNSName, dnsName) 68 69 return &port, nil 70 } 71 72 // CreateFloatingIPDNS creates a floating IP with the DNS extension on a given 73 // network and port. An error will be returned if the creation failed. 74 func CreateFloatingIPDNS(t *testing.T, client *gophercloud.ServiceClient, networkID, portID, dnsName, dnsDomain string) (*FloatingIPWithDNSExt, error) { 75 t.Logf("Attempting to create floating IP on port: %s", portID) 76 77 fipDescription := "Test floating IP" 78 fipCreateOpts := &floatingips.CreateOpts{ 79 Description: fipDescription, 80 FloatingNetworkID: networkID, 81 PortID: portID, 82 } 83 84 createOpts := dns.FloatingIPCreateOptsExt{ 85 CreateOptsBuilder: fipCreateOpts, 86 DNSName: dnsName, 87 DNSDomain: dnsDomain, 88 } 89 90 var floatingIP FloatingIPWithDNSExt 91 err := floatingips.Create(context.TODO(), client, createOpts).ExtractInto(&floatingIP) 92 if err != nil { 93 return &floatingIP, err 94 } 95 96 t.Logf("Created floating IP.") 97 98 th.AssertEquals(t, floatingIP.Description, fipDescription) 99 th.AssertEquals(t, floatingIP.FloatingNetworkID, networkID) 100 th.AssertEquals(t, floatingIP.PortID, portID) 101 th.AssertEquals(t, floatingIP.DNSName, dnsName) 102 th.AssertEquals(t, floatingIP.DNSDomain, dnsDomain) 103 104 return &floatingIP, err 105 } 106 107 // CreateNetworkDNS will create a network with a DNS domain set. 108 // An error will be returned if the network could not be created. 109 func CreateNetworkDNS(t *testing.T, client *gophercloud.ServiceClient, dnsDomain string) (*NetworkWithDNSExt, error) { 110 networkName := tools.RandomString("TESTACC-", 8) 111 networkCreateOpts := networks.CreateOpts{ 112 Name: networkName, 113 AdminStateUp: gophercloud.Enabled, 114 } 115 116 createOpts := dns.NetworkCreateOptsExt{ 117 CreateOptsBuilder: networkCreateOpts, 118 DNSDomain: dnsDomain, 119 } 120 121 t.Logf("Attempting to create network: %s", networkName) 122 123 var network NetworkWithDNSExt 124 125 err := networks.Create(context.TODO(), client, createOpts).ExtractInto(&network) 126 if err != nil { 127 return &network, err 128 } 129 130 t.Logf("Successfully created network.") 131 132 th.AssertEquals(t, network.Name, networkName) 133 th.AssertEquals(t, network.DNSDomain, dnsDomain) 134 135 return &network, nil 136 }