diff --git a/LICENSE b/LICENSE new file mode 100644 index 00000000..58ef15cc --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +Copyright 2017 Battelle Memorial Institute + +Redistribution and use in source and binary forms, with or without modification, arepermitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AREDISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCHDAMAGE. diff --git a/main.go b/main.go new file mode 100644 index 00000000..85b0e6e5 --- /dev/null +++ b/main.go @@ -0,0 +1,12 @@ +package main + +import ( + "github.com/hashicorp/terraform/plugin" + "github.com/EMSL-MSC/terraform-provider-ovirt/ovirt" +) + +func main() { + plugin.Serve(&plugin.ServeOpts{ + ProviderFunc: ovirt.Provider, + }) +} diff --git a/main.tf b/main.tf new file mode 100644 index 00000000..5e84fd0f --- /dev/null +++ b/main.tf @@ -0,0 +1,5 @@ +resource "ovirt_vm" "my_vm" { + name = "my_first_vm" + cluster = "Default" + template = "centos-7-b" +} diff --git a/ovirt/config.go b/ovirt/config.go new file mode 100644 index 00000000..5034336c --- /dev/null +++ b/ovirt/config.go @@ -0,0 +1,8 @@ +package ovirt + +type Config struct { + Username string + Password string + Profile string + Address string +} diff --git a/ovirt/provider.go b/ovirt/provider.go new file mode 100644 index 00000000..485b6c5c --- /dev/null +++ b/ovirt/provider.go @@ -0,0 +1,39 @@ +package ovirt + +import ( + "github.com/EMSL-MSC/ovirtapi" + "github.com/hashicorp/terraform/helper/schema" + "github.com/hashicorp/terraform/terraform" +) + +// Provider returns oVirt provider configuration +func Provider() terraform.ResourceProvider { + return &schema.Provider{ + Schema: map[string]*schema.Schema{ + "username": &schema.Schema{ + Type: schema.TypeString, + Required: true, + Description: "Login username", + }, + "password": &schema.Schema{ + Type: schema.TypeString, + Required: true, + Description: "Login password", + Sensitive: true, + }, + "url": &schema.Schema{ + Type: schema.TypeString, + Required: true, + Description: "Ovirt server url", + }, + }, + ConfigureFunc: ConfigureProvider, + ResourcesMap: map[string]*schema.Resource{ + "ovirt_vm": resourceVM(), + }, + } +} + +func ConfigureProvider(d *schema.ResourceData) (interface{}, error) { + return ovirtapi.NewConnection(d.Get("url").(string), d.Get("username").(string), d.Get("password").(string)) +} diff --git a/ovirt/vm.go b/ovirt/vm.go new file mode 100644 index 00000000..dcfeca85 --- /dev/null +++ b/ovirt/vm.go @@ -0,0 +1,92 @@ +package ovirt + +import ( + "time" + + "github.com/EMSL-MSC/ovirtapi" + "github.com/hashicorp/terraform/helper/schema" +) + +func resourceVM() *schema.Resource { + return &schema.Resource{ + Create: resourceVMCreate, + Read: resourceVMRead, + Update: resourceVMUpdate, + Delete: resourceVMDelete, + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + }, + "cluster": { + Type: schema.TypeString, + Required: true, + }, + "template": { + Type: schema.TypeString, + Required: true, + }, + }, + } +} + +func resourceVMCreate(d *schema.ResourceData, meta interface{}) error { + con := meta.(*ovirtapi.Connection) + newVM := con.NewVM() + newVM.Name = d.Get("name").(string) + + cluster := con.NewCluster() + cluster.Name = d.Get("cluster").(string) + newVM.Cluster = cluster + + template := con.NewTemplate() + template.Name = d.Get("template").(string) + newVM.Template = template + + err := newVM.Save() + if err != nil { + return err + } + + for newVM.Status != "down" { + time.Sleep(time.Second) + newVM.Update() + } + + newVM.Start("", "", "", "", "", nil) + d.SetId(newVM.ID) + return nil +} + +func resourceVMUpdate(d *schema.ResourceData, meta interface{}) error { + return nil +} +func resourceVMRead(d *schema.ResourceData, meta interface{}) error { + con := meta.(*ovirtapi.Connection) + vm, err := con.GetVM(d.Id()) + + if err != nil { + d.SetId("") + return nil + } + d.Set("name", vm.Name) + d.Set("cluster", vm.Cluster.Name) + d.Set("template", vm.Template.Name) + return nil +} + +func resourceVMDelete(d *schema.ResourceData, meta interface{}) error { + con := meta.(*ovirtapi.Connection) + vm, err := con.GetVM(d.Id()) + if err != nil { + return nil + } + if vm.Status != "down" { + vm.Stop("false") + } + for vm.Status != "down" { + time.Sleep(time.Second) + vm.Update() + } + return vm.Delete() +}