// Deletes an image by id func deleteImage(c *cli.Context) error { err := checkArgNum(c.Args(), 1, "image delete <path>") if err != nil { return err } id := c.Args().First() if confirmed(utils.IsNonInteractive(c)) { client.Esxclient, err = client.GetClient(utils.IsNonInteractive(c)) if err != nil { return err } deleteTask, err := client.Esxclient.Images.Delete(id) if err != nil { return err } _, err = waitOnTaskOperation(deleteTask.ID, c) if err != nil { return err } } else { fmt.Println("OK, canceled") } return nil }
func setDefaultNetwork(c *cli.Context, w io.Writer) error { err := checkArgNum(c.Args(), 1, "network set-default <id>") if err != nil { return err } id := c.Args().First() client.Esxclient, err = client.GetClient(utils.IsNonInteractive(c)) if err != nil { return err } task, err := client.Esxclient.Subnets.SetDefault(id) if err != nil { return err } if confirmed(utils.IsNonInteractive(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 }
// 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 := checkArgNum(c.Args(), 2, "cluster resize <id> <new worker count> [<options>]") 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(utils.IsNonInteractive(c)) if err != nil { return err } if !utils.IsNonInteractive(c) { fmt.Printf("\nResizing cluster %s to worker count %d\n", cluster_id, worker_count) } if confirmed(utils.IsNonInteractive(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 }
// Shows an image based on id func showImage(c *cli.Context, w io.Writer) error { id := c.Args().First() if !utils.IsNonInteractive(c) { 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(utils.IsNonInteractive(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 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 := checkArgNum(c.Args(), 0, "availability-zone create [<options>]") if err != nil { return err } name := c.String("name") if !utils.IsNonInteractive(c) { 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(utils.IsNonInteractive(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 showNetwork(c *cli.Context, w io.Writer) error { err := checkArgNum(c.Args(), 1, "network show <id>") if err != nil { return err } id := c.Args().First() client.Esxclient, err = client.GetClient(utils.IsNonInteractive(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 }
// Retrieves availability zone against specified id. func showAvailabilityZone(c *cli.Context, w io.Writer) error { err := checkArgNum(c.Args(), 1, "availability-zone show <id>") if err != nil { return err } id := c.Args().First() client.Esxclient, err = client.GetClient(utils.IsNonInteractive(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 }
// 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 := checkArgNum(c.Args(), 2, "host set-availability-zone <id> <availability-zone-id>") if err != nil { return err } id := c.Args().First() availabilityZoneId := c.Args()[1] client.Esxclient, err = client.GetClient(utils.IsNonInteractive(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 }
// Sends a delete tenant task to client based on the cli.Context // Returns an error if one occurred func deleteTenant(c *cli.Context) error { err := checkArgNum(c.Args(), 1, "tenant delete <id>") if err != nil { return err } id := c.Args().First() client.Esxclient, err = client.GetClient(utils.IsNonInteractive(c)) if err != nil { return err } deleteTask, err := client.Esxclient.Tenants.Delete(id) if err != nil { return err } _, err = waitOnTaskOperation(deleteTask.ID, c) if err != nil { return err } err = clearConfigTenant(id) if err != nil { return err } return nil }
// Set security groups for a tenant func setSecurityGroups(c *cli.Context) error { err := checkArgNum(c.Args(), 2, "tenant set_security_groups <id> <comma-separated security group names>") if err != nil { return err } id := c.Args().First() items := []string{} if c.Args()[1] != "" { items = regexp.MustCompile(`\s*,\s*`).Split(c.Args()[1], -1) } securityGroups := &photon.SecurityGroupsSpec{ Items: items, } client.Esxclient, err = client.GetClient(utils.IsNonInteractive(c)) if err != nil { return err } task, err := client.Esxclient.Tenants.SetSecurityGroups(id, securityGroups) if err != nil { return err } _, err = waitOnTaskOperation(task.ID, c) if err != nil { return err } return nil }
// List all the hosts in the current deployment // This uses the same back-end code as "deployments list-hosts", but we look up the // deployment ID so that users don't have to specify it. In most or all installations, // there will not be more than one deployment ID. func listHosts(c *cli.Context, w io.Writer) error { err := checkArgNum(c.Args(), 0, "host list") if err != nil { return err } client.Esxclient, err = client.GetClient(utils.IsNonInteractive(c)) if err != nil { return err } // Find the current deployment deployments, err := client.Esxclient.Deployments.GetAll() if err != nil { return err } numDeployments := len(deployments.Items) if numDeployments == 0 { return fmt.Errorf("There are no deployments, so the hosts cannot be listed.") } else if numDeployments > 1 { return fmt.Errorf("There are multiple deployments, which normally should not happen. Use deployments list-hosts.") } id := deployments.Items[0].ID hosts, err := client.Esxclient.Deployments.GetHosts(id) if err != nil { return err } err = printHostList(hosts.Items, w, c) if err != nil { return err } return nil }
func deleteNetwork(c *cli.Context) error { err := checkArgNum(c.Args(), 1, "network delete <id>") if err != nil { return err } id := c.Args().First() client.Esxclient, err = client.GetClient(utils.IsNonInteractive(c)) if err != nil { return err } task, err := client.Esxclient.Subnets.Delete(id) if err != nil { return err } if confirmed(c.GlobalIsSet("non-interactive")) { _, err = waitOnTaskOperation(task.ID, c) if err != nil { return err } } else { fmt.Println("OK. Canceled") } return nil }
// Retrieves tasks from specified flavor func getFlavorTasks(c *cli.Context, w io.Writer) error { err := checkArgNum(c.Args(), 1, "flavor tasks <id> [<options>]") if err != nil { return err } id := c.Args().First() state := c.String("state") options := &photon.TaskGetOptions{ State: state, } client.Esxclient, err = client.GetClient(utils.IsNonInteractive(c)) if err != nil { return err } taskList, err := client.Esxclient.Flavors.GetTasks(id, options) if err != nil { return err } err = printTaskList(taskList.Items, c) if err != nil { return err } return nil }
// Retrieves a list of projects, returns an error if one occurred func listProjects(c *cli.Context, w io.Writer) error { err := checkArgNum(c.Args(), 0, "project list [<options>]") if err != nil { return err } tenantName := c.String("tenant") client.Esxclient, err = client.GetClient(utils.IsNonInteractive(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 }
// Show project info with the specified project id, returns an error if one occurred func showProject(c *cli.Context, w io.Writer) error { err := checkArgNum(c.Args(), 1, "project show <id>") if err != nil { return err } id := c.Args().First() client.Esxclient, err = client.GetClient(utils.IsNonInteractive(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 }
// Sends a "show cluster" request to the API client based on the cli.Context // Returns an error if one occurred func showCluster(c *cli.Context, w io.Writer) error { err := checkArgNum(c.Args(), 1, "cluster show <id>") if err != nil { return err } id := c.Args().First() client.Esxclient, err = client.GetClient(utils.IsNonInteractive(c)) if err != nil { return err } cluster, err := client.Esxclient.Clusters.Get(id) if err != nil { return err } vms, err := client.Esxclient.Clusters.GetVMs(id) if err != nil { return err } var master_vms []photon.VM for _, vm := range vms.Items { for _, tag := range vm.Tags { if strings.Count(tag, ":") == 2 && !strings.Contains(strings.ToLower(tag), "worker") { master_vms = append(master_vms, vm) break } } } if c.GlobalIsSet("non-interactive") { extendedProperties := strings.Trim(strings.TrimLeft(fmt.Sprint(cluster.ExtendedProperties), "map"), "[]") fmt.Printf("%s\t%s\t%s\t%s\t%d\t%s\n", cluster.ID, cluster.Name, cluster.State, cluster.Type, cluster.WorkerCount, extendedProperties) } else if utils.NeedsFormatting(c) { utils.FormatObject(cluster, w, c) } else { fmt.Println("Cluster ID: ", cluster.ID) fmt.Println(" Name: ", cluster.Name) fmt.Println(" State: ", cluster.State) fmt.Println(" Type: ", cluster.Type) fmt.Println(" Worker count: ", cluster.WorkerCount) fmt.Println(" Extended Properties: ", cluster.ExtendedProperties) fmt.Println() } err = printClusterVMs(master_vms, c.GlobalIsSet("non-interactive")) if err != nil { return err } 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 := checkArgNum(c.Args(), 1, "resource-ticket show <name> [<options>]") if err != nil { return err } name := c.Args().First() tenantName := c.String("tenant") client.Esxclient, err = client.GetClient(utils.IsNonInteractive(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 }
// Overwrites the tenant in the config file func setTenant(c *cli.Context) error { err := checkArgNum(c.Args(), 1, "tenant set <name>") if err != nil { return err } name := c.Args().First() client.Esxclient, err = client.GetClient(utils.IsNonInteractive(c)) if err != nil { return err } // Ensure tenant exists id, err := findTenantID(name) if len(id) == 0 || err != nil { return err } config, err := cf.LoadConfig() if err != nil { return err } config.Tenant = &cf.TenantConfiguration{Name: name, ID: id} err = cf.SaveConfig(config) if err != nil { return err } err = clearConfigProject("") if err != nil { return err } if !utils.IsNonInteractive(c) { fmt.Printf("Tenant set to '%s'\n", name) } return nil }
// Set project name and id to config file // Returns an error if one occurred func setProject(c *cli.Context) error { err := checkArgNum(c.Args(), 1, "project set <project name>") if err != nil { return err } name := c.Args().First() client.Esxclient, err = client.GetClient(utils.IsNonInteractive(c)) if err != nil { return err } config, err := cf.LoadConfig() if err != nil { return err } if config == nil || config.Tenant == nil { return fmt.Errorf("Error: Set tenant first using 'tenant set <name>' or '-t <name>' option") } project, err := findProject(config.Tenant.ID, name) if err != nil { return err } config.Project = &cf.ProjectConfiguration{Name: project.Name, ID: project.ID} err = cf.SaveConfig(config) if err != nil { return err } if !utils.IsNonInteractive(c) { fmt.Printf("Project set to '%s'\n", name) } return nil }
// Sends a "delete cluster" request to the API client based on the cli.Context // Returns an error if one occurred func deleteCluster(c *cli.Context) error { err := checkArgNum(c.Args(), 1, "cluster delete <id>") if err != nil { return nil } cluster_id := c.Args().First() if len(cluster_id) == 0 { return fmt.Errorf("Please provide a valid cluster ID") } client.Esxclient, err = client.GetClient(utils.IsNonInteractive(c)) if err != nil { return err } if !utils.IsNonInteractive(c) { fmt.Printf("\nDeleting cluster %s\n", cluster_id) } if confirmed(utils.IsNonInteractive(c)) { deleteTask, err := client.Esxclient.Clusters.Delete(cluster_id) if err != nil { return err } _, err = waitOnTaskOperation(deleteTask.ID, c) if err != nil { return err } } else { fmt.Println("Cancelled") } return nil }
// Retrieves a list of flavors func listFlavors(c *cli.Context, w io.Writer) error { err := checkArgNum(c.Args(), 0, "flavor list [<options>]") if err != nil { return err } client.Esxclient, err = client.GetClient(utils.IsNonInteractive(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 resource tickets, returns an error if one occurred func listResourceTickets(c *cli.Context, w io.Writer) error { err := checkArgNum(c.Args(), 0, "resource-ticket list [<options>]") if err != nil { return err } tenantName := c.String("tenant") client.Esxclient, err = client.GetClient(utils.IsNonInteractive(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 }
func listNetworks(c *cli.Context, w io.Writer) error { err := checkArgNum(c.Args(), 0, "network list [<options>]") if err != nil { return err } client.Esxclient, err = client.GetClient(utils.IsNonInteractive(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\tDescriptions\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 }
// Set security groups for a deployment func setDeploymentSecurityGroups(c *cli.Context) error { var err error var deploymentId string var groups string // We have two cases: // Case 1: arguments are: id groups // Case 2: arguments are: groups if len(c.Args()) == 2 { deploymentId = c.Args()[0] groups = c.Args()[1] } else if len(c.Args()) == 1 { deploymentId, err = getDefaultDeploymentId() if err != nil { return err } groups = c.Args()[0] } else { return fmt.Errorf("Usage: deployments set-security-groups [id] groups") } items := regexp.MustCompile(`\s*,\s*`).Split(groups, -1) securityGroups := &photon.SecurityGroupsSpec{ Items: items, } client.Esxclient, err = client.GetClient(utils.IsNonInteractive(c)) if err != nil { return err } task, err := client.Esxclient.Deployments.SetSecurityGroups(deploymentId, securityGroups) if err != nil { return err } _, err = waitOnTaskOperation(task.ID, c) if err != nil { return err } return nil }
// Lists all images func listImages(c *cli.Context, w io.Writer) error { err := checkArgNum(c.Args(), 0, "image list [<options>]") if err != nil { return err } client.Esxclient, err = client.GetClient(utils.IsNonInteractive(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 }
// Show host info with the specified host ID, returns an error if one occurred func showHost(c *cli.Context, w io.Writer) error { err := checkArgNum(c.Args(), 1, "host show <id>") if err != nil { return err } id := c.Args().First() client.Esxclient, err = client.GetClient(utils.IsNonInteractive(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\t%s\n", host.ID, host.Username, host.Password, host.Address, scriptTag, host.State, scriptMetadata, host.AvailabilityZone, host.EsxVersion) } else if utils.NeedsFormatting(c) { utils.FormatObject(host, w, c) } else { fmt.Println("Host ID: ", host.ID) fmt.Println(" Username: "******" Password: "******" 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 }
// Retrieves tasks for resource ticket func getResourceTicketTasks(c *cli.Context, w io.Writer) error { err := checkArgNum(c.Args(), 1, "resource-ticket tasks <name> [<options>]") if err != nil { return err } name := c.Args().First() tenantName := c.String("tenant") state := c.String("state") client.Esxclient, err = client.GetClient(utils.IsNonInteractive(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 } options := &photon.TaskGetOptions{ State: state, } taskList, err := client.Esxclient.ResourceTickets.GetTasks(rt.ID, options) if err != nil { return err } err = printTaskList(taskList.Items, c) if err != nil { return err } return nil }
func showTenant(c *cli.Context, w io.Writer) error { err := checkArgNum(c.Args(), 1, "tenant show <id>") if err != nil { return err } id := c.Args().First() client.Esxclient, err = client.GetClient(utils.IsNonInteractive(c)) if err != nil { return err } tenant, err := client.Esxclient.Tenants.Get(id) if err != nil { return err } if c.GlobalIsSet("non-interactive") { securityGroups := []string{} for _, s := range tenant.SecurityGroups { securityGroups = append(securityGroups, fmt.Sprintf("%s:%t", s.Name, s.Inherited)) } scriptSecurityGroups := strings.Join(securityGroups, ",") fmt.Printf("%s\t%s\t%s\n", tenant.ID, tenant.Name, scriptSecurityGroups) } else if utils.NeedsFormatting(c) { utils.FormatObject(tenant, w, c) } else { fmt.Println("Tenant ID: ", tenant.ID) fmt.Println(" Name: ", tenant.Name) for i, s := range tenant.SecurityGroups { fmt.Printf(" SecurityGroups %d:\n", i+1) fmt.Println(" Name: ", s.Name) fmt.Println(" Inherited: ", s.Inherited) } } return nil }
// Sends a resume host task to client based on the cli.Context // Returns an error if one occurred func resumeHost(c *cli.Context, w io.Writer) error { err := checkArgNum(c.Args(), 1, "host resume <id>") if err != nil { return err } id := c.Args().First() client.Esxclient, err = client.GetClient(utils.IsNonInteractive(c)) if err != nil { return err } resumeTask, err := client.Esxclient.Hosts.Resume(id) if err != nil { return err } _, err = waitOnTaskOperation(resumeTask.ID, c) if err != nil { return err } return nil }
// Take host with specified id out of maintenance mode // Returns an error if one occurred func exitMaintenanceMode(c *cli.Context, w io.Writer) error { err := checkArgNum(c.Args(), 1, "host exit-maintenance <id>") if err != nil { return err } id := c.Args().First() client.Esxclient, err = client.GetClient(utils.IsNonInteractive(c)) if err != nil { return err } exitTask, err := client.Esxclient.Hosts.ExitMaintenanceMode(id) if err != nil { return err } _, err = waitOnTaskOperation(exitTask.ID, c) if err != nil { return err } return nil }