Exemple #1
0
func CreateNetworkWithSubnet(
	client contrail.ApiClient, project_id, name, prefix string) (
	string, error) {

	expr := regexp.MustCompile(`(([0-9]{1,3}\.){3}[0-9]{1,3})/([0-9]{1,2})`)
	match := expr.FindStringSubmatch(prefix)
	if match == nil {
		return "", fmt.Errorf("Invalid subnet prefix %s", prefix)
	}
	address := match[1]
	prefixlen, _ := strconv.Atoi(match[3])

	obj, err := client.FindByUuid("project", project_id)
	if err != nil {
		return "", err
	}

	project := obj.(*types.Project)
	refList, err := project.GetNetworkIpams()
	if err != nil {
		return "", err
	}

	var ipam *types.NetworkIpam
	if len(refList) > 0 {
		obj, err := client.FindByUuid("network-ipam", refList[0].Uuid)
		if err != nil {
			return "", err
		}
		ipam = obj.(*types.NetworkIpam)
	} else {
		obj, err := client.FindByName("network-ipam",
			"default-domain:default-project:default-network-ipam")
		if err != nil {
			return "", err
		}
		ipam = obj.(*types.NetworkIpam)
	}

	net := new(types.VirtualNetwork)
	net.SetParent(project)
	net.SetName(name)

	subnets := types.VnSubnetsType{}
	subnets.AddIpamSubnets(
		&types.IpamSubnetType{
			Subnet: &types.SubnetType{address, prefixlen}})
	net.AddNetworkIpam(ipam, subnets)
	err = client.Create(net)
	if err != nil {
		return "", err
	}
	return net.GetUuid(), nil
}
Exemple #2
0
func CreateNetwork(client contrail.ApiClient, project_id, name string) (
	string, error) {

	obj, err := client.FindByUuid("project", project_id)
	if err != nil {
		return "", err
	}

	project := obj.(*types.Project)

	net := new(types.VirtualNetwork)
	net.SetParent(project)
	net.SetName(name)

	err = client.Create(net)
	if err != nil {
		return "", err
	}
	return net.GetUuid(), nil
}