示例#1
0
func Delete(args ...string) (string, error) {
	// Format: <entity> <ID>
	if len(args) != 2 {
		return "Format:\n    DELETE <entity> <ID>", nil
	}
	entity := args[0]
	id := args[1]
	switch entity {
	case "enterprise": // DELETE enterprise <ID>
		org := new(nuage_v3_2.Enterprise)
		org.ID = id
		err := org.Delete(myconn)
		if err != nil {
			return "", err
		}
		return "", err
	case "domaintemplate": // DELETE domaintemplate <ID>
		dt := new(nuage_v3_2.Domaintemplate)
		dt.ID = id
		err := dt.Delete(myconn)
		if err != nil {
			return "", err
		}
		return "", err
	case "domain": // DELETE domain <ID>
		domain := new(nuage_v3_2.Domain)
		domain.ID = id
		err := domain.Delete(myconn)
		if err != nil {
			return "", err
		}
		return "", err
	case "zonetemplate": // DELETE zonetemplate <ID>
		zt := new(nuage_v3_2.Zonetemplate)
		zt.ID = id
		err := zt.Delete(myconn)
		if err != nil {
			return "", err
		}
		return "", err

	case "zone": // DELETE zone <ID>
		zone := new(nuage_v3_2.Zone)
		zone.ID = id
		err := zone.Delete(myconn)
		if err != nil {
			return "", err
		}
		return "", err

	case "subnet": // DELETE subnet <ID>
		subnet := new(nuage_v3_2.Subnet)
		subnet.ID = id
		err := subnet.Delete(myconn)
		if err != nil {
			return "", err
		}
		return "", err

	case "vport": // DELETE vport <ID>
		var vp nuage_v3_2.VPort
		vp.ID = id
		err := (&vp).Delete(myconn)
		if err != nil {
			return "", err
		}
		return "", err

	case "vminterface": // DELETE vminterface <ID>
		var vmi nuage_v3_2.VMInterface
		vmi.ID = id
		err := (&vmi).Delete(myconn)
		if err != nil {
			return "", err
		}
		return "", err

	case "vm": // DELETE vm <ID>
		var vm nuage_v3_2.VirtualMachine
		vm.ID = id
		err := (&vm).Delete(myconn)
		if err != nil {
			return "", err
		}
		return "", err

	default:
		return "Don't know how to DELETE entity: " + entity, nil
	}
	return "", nil
}
示例#2
0
func Get(args ...string) (string, error) {

	// 1 argument:  <entity>
	// 2 arguments: <entity> <ID>
	// 3 arguments: <entity> <ID> <children>

	if len(args) < 1 || len(args) > 3 {
		return "GET <entity> [ <ID> [ <children> ] ]  ", nil
	}

	entity := args[0]

	switch entity {
	case "enterprises":
		switch len(args) {
		case 1: // GET enterprises
			var orglist nuage_v3_2.EnterpriseSlice
			err := orglist.List(myconn)

			if err != nil {
				return "", err
			}

			// Yuck -- We need to to this hacky type cast since we can iterate through "[]nuage_v3_2.Enterprise"  but not "nuage_v3_2.EnterpriseSlice"
			var orgs []nuage_v3_2.Enterprise
			orgs = orglist

			// Iterate through the list of org's and JSON pretty-print them
			for i, v := range orgs {
				org, _ := json.MarshalIndent(v, "", "\t")
				fmt.Printf("\n\n ===> Org nr [%d]: Name [%s] <=== \n%#s\n", i, orgs[i].Name, string(org))
			}

			return "Enterprise list -- done", err

		case 2: // GET enterprises <ID>

			org := new(nuage_v3_2.Enterprise)
			org.ID = args[1]
			err := org.Get(myconn)
			if err != nil {
				return "", err
			}

			// JSON pretty-print the org
			jsonorg, _ := json.MarshalIndent(org, "", "\t")
			fmt.Printf("\n\n ===> Org: Name [%s] <=== \n%#s\n", org.Name, string(jsonorg))

			return "Enterprise Get ID -- done", err

		case 3:
			entityid := args[1]
			child := args[2]

			switch child {
			case "domaintemplates": // GET enterprises <ID> domaintemplates

				// Get list of domain templates for that org
				var dts nuage_v3_2.Domaintemplateslice
				err := dts.List(myconn, entityid)
				if err != nil {
					return "", err
				}
				// Yucky -- type cast from nuage_v3_2.Domaintemplateslice to []nuage_v3_2.Domaintemplate
				var dtl []nuage_v3_2.Domaintemplate
				dtl = dts
				// Iterate through the list of domain templates and JSON pretty-print them
				fmt.Printf("\n ######## Domain templates for Enterprise ID: [%s] ########\n", entityid)
				for i, v := range dtl {
					dt, _ := json.MarshalIndent(v, "", "\t")
					fmt.Printf("\n ===> Domain template nr [%d]: Name [%s] <=== \n%#s\n", i, dtl[i].Name, string(dt))
				}

				return "Domain template list -- done", err

			case "domains": // GET enterprises <ID> domains
				// Get list of domains for the Enterprise ID

				var ds nuage_v3_2.Domainslice
				err := ds.List(myconn, entityid)
				if err != nil {
					return "", err
				}
				// Yucky -- type cast from nuage_v3_2.Domainslice to []nuage_v3_2.Domain
				var dl []nuage_v3_2.Domain
				dl = ds
				// Iterate through the list of domains and JSON pretty-print them
				fmt.Printf("\n ######## Domains for Enterprise ID: [%s] ########\n", entityid)
				for i, v := range dl {
					domain, _ := json.MarshalIndent(v, "", "\t")
					fmt.Printf("\n ===> Domain nr [%d]: Name [%s] <=== \n%#s\n", i, dl[i].Name, string(domain))
				}

				return "Domain list -- done", err
			}
		}
	case "domaintemplates":
		switch len(args) {
		case 2: // GET domaintemplates <ID>
			dt := new(nuage_v3_2.Domaintemplate)
			dt.ID = args[1]
			err := dt.Get(myconn)

			if err != nil {
				return "", err
			}
			// JSON pretty-print the domain template
			jsondt, _ := json.MarshalIndent(dt, "", "\t")
			fmt.Printf("\n ===> Domain Template: Name [%s] <=== \n%#s\n", dt.Name, string(jsondt))
			return "Domain Template Get -- done", err
		case 3:
			dtid := args[1]
			child := args[2]
			switch child {
			case "zonetemplates": // GET domaintemplates <ID> zonetemplates
				var zts nuage_v3_2.Zonetemplateslice
				err := zts.List(myconn, dtid)
				if err != nil {
					return "", err
				}
				// Yucky -- type cast from nuage_v3_2.Zonetemplateslice to []nuage_v3_2.Zonetemplate
				var zta []nuage_v3_2.Zonetemplate
				zta = zts
				// Iterate through the list of zone templates and JSON pretty-print them
				fmt.Printf("\n ######## Zone templates for Domain template ID: [%s] ########\n", dtid)
				for i, v := range zta {
					zt, _ := json.MarshalIndent(v, "", "\t")
					fmt.Printf("\n ===> Zone template nr [%d]: Name [%s] <=== \n%#s\n", i, zta[i].Name, string(zt))
				}

				return "Zone template list -- done", err
			}

		}
	case "domains":
		switch len(args) {
		case 1: // GET domains
			// Get list of domains with "nil" as parent enterprise -- i.e. global list of all domains
			var ds nuage_v3_2.Domainslice
			err := ds.List(myconn, "")
			if err != nil {
				return "", err
			}
			// Yucky -- type cast from nuage_v3_2.Domainslice to []nuage_v3_2.Domain
			var dl []nuage_v3_2.Domain
			dl = ds
			for i, v := range dl {
				jsondomain, _ := json.MarshalIndent(v, "", "\t")
				fmt.Printf("\n ===> Domain nr [%d]: Name [%s] <=== \n%#s\n", i, dl[i].Name, string(jsondomain))
			}
			return "Domain list -- done", err
		case 2: // GET domains <ID>
			// Get a specific Domain ID
			domain := new(nuage_v3_2.Domain)
			domain.ID = args[1]
			err := domain.Get(myconn)
			if err != nil {
				return "", err
			}
			jsondomain, _ := json.MarshalIndent(domain, "", "\t")
			fmt.Printf("\n ===> Domain Name [%s] <=== \n%#s\n", domain.Name, string(jsondomain))
			return "Domain Get -- done", err
		case 3:
			switch args[2] {
			case "vports": // GET domains <ID> vports
				var vports []nuage_v3_2.VPort
				var err error
				domain := new(nuage_v3_2.Domain)
				domain.ID = args[1]
				vports, err = domain.VPortsList(myconn)
				if err != nil {
					return "", err
				}
				for i, v := range vports {
					jsonvport, _ := json.MarshalIndent(v, "", "\t")
					fmt.Printf("\n ===> VPort nr [%d]: Name [%s] <=== \n%#s\n", i, vports[i].Name, string(jsonvport))
				}
				return "Domain VPorts list -- done", err

			case "vminterfaces": // GET domains <ID> vminterfaces
				// var vmis []nuage_v3_2.VMInterface
				// var err error
				var domain nuage_v3_2.Domain
				domain.ID = args[1]
				vmis, err := (&domain).VMInterfacesList(myconn)
				if err != nil {
					return "", err
				}
				for i, v := range vmis {
					jsonvmi, _ := json.MarshalIndent(v, "", "\t")
					fmt.Printf("\n ===> VMInterface nr [%d]: Name [%s] <=== \n%#s\n", i, vmis[i].Name, string(jsonvmi))
				}
				return "Subnet VMInterfaces list -- done", err

			}
		}
	case "zonetemplates":
		if len(args) < 2 {
			return "Format:\n    GET zonetemplates <ID>", nil
		}

		switch len(args) {
		case 2: // GET zonetemplates <ID>
			zt := new(nuage_v3_2.Zonetemplate)
			zt.ID = args[1]
			err := zt.Get(myconn)

			if err != nil {
				return "", err
			}
			// JSON pretty-print the zone template
			jsonzt, _ := json.MarshalIndent(zt, "", "\t")
			fmt.Printf("\n ===> Zone Template: Name [%s] <=== \n%#s\n", zt.Name, string(jsonzt))
			return "Zone Template Get -- done", err
		}
	case "zones":
		switch len(args) {
		case 1: // GET zones
			// Get list of zones with "nil" as parent domain -- i.e. global list of all zones
			var zs nuage_v3_2.Zoneslice
			err := zs.List(myconn, "")
			if err != nil {
				return "", err
			}
			// Yucky -- type cast from nuage_v3_2.Zoneslice to []nuage_v3_2.Zone
			var zl []nuage_v3_2.Zone
			zl = zs
			for i, v := range zl {
				jsonzone, _ := json.MarshalIndent(v, "", "\t")
				fmt.Printf("\n ===> Zone nr [%d]: Name [%s] <=== \n%#s\n", i, zl[i].Name, string(jsonzone))
			}
			return "Zone list -- done", err
		case 2: // GET zones <ID>
			// Get a specific Zone ID
			zone := new(nuage_v3_2.Zone)
			zone.ID = args[1]
			err := zone.Get(myconn)
			if err != nil {
				return "", err
			}
			jsonzone, _ := json.MarshalIndent(zone, "", "\t")
			fmt.Printf("\n ===> Zone Name [%s] <=== \n%#s\n", zone.Name, string(jsonzone))
			return "Zone Get -- done", err
		}

	case "subnets":
		switch len(args) {
		case 1: // GET subnets
			// Get list of subnets with "nil" as parent domain -- i.e. global list of all subnets
			var ss nuage_v3_2.Subnetslice
			err := ss.List(myconn, "")
			if err != nil {
				return "", err
			}
			// Yucky -- type cast from nuage_v3_2.Subnetslice to []nuage_v3_2.Subnet
			var za []nuage_v3_2.Subnet
			za = ss
			for i, v := range za {
				jsonsubnet, _ := json.MarshalIndent(v, "", "\t")
				fmt.Printf("\n ===> Subnet nr [%d]: Name [%s] <=== \n%#s\n", i, za[i].Name, string(jsonsubnet))
			}
			return "Subnet list -- done", err
		case 2: // GET subnets <ID>
			// Get a specific Subnet ID
			subnet := new(nuage_v3_2.Subnet)
			subnet.ID = args[1]
			err := subnet.Get(myconn)
			if err != nil {
				return "", err
			}
			jsonsubnet, _ := json.MarshalIndent(subnet, "", "\t")
			fmt.Printf("\n ===> Subnet Name [%s] <=== \n%#s\n", subnet.Name, string(jsonsubnet))
			return "Subnet Get -- done", err

		case 3:
			switch args[2] {
			case "vports": // GET subnets <ID> vports
				var vports []nuage_v3_2.VPort
				var err error
				subnet := new(nuage_v3_2.Subnet)
				subnet.ID = args[1]
				vports, err = subnet.VPortsList(myconn)
				if err != nil {
					return "", err
				}
				for i, v := range vports {
					jsonvport, _ := json.MarshalIndent(v, "", "\t")
					fmt.Printf("\n ===> VPort nr [%d]: Name [%s] <=== \n%#s\n", i, vports[i].Name, string(jsonvport))
				}
				return "Subnet VPorts list -- done", err
			case "vminterfaces": // GET subnets <ID> vminterfaces
				var vmis []nuage_v3_2.VMInterface
				var err error
				var subnet nuage_v3_2.Subnet
				subnet.ID = args[1]
				vmis, err = (&subnet).VMInterfacesList(myconn)
				if err != nil {
					return "", err
				}
				for i, v := range vmis {
					jsonvmi, _ := json.MarshalIndent(v, "", "\t")
					fmt.Printf("\n ===> VMInterface nr [%d]: Name [%s] <=== \n%#s\n", i, vmis[i].Name, string(jsonvmi))
				}
				return "Subnet VMInterfaces list -- done", err

			}
		}

	case "vports": // GET vports <ID>
		if len(args) != 2 {
			return "Format:\n    GET vports <ID>", nil
		}
		vport := new(nuage_v3_2.VPort)
		vport.ID = args[1]
		err := vport.Get(myconn)
		if err != nil {
			return "", err
		}
		jsonvport, _ := json.MarshalIndent(vport, "", "\t")
		fmt.Printf("\n ===> VPort Name [%s] <=== \n%#s\n", vport.Name, string(jsonvport))
		return "VPort Get -- done", err

	case "vminterfaces":
		switch len(args) {
		case 1: // GET vminterfaces
			var vmis nuage_v3_2.VMInterfaceslice
			err := vmis.List(myconn)
			if err != nil {
				return "", err
			}
			// Yucky -- type cast from nuage_v3_2.VMInterfaceslice to []nuage_v3_2.VMInterface
			var vmia []nuage_v3_2.VMInterface
			vmia = vmis
			for i, v := range vmia {
				jsonvminterface, _ := json.MarshalIndent(v, "", "\t")
				fmt.Printf("\n ===> VMInterface nr [%d]: Name [%s] <=== \n%#s\n", i, vmia[i].Name, string(jsonvminterface))
			}
			return "VMinterfaces list -- done", err

		case 2: // GET vminterfaces <ID>
			var vminterface nuage_v3_2.VMInterface
			vminterface.ID = args[1]
			err := (&vminterface).Get(myconn)
			if err != nil {
				return "", err
			}
			jsonvminterface, _ := json.MarshalIndent(vminterface, "", "\t")
			fmt.Printf("\n ===> VMinterface Name [%s] <=== \n%#s\n", vminterface.Name, string(jsonvminterface))
			return "VMinterface Get -- done", err
		}

	case "vms":
		switch len(args) {
		case 1: // GET vms
			var vms nuage_v3_2.VirtualMachineslice
			err := vms.List(myconn)
			if err != nil {
				return "", err
			}
			// Yucky -- type cast from nuage_v3_2.VirtualMachineslice to []nuage_v3_2.VirtualMachine
			var vma []nuage_v3_2.VirtualMachine
			vma = vms
			for i, v := range vma {
				jsonvm, _ := json.MarshalIndent(v, "", "\t")
				fmt.Printf("\n ===> VirtualMachine nr [%d]: Name [%s] <=== \n%#s\n", i, vma[i].Name, string(jsonvm))
			}
			return "VirtualMachine list -- done", err

		case 2: // GET vms <ID>
			var vm nuage_v3_2.VirtualMachine
			vm.ID = args[1]
			err := (&vm).Get(myconn)
			if err != nil {
				return "", err
			}
			jsonvm, _ := json.MarshalIndent(vm, "", "\t")
			fmt.Printf("\n ===> VirtualMachine Name [%s] <=== \n%#s\n", vm.Name, string(jsonvm))
			return "Virtual Machine Get -- done", err
		}

	default:
		// Unknown entity request
		break
	}
	return "Don't know how to process Nuage API entity: " + strings.Join(args, " "), nil
}