Example #1
0
// Sends a create disk task to client based on the cli.Context
// Returns an error if one occurred
func createDisk(c *cli.Context) error {
	err := checkArgNum(c.Args(), 0, "disk create [<options>]")
	if err != nil {
		return err
	}
	name := c.String("name")
	flavor := c.String("flavor")
	capacityGB := c.Int("capacityGB")
	affinities := c.String("affinities")
	tenantName := c.String("tenant")
	projectName := c.String("project")
	tags := c.String("tags")

	client.Esxclient, err = client.GetClient(c.GlobalIsSet("non-interactive"))
	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
	}

	if !c.GlobalIsSet("non-interactive") {
		name, err = askForInput("Disk name: ", name)
		if err != nil {
			return err
		}
		flavor, err = askForInput("Disk Flavor: ", flavor)
		if err != nil {
			return err
		}
		if !c.IsSet("capacityGB") {
			capacity, err := askForInput("Disk capacity in GB: ", "")
			if err != nil {
				return err
			}
			capacityGB, err = strconv.Atoi(capacity)
			if err != nil {
				return err
			}
		}
	}

	if len(name) == 0 || len(flavor) == 0 {
		return fmt.Errorf("please provide disk name and flavor")
	}

	affinitiesList, err := parseAffinitiesListFromFlag(affinities)
	if err != nil {
		return err
	}

	diskSpec := photon.DiskCreateSpec{}
	diskSpec.Name = name
	diskSpec.Flavor = flavor
	diskSpec.CapacityGB = capacityGB
	diskSpec.Kind = "persistent-disk"
	diskSpec.Affinities = affinitiesList
	diskSpec.Tags = regexp.MustCompile(`\s*,\s*`).Split(tags, -1)

	if !c.GlobalIsSet("non-interactive") {
		fmt.Printf("\nCreating disk: %s (%s)\n", diskSpec.Name, diskSpec.Flavor)
		fmt.Printf("Tenant: %s, project: %s\n", tenant.Name, project.Name)
	}

	if confirmed(c.GlobalIsSet("non-interactive")) {
		createTask, err := client.Esxclient.Projects.CreateDisk(project.ID, &diskSpec)
		if err != nil {
			return err
		}

		_, err = waitOnTaskOperation(createTask.ID, c)
		if err != nil {
			return err
		}
	} else {
		fmt.Println("OK. Canceled")
	}

	return nil
}