-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 303c203
Showing
6 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |