func makeAddresses(optAddress, optNetwork string) []types.AddressType {
	addresses := make([]types.AddressType, 1)
	address := &addresses[0]
	if len(optAddress) > 0 {
		comp := strings.Split(optAddress, "/")
		if len(comp) != 2 {
			fmt.Fprintf(os.Stderr,
				"Expected IP prefix in the format n.n.n.n/n"+
					", got %s\n", optAddress)
			os.Exit(2)
		}
		matched, _ := regexp.MatchString(config.IpAddressPattern,
			comp[0])
		if !matched {
			fmt.Fprintf(os.Stderr, "Invalid IP address: %s\n",
				comp[0])
			os.Exit(2)
		}
		address.Subnet.IpPrefix = comp[0]
		var err error
		address.Subnet.IpPrefixLen, err = strconv.Atoi(comp[1])
		if err != nil {
			fmt.Fprintf(os.Stderr, "Invalid prefix length")
			os.Exit(2)
		}
	}
	if len(optNetwork) > 0 {
		address.VirtualNetwork = optNetwork
	} else {
		address.VirtualNetwork = "any"
	}
	return addresses
}
Parse command line arguments and set the Address field in the rule.
package main
import (
	"flag"
	"fmt"
	"net"
	"os"
	"regexp"
	"strconv"
	"strings"
	"text/tabwriter"
	"text/template"
	"github.com/Juniper/contrail-go-api"
	"github.com/Juniper/contrail-go-api/config"
	"github.com/Juniper/contrail-go-api/types"
	"github.com/pborman/uuid"
)
type policyCommonOptions struct {
	project   string
	projectId string
}
type policyOpOptions struct {
	policy string
}
type policyListOptions struct {
	allTenants bool
	detail     bool
}
type policyAttachOptions struct {
	policy  string
	network string
}
type policyProtocolValue string
type policyPortValue int
var protocolValues = map[string]int{
	"tcp":  6,
	"udp":  17,
	"icmp": 1,
	"any":  0,
}
type policyRuleOptions struct {
	ruleId       string
	afterRule    string
	policy       string
	srcIpAddress string
	srcNetwork   string
	dstIpAddress string
	dstNetwork   string
	protocol     policyProtocolValue
	srcPort      policyPortValue
	dstPort policyPortValue
	actionDrop bool
	actionPass bool
}
var (
	policyCommonOpts policyCommonOptions
	policyOpOpts     policyOpOptions
	policyListOpts   policyListOptions
	policyRuleOpts   policyRuleOptions
	policyAttachOpts policyAttachOptions
)
const policyShowTmpl = `
{{define "AddressList"}}{{range .}}{{if .Subnet.IpPrefixLen}}{{.Subnet.IpPrefix}}/{{.Subnet.IpPrefixLen}}{{end}}{{.VirtualNetwork}}{{.SecurityGroup}}{{.NetworkPolicy}}{{end}}{{end}}
{{define "PortList"}}{{range .}}[{{.StartPort}}, {{.EndPort}}]{{end}}{{end}}
{{define "ActionList"}}{{.SimpleAction}}{{if .MirrorTo.AnalyzerName}}Analyzer({{.MirrorTo.AnalyzerName}}){{end}}{{end}}
`
const policyShowDetail = `  Policy: {{.GetName}}
    Uuid: {{.GetUuid}}{{range .GetNetworkPolicyEntries.PolicyRule}}
        Rule: {{.RuleUuid}}
            Source: {{template "AddressList" .SrcAddresses}}
            Destination: {{template "AddressList" .DstAddresses}}
            Protocol: {{.Protocol}}
            Source Ports: {{template "PortList" .SrcPorts}}
            Destination Ports: {{template "PortList" .DstPorts}}
	    Action: {{template "ActionList" .ActionList}}{{end}}
    Attached Networks:{{range .GetVirtualNetworkBackRefs}}
        {{join .To ":"}}{{end}}
`
func getRulesNetworks(policy *types.NetworkPolicy) (string, string) {
	displayValue := func(m map[string]bool) string {
		if len(m) > 1 {
			return "<multiple>"
		}
		for key, _ := range m {
			fqn := strings.Split(key, ":")
			return fqn[len(fqn)-1]
		}
		return "none"
	}
	sourceMap := make(map[string]bool, 0)
	destMap := make(map[string]bool, 0)
	for _, rule := range policy.GetNetworkPolicyEntries().PolicyRule {
		if len(rule.SrcAddresses) > 0 &&
			len(rule.SrcAddresses[0].VirtualNetwork) > 0 {
			sourceMap[rule.SrcAddresses[0].VirtualNetwork] = true
		}
		if len(rule.DstAddresses) > 0 &&
			len(rule.DstAddresses[0].VirtualNetwork) > 0 {
			destMap[rule.DstAddresses[0].VirtualNetwork] = true
		}
	}
	source := displayValue(sourceMap)
	destination := displayValue(destMap)
	return source, destination
}
func policyListTerse(client *contrail.Client, projectId string) {
	poList, err := client.ListDetailByParent(
		"network-policy", projectId, nil)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(2)
	}
	writer := new(tabwriter.Writer)
	writer.Init(os.Stdout, 0, 0, 1, ' ', 0)
	fmt.Fprintln(writer, "Uuid\tPolicy\tSource\tDestination")
	for _, obj := range poList {
		policy := obj.(*types.NetworkPolicy)
		source, destination := getRulesNetworks(policy)
		fmt.Fprintf(writer, "%s\t%s\t%s\t%s\n",
			policy.GetUuid(), policy.GetName(), source, destination)
	}
	writer.Flush()
}
func makeShowTemplate() *template.Template {
	fm := template.FuncMap{
		"join": strings.Join,
	}
	tmpl := template.Must(template.New("policy-list").Funcs(fm).Parse(
		policyShowTmpl))
	tmpl.Parse(policyShowDetail)
	return tmpl
}
func policyListDetail(client *contrail.Client, projectId string) {
	poList, err := client.ListDetailByParent(
		"network-policy", projectId,
		[]string{"virtual_network_back_refs"})
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	tmpl := makeShowTemplate()
	for _, policy := range poList {
		tmpl.Execute(os.Stdout, policy)
	}
}
func policyList(client *contrail.Client, flagSet *flag.FlagSet) {
	var projectId string
	if !policyListOpts.allTenants {
		var err error
		projectId, err = config.GetProjectId(
			client, policyCommonOpts.project,
			policyCommonOpts.projectId)
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
			os.Exit(1)
		}
	}
	if policyListOpts.detail {
		policyListDetail(client, projectId)
	} else {
		policyListTerse(client, projectId)
	}
}
func policyShow(client *contrail.Client, flagSet *flag.FlagSet) {
	policy, err := getPolicyObject(
		client,
		policyCommonOpts.project,
		policyCommonOpts.projectId,
		policyOpOpts.policy)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	tmpl := makeShowTemplate()
	tmpl.Execute(os.Stdout, policy)
}
func policyCreate(client *contrail.Client, flagSet *flag.FlagSet) {
	if flagSet.NArg() < 1 {
		flagSet.Usage()
		os.Exit(2)
	}
	name := flagSet.Args()[0]
	projectFQN, err := config.GetProjectFQN(client,
		policyCommonOpts.project, policyCommonOpts.projectId)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	policy := new(types.NetworkPolicy)
	policy.SetFQName("project", append(projectFQN, name))
	err = client.Create(policy)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}
func policyDelete(client *contrail.Client, flagSet *flag.FlagSet) {
	if len(policyOpOpts.policy) == 0 {
		fmt.Fprintf(os.Stderr, "policy name or uuid must be specified")
		os.Exit(2)
	}
	uuid, err := getPolicyId(client, policyCommonOpts.project,
		policyCommonOpts.projectId, policyOpOpts.policy)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	err = client.DeleteByUuid("network-policy", uuid)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}
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
}
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
}