Esempio n. 1
0
func (cloug *Cloug) VmCreate(vm *lobster.VirtualMachine, options *lobster.VMIVmCreateOptions) (string, error) {
	tmpl := compute.Instance{
		Name:      vm.Name,
		Region:    cloug.config.Region,
		NetworkID: cloug.config.NetworkID,
		Image:     compute.Image{ID: options.ImageIdentification},
		Flavor: compute.Flavor{
			ID:         vm.Plan.Identification,
			NumCores:   vm.Plan.Cpu,
			DiskGB:     vm.Plan.Storage,
			MemoryMB:   vm.Plan.Ram,
			TransferGB: vm.Plan.Bandwidth,
		},
	}

	if options.SSHKey.Key != "" {
		tmpl.PublicKey = compute.PublicKey{
			Key: []byte(options.SSHKey.Key),
		}
	}

	tmpl.Details = make(map[string]string)
	for k, v := range vm.Plan.Metadata {
		tmpl.Details[k] = v
	}

	instance, err := cloug.service.CreateInstance(&tmpl)

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

	if instance.Username != "" {
		vm.SetMetadata("username", instance.Username)
	}
	if instance.Password != "" {
		vm.SetMetadata("password", instance.Password)
	}

	return instance.ID, nil
}