Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Maigard committed Aug 4, 2017
0 parents commit 303c203
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 0 deletions.
9 changes: 9 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
12 changes: 12 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -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,
})
}
5 changes: 5 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resource "ovirt_vm" "my_vm" {
name = "my_first_vm"
cluster = "Default"
template = "centos-7-b"
}
8 changes: 8 additions & 0 deletions ovirt/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ovirt

type Config struct {
Username string
Password string
Profile string
Address string
}
39 changes: 39 additions & 0 deletions ovirt/provider.go
Original file line number Diff line number Diff line change
@@ -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))
}
92 changes: 92 additions & 0 deletions ovirt/vm.go
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit 303c203

Please sign in to comment.