Commit 86ec9747 authored by Yann KAISER's avatar Yann KAISER

Fixed errors and added timeout option in provider

parent 66cad54d
......@@ -17,6 +17,7 @@ type Config struct {
username string
password string
oneflowHost string
vmTimeout int
}
/*
......@@ -29,6 +30,7 @@ type Client struct {
ServiceSession *oneflow.Client
NetworkSession *network.Client
SystemSession *system.Client
VMTimeout int
}
/*
......@@ -52,5 +54,6 @@ func (c *Config) Client() (interface{}, error) {
Password: c.password,
Host: c.oneflowHost,
})
oclient.VMTimeout = c.vmTimeout
return &oclient, nil
}
......@@ -39,6 +39,12 @@ func Provider() *schema.Provider {
Optional: true,
Description: "oneflow_host",
},
"vm_timeout": {
Type: schema.TypeInt,
Optional: true,
Description: "The time (in seconds) the provider shall while creating a VM, until it considers the VM is either blocked or not created.",
},
},
ResourcesMap: map[string]*schema.Resource{
"one_host": resourceHost(),
......@@ -109,11 +115,17 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
oneflowHost = v.(string)
}
var vmTimeout = 60
if v, ok := d.GetOk("vm_timeout"); ok {
vmTimeout = v.(int)
}
config := Config{
endpoint: d.Get("endpoint").(string),
username: username,
password: passwd,
oneflowHost: oneflowHost,
vmTimeout: vmTimeout,
}
// Return new client
return config.Client()
......
......@@ -30,7 +30,20 @@ func resourceVM() *schema.Resource {
},
"template_id": &schema.Schema{
Type: schema.TypeInt,
Required: true,
Optional: true,
},
"cpu": &schema.Schema{
Type: schema.TypeFloat,
Optional: true,
},
"vcpu": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
},
"memory": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
},
"datastore_id": &schema.Schema{
......@@ -141,7 +154,6 @@ func resourceVMCreate(d *schema.ResourceData, m interface{}) error {
vmData := vm.VMData{
Name: d.Get("name").(string),
TemplateID: d.Get("template_id").(int),
OnHold: d.Get("on_hold").(bool),
NotEnforceOvercommit: d.Get("no_overcommit").(bool),
}
......@@ -236,13 +248,29 @@ func resourceVMCreate(d *schema.ResourceData, m interface{}) error {
vmData.Template.NICList = append(vmData.Template.NICList, &network)
}
datastoreID, hasDID := d.GetOk("datastore_id")
if v, ok := d.GetOk("host_id"); !ok || !hasDID {
templateID, hasTplID := d.GetOk("template_id")
cpuCount, hasCPUCount := d.GetOk("cpu")
vcpuCount, hasVCpuCount := d.GetOk("vcpu")
memory, hasMemory := d.GetOk("memory")
if hasTplID {
vmData.TemplateID = templateID.(int)
if hasCPUCount && hasVCpuCount && hasMemory {
vmData.Template.CPUCount = cpuCount.(float64)
vmData.Template.VcpuCount = vcpuCount.(int)
vmData.Template.Memory = memory.(int)
}
if err = session.InstanciateVMTemplate(&vmData); err != nil {
return err
}
} else {
if !hasCPUCount || !hasVCpuCount || !hasMemory {
return fmt.Errorf("")
}
vmData.Template.CPUCount = cpuCount.(float64)
vmData.Template.VcpuCount = vcpuCount.(int)
vmData.Template.Memory = memory.(int)
if err = session.AllocateVM(&vmData); err != nil {
return err
}
......@@ -250,8 +278,26 @@ func resourceVMCreate(d *schema.ResourceData, m interface{}) error {
if err = session.WaitForVMState(vmData.ID, 1, -1, 10, 1, time.Second); err != nil {
return err
}
vmData.HostID = v.(int)
vmData.DatastoreID = datastoreID.(int)
datastoreID, hasDID := d.GetOk("datastore_id")
hostID, hasHID := d.GetOk("host_id")
if hasDID && hasHID {
vmData.HostID = hostID.(int)
vmData.DatastoreID = datastoreID.(int)
} else {
if !hasDID {
return fmt.Errorf("You need to set a datastore_id")
}
hostList, err := session.FetchHostList()
if err != nil {
return err
}
host := vm.BestHostSelection(hostList)
vmData.HostID = host.ID
vmData.DatastoreID = datastoreID.(int)
}
if err = session.DeployVM(&vmData); err != nil {
return err
}
......@@ -274,7 +320,7 @@ func resourceVMCreate(d *schema.ResourceData, m interface{}) error {
d.SetId(strconv.Itoa(vmData.ID))
if err = session.WaitForVMState(vmData.ID, vm.StateActive, vm.LCMStateRunning, 60, 1, time.Second); err != nil {
if err = session.WaitForVMState(vmData.ID, vm.StateActive, vm.LCMStateRunning, m.(*Client).VMTimeout, 1, time.Second); err != nil {
return err
}
......@@ -301,6 +347,10 @@ func resourceVMRead(d *schema.ResourceData, m interface{}) error {
d.SetId(strconv.Itoa(foundList[0].ID))
} else if foundListLen > 1 {
return fmt.Errorf("Found multiple VMs with same name or ip")
} else {
if d.Id() != "" {
return resourceVMDelete(d, m)
}
}
return nil
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment