func showPhysicalNetwork(c *cli.Context, w io.Writer) error { err := checkArgCount(c, 1) if err != nil { return err } id := c.Args().First() client.Esxclient, err = client.GetClient(c) if err != nil { return err } network, err := client.Esxclient.Subnets.Get(id) if err != nil { return err } if c.GlobalIsSet("non-interactive") { portGroups := getCommaSeparatedStringFromStringArray(network.PortGroups) fmt.Printf("%s\t%s\t%s\t%s\t%s\t%t\n", network.ID, network.Name, network.State, portGroups, network.Description, network.IsDefault) } else if utils.NeedsFormatting(c) { utils.FormatObject(network, w, c) } else { fmt.Printf("Network ID: %s\n", network.ID) fmt.Printf(" Name: %s\n", network.Name) fmt.Printf(" State: %s\n", network.State) fmt.Printf(" Description: %s\n", network.Description) fmt.Printf(" Port Groups: %s\n", network.PortGroups) fmt.Printf(" Is Default: %t\n", network.IsDefault) } return nil }
// Prints out the output of tasks func printTaskList(taskList []photon.Task, c *cli.Context) error { if c.GlobalIsSet("non-interactive") { for _, task := range taskList { fmt.Printf("%s\t%s\t%s\t%d\t%d\n", task.ID, task.State, task.Operation, task.StartedTime, task.EndTime-task.StartedTime) } } else if utils.NeedsFormatting(c) { utils.FormatObjects(taskList, os.Stdout, c) } else { w := new(tabwriter.Writer) w.Init(os.Stdout, 4, 4, 2, ' ', 0) fmt.Fprintf(w, "\nTask\tStart Time\tDuration\n") for _, task := range taskList { var duration int64 startTime := timestampToString(task.StartedTime) if task.EndTime-task.StartedTime > 0 { duration = (task.EndTime - task.StartedTime) / 1000 } else { duration = 0 } fmt.Fprintf(w, "%s\t%s\t%.2d:%.2d:%.2d\n", task.ID, startTime, duration/3600, (duration/60)%60, duration%60) err := w.Flush() if err != nil { return err } fmt.Printf("%s, %s\n", task.Operation, task.State) } if len(taskList) > 0 { fmt.Printf("\nYou can run 'photon task show <id>' for more information\n") } fmt.Printf("Total: %d\n", len(taskList)) } return nil }
// Lists all the hosts associated with the deployment func listDeploymentVms(c *cli.Context, w io.Writer) error { id, err := getDeploymentId(c) if err != nil { return err } client.Esxclient, err = client.GetClient(c) if err != nil { return err } vms, err := client.Esxclient.Deployments.GetVms(id) if err != nil { return err } if utils.NeedsFormatting(c) { utils.FormatObjects(vms, w, c) } else { err = printVMList(vms.Items, os.Stdout, c, false) if err != nil { return err } } return nil }
// Retrieves availability zone against specified id. func showAvailabilityZone(c *cli.Context, w io.Writer) error { err := checkArgCount(c, 1) if err != nil { return err } id := c.Args().First() client.Esxclient, err = client.GetClient(c) if err != nil { return err } zone, err := client.Esxclient.AvailabilityZones.Get(id) if err != nil { return err } if c.GlobalIsSet("non-interactive") { fmt.Printf("%s\t%s\t%s\t%s\n", zone.ID, zone.Name, zone.Kind, zone.State) } else if utils.NeedsFormatting(c) { utils.FormatObject(zone, w, c) } else { fmt.Printf("AvailabilityZone ID: %s\n", zone.ID) fmt.Printf(" Name: %s\n", zone.Name) fmt.Printf(" Kind: %s\n", zone.Kind) fmt.Printf(" State: %s\n", zone.State) } return nil }
func listVMNetworks(c *cli.Context, w io.Writer) error { err := checkArgCount(c, 1) if err != nil { return err } id := c.Args().First() client.Esxclient, err = client.GetClient(c) if err != nil { return err } networks, err := getVMNetworks(id, c) if err != nil { return err } if !utils.NeedsFormatting(c) { err = printVMNetworks(networks, c.GlobalIsSet("non-interactive")) if err != nil { return err } } else { utils.FormatObjects(networks, w, c) } return nil }
// Retrieves tasks for VM func getVMTasks(c *cli.Context, w io.Writer) error { err := checkArgCount(c, 1) if err != nil { return err } id := c.Args().First() state := c.String("state") client.Esxclient, err = client.GetClient(c) if err != nil { return err } options := &photon.TaskGetOptions{ State: state, } taskList, err := client.Esxclient.VMs.GetTasks(id, options) if err != nil { return err } if !utils.NeedsFormatting(c) { err = printTaskList(taskList.Items, c) if err != nil { return err } } else { utils.FormatObjects(taskList, w, c) } return nil }
// Get endpoint in config file and its status func getStatus(c *cli.Context, w io.Writer) error { err := checkArgCount(c, 0) if err != nil { return err } client.Esxclient, err = client.GetClient(c) if err != nil { return err } status, err := client.Esxclient.Status.Get() if err != nil { return err } if !utils.NeedsFormatting(c) { err = printStatus(status) } else { utils.FormatObject(status, w, c) } if err != nil { return err } return nil }
// Set host's availability zone with the specified host ID, returns an error if one occurred func setHostAvailabilityZone(c *cli.Context, w io.Writer) error { err := checkArgCount(c, 2) if err != nil { return err } id := c.Args().First() availabilityZoneId := c.Args()[1] client.Esxclient, err = client.GetClient(c) if err != nil { return err } setAvailabilityZoneSpec := photon.HostSetAvailabilityZoneOperation{} setAvailabilityZoneSpec.AvailabilityZoneId = availabilityZoneId setTask, err := client.Esxclient.Hosts.SetAvailabilityZone(id, &setAvailabilityZoneSpec) if err != nil { return err } id, err = waitOnTaskOperation(setTask.ID, c) if err != nil { return err } if utils.NeedsFormatting(c) { host, err := client.Esxclient.Hosts.Get(id) if err != nil { return err } utils.FormatObject(host, w, c) } return nil }
// Retrieves a list of projects, returns an error if one occurred func listProjects(c *cli.Context, w io.Writer) error { err := checkArgCount(c, 0) if err != nil { return err } tenantName := c.String("tenant") client.Esxclient, err = client.GetClient(c) if err != nil { return err } tenant, err := verifyTenant(tenantName) if err != nil { return err } projects, err := client.Esxclient.Tenants.GetProjects(tenant.ID, nil) if err != nil { return err } if c.GlobalIsSet("non-interactive") { for _, t := range projects.Items { limits := quotaLineItemListToString(t.ResourceTicket.Limits) usage := quotaLineItemListToString(t.ResourceTicket.Usage) fmt.Printf("%s\t%s\t%s\t%s\n", t.ID, t.Name, limits, usage) } } else if utils.NeedsFormatting(c) { utils.FormatObjects(projects.Items, w, c) } else { w := new(tabwriter.Writer) w.Init(os.Stdout, 4, 4, 2, ' ', 0) fmt.Fprintf(w, "ID\tName\tLimit\tUsage\n") for _, t := range projects.Items { rt := t.ResourceTicket for i := 0; i < len(rt.Limits); i++ { if i == 0 { fmt.Fprintf(w, "%s\t%s\t%s %g %s\t%s %g %s\n", t.ID, t.Name, rt.Limits[i].Key, rt.Limits[i].Value, rt.Limits[i].Unit, rt.Usage[i].Key, rt.Usage[i].Value, rt.Usage[i].Unit) } else { fmt.Fprintf(w, "\t\t%s %g %s\t%s %g %s\n", rt.Limits[i].Key, rt.Limits[i].Value, rt.Limits[i].Unit, rt.Usage[i].Key, rt.Usage[i].Value, rt.Usage[i].Unit) } } for i := len(rt.Limits); i < len(rt.Usage); i++ { fmt.Fprintf(w, "\t\t\t%s %g %s\n", rt.Usage[i].Key, rt.Usage[i].Value, rt.Usage[i].Unit) } } err := w.Flush() if err != nil { return err } fmt.Printf("\nTotal projects: %d\n", len(projects.Items)) } return nil }
// Sends a "resize cluster" request to the API client based on the cli.Context // Returns an error if one occurred func resizeCluster(c *cli.Context, w io.Writer) error { err := checkArgCount(c, 2) if err != nil { return err } cluster_id := c.Args()[0] worker_count_string := c.Args()[1] worker_count, err := strconv.Atoi(worker_count_string) wait_for_ready := c.IsSet("wait-for-ready") if len(cluster_id) == 0 || err != nil || worker_count <= 0 { return fmt.Errorf("Provide a valid cluster ID and worker count") } client.Esxclient, err = client.GetClient(c) if err != nil { return err } if !c.GlobalIsSet("non-interactive") { fmt.Printf("\nResizing cluster %s to worker count %d\n", cluster_id, worker_count) } if confirmed(c) { resizeSpec := photon.ClusterResizeOperation{} resizeSpec.NewWorkerCount = worker_count resizeTask, err := client.Esxclient.Clusters.Resize(cluster_id, &resizeSpec) if err != nil { return err } _, err = waitOnTaskOperation(resizeTask.ID, c) if err != nil { return err } if wait_for_ready { cluster, err := waitForCluster(cluster_id) if err != nil { return err } if utils.NeedsFormatting(c) { utils.FormatObject(cluster, w, c) } else { fmt.Printf("Cluster %s is ready\n", cluster.ID) } } else { fmt.Println("Note: A background task is running to gradually resize the cluster to its target capacity.") fmt.Printf("You may continue to use the cluster. You can run 'cluster show %s'\n", resizeTask.Entity.ID) fmt.Println("to see the state of the cluster. If the resize operation is still in progress, the cluster state") fmt.Println("will show as RESIZING. Once the cluster is resized, the cluster state will show as READY.") } } else { fmt.Println("Cancelled") } return nil }
// Show project info with the specified project id, returns an error if one occurred func showProject(c *cli.Context, w io.Writer) error { err := checkArgCount(c, 1) if err != nil { return err } id := c.Args().First() client.Esxclient, err = client.GetClient(c) if err != nil { return err } project, err := client.Esxclient.Projects.Get(id) if err != nil { return err } if c.GlobalIsSet("non-interactive") { securityGroups := []string{} for _, s := range project.SecurityGroups { securityGroups = append(securityGroups, fmt.Sprintf("%s:%t", s.Name, s.Inherited)) } scriptSecurityGroups := strings.Join(securityGroups, ",") limits := quotaLineItemListToString(project.ResourceTicket.Limits) usages := quotaLineItemListToString(project.ResourceTicket.Usage) fmt.Printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\n", project.ID, project.Name, project.ResourceTicket.TenantTicketID, project.ResourceTicket.TenantTicketName, limits, usages, scriptSecurityGroups) } else if utils.NeedsFormatting(c) { utils.FormatObject(project, w, c) } else { w := new(tabwriter.Writer) w.Init(os.Stdout, 4, 4, 2, ' ', 0) fmt.Fprintf(w, "Project ID: %s\n", project.ID) fmt.Fprintf(w, " Name: %s\n", project.Name) fmt.Fprintf(w, " TenantTicketID: %s\n", project.ResourceTicket.TenantTicketID) fmt.Fprintf(w, " TenantTicketName: %s\n", project.ResourceTicket.TenantTicketName) fmt.Fprintf(w, " Limits:\n") for _, l := range project.ResourceTicket.Limits { fmt.Fprintf(w, " %s\t%g\t%s\n", l.Key, l.Value, l.Unit) } fmt.Fprintf(w, " Usage:\n") for _, u := range project.ResourceTicket.Usage { fmt.Fprintf(w, " %s\t%g\t%s\n", u.Key, u.Value, u.Unit) } if len(project.SecurityGroups) != 0 { fmt.Fprintf(w, " SecurityGroups:\n") for _, s := range project.SecurityGroups { fmt.Fprintf(w, " %s\t%t\n", s.Name, s.Inherited) } } err = w.Flush() if err != nil { return err } } return nil }
func createVmImage(c *cli.Context, w io.Writer) error { err := checkArgCount(c, 1) if err != nil { return err } id := c.Args().First() name := c.String("name") replicationType := c.String("image_replication") if !c.GlobalIsSet("non-interactive") && !utils.NeedsFormatting(c) { defaultName := "image-from-vm-" + id defaultReplication := "EAGER" name, err = askForInput("Image name (default: "+defaultName+"): ", name) if err != nil { return err } replicationType, err = askForInput("Image replication type (default: "+defaultReplication+"): ", replicationType) if err != nil { return err } if len(name) == 0 { name = defaultName } if len(replicationType) == 0 { replicationType = defaultReplication } } options := &photon.ImageCreateSpec{ Name: name, ReplicationType: replicationType, } client.Esxclient, err = client.GetClient(c) if err != nil { return err } task, err := client.Esxclient.VMs.CreateImage(id, options) if err != nil { return err } _, err = waitOnTaskOperation(task.ID, c) if err != nil { return err } err = formatHelper(c, w, client.Esxclient, id) return err }
func formatHelper(c *cli.Context, w io.Writer, client *photon.Client, id string) error { if utils.NeedsFormatting(c) { vm, err := client.VMs.Get(id) if err != nil { return err } utils.FormatObject(vm, w, c) } return nil }
func deploymentJsonHelper(c *cli.Context, id string, client *photon.Client) error { if utils.NeedsFormatting(c) { deployment, err := client.Deployments.Get(id) if err != nil { return err } utils.FormatObject(deployment, os.Stdout, c) } return nil }
// Shows an image based on id func showImage(c *cli.Context, w io.Writer) error { id := c.Args().First() if !c.GlobalIsSet("non-interactive") { var err error id, err = askForInput("Image id: ", id) if err != nil { return err } } if len(id) == 0 { return fmt.Errorf("Please provide image id") } var err error client.Esxclient, err = client.GetClient(c) if err != nil { return err } image, err := client.Esxclient.Images.Get(id) if err != nil { return err } if c.GlobalIsSet("non-interactive") { settings := []string{} for _, setting := range image.Settings { settings = append(settings, fmt.Sprintf("%s:%s", setting.Name, setting.DefaultValue)) } scriptSettings := strings.Join(settings, ",") fmt.Printf("%s\t%s\t%s\t%d\t%s\t%s\t%s\t%s\n", image.ID, image.Name, image.State, image.Size, image.ReplicationType, image.ReplicationProgress, image.SeedingProgress, scriptSettings) } else if utils.NeedsFormatting(c) { utils.FormatObject(image, w, c) } else { fmt.Printf("Image ID: %s\n", image.ID) fmt.Printf(" Name: %s\n", image.Name) fmt.Printf(" State: %s\n", image.State) fmt.Printf(" Size: %d Byte(s)\n", image.Size) fmt.Printf(" Image Replication Type: %s\n", image.ReplicationType) fmt.Printf(" Image Replication Progress: %s\n", image.ReplicationProgress) fmt.Printf(" Image Seeding Progress: %s\n", image.SeedingProgress) fmt.Printf(" Settings: \n") for _, setting := range image.Settings { fmt.Printf(" %s : %s\n", setting.Name, setting.DefaultValue) } } return nil }
// Sends a show resource-ticket task to client based on the cli.Context // Returns an error if one occurred func showResourceTicket(c *cli.Context, w io.Writer) error { err := checkArgCount(c, 1) if err != nil { return err } name := c.Args().First() tenantName := c.String("tenant") client.Esxclient, err = client.GetClient(c) if err != nil { return err } tenant, err := verifyTenant(tenantName) if err != nil { return err } rt, err := findResourceTicket(tenant.ID, name) if err != nil { return err } if c.GlobalIsSet("non-interactive") { usage := quotaLineItemListToString(rt.Usage) limits := quotaLineItemListToString(rt.Limits) fmt.Printf("%s\t%s\t%s\t%s\n", rt.Name, rt.ID, limits, usage) } else if utils.NeedsFormatting(c) { utils.FormatObject(rt, w, c) } else { w := new(tabwriter.Writer) w.Init(os.Stdout, 4, 4, 2, ' ', 0) fmt.Fprintf(w, "ID\tName\tLimit\tUsage\n") for i := 0; i < len(rt.Limits); i++ { if i == 0 { fmt.Fprintf(w, "%s\t%s\t%s %g %s\t%s %g %s\n", rt.ID, rt.Name, rt.Limits[i].Key, rt.Limits[i].Value, rt.Limits[i].Unit, rt.Usage[i].Key, rt.Usage[i].Value, rt.Usage[i].Unit) } else { fmt.Fprintf(w, "\t\t%s %g %s\t%s %g %s\n", rt.Limits[i].Key, rt.Limits[i].Value, rt.Limits[i].Unit, rt.Usage[i].Key, rt.Usage[i].Value, rt.Usage[i].Unit) } } err := w.Flush() if err != nil { return err } } return nil }
func printClusterVMs(vms []photon.VM, w io.Writer, c *cli.Context) (err error) { clusterVMs := []ClusterVM{} for _, vm := range vms { ipAddr := "-" networks, err := getVMNetworks(vm.ID, c) if err != nil { continue } for _, nt := range networks { network := nt.(map[string]interface{}) if val, ok := network["network"]; !ok || val == nil { continue } if val, ok := network["ipAddress"]; ok && val != nil { ipAddr = val.(string) break } } clusterVM := ClusterVM{ vm, ipAddr, } clusterVMs = append(clusterVMs, clusterVM) } if c.GlobalIsSet("non-interactive") { fmt.Printf("%d\n", len(vms)) for _, clusterVM := range clusterVMs { fmt.Printf("%s\t%s\t%s\n", clusterVM.ClusterVM.ID, clusterVM.ClusterVM.Name, clusterVM.IPAddress) } } else if utils.NeedsFormatting(c) { utils.FormatObjects(clusterVMs, w, c) } else { w := new(tabwriter.Writer) w.Init(os.Stdout, 4, 4, 2, ' ', 0) fmt.Fprintf(w, "VM ID\tVM Name\tVM IP\n") for _, clusterVM := range clusterVMs { fmt.Fprintf(w, "%s\t%s\t%s\n", clusterVM.ClusterVM.ID, clusterVM.ClusterVM.Name, clusterVM.IPAddress) } err := w.Flush() if err != nil { return err } } return nil }
// Sends a create availability-zone task to client based on the cli.Context // Returns an error if one occurred func createAvailabilityZone(c *cli.Context, w io.Writer) error { err := checkArgCount(c, 0) if err != nil { return err } name := c.String("name") if !c.GlobalIsSet("non-interactive") { var err error name, err = askForInput("AvailabilityZone name: ", name) if err != nil { return err } } if len(name) == 0 { return fmt.Errorf("Please provide availability zone name") } azSpec := &photon.AvailabilityZoneCreateSpec{ Name: name, } client.Esxclient, err = client.GetClient(c) if err != nil { return err } createTask, err := client.Esxclient.AvailabilityZones.Create(azSpec) if err != nil { return err } id, err := waitOnTaskOperation(createTask.ID, c) if err != nil { return err } if utils.NeedsFormatting(c) { zone, err := client.Esxclient.AvailabilityZones.Get(id) if err != nil { return err } utils.FormatObject(zone, w, c) } return nil }
func setDefaultNetwork(c *cli.Context, w io.Writer) error { err := checkArgCount(c, 1) if err != nil { return err } id := c.Args().First() client.Esxclient, err = client.GetClient(c) if err != nil { return err } sdnEnabled, err := isSoftwareDefinedNetwork(c) if err != nil { log.Fatal("Error: ", err) } var task *photon.Task if sdnEnabled { task, err = client.Esxclient.VirtualSubnets.SetDefault(id) } else { task, err = client.Esxclient.Subnets.SetDefault(id) } if err != nil { return err } if confirmed(c) { id, err := waitOnTaskOperation(task.ID, c) if err != nil { return err } if utils.NeedsFormatting(c) { network, err := client.Esxclient.Subnets.Get(id) if err != nil { return err } utils.FormatObject(network, w, c) } } else { fmt.Println("OK. Canceled") } return nil }
// Retrieves a list of flavors func listFlavors(c *cli.Context, w io.Writer) error { err := checkArgCount(c, 0) if err != nil { return err } client.Esxclient, err = client.GetClient(c) if err != nil { return err } name := c.String("name") kind := c.String("kind") options := &photon.FlavorGetOptions{ Name: name, Kind: kind, } flavors, err := client.Esxclient.Flavors.GetAll(options) if err != nil { return err } if c.GlobalIsSet("non-interactive") { for _, flavor := range flavors.Items { costs := quotaLineItemListToString(flavor.Cost) fmt.Printf("%s\t%s\t%s\t%s\n", flavor.ID, flavor.Name, flavor.Kind, costs) } } else if utils.NeedsFormatting(c) { utils.FormatObjects(flavors.Items, w, c) } else { w := new(tabwriter.Writer) w.Init(os.Stdout, 4, 4, 2, ' ', 0) fmt.Fprintf(w, "ID\tName\tKind\tCost\n") for _, flavor := range flavors.Items { printQuotaList(w, flavor.Cost, flavor.ID, flavor.Name, flavor.Kind) } err = w.Flush() if err != nil { return err } fmt.Printf("Total: %d\n", len(flavors.Items)) } return nil }
// Retrieves a list of VMs, returns an error if one occurred func listVMs(c *cli.Context, w io.Writer) error { err := checkArgCount(c, 0) if err != nil { return err } tenantName := c.String("tenant") projectName := c.String("project") summaryView := c.IsSet("summary") name := c.String("name") options := &photon.VmGetOptions{ Name: name, } client.Esxclient, err = client.GetClient(c) if err != nil { return err } tenant, err := verifyTenant(tenantName) if err != nil { return err } project, err := verifyProject(tenant.ID, projectName) if err != nil { return err } vmList, err := client.Esxclient.Projects.GetVMs(project.ID, options) if err != nil { return err } if !utils.NeedsFormatting(c) { err = printVMList(vmList.Items, os.Stdout, c, summaryView) } else { utils.FormatObjects(vmList, w, c) } if err != nil { return err } return nil }
// Starts the recurring copy state of source system into destination func deploymentMigrationPrepare(c *cli.Context) error { id, err := getDeploymentId(c) if err != nil { return err } sourceAddress := c.String("endpoint") if len(sourceAddress) == 0 { return fmt.Errorf("Please provide the API endpoint of the old control plane") } client.Esxclient, err = client.GetClient(c) if err != nil { return err } deployment, err := client.Esxclient.Deployments.Get(id) if err != nil { return err } initializeMigrationSpec := photon.InitializeMigrationOperation{} initializeMigrationSpec.SourceNodeGroupReference = sourceAddress // Initialize deployment migration initializeMigrate, err := client.Esxclient.Deployments.InitializeDeploymentMigration(&initializeMigrationSpec, deployment.ID) if err != nil { return err } _, err = waitOnTaskOperation(initializeMigrate.ID, c) if err != nil { return err } if !utils.NeedsFormatting(c) { fmt.Printf("Deployment '%s' migration started [source management endpoint: '%s'].\n", deployment.ID, sourceAddress) } err = deploymentJsonHelper(c, id, client.Esxclient) if err != nil { return err } return nil }
// Retrieves a list of resource tickets, returns an error if one occurred func listResourceTickets(c *cli.Context, w io.Writer) error { err := checkArgCount(c, 0) if err != nil { return err } tenantName := c.String("tenant") client.Esxclient, err = client.GetClient(c) if err != nil { return err } tenant, err := verifyTenant(tenantName) if err != nil { return err } tickets, err := client.Esxclient.Tenants.GetResourceTickets(tenant.ID, nil) if err != nil { return err } if c.GlobalIsSet("non-interactive") { for _, t := range tickets.Items { limits := quotaLineItemListToString(t.Limits) fmt.Printf("%s\t%s\t%s\n", t.ID, t.Name, limits) } } else if utils.NeedsFormatting(c) { utils.FormatObjects(tickets.Items, w, c) } else { w := new(tabwriter.Writer) w.Init(os.Stdout, 4, 4, 2, ' ', 0) fmt.Fprintf(w, "ID\tName\tLimit\n") for _, t := range tickets.Items { printQuotaList(w, t.Limits, t.ID, t.Name) } err := w.Flush() if err != nil { return err } fmt.Printf("\nTotal resource tickets: %d\n", len(tickets.Items)) } return nil }
// Prompts the user to confirm action, will repeat until a response is yes, y, no, or n. func confirmed(c *cli.Context) bool { if c.GlobalIsSet("non-interactive") || utils.NeedsFormatting(c) { return true } else { response := "" for { response, _ = askForInput("Are you sure [y/n]? ", response) response = strings.ToLower(response) if response == "y" || response == "yes" { return true } if response == "n" || response == "no" { return false } fmt.Printf("Please enter \"yes\" or \"no\".\n") response = "" } } }
func listPhysicalNetworks(c *cli.Context, w io.Writer) error { err := checkArgCount(c, 0) if err != nil { return err } client.Esxclient, err = client.GetClient(c) if err != nil { return err } name := c.String("name") options := &photon.SubnetGetOptions{ Name: name, } networks, err := client.Esxclient.Subnets.GetAll(options) if err != nil { return err } if c.GlobalIsSet("non-interactive") { for _, network := range networks.Items { fmt.Printf("%s\t%s\t%s\t%s\t%s\n", network.ID, network.Name, network.State, network.PortGroups, network.Description) } } else if utils.NeedsFormatting(c) { utils.FormatObjects(networks.Items, w, c) } else { w := new(tabwriter.Writer) w.Init(os.Stdout, 4, 4, 2, ' ', 0) fmt.Fprintf(w, "ID\tName\tState\tPortGroups\tDescription\tIsDefault\n") for _, network := range networks.Items { fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%t\n", network.ID, network.Name, network.State, network.PortGroups, network.Description, network.IsDefault) } err = w.Flush() if err != nil { return err } fmt.Printf("Total: %d\n", len(networks.Items)) } return nil }
// Lists all images func listImages(c *cli.Context, w io.Writer) error { err := checkArgCount(c, 0) if err != nil { return err } client.Esxclient, err = client.GetClient(c) if err != nil { return err } name := c.String("name") options := &photon.ImageGetOptions{ Name: name, } images, err := client.Esxclient.Images.GetAll(options) if err != nil { return err } if c.GlobalIsSet("non-interactive") { for _, image := range images.Items { fmt.Printf("%s\t%s\t%s\t%d\t%s\t%s\t%s\n", image.ID, image.Name, image.State, image.Size, image.ReplicationType, image.ReplicationProgress, image.SeedingProgress) } } else if utils.NeedsFormatting(c) { utils.FormatObjects(images.Items, w, c) } else { w := new(tabwriter.Writer) w.Init(os.Stdout, 4, 4, 2, ' ', 0) fmt.Fprintf(w, "ID\tName\tState\tSize(Byte)\tReplication_type\tReplicationProgress\tSeedingProgress\n") for _, image := range images.Items { fmt.Fprintf(w, "%s\t%s\t%s\t%d\t%s\t%s\t%s\n", image.ID, image.Name, image.State, image.Size, image.ReplicationType, image.ReplicationProgress, image.SeedingProgress) } err = w.Flush() if err != nil { return err } fmt.Printf("\nTotal: %d\n", len(images.Items)) } return nil }
func getVMMksTicket(c *cli.Context, w io.Writer) error { err := checkArgCount(c, 1) if err != nil { return err } id := c.Args().First() client.Esxclient, err = client.GetClient(c) if err != nil { return err } task, err := client.Esxclient.VMs.GetMKSTicket(id) if err != nil { return err } if c.GlobalIsSet("non-interactive") { task, err := client.Esxclient.Tasks.Wait(task.ID) if err != nil { return err } mksTicket := task.ResourceProperties.(map[string]interface{}) fmt.Printf("%s\t%v\n", task.Entity.ID, mksTicket["ticket"]) } else if utils.NeedsFormatting(c) { task, err := client.Esxclient.Tasks.Wait(task.ID) if err != nil { return err } mksTicket := task.ResourceProperties.(map[string]interface{}) utils.FormatObject(mksTicket, w, c) } else { task, err = pollTask(task.ID) if err != nil { return err } mksTicket := task.ResourceProperties.(map[string]interface{}) fmt.Printf("VM ID: %s \nMks ticket ID is %v\n", task.Entity.ID, mksTicket["ticket"]) } return nil }
func waitOnTaskOperation(taskId string, c *cli.Context) (string, error) { var task *photon.Task var err error needsFormatting := utils.NeedsFormatting(c) if c.GlobalIsSet("non-interactive") || needsFormatting { task, err = client.Esxclient.Tasks.Wait(taskId) if err != nil { return "", err } if !needsFormatting { fmt.Println(task.Entity.ID) } } else { task, err = pollTask(taskId) if err != nil { return "", err } fmt.Printf("%s completed for '%s' entity %s\n", task.Operation, task.Entity.Kind, task.Entity.ID) } return task.Entity.ID, err }
// Show host info with the specified host ID, returns an error if one occurred func showHost(c *cli.Context, w io.Writer) error { err := checkArgCount(c, 1) if err != nil { return err } id := c.Args().First() client.Esxclient, err = client.GetClient(c) if err != nil { return err } host, err := client.Esxclient.Hosts.Get(id) if err != nil { return err } if c.GlobalIsSet("non-interactive") { tag := strings.Trim(fmt.Sprint(host.Tags), "[]") scriptTag := strings.Replace(tag, " ", ",", -1) metadata := strings.Trim(strings.TrimLeft(fmt.Sprint(host.Metadata), "map"), "[]") scriptMetadata := strings.Replace(metadata, " ", ",", -1) fmt.Printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", host.ID, host.Username, host.Address, scriptTag, host.State, scriptMetadata, host.AvailabilityZone, host.EsxVersion) } else if utils.NeedsFormatting(c) { host.Password = "" utils.FormatObject(host, w, c) } else { fmt.Println("Host ID: ", host.ID) fmt.Println(" Username: "******" IP: ", host.Address) fmt.Println(" Tags: ", host.Tags) fmt.Println(" State: ", host.State) fmt.Println(" Metadata: ", host.Metadata) fmt.Println(" AvailabilityZone: ", host.AvailabilityZone) fmt.Println(" Version: ", host.EsxVersion) } return nil }
func showVirtualNetwork(c *cli.Context, w io.Writer) error { err := checkArgCount(c, 1) if err != nil { return err } id := c.Args().First() client.Esxclient, err = client.GetClient(c) if err != nil { return err } network, err := client.Esxclient.VirtualSubnets.Get(id) if err != nil { return err } if c.GlobalIsSet("non-interactive") { fmt.Printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", network.ID, network.Name, network.State, network.Description, network.RoutingType, network.IsDefault, network.Cidr, network.LowIpDynamic, network.HighIpDynamic, network.LowIpStatic, network.HighIpStatic, network.ReservedIpList) } else if utils.NeedsFormatting(c) { utils.FormatObject(network, w, c) } else { fmt.Printf("Network ID: %s\n", network.ID) fmt.Printf(" Name: %s\n", network.Name) fmt.Printf(" State: %s\n", network.State) fmt.Printf(" Description: %s\n", network.Description) fmt.Printf(" Routing Type: %s\n", network.RoutingType) fmt.Printf(" Is Default: %s\n", network.IsDefault) fmt.Printf(" CIDR: %s\n", network.Cidr) fmt.Printf(" Start Dynamic IP: %s\n", network.LowIpDynamic) fmt.Printf(" End Dynamic IP: %s\n", network.HighIpDynamic) fmt.Printf(" Start Static IP: %s\n", network.LowIpStatic) fmt.Printf(" End Static IP: %s\n", network.HighIpStatic) fmt.Printf(" Reserved IP List: %s\n", network.ReservedIpList) } return nil }