func (a *AddressAllocatorImpl) initializeAllocator() {
	obj, err := a.client.FindByName("virtual-network", AddressAllocationNetwork)
	if err == nil {
		a.network = obj.(*types.VirtualNetwork)
		return
	}

	fqn := strings.Split(AddressAllocationNetwork, ":")
	parent := strings.Join(fqn[0:len(fqn)-1], ":")
	projectId, err := a.client.UuidByName("project", parent)
	if err != nil {
		glog.Fatalf("%s: %v", parent, err)
	}
	netId, err := config.CreateNetworkWithSubnet(
		a.client, projectId, fqn[len(fqn)-1], a.privateSubnet)
	if err != nil {
		glog.Fatalf("%s: %v", parent, err)
	}
	glog.Infof("Created network %s", AddressAllocationNetwork)
	obj, err = a.client.FindByUuid("virtual-network", netId)
	if err != nil {
		glog.Fatal("Get virtual-network %s: %v", netId, err)
	}
	a.network = obj.(*types.VirtualNetwork)
}
func (m *NetworkManagerImpl) LocateNetwork(project, name, subnet string) (*types.VirtualNetwork, error) {
	fqn := []string{DefaultDomain, project, name}
	fqname := strings.Join(fqn, ":")

	obj, err := m.client.FindByName("virtual-network", fqname)
	if err == nil {
		return obj.(*types.VirtualNetwork), nil
	}

	projectId, err := m.client.UuidByName("project", fmt.Sprintf("%s:%s", DefaultDomain, project))
	if err != nil {
		glog.Infof("GET %s: %v", project, err)
		return nil, err
	}
	uid, err := config.CreateNetworkWithSubnet(
		m.client, projectId, name, subnet)
	if err != nil {
		glog.Infof("Create %s: %v", name, err)
		return nil, err
	}
	obj, err = m.client.FindByUuid("virtual-network", uid)
	if err != nil {
		glog.Infof("GET %s: %v", name, err)
		return nil, err
	}
	glog.Infof("Create network %s", fqname)
	return obj.(*types.VirtualNetwork), nil
}
func (m *NetworkManagerImpl) initializePublicNetwork() {
	var network *types.VirtualNetwork
	obj, err := m.client.FindByName("virtual-network", m.config.PublicNetwork)
	if err != nil {
		fqn := strings.Split(m.config.PublicNetwork, ":")
		parent := strings.Join(fqn[0:len(fqn)-1], ":")
		projectId, err := m.client.UuidByName("project", parent)
		if err != nil {
			glog.Fatalf("%s: %v", parent, err)
		}
		var networkID string
		networkName := fqn[len(fqn)-1]
		networkID, err = config.CreateNetworkWithSubnet(
			m.client, projectId, networkName, m.config.PublicSubnet)
		if err != nil {
			glog.Fatalf("%s: %v", parent, err)
		}

		glog.Infof("Created network %s", m.config.PublicNetwork)

		obj, err := m.client.FindByUuid("virtual-network", networkID)
		if err != nil {
			glog.Fatalf("GET %s %v", networkID, err)
		}
		network = obj.(*types.VirtualNetwork)
	} else {
		network = obj.(*types.VirtualNetwork)
		m.updateSubnetConfig(network, m.config.PublicSubnet)
	}

	m.publicNetwork = network

	m.LocateFloatingIpPool(network)
}
func TestNetworkList(t *testing.T) {
	client := contrail.NewClient("localhost", 8082)

	project_id, err := config.CreateProject(client, "demo", true)
	if err != nil {
		t.Fatal(err)
	}
	defer config.DeleteProject(client, project_id)

	type TestData struct {
		Subnet string
		InList bool
	}

	data := map[string]*TestData{
		"test1": &TestData{"192.168.0.0/24", false},
		"test2": &TestData{"192.168.1.0/24", false},
		"test3": &TestData{"10.0.0.0/8", false},
		"test4": &TestData{"100.64.0.0/24", false},
	}

	for name, value := range data {
		nid, err := config.CreateNetworkWithSubnet(
			client, project_id, name, value.Subnet)
		if err != nil {
			t.Fatal(err)
		}
		defer client.DeleteByUuid("virtual-network", nid)
	}

	netList, err := config.NetworkList(client, project_id, false)
	if err != nil {
		t.Fatal(err)
	}

	if len(netList) != len(data) {
		t.Errorf("Expected %d networks, got %d",
			len(data), len(netList))
	}

	for _, net := range netList {
		fmt.Printf("%s %s %s\n", net.Uuid, net.Name, net.Subnets)
		if data[net.Name].Subnet != net.Subnets[0] {
			t.Errorf("%s subnet %s, expected %s", net.Name,
				net.Subnets, data[net.Name].Subnet)
		}
		data[net.Name].InList = true
	}
	for name, value := range data {
		if !value.InList {
			t.Errorf("%s not present", name)
		}
	}
}
func (m *NetworkManagerImpl) initializePublicNetwork() {
	var network *types.VirtualNetwork
	obj, err := m.client.FindByName("virtual-network", m.config.PublicNetwork)
	if err != nil {
		fqn := strings.Split(m.config.PublicNetwork, ":")
		parent := strings.Join(fqn[0:len(fqn)-1], ":")
		projectId, err := m.client.UuidByName("project", parent)
		if err != nil {
			glog.Fatalf("%s: %v", parent, err)
		}
		var networkId string
		networkName := fqn[len(fqn)-1]
		if len(m.config.PublicSubnet) > 0 {
			networkId, err = config.CreateNetworkWithSubnet(
				m.client, projectId, networkName, m.config.PublicSubnet)
		} else {
			networkId, err = config.CreateNetwork(m.client, projectId, networkName)
		}
		if err != nil {
			glog.Fatalf("%s: %v", parent, err)
		}

		glog.Infof("Created network %s", m.config.PublicNetwork)

		obj, err := m.client.FindByUuid("virtual-network", networkId)
		if err != nil {
			glog.Fatalf("GET %s %v", networkId, err)
		}
		network = obj.(*types.VirtualNetwork)
	} else {
		network = obj.(*types.VirtualNetwork)
	}

	m.publicNetwork = network

	// TODO(prm): Ensure that the subnet is as specified.
	if len(m.config.PublicSubnet) > 0 {
		m.LocateFloatingIpPool(network, m.config.PublicSubnet)
	}
}
Beispiel #6
0
func networkCreate(client *contrail.Client, flagSet *flag.FlagSet) {
	if flagSet.NArg() < 1 {
		flagSet.Usage()
		os.Exit(2)
	}

	name := flagSet.Args()[0]

	parent_id, err := config.GetProjectId(
		client, networkCommonOpts.project, networkCommonOpts.project_id)
	if err != nil {
		fmt.Fprint(os.Stderr, err)
		os.Exit(1)
	}

	if len(networkCreateOpts.subnet) > 0 {
		config.CreateNetworkWithSubnet(client, parent_id, name,
			networkCreateOpts.subnet)
	} else {
		config.CreateNetwork(client, parent_id, name)
	}
}