Beispiel #1
0
func policyAttachNetwork(client *contrail.Client) *types.VirtualNetwork {
	if len(policyAttachOpts.network) == 0 {
		fmt.Fprintf(os.Stderr, "network name or id must be specified")
		os.Exit(2)
	}
	uuid := policyAttachOpts.network
	if !config.IsUuid(uuid) {
		var err error
		uuid, err = getNetworkUuidByName(client,
			policyCommonOpts.project,
			policyCommonOpts.projectId,
			policyAttachOpts.network)
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
			os.Exit(1)
		}
	}

	obj, err := client.FindByUuid("virtual-network", uuid)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	return obj.(*types.VirtualNetwork)

}
Beispiel #2
0
// Locate the policy given project name/id and policy name/id.
func getPolicyObject(
	client *contrail.Client,
	projectName, projectId, policyNameOrId string) (
	*types.NetworkPolicy, error) {

	if len(policyNameOrId) == 0 {
		return nil, fmt.Errorf("policy name or uuid must be specified")
	}

	if uuid := strings.ToLower(policyNameOrId); config.IsUuid(uuid) {
		obj, err := client.FindByUuid("network-policy", uuid)
		if err != nil {
			return nil, err
		}
		return obj.(*types.NetworkPolicy), nil
	}

	fqn, err := config.GetProjectFQN(client, projectName, projectId)
	if err != nil {
		return nil, err
	}
	fqn = append(fqn, policyNameOrId)
	obj, err := client.FindByName("network-policy", strings.Join(fqn, ":"))
	if err != nil {
		return nil, err
	}
	return obj.(*types.NetworkPolicy), nil
}
Beispiel #3
0
func getNetworkUuidByName(
	client *contrail.Client, project_name, project_id, name string) (
	string, error) {
	var fqn []string
	if len(project_id) > 0 {
		uuid := strings.ToLower(project_id)
		if !config.IsUuid(uuid) {
			fmt.Fprintf(os.Stderr,
				"Invalid project-id value: %s\n", uuid)
			os.Exit(2)
		}
		obj, err := client.FindByUuid("project", project_id)
		if err != nil {
			fmt.Fprint(os.Stderr, err)
			os.Exit(2)
		}
		fqn = obj.GetFQName()
	} else {
		fqn = strings.Split(project_name, ":")
		if len(fqn) == 1 {
			obj := new(types.Project)
			fqn = append(obj.GetDefaultParent(), project_name)
		}
	}
	fqn = append(fqn, name)
	return client.UuidByName("virtual-network", strings.Join(fqn, ":"))
}
Beispiel #4
0
func getPolicyId(client *contrail.Client,
	projectName, projectId, policyNameOrId string) (
	string, error) {

	uuid := strings.ToLower(policyNameOrId)
	if !config.IsUuid(uuid) {
		fqn, err := config.GetProjectFQN(client, projectName, projectId)
		if err != nil {
			return "", err
		}
		fqn = append(fqn, policyNameOrId)
		uuid, err = client.UuidByName("network-policy",
			strings.Join(fqn, ":"))
		if err != nil {
			return "", err
		}
	}
	return uuid, nil
}
Beispiel #5
0
func networkDelete(client *contrail.Client, flagSet *flag.FlagSet) {
	if flagSet.NArg() < 1 {
		flagSet.Usage()
		os.Exit(2)
	}
	nameOrId := flagSet.Args()[0]

	var uuid string
	if config.IsUuid(nameOrId) {
		uuid = nameOrId
	} else {
		var err error
		uuid, err = getNetworkUuidByName(client,
			networkCommonOpts.project,
			networkCommonOpts.project_id, nameOrId)
		if err != nil {
			fmt.Fprint(os.Stderr, err)
			os.Exit(1)
		}
	}
	// TODO: purge requires deleting all children before the object
	client.DeleteByUuid("virtual-network", uuid)
}
Beispiel #6
0
func networkShow(client *contrail.Client, flagSet *flag.FlagSet) {
	if flagSet.NArg() < 1 {
		flagSet.Usage()
		os.Exit(2)
	}
	nameOrId := flagSet.Args()[0]

	var uuid string
	if config.IsUuid(nameOrId) {
		uuid = nameOrId
	} else {
		var err error
		uuid, err = getNetworkUuidByName(client,
			networkCommonOpts.project,
			networkCommonOpts.project_id, nameOrId)
		if err != nil {
			fmt.Fprint(os.Stderr, err)
			os.Exit(1)
		}
	}

	info, err := config.NetworkShow(client, uuid, networkShowOpts.detail)
	if err != nil {
		fmt.Fprint(os.Stderr, err)
		os.Exit(1)
	}

	var tmpl string
	if networkShowOpts.detail {
		tmpl = networkShowDetail
	} else {
		tmpl = networkShowBrief
	}

	t := template.Must(template.New("network-show").Parse(tmpl))
	t.Execute(os.Stdout, info)
}