示例#1
0
func cmdGetOrgVdcNetwork(cmd *cobra.Command, args []string) {
	initConfig(cmd, "goair_compute", true, map[string]FlagValue{
		"vdchref":        {vdchref, true, false, ""},
		"vdcnetworkname": {vdcnetworkname, false, false, ""},
	})

	client, err := authenticate(false)
	if err != nil {
		log.Fatalf("failed authenticating: %s", err)
	}

	err = authenticatecompute(client, false, "")
	if err != nil {
		log.Fatalf("Error authenticating compute: %s", err)
	}

	vdcuri, err := url.Parse(viper.GetString("vdchref"))
	if err != nil {
		log.Fatal(err)
	}

	client.VCDVDCHREF = *vdcuri

	vdc := govcloudair.NewVdc(client)
	vdc.Vdc = &types.Vdc{HREF: client.VCDVDCHREF.String()}
	err = vdc.Refresh()
	if err != nil {
		log.Fatalf("err refreshing vdc: %v", err)
	}

	if viper.GetString("vdcnetworkname") != "" {
		VdcNetwork, err := vdc.FindVDCNetwork(viper.GetString("vdcnetworkname"))
		if err != nil {
			log.Fatalf("error finding Vdc network: %v", err)
		}

		yamlOutput, err := yaml.Marshal(&VdcNetwork)
		if err != nil {
			log.Fatalf("error marshaling: %s", err)
		}
		fmt.Println(string(yamlOutput))
		return
	}

	VdcNetworks, err := vdc.GetVDCNetwork()
	if err != nil {
		log.Fatalf("error finding Vdc network: %v", err)
	}

	yamlOutput, err := yaml.Marshal(&VdcNetworks)
	if err != nil {
		log.Fatalf("error marshaling: %s", err)
	}
	fmt.Println(string(yamlOutput))
	return

}
示例#2
0
func cmdRemovePublicIPEdgeGateway(cmd *cobra.Command, args []string) {
	initConfig(cmd, "goair_compute", true, map[string]FlagValue{
		"vdchref":     {vdchref, true, false, ""},
		"publicip":    {publicip, true, false, ""},
		"networkname": {networkname, true, false, ""},
		"runasync":    {runasync, false, false, ""},
	})

	client, err := authenticate(false)
	if err != nil {
		log.Fatalf("failed authenticating: %s", err)
	}

	err = authenticatecompute(client, false, "")
	if err != nil {
		log.Fatalf("Error authenticating compute: %s", err)
	}

	vdcuri, err := url.Parse(viper.GetString("vdchref"))
	if err != nil {
		log.Fatal(err)
	}

	client.VCDVDCHREF = *vdcuri

	vdc := govcloudair.NewVdc(client)
	vdc.Vdc = &types.Vdc{HREF: client.VCDVDCHREF.String()}
	err = vdc.Refresh()
	if err != nil {
		log.Fatalf("err refreshing vdc: %v", err)
	}

	edgeGateway, err := vdc.FindEdgeGateway("")
	if err != nil {
		log.Fatalf("err getting edge gateway: %v", err)
	}

	task, err := edgeGateway.RemovePublicIP(networkname, publicip)
	if err != nil {
		log.Fatalf("err removing public ip: %v", err)
	}

	//	fmt.Printf("%+v", externalIPAddressActionList)

	if viper.GetString("runasync") == "true" {
		yamlOutput, err := yaml.Marshal(&task)
		if err != nil {
			log.Fatalf("error marshaling: %s", err)
		}
		fmt.Println(string(yamlOutput))
		return
	}

}
示例#3
0
文件: vapp.go 项目: nmcclain/goair
func cmdGetStatusVApp(cmd *cobra.Command, args []string) {
	initConfig(cmd, "goair_compute", true, map[string]FlagValue{
		"vdchref":  {vdchref, true, false, ""},
		"vappid":   {vappid, true, false, ""},
		"vappname": {vappname, true, false, "vappid"},
	})

	client, err := authenticate(false)
	if err != nil {
		log.Fatalf("failed authenticating: %s", err)
	}

	err = authenticatecompute(client, false, "")
	if err != nil {
		log.Fatalf("Error authenticating compute: %s", err)
	}

	vdcuri, err := url.Parse(viper.GetString("vdchref"))
	if err != nil {
		log.Fatal(err)
	}

	client.VCDVDCHREF = *vdcuri

	vdc := govcloudair.NewVdc(client)
	vdc.Vdc = &types.Vdc{HREF: client.VCDVDCHREF.String()}

	vapp := *govcloudair.NewVApp(client)
	if viper.GetString("vappname") != "" {
		vapp, err = vdc.FindVAppByName(viper.GetString("vappname"))
		if err != nil {
			log.Fatal(err)
		}
	}

	if viper.GetString("vappid") != "" {
		vapp, err = vdc.FindVAppByID(viper.GetString("vappid"))
		if err != nil {
			log.Fatal(err)
		}
	}

	if vapp.VApp == nil {
		log.Fatalf("Couldn't find VApp")
	}

	vappStatus, err := vapp.GetStatus()
	if err != nil {
		log.Fatalf("error getting VApp status: %v", err)
	}

	fmt.Println(vappStatus)
}
示例#4
0
文件: media.go 项目: nmcclain/goair
func cmdGetMedia(cmd *cobra.Command, args []string) {
	initConfig(cmd, "goair_compute", true, map[string]FlagValue{
		"vdchref": {vdchref, true, false, ""},
	})

	client, err := authenticate(false)
	if err != nil {
		log.Fatalf("failed authenticating: %s", err)
	}

	err = authenticatecompute(client, false, "")
	if err != nil {
		log.Fatalf("Error authenticating compute: %s", err)
	}

	vdcuri, err := url.Parse(viper.GetString("vdchref"))
	if err != nil {
		log.Fatal(err)
	}

	client.VCDVDCHREF = *vdcuri

	vdc := govcloudair.NewVdc(client)
	vdc.Vdc = &types.Vdc{HREF: client.VCDVDCHREF.String()}
	err = vdc.Refresh()
	if err != nil {
		log.Fatalf("err refreshing vdc: %v", err)
	}

	media, err := vdc.GetMedia()
	if err != nil {
		log.Fatalf("Error problem getting media from vdc: %v", err)
	}

	yamlOutput, err := yaml.Marshal(&media)
	if err != nil {
		log.Fatalf("error marshaling: %s", err)
	}
	fmt.Println(string(yamlOutput))
	return

}
示例#5
0
func cmdRemoveFirewallEdgeGateway(cmd *cobra.Command, args []string) {
	initConfig(cmd, "goair_compute", true, map[string]FlagValue{
		"vdchref":  {vdchref, true, false, ""},
		"ruleid":   {ruleid, true, false, ""},
		"runasync": {runasync, false, false, ""},
	})

	if len(args) > 0 && args[0] != "1to1" {
		log.Fatalf("Missing type of NAT, currently 1to1 is supported.")
	}

	client, err := authenticate(false)
	if err != nil {
		log.Fatalf("failed authenticating: %s", err)
	}

	err = authenticatecompute(client, false, "")
	if err != nil {
		log.Fatalf("Error authenticating compute: %s", err)
	}

	vdcuri, err := url.Parse(viper.GetString("vdchref"))
	if err != nil {
		log.Fatal(err)
	}

	client.VCDVDCHREF = *vdcuri

	vdc := govcloudair.NewVdc(client)
	vdc.Vdc = &types.Vdc{HREF: client.VCDVDCHREF.String()}
	err = vdc.Refresh()
	if err != nil {
		log.Fatalf("err refreshing vdc: %v", err)
	}

	edgeGateway, err := vdc.FindEdgeGateway("")
	if err != nil {
		log.Fatalf("err getting edge gateway: %v", err)
	}

	newedgeconfig := edgeGateway.EdgeGateway.Configuration.EdgeGatewayServiceConfiguration

	var newFirewallRules []*types.FirewallRule
	for _, firewallRule := range newedgeconfig.FirewallService.FirewallRule {
		if firewallRule.ID != ruleid {
			newFirewallRules = append(newFirewallRules, firewallRule)
		}
	}

	newedgeconfig.FirewallService.FirewallRule = newFirewallRules

	task, err := edgeGateway.UpdateFirewall(newedgeconfig)
	if err != nil {
		log.Fatalf("error updating firewall: %v", err)
	}

	if viper.GetString("runasync") == "true" {
		yamlOutput, err := yaml.Marshal(&task)
		if err != nil {
			log.Fatalf("error marshaling: %s", err)
		}
		fmt.Println(string(yamlOutput))
		return
	}

	err = task.WaitTaskCompletion()
	if err != nil {
		log.Fatalf("error waiting for task to complete: %v", err)
	}
}
示例#6
0
func cmdNewFirewallEdgeGateway(cmd *cobra.Command, args []string) {
	initConfig(cmd, "goair_compute", true, map[string]FlagValue{
		"vdchref":         {vdchref, true, false, ""},
		"sourceip":        {sourceip, true, false, ""},
		"sourceport":      {sourceport, false, false, ""},
		"destinationip":   {destinationip, true, false, ""},
		"destinationport": {destinationport, false, false, ""},
		"description":     {description, true, false, ""},
		"protocol":        {protocol, false, false, ""},
		"runasync":        {runasync, false, false, ""},
	})

	if len(args) > 0 && args[0] != "1to1" {
		log.Fatalf("Missing type of NAT, currently 1to1 is supported.")
	}

	client, err := authenticate(false)
	if err != nil {
		log.Fatalf("failed authenticating: %s", err)
	}

	err = authenticatecompute(client, false, "")
	if err != nil {
		log.Fatalf("Error authenticating compute: %s", err)
	}

	vdcuri, err := url.Parse(viper.GetString("vdchref"))
	if err != nil {
		log.Fatal(err)
	}

	client.VCDVDCHREF = *vdcuri

	vdc := govcloudair.NewVdc(client)
	vdc.Vdc = &types.Vdc{HREF: client.VCDVDCHREF.String()}
	err = vdc.Refresh()
	if err != nil {
		log.Fatalf("err refreshing vdc: %v", err)
	}

	edgeGateway, err := vdc.FindEdgeGateway("")
	if err != nil {
		log.Fatalf("err getting edge gateway: %v", err)
	}

	//sourceip sourceport destinationip destinationport
	//tcp:1000,1001,1002-1004
	//DestinationPortRange "Any"
	//Protocols.Tcp = true

	var icmp bool
	var tcp bool
	var udp bool
	var any bool
	switch protocol {
	case "icmp":
		icmp = true
	case "tcp":
		tcp = true
	case "udp":
		udp = true
	case "any":
		any = true
	default:
		log.Fatalf("error: protocol is invalid, must be icmp|tcp|udp|any")
	}

	newedgeconfig := edgeGateway.EdgeGateway.Configuration.EdgeGatewayServiceConfiguration

	var destinationPortRange string
	var destinationPort int
	destinationPorts := strings.Split(destinationport, ",")
	for _, arg := range destinationPorts {
		matched, err := regexp.MatchString("-", arg)
		if err != nil {
			log.Fatalf("err matching: %v", err)
		}
		if matched || arg == "Any" {
			destinationPortRange = arg
			destinationPort = -1
		} else {
			destinationPortRange = ""
			spi, err := strconv.Atoi(arg)
			if err != nil {
				log.Fatalf("problem converting port to integer: %v", err)
			}
			destinationPort = spi
		}

		fwin := &types.FirewallRule{
			Description: description,
			IsEnabled:   true,
			Policy:      "allow",
			Protocols: &types.FirewallRuleProtocols{
				Tcp:  tcp,
				Udp:  udp,
				Icmp: icmp,
				Any:  any,
			},
			DestinationPortRange: destinationPortRange,
			Port:                 destinationPort,
			DestinationIP:        destinationip,
			SourcePortRange:      sourceport,
			SourceIP:             sourceip,
			EnableLogging:        false,
		}

		newedgeconfig.FirewallService.FirewallRule = append(newedgeconfig.FirewallService.FirewallRule, fwin)

	}

	task, err := edgeGateway.UpdateFirewall(newedgeconfig)
	if err != nil {
		log.Fatalf("error updating firewall: %v", err)
	}

	if viper.GetString("runasync") == "true" {
		yamlOutput, err := yaml.Marshal(&task)
		if err != nil {
			log.Fatalf("error marshaling: %s", err)
		}
		fmt.Println(string(yamlOutput))
		return
	}

	err = task.WaitTaskCompletion()
	if err != nil {
		log.Fatalf("error waiting for task to complete: %v", err)
	}

}
示例#7
0
func cmdRemoveNatEdgeGateway(cmd *cobra.Command, args []string) {
	initConfig(cmd, "goair_compute", true, map[string]FlagValue{
		"vdchref":    {vdchref, true, false, ""},
		"externalip": {externalip, true, false, ""},
		"internalip": {internalip, true, false, ""},
		"runasync":   {runasync, false, false, ""},
	})

	if len(args[0]) == 0 || args[0] != "1to1" {
		log.Fatalf("Missing type of NAT, currently 1to1 is supported.")
	}

	client, err := authenticate(false)
	if err != nil {
		log.Fatalf("failed authenticating: %s", err)
	}

	err = authenticatecompute(client, false, "")
	if err != nil {
		log.Fatalf("Error authenticating compute: %s", err)
	}

	vdcuri, err := url.Parse(viper.GetString("vdchref"))
	if err != nil {
		log.Fatal(err)
	}

	client.VCDVDCHREF = *vdcuri

	vdc := govcloudair.NewVdc(client)
	vdc.Vdc = &types.Vdc{HREF: client.VCDVDCHREF.String()}
	err = vdc.Refresh()
	if err != nil {
		log.Fatalf("err refreshing vdc: %v", err)
	}

	edgeGateway, err := vdc.FindEdgeGateway("")
	if err != nil {
		log.Fatalf("err getting edge gateway: %v", err)
	}

	task, err := edgeGateway.Remove1to1Mapping(internalip, externalip)
	if err != nil {
		log.Fatalf("err creating 1 to 1 mapping: %v", err)
	}

	if viper.GetString("runasync") == "true" {
		yamlOutput, err := yaml.Marshal(&task)
		if err != nil {
			log.Fatalf("error marshaling: %s", err)
		}
		fmt.Println(string(yamlOutput))
		return
	}

	err = task.WaitTaskCompletion()
	if err != nil {
		log.Fatalf("error waiting for task to complete: %v", err)
	}

}
示例#8
0
func cmdGetEdgeGateway(cmd *cobra.Command, args []string) {
	initConfig(cmd, "goair_compute", true, map[string]FlagValue{})

	client, err := authenticate(false)
	if err != nil {
		log.Fatalf("failed authenticating: %s", err)
	}

	err = authenticatecompute(client, false, "")
	if err != nil {
		log.Fatalf("Error authenticating compute: %s", err)
	}

	vdcuri, err := url.Parse(viper.GetString("vdchref"))
	if err != nil {
		log.Fatal(err)
	}

	client.VCDVDCHREF = *vdcuri

	vdc := govcloudair.NewVdc(client)
	vdc.Vdc = &types.Vdc{HREF: client.VCDVDCHREF.String()}
	err = vdc.Refresh()
	if err != nil {
		log.Fatalf("err refreshing vdc: %v", err)
	}

	edgeGateway, err := vdc.FindEdgeGateway("")
	if err != nil {
		log.Fatalf("err getting edge gateway: %v", err)
	}

	if len(args) == 0 {
		yamlOutput, err := yaml.Marshal(&edgeGateway)
		if err != nil {
			log.Fatalf("error marshaling: %s", err)
		}
		fmt.Println(string(yamlOutput))
		return
	}

	var yamlOutput []byte
	switch args[0] {
	case "natrule":
		natRules := edgeGateway.EdgeGateway.Configuration.EdgeGatewayServiceConfiguration.NatService.NatRule
		yamlOutput, err = yaml.Marshal(&natRules)
	case "gatewayinterface":
		gatewayInterfaces := edgeGateway.EdgeGateway.Configuration.GatewayInterfaces.GatewayInterface
		yamlOutput, err = yaml.Marshal(&gatewayInterfaces)
	case "publicip":
		gatewayInterfaces := edgeGateway.EdgeGateway.Configuration.GatewayInterfaces.GatewayInterface
		ipRanges := types.IPRanges{}
		for _, gatewayInterface := range gatewayInterfaces {
			if gatewayInterface.InterfaceType == "uplink" {
				if gatewayInterface.SubnetParticipation.IPRanges != nil {
					for _, ipRange := range gatewayInterface.SubnetParticipation.IPRanges.IPRange {
						ipRanges.IPRange = append(ipRanges.IPRange, ipRange)
					}
				}
			}
		}

		var publicIPInfoMap map[string]*PublicIPInfo
		publicIPInfoMap = make(map[string]*PublicIPInfo)

		for _, ipRange := range ipRanges.IPRange {
			fromToIPRange, err := GetIPRange(ipRange.StartAddress, ipRange.EndAddress)
			if err != nil {
				log.Fatalf("err: problem getting IP range: %v", err)
			}
			for _, actualIP := range fromToIPRange {
				publicIPInfoMap[actualIP] = &PublicIPInfo{"", false, false, actualIP, ""}
			}

		}

		natRules := edgeGateway.EdgeGateway.Configuration.EdgeGatewayServiceConfiguration.NatService.NatRule
		for _, natRule := range natRules {
			switch natRule.RuleType {
			case "DNAT":
				if publicIPInfoMap[natRule.GatewayNatRule.OriginalIP] != nil {
					publicIPInfoMap[natRule.GatewayNatRule.OriginalIP].DNATRuleExists = true
				}
			case "SNAT":
				if publicIPInfoMap[natRule.GatewayNatRule.TranslatedIP] != nil {
					publicIPInfoMap[natRule.GatewayNatRule.TranslatedIP].SNATRuleExists = true
					publicIPInfoMap[natRule.GatewayNatRule.TranslatedIP].InternalIP = natRule.GatewayNatRule.OriginalIP
				}
			}
		}

		publicIPInfo := make([]PublicIPInfo, 0, len(publicIPInfoMap))

		for _, value := range publicIPInfoMap {
			if value.ExternalIP != "" && value.InternalIP != "" {
				value.Type = "1to1"
			}
			publicIPInfo = append(publicIPInfo, *value)
		}

		yamlOutput, err = yaml.Marshal(&publicIPInfo)

	case "firewall":
		firewallService := edgeGateway.EdgeGateway.Configuration.EdgeGatewayServiceConfiguration.FirewallService
		yamlOutput, err = yaml.Marshal(&firewallService)

	default:
		log.Fatalf("need to specify proper parameter after get natrule|gatewayinterface|publicip")
	}

	if err != nil {
		log.Fatalf("error marshaling: %s", err)
	}
	fmt.Println(string(yamlOutput))
	return

}
示例#9
0
文件: vapp.go 项目: nmcclain/goair
func cmdEjectMediaVApp(cmd *cobra.Command, args []string) {
	initConfig(cmd, "goair_compute", true, map[string]FlagValue{
		"vdchref":  {vdchref, true, false, ""},
		"vappid":   {vappid, true, false, ""},
		"vappname": {vappname, true, false, "vappid"},
		"runasync": {runasync, false, false, "runasync"},
	})

	client, err := authenticate(false)
	if err != nil {
		log.Fatalf("failed authenticating: %s", err)
	}

	err = authenticatecompute(client, false, "")
	if err != nil {
		log.Fatalf("Error authenticating compute: %s", err)
	}

	vdcuri, err := url.Parse(viper.GetString("vdchref"))
	if err != nil {
		log.Fatal(err)
	}

	client.VCDVDCHREF = *vdcuri

	vdc := govcloudair.NewVdc(client)
	vdc.Vdc = &types.Vdc{HREF: client.VCDVDCHREF.String()}

	vapp := *govcloudair.NewVApp(client)
	if viper.GetString("vappname") != "" {
		vapp, err = vdc.FindVAppByName(viper.GetString("vappname"))
		if err != nil {
			log.Fatal(err)
		}
	}

	if viper.GetString("vappid") != "" {
		vapp, err = vdc.FindVAppByID(viper.GetString("vappid"))
		if err != nil {
			log.Fatal(err)
		}
	}

	if vapp.VApp == nil {
		log.Fatalf("Couldn't find VApp")
	}

	task, err := vapp.EjectMedia(medianame)
	if err != nil {
		log.Fatalf("error ejecting media: %v", err)
	}

	if viper.GetString("runasync") == "true" {
		yamlOutput, err := yaml.Marshal(&task)
		if err != nil {
			log.Fatalf("error marshaling: %s", err)
		}
		fmt.Println(string(yamlOutput))
		return
	}

	err = task.WaitTaskCompletion()
	if err != nil {
		log.Fatalf("error waiting for task to complete: %v", err)
	}

}
示例#10
0
文件: vapp.go 项目: nmcclain/goair
func cmdUpdateVApp(cmd *cobra.Command, args []string) {
	initConfig(cmd, "goair_compute", true, map[string]FlagValue{
		"vdchref":      {vdchref, true, false, ""},
		"vappid":       {vappid, true, false, ""},
		"vappname":     {vappname, true, false, "vappid"},
		"memorysizemb": {memorysizemb, false, false, "memorysizemb"},
		"cpucount":     {cpucount, false, false, "cpucount"},
		"runasync":     {cpucount, false, false, "runasync"},
	})

	client, err := authenticate(false)
	if err != nil {
		log.Fatalf("failed authenticating: %s", err)
	}

	err = authenticatecompute(client, false, "")
	if err != nil {
		log.Fatalf("Error authenticating compute: %s", err)
	}

	vdcuri, err := url.Parse(viper.GetString("vdchref"))
	if err != nil {
		log.Fatal(err)
	}

	client.VCDVDCHREF = *vdcuri

	vdc := govcloudair.NewVdc(client)
	vdc.Vdc = &types.Vdc{HREF: client.VCDVDCHREF.String()}

	vapp := *govcloudair.NewVApp(client)
	if viper.GetString("vappname") != "" {
		vapp, err = vdc.FindVAppByName(viper.GetString("vappname"))
		if err != nil {
			log.Fatal(err)
		}
	}

	if viper.GetString("vappid") != "" {
		vapp, err = vdc.FindVAppByID(viper.GetString("vappid"))
		if err != nil {
			log.Fatal(err)
		}
	}

	if vapp.VApp == nil {
		log.Fatalf("Couldn't find VApp")
	}

	if memorysizemb != "" {
		ms, _ := strconv.Atoi(memorysizemb)
		task, err := vapp.ChangeMemorySize(ms)
		if err != nil {
			log.Fatalf("err problem changing memory size: %v", err)
		}

		err = task.WaitTaskCompletion()
		if err != nil {
			log.Fatalf("error waiting for task to complete: %v", err)
		}
	}

	if cpucount != "" {
		cc, _ := strconv.Atoi(cpucount)
		task, err := vapp.ChangeCPUcount(cc)
		if err != nil {
			log.Fatalf("err problem updating cpu count: %v", err)
		}

		err = task.WaitTaskCompletion()
		if err != nil {
			log.Fatalf("error waiting for task to complete: %v", err)
		}
	}

	if len(args) == 2 && args[0] == "guestcustomization" && args[1] == "script" {
		bytes, _ := ioutil.ReadAll(os.Stdin)
		customizationScript := string(bytes)

		guestCustomizationSection, err := vapp.GetGuestCustomization()
		if err != nil {
			log.Fatalf("error getting guest customization: %s", err)
		}

		task, err := vapp.RunCustomizationScript(guestCustomizationSection.ComputerName, customizationScript, &guestCustomizationSection)
		if err != nil {
			log.Fatalf("err problem updating cpu count: %v", err)
		}

		err = task.WaitTaskCompletion()
		if err != nil {
			log.Fatalf("error waiting for task to complete: %v", err)
		}

	}

}
示例#11
0
文件: vapp.go 项目: nmcclain/goair
func cmdActionVApp(cmd *cobra.Command, args []string) {
	initConfig(cmd, "goair_compute", true, map[string]FlagValue{
		"vdchref":  {vdchref, true, false, ""},
		"vappid":   {vappid, true, false, ""},
		"vappname": {vappname, true, false, "vappid"},
	})

	client, err := authenticate(false)
	if err != nil {
		log.Fatalf("failed authenticating: %s", err)
	}

	err = authenticatecompute(client, false, "")
	if err != nil {
		log.Fatalf("Error authenticating compute: %s", err)
	}

	vdcuri, err := url.Parse(viper.GetString("vdchref"))
	if err != nil {
		log.Fatal(err)
	}

	client.VCDVDCHREF = *vdcuri

	vdc := govcloudair.NewVdc(client)
	vdc.Vdc = &types.Vdc{HREF: client.VCDVDCHREF.String()}

	vapp := *govcloudair.NewVApp(client)
	if viper.GetString("vappname") != "" {
		vapp, err = vdc.FindVAppByName(viper.GetString("vappname"))
		if err != nil {
			log.Fatal(err)
		}
	}

	if viper.GetString("vappid") != "" {
		vapp, err = vdc.FindVAppByID(viper.GetString("vappid"))
		if err != nil {
			log.Fatal(err)
		}
	}

	if vapp.VApp == nil {
		log.Fatalf("Couldn't find VApp")
	}

	if len(args) > 1 {
		log.Fatalf("Too many action statements")
	}

	action := args[0]

	switch action {
	case "poweron":
		vapp.PowerOn()
	case "poweroff":
		vapp.PowerOff()
	case "reboot":
		vapp.Reboot()
	case "reset":
		vapp.Reset()
	case "suspend":
		vapp.Suspend()
	case "shutdown":
		vapp.Shutdown()
	case "undeploy":
		vapp.Undeploy()
	case "deploy":
		vapp.Deploy()
	case "delete":
		vapp.Delete()
	}

	return

}
示例#12
0
文件: vapp.go 项目: nmcclain/goair
func cmdGetVApp(cmd *cobra.Command, args []string) {
	initConfig(cmd, "goair_compute", true, map[string]FlagValue{
		"vdchref":  {vdchref, true, false, ""},
		"vappid":   {vappid, false, false, ""},
		"vappname": {vappname, false, false, "vappid"},
	})

	if len(args) > 1 {
		log.Fatalf("Too many action statements")
	}

	client, err := authenticate(false)
	if err != nil {
		log.Fatalf("failed authenticating: %s", err)
	}

	err = authenticatecompute(client, false, "")
	if err != nil {
		log.Fatalf("Error authenticating compute: %s", err)
	}

	vdcuri, err := url.Parse(viper.GetString("vdchref"))
	if err != nil {
		log.Fatal(err)
	}

	client.VCDVDCHREF = *vdcuri

	vdc := govcloudair.NewVdc(client)
	vdc.Vdc = &types.Vdc{HREF: client.VCDVDCHREF.String()}

	vapp := *govcloudair.NewVApp(client)

	if viper.GetString("vappname") != "" {
		vapp, err = vdc.FindVAppByName(viper.GetString("vappname"))
		if err != nil {
			log.Fatal(err)
		}
	}

	if viper.GetString("vappid") != "" {
		vapp, err = vdc.FindVAppByID(viper.GetString("vappid"))
		if err != nil {
			log.Fatal(err)
		}
	}

	if len(args) == 0 && vapp.VApp != nil && (viper.GetString("vappname") != "" || viper.GetString("vappid") != "") {
		yamlOutput, err := yaml.Marshal(&vapp)
		if err != nil {
			log.Fatalf("error marshaling: %s", err)
		}
		fmt.Println(string(yamlOutput))
		return
	}

	if len(args) == 1 {
		if vapp.VApp == nil {
			log.Fatalf("err vapp not found")
		}
		extra := args[0]

		switch extra {
		case "vm":
			vm := vapp.VApp.Children.VM
			yamlOutput, err := yaml.Marshal(vm)
			if err != nil {
				log.Fatalf("error marshaling: %s", err)
			}
			fmt.Println(string(yamlOutput))
			return
		case "guestcustomization":
			guestCustomizationSection, err := vapp.GetGuestCustomization()
			if err != nil {
				log.Fatalf("error getting guest customization: %s", err)
			}

			yamlOutput, err := yaml.Marshal(&guestCustomizationSection)
			fmt.Println(string(yamlOutput))
			return
		default:
			log.Fatalf("Did not specify proper extra get command of vm|guestcustomization")
		}
	}

	vapps, err := vdc.GetVApp()
	if err != nil {
		log.Fatal(err)
	}
	yamlOutput, err := yaml.Marshal(&vapps)
	if err != nil {
		log.Fatalf("error marshaling: %s", err)
	}
	fmt.Println(string(yamlOutput))
	return

}