<commit_before>package profitbricks

import (
	"errors"
	"fmt"
	"time"

	"github.com/docker/machine/libmachine/drivers"
	"github.com/docker/machine/libmachine/log"
	"github.com/docker/machine/libmachine/mcnflag"
	"github.com/docker/machine/libmachine/ssh"
	"github.com/docker/machine/libmachine/state"
	"github.com/profitbricks/profitbricks-sdk-go"
	"io/ioutil"
	"strings"
	"strconv"
)

type Driver struct {
	*drivers.BaseDriver
	URL          string
	Username     string
	Password     string
	ServerId     string
	Ram          int
	Cores        int
	SSHKey       string
	DatacenterId string
	DiskSize     int
	DiskType     string
	Image        string
	Size         int
	Location     string
}

const (
	defaultRegion = "us/lasdev"
	defaultSize = 10
	waitCount = 5
)

func (d *Driver) GetCreateFlags() []mcnflag.Flag {
	return []mcnflag.Flag{
		mcnflag.StringFlag{
			EnvVar: "PROFITBRICKS_ENDPOINT",
			Name:   "profitbricks-endpoint",
			Value:  "https://api.profitbricks.com/rest/v2",
			Usage:  "profitbricks API endpoint",
		},
		mcnflag.StringFlag{
			EnvVar: "PROFITBRICKS_USERNAME",
			Name:   "profitbricks-username",
			Usage:  "profitbricks username",
		},
		mcnflag.StringFlag{
			EnvVar: "PROFITBRICKS_PASSWORD",
			Name:   "profitbricks-password",
			Usage:  "profitbricks password",
		},
		mcnflag.IntFlag{
			EnvVar: "PROFITBRICKS_CORES",
			Name:   "profitbricks-cores",
			Value:  4,
			Usage:  "profitbricks cores (2, 3, 4, 5, 6, etc.)",
		},
		mcnflag.IntFlag{
			EnvVar: "PROFITBRICKS_RAM",
			Name:   "profitbricks-ram",
			Value:  2048,
			Usage:  "profitbricks ram (1024, 2048, 3072, 4096, etc.)",
		},
		mcnflag.IntFlag{
			EnvVar: "PROFITBRICKS_DISK_SIZE",
			Name:   "profitbricks-disk-size",
			Value:  50,
			Usage:  "profitbricks disk size (10, 50, 100, 200, 400)",
		},
		mcnflag.StringFlag{
			EnvVar: "PROFITBRICKS_IMAGE",
			Name:   "profitbricks-image",
			Value:  "Ubuntu-16.04",
			Usage:  "profitbricks image",
		},
		mcnflag.StringFlag{
			EnvVar: "PROFITBRICKS_LOCATION",
			Name:   "profitbricks-location",
			Value:  "us/las",
			Usage:  "profitbricks location",
		},
		mcnflag.StringFlag{
			EnvVar: "PROFITBRICKS_DISK_TYPE",
			Name:   "profitbricks-disk-type",
			Value:  "HDD",
			Usage:  "profitbricks disk type (HDD, SSD)",
		},
	}
}

func NewDriver(hostName, storePath string) drivers.Driver {
	return &Driver{
		Size:     defaultSize,
		Location: defaultRegion,
		BaseDriver: &drivers.BaseDriver{
			MachineName: hostName,
			StorePath:   storePath,
		},
	}
}

func (d *Driver) GetSSHHostname() (string, error) {
	return d.GetIP()
}

func (d *Driver) DriverName() string {
	return "profitbricks"
}

func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {

	d.URL = flags.String("profitbricks-endpoint")
	d.Username = flags.String("profitbricks-username")

	d.Password = flags.String("<PASSWORD>")
	d.DiskSize = flags.Int("profitbricks-disk-size")
	d.Image = flags.String("profitbricks-image")
	d.Cores = flags.Int("profitbricks-cores")
	d.Ram = flags.Int("profitbricks-ram")
	d.Location = flags.String("profitbricks-location")
	d.DiskType = flags.String("profitbricks-disk-type")
	d.SwarmMaster = flags.Bool("swarm-master")
	d.SwarmHost = flags.String("swarm-host")
	d.SwarmDiscovery = flags.String("swarm-discovery")
	d.SetSwarmConfigFromFlags(flags)

	if d.URL == "" {
		d.URL = "https://api.profitbricks.com/rest/v2"
	}

	return nil
}

func (d *Driver) PreCreateCheck() error {
	if d.Username == "" {
		return fmt.Errorf("Please provide username as paramter --profitbricks-username or as environment variable $PROFITBRICKS_USERNAME")
	}
	if d.getImageId(d.Image) == "" {
		return fmt.Errorf("The image %s %s %s", d.Image, d.Location, "does not exist.")
	}
	return nil
}

func (d *Driver) Create() error {

	d.setPB()

	var err error
	if d.SSHKey == "" {
		d.SSHKey, err = d.createSSHKey()
		if err != nil {
			return err
		}
	}
	image := d.getImageId(d.Image)

	ipblockreq := profitbricks.IpBlock{
		Properties: profitbricks.IpBlockProperties{
			Size:     1,
			Location: d.Location,
		},
	}

	ipblockresp := profitbricks.ReserveIpBlock(ipblockreq)

	d.waitTillProvisioned(ipblockresp.Headers.Get("Location"))

	datacenter := profitbricks.Datacenter{
		Properties: profitbricks.DatacenterProperties{
			Name: d.MachineName,
			Location:d.Location,
		},
		Entities:profitbricks.DatacenterEntities{
			Servers: &profitbricks.Servers{
				Items:[]profitbricks.Server{
					profitbricks.Server{
						Properties: profitbricks.ServerProperties{
							Name : d.MachineName,
							Ram: d.Ram,
							Cores: d.Cores,
						},
						Entities:&profitbricks.ServerEntities{
							Volumes: &profitbricks.Volumes{
								Items:[]profitbricks.Volume{
									profitbricks.Volume{
										Properties: profitbricks.VolumeProperties{
											Type:d.DiskType,<commit_msg>Added error handling<commit_after>package profitbricks

import (
	"errors"
	"fmt"
	"time"

	"github.com/docker/machine/libmachine/drivers"
	"github.com/docker/machine/libmachine/log"
	"github.com/docker/machine/libmachine/mcnflag"
	"github.com/docker/machine/libmachine/ssh"
	"github.com/docker/machine/libmachine/state"
	"github.com/profitbricks/profitbricks-sdk-go"
	"io/ioutil"
	"strings"
	"strconv"
)

type Driver struct {
	*drivers.BaseDriver
	URL          string
	Username     string
	Password     string
	ServerId     string
	Ram          int
	Cores        int
	SSHKey       string
	DatacenterId string
	DiskSize     int
	DiskType     string
	Image        string
	Size         int
	Location     string
}

const (
	defaultRegion = "us/lasdev"
	defaultSize = 10
	waitCount = 5
)

func (d *Driver) GetCreateFlags() []mcnflag.Flag {
	return []mcnflag.Flag{
		mcnflag.StringFlag{
			EnvVar: "PROFITBRICKS_ENDPOINT",
			Name:   "profitbricks-endpoint",
			Value:  "https://api.profitbricks.com/rest/v2",
			Usage:  "profitbricks API endpoint",
		},
		mcnflag.StringFlag{
			EnvVar: "PROFITBRICKS_USERNAME",
			Name:   "profitbricks-username",
			Usage:  "profitbricks username",
		},
		mcnflag.StringFlag{
			EnvVar: "PROFITBRICKS_PASSWORD",
			Name:   "profitbricks-password",
			Usage:  "profitbricks password",
		},
		mcnflag.IntFlag{
			EnvVar: "PROFITBRICKS_CORES",
			Name:   "profitbricks-cores",
			Value:  4,
			Usage:  "profitbricks cores (2, 3, 4, 5, 6, etc.)",
		},
		mcnflag.IntFlag{
			EnvVar: "PROFITBRICKS_RAM",
			Name:   "profitbricks-ram",
			Value:  2048,
			Usage:  "profitbricks ram (1024, 2048, 3072, 4096, etc.)",
		},
		mcnflag.IntFlag{
			EnvVar: "PROFITBRICKS_DISK_SIZE",
			Name:   "profitbricks-disk-size",
			Value:  50,
			Usage:  "profitbricks disk size (10, 50, 100, 200, 400)",
		},
		mcnflag.StringFlag{
			EnvVar: "PROFITBRICKS_IMAGE",
			Name:   "profitbricks-image",
			Value:  "Ubuntu-16.04",
			Usage:  "profitbricks image",
		},
		mcnflag.StringFlag{
			EnvVar: "PROFITBRICKS_LOCATION",
			Name:   "profitbricks-location",
			Value:  "us/las",
			Usage:  "profitbricks location",
		},
		mcnflag.StringFlag{
			EnvVar: "PROFITBRICKS_DISK_TYPE",
			Name:   "profitbricks-disk-type",
			Value:  "HDD",
			Usage:  "profitbricks disk type (HDD, SSD)",
		},
	}
}

func NewDriver(hostName, storePath string) drivers.Driver {
	return &Driver{
		Size:     defaultSize,
		Location: defaultRegion,
		BaseDriver: &drivers.BaseDriver{
			MachineName: hostName,
			StorePath:   storePath,
		},
	}
}

func (d *Driver) GetSSHHostname() (string, error) {
	return d.GetIP()
}

func (d *Driver) DriverName() string {
	return "profitbricks"
}

func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {

	d.URL = flags.String("profitbricks-endpoint")
	d.Username = flags.String("profitbricks-username")

	d.Password = flags.String("<PASSWORD>")
	d.DiskSize = flags.Int("profitbricks-disk-size")
	d.Image = flags.String("profitbricks-image")
	d.Cores = flags.Int("profitbricks-cores")
	d.Ram = flags.Int("profitbricks-ram")
	d.Location = flags.String("profitbricks-location")
	d.DiskType = flags.String("profitbricks-disk-type")
	d.SwarmMaster = flags.Bool("swarm-master")
	d.SwarmHost = flags.String("swarm-host")
	d.SwarmDiscovery = flags.String("swarm-discovery")
	d.SetSwarmConfigFromFlags(flags)

	if d.URL == "" {
		d.URL = "https://api.profitbricks.com/rest/v2"
	}

	return nil
}

func (d *Driver) PreCreateCheck() error {
	if d.Username == "" {
		return fmt.Errorf("Please provide username as paramter --profitbricks-username or as environment variable $PROFITBRICKS_USERNAME")
	}
	if d.getImageId(d.Image) == "" {
		return fmt.Errorf("The image %s %s %s", d.Image, d.Location, "does not exist.")
	}
	return nil
}

func (d *Driver) Create() error {

	d.setPB()

	var err error
	if d.SSHKey == "" {
		d.SSHKey, err = d.createSSHKey()
		if err != nil {
			return err
		}
	}
	image := d.getImageId(d.Image)

	ipblockreq := profitbricks.IpBlock{
		Properties: profitbricks.IpBlockProperties{
			Size:     1,
			Location: d.Location,
		},
	}

	ipblockresp := profitbricks.ReserveIpBlock(ipblockreq)

	if (ipblockresp.StatusCode > 299) {
		return fmt.Errorf("An error occurred while reserving an ipblock: %s", ipblockresp.StatusCode)
	}

	d.waitTillProvisioned(ipblockresp.Headers.Get("Location"))

	datacenter := profitbricks.Datacenter{
		Properties: profitbricks.DatacenterProperties{
			Name: d.MachineName,
			Location:d.Location,
		},
		Entities:profitbricks.DatacenterEntities{
			Servers: &profitbricks.Servers{
				Items:[]profitbricks.Server{
					profitbricks.Server{
						Properties: profitbricks.ServerProperties{
							Name : d.MachineName,
							Ram: d.Ram,
							Cores: d.Cores,
						},
						Entities:&profitbricks.ServerEntities{
							Volumes: &profitbricks.Volumes{
								Items:[]profitbricks.Volume{
									profitbricks.Volume{
										Properties: profitbricks.VolumeProperties{
											Type:d.DiskType,
{'fraction_non_alphanumeric': 0.13305464926590538, 'fraction_numerical': 0.010705546492659054, 'mean_word_length': 3.620348563353745, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3255100', 'n_tokens_mistral': 4128, 'n_tokens_neox': 3602, 'n_words': 672}