// CreatePort will create a port on the specified subnet. An error will be // returned if the port could not be created. func CreatePort(t *testing.T, client *gophercloud.ServiceClient, networkID, subnetID string) (*ports.Port, error) { portName := tools.RandomString("TESTACC-", 8) t.Logf("Attempting to create port: %s", portName) createOpts := ports.CreateOpts{ NetworkID: networkID, Name: portName, AdminStateUp: gophercloud.Enabled, FixedIPs: []ports.IP{ports.IP{SubnetID: subnetID}}, } port, err := ports.Create(client, createOpts).Extract() if err != nil { return port, err } if err := WaitForPortToCreate(client, port.ID, 60); err != nil { return port, err } newPort, err := ports.Get(client, port.ID).Extract() if err != nil { return newPort, err } t.Logf("Successfully created port: %s", portName) return newPort, nil }
func resourceNetworkingPortV2Create(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) networkingClient, err := config.networkingV2Client(d.Get("region").(string)) if err != nil { return fmt.Errorf("Error creating OpenStack networking client: %s", err) } createOpts := PortCreateOpts{ ports.CreateOpts{ Name: d.Get("name").(string), AdminStateUp: resourcePortAdminStateUpV2(d), NetworkID: d.Get("network_id").(string), MACAddress: d.Get("mac_address").(string), TenantID: d.Get("tenant_id").(string), DeviceOwner: d.Get("device_owner").(string), SecurityGroups: resourcePortSecurityGroupsV2(d), DeviceID: d.Get("device_id").(string), FixedIPs: resourcePortFixedIpsV2(d), AllowedAddressPairs: resourceAllowedAddressPairsV2(d), }, MapValueSpecs(d), } log.Printf("[DEBUG] Create Options: %#v", createOpts) p, err := ports.Create(networkingClient, createOpts).Extract() if err != nil { return fmt.Errorf("Error creating OpenStack Neutron network: %s", err) } log.Printf("[INFO] Network ID: %s", p.ID) log.Printf("[DEBUG] Waiting for OpenStack Neutron Port (%s) to become available.", p.ID) stateConf := &resource.StateChangeConf{ Target: []string{"ACTIVE"}, Refresh: waitForNetworkPortActive(networkingClient, p.ID), Timeout: 2 * time.Minute, Delay: 5 * time.Second, MinTimeout: 3 * time.Second, } _, err = stateConf.WaitForState() d.SetId(p.ID) return resourceNetworkingPortV2Read(d, meta) }
// CreatePortWithSecurityGroup will create a port with a security group // attached. An error will be returned if the port could not be created. func CreatePortWithSecurityGroup(t *testing.T, client *gophercloud.ServiceClient, networkID, subnetID, secGroupID string) (*ports.Port, error) { portName := tools.RandomString("TESTACC-", 8) iFalse := false t.Logf("Attempting to create port: %s", portName) createOpts := ports.CreateOpts{ NetworkID: networkID, Name: portName, AdminStateUp: &iFalse, FixedIPs: []ports.IP{ports.IP{SubnetID: subnetID}}, SecurityGroups: []string{secGroupID}, } port, err := ports.Create(client, createOpts).Extract() if err != nil { return port, err } t.Logf("Successfully created port: %s", portName) return port, nil }
func TestNeutron(t *testing.T) { authUrl := os.Getenv("OS_AUTH_URL") username := os.Getenv("OS_USERNAME") password := os.Getenv("OS_PASSWORD") tenantName := os.Getenv("OS_TENANT_NAME") regionName := os.Getenv("OS_REGION_NAME") opts := gophercloud.AuthOptions{ IdentityEndpoint: authUrl, Username: username, Password: password, TenantName: tenantName, } provider, err := openstack.AuthenticatedClient(opts) if err != nil { t.Fatal(err.Error()) } client, err := openstack.NewNetworkV2(provider, gophercloud.EndpointOpts{ Name: "neutron", Region: regionName, Availability: gophercloud.AvailabilityPublic, }) if err != nil { t.Fatalf("Failed to create neutron client: %s", err.Error()) } netResult := networks.Create(client, networks.CreateOpts{Name: "skydive-test-network"}) if netResult.Err != nil { t.Fatalf("Failed to create neutron network: %s", netResult.Err.Error()) } network, err := netResult.Extract() if err != nil { t.Fatalf("Failed to create neutron network: %s", err.Error()) } subResult := subnets.Create(client, subnets.CreateOpts{Name: "skydive-test-subnet", NetworkID: network.ID, CIDR: "192.168.1.0/24", IPVersion: 4}) if subResult.Err != nil { t.Fatalf("Failed to create neutron subnet: %s", subResult.Err.Error()) } subnet, err := subResult.Extract() if err != nil { t.Fatalf("Failed to create neutron subnet: %s", err.Error()) } portResult := ports.Create(client, ports.CreateOpts{NetworkID: network.ID, DeviceID: "skydive-123", DeviceOwner: "skydive-test"}) if portResult.Err != nil { t.Fatalf("Failed to create neutron port: %s", subResult.Err.Error()) } port, err := portResult.Extract() if err != nil { t.Fatalf("Failed to create neutron port: %s", err.Error()) } defer ports.Delete(client, port.ID) defer subnets.Delete(client, subnet.ID) defer networks.Delete(client, network.ID) authOptions := &shttp.AuthenticationOpts{ Username: username, Password: password, } subID := port.ID[0:11] dev := fmt.Sprintf("tap%s", subID) ovsctl := `ovs-vsctl add-port br-int %s -- set Interface %s external-ids:iface-id=%s` ovsctl += ` external-ids:iface-status=active external-ids:attached-mac=%s external-ids:vm-uuid=skydive-vm type=internal` setupCmds := []helper.Cmd{ {fmt.Sprintf(ovsctl, dev, dev, port.ID, port.MACAddress), true}, {"sleep 1", true}, {fmt.Sprintf("ip link set %s up", dev), true}, } tearDownCmds := []helper.Cmd{ {fmt.Sprintf("ovs-vsctl del-port %s", dev), true}, } helper.ExecCmds(t, setupCmds...) defer helper.ExecCmds(t, tearDownCmds...) gh := helper.NewGremlinQueryHelper(authOptions) // let neutron update the port time.Sleep(5 * time.Second) nodes := gh.GetNodesFromGremlinReply(t, `g.V().Has("Manager", "neutron", "ExtID/vm-uuid", "skydive-vm", "Name", "`+dev+`", "Neutron/PortID", "`+port.ID+`")`) if len(nodes) != 1 { t.Errorf("Should find the neutron port in the topology: %v", gh.GetNodesFromGremlinReply(t, `g.V()`)) } }
func TestRequiredCreateOpts(t *testing.T) { res := ports.Create(fake.ServiceClient(), ports.CreateOpts{}) if res.Err == nil { t.Fatalf("Expected error, got none") } }
func TestCreate(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() th.Mux.HandleFunc("/v2.0/ports", func(w http.ResponseWriter, r *http.Request) { th.TestMethod(t, r, "POST") th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) th.TestHeader(t, r, "Content-Type", "application/json") th.TestHeader(t, r, "Accept", "application/json") th.TestJSONRequest(t, r, ` { "port": { "network_id": "a87cc70a-3e15-4acf-8205-9b711a3531b7", "name": "private-port", "admin_state_up": true, "fixed_ips": [ { "subnet_id": "a0304c3a-4f08-4c43-88af-d796509c97d2", "ip_address": "10.0.0.2" } ], "security_groups": ["foo"], "allowed_address_pairs": [ { "ip_address": "10.0.0.4", "mac_address": "fa:16:3e:c9:cb:f0" } ] } } `) w.Header().Add("Content-Type", "application/json") w.WriteHeader(http.StatusCreated) fmt.Fprintf(w, ` { "port": { "status": "DOWN", "name": "private-port", "admin_state_up": true, "network_id": "a87cc70a-3e15-4acf-8205-9b711a3531b7", "tenant_id": "d6700c0c9ffa4f1cb322cd4a1f3906fa", "device_owner": "", "mac_address": "fa:16:3e:c9:cb:f0", "fixed_ips": [ { "subnet_id": "a0304c3a-4f08-4c43-88af-d796509c97d2", "ip_address": "10.0.0.2" } ], "id": "65c0ee9f-d634-4522-8954-51021b570b0d", "security_groups": [ "f0ac4394-7e4a-4409-9701-ba8be283dbc3" ], "allowed_address_pairs": [ { "ip_address": "10.0.0.4", "mac_address": "fa:16:3e:c9:cb:f0" } ], "device_id": "" } } `) }) asu := true options := ports.CreateOpts{ Name: "private-port", AdminStateUp: &asu, NetworkID: "a87cc70a-3e15-4acf-8205-9b711a3531b7", FixedIPs: []ports.IP{ {SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.2"}, }, SecurityGroups: []string{"foo"}, AllowedAddressPairs: []ports.AddressPair{ {IPAddress: "10.0.0.4", MACAddress: "fa:16:3e:c9:cb:f0"}, }, } n, err := ports.Create(fake.ServiceClient(), options).Extract() th.AssertNoErr(t, err) th.AssertEquals(t, n.Status, "DOWN") th.AssertEquals(t, n.Name, "private-port") th.AssertEquals(t, n.AdminStateUp, true) th.AssertEquals(t, n.NetworkID, "a87cc70a-3e15-4acf-8205-9b711a3531b7") th.AssertEquals(t, n.TenantID, "d6700c0c9ffa4f1cb322cd4a1f3906fa") th.AssertEquals(t, n.DeviceOwner, "") th.AssertEquals(t, n.MACAddress, "fa:16:3e:c9:cb:f0") th.AssertDeepEquals(t, n.FixedIPs, []ports.IP{ {SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.2"}, }) th.AssertEquals(t, n.ID, "65c0ee9f-d634-4522-8954-51021b570b0d") th.AssertDeepEquals(t, n.SecurityGroups, []string{"f0ac4394-7e4a-4409-9701-ba8be283dbc3"}) th.AssertDeepEquals(t, n.AllowedAddressPairs, []ports.AddressPair{ {IPAddress: "10.0.0.4", MACAddress: "fa:16:3e:c9:cb:f0"}, }) }