Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use go.ovirtsdk4 library to refactor the provider #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Local .terraform directories
**/.terraform/*

# .tfstate files
*.tfstate
*.tfstate.*

# Crash log files
crash.log

# Ignore any .tfvars files that are generated automatically for each Terraform run. Most
# .tfvars files are managed as part of configuration and so should be included in
# version control.
#
# example.tfvars

34 changes: 17 additions & 17 deletions ovirt/data_source_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ package ovirt

import (
"fmt"
"strconv"

"github.com/EMSL-MSC/ovirtapi"
"github.com/hashicorp/terraform/helper/schema"
ovirtsdk4 "gopkg.in/imjoey/go-ovirt.v4"
)

func dataSourceDisk() *schema.Resource {
Expand Down Expand Up @@ -47,24 +46,25 @@ func dataSourceDisk() *schema.Resource {
}

func dataSourceDiskRead(d *schema.ResourceData, meta interface{}) error {
con := meta.(*ovirtapi.Connection)
disks, err := con.GetAllDisks()
conn := meta.(*ovirtsdk4.Connection)

listResp, err := conn.SystemService().DisksService().
List().Search(fmt.Sprintf("name=%s", d.Get("name"))).Send()
if err != nil {
d.SetId("")
return err
}
for _, disk := range disks {
if disk.Name == d.Get("name") {
d.Set("size", disk.ProvisionedSize)
d.Set("format", disk.Format)
d.Set("storage_domain_id", disk.StorageDomains.StorageDomain[0].ID)
shareable, _ := strconv.ParseBool(disk.Shareable)
d.Set("shareable", shareable)
sparse, _ := strconv.ParseBool(disk.Sparse)
d.Set("sparse", sparse)
return nil
}

disks, ok := listResp.Disks()
if !ok && len(disks.Slice()) > 0 {
d.SetId("")
return nil
}

return fmt.Errorf("Disk %s not found", d.Get("name"))
disk := disks.Slice()[0]
d.Set("size", disk.MustProvisionedSize())
d.Set("format", disk.MustFormat())
d.Set("storage_domain_id", disk.MustStorageDomains().Slice()[0].MustId())
d.Set("shareable", disk.MustShareable())
d.Set("sparse", disk.MustSparse())
return nil
}
9 changes: 7 additions & 2 deletions ovirt/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
package ovirt

import (
"github.com/EMSL-MSC/ovirtapi"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
ovirtsdk4 "gopkg.in/imjoey/go-ovirt.v4"
)

// Provider returns oVirt provider configuration
Expand Down Expand Up @@ -45,5 +45,10 @@ func Provider() terraform.ResourceProvider {
}

func ConfigureProvider(d *schema.ResourceData) (interface{}, error) {
return ovirtapi.NewConnection(d.Get("url").(string), d.Get("username").(string), d.Get("password").(string), false)
return ovirtsdk4.NewConnectionBuilder().
URL(d.Get("url").(string)).
Username(d.Get("username").(string)).
Password(d.Get("password").(string)).
Insecure(true).
Build()
}
95 changes: 56 additions & 39 deletions ovirt/resource_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
package ovirt

import (
"strconv"

"github.com/EMSL-MSC/ovirtapi"
"github.com/hashicorp/terraform/helper/schema"
ovirtsdk4 "gopkg.in/imjoey/go-ovirt.v4"
)

func resourceDisk() *schema.Resource {
Expand Down Expand Up @@ -49,65 +47,84 @@ func resourceDisk() *schema.Resource {
Optional: true,
ForceNew: true,
},
// "qcow_version" is the only field supporting Disk-Update
},
}
}

func resourceDiskCreate(d *schema.ResourceData, meta interface{}) error {
con := meta.(*ovirtapi.Connection)
conn := meta.(*ovirtsdk4.Connection)

newDisk := con.NewDisk()
err := resourceDiskModify(d, newDisk)
diskBuilder := ovirtsdk4.NewDiskBuilder().
Name(d.Get("name").(string)).
Format(ovirtsdk4.DiskFormat(d.Get("format").(string))).
ProvisionedSize(int64(d.Get("size").(int))).
StorageDomainsOfAny(
ovirtsdk4.NewStorageDomainBuilder().
Id(d.Get("storage_domain_id").(string)).
MustBuild())
if shareable, ok := d.GetOkExists("shareable"); ok {
diskBuilder.Shareable(shareable.(bool))
}
if sparse, ok := d.GetOkExists("sparse"); ok {
diskBuilder.Sparse(sparse.(bool))
}
disk, err := diskBuilder.Build()
if err != nil {
newDisk.Delete()
return err
}
d.SetId(newDisk.ID)
return nil
}

func resourceDiskModify(d *schema.ResourceData, disk *ovirtapi.Disk) error {
disk.ProvisionedSize = d.Get("size").(int)
disk.Format = d.Get("format").(string)
disk.Name = d.Get("name").(string)
storageDomains := ovirtapi.StorageDomains{}
storageDomains.StorageDomain = append(storageDomains.StorageDomain, ovirtapi.Link{
ID: d.Get("storage_domain_id").(string),
})
disk.StorageDomains = &storageDomains
if d.Get("shareable").(bool) {
disk.Shareable = "true"
}
if d.Get("sparse").(bool) {
disk.Sparse = "true"
addResp, err := conn.SystemService().DisksService().Add().Disk(disk).Send()
if err != nil {
return err
}
return disk.Save()

d.SetId(addResp.MustDisk().MustId())
return nil
}

func resourceDiskRead(d *schema.ResourceData, meta interface{}) error {
con := meta.(*ovirtapi.Connection)
disk, err := con.GetDisk(d.Id())
conn := meta.(*ovirtsdk4.Connection)
getDiskResp, err := conn.SystemService().DisksService().
DiskService(d.Id()).Get().Send()
if err != nil {
return err
}

disk, ok := getDiskResp.Disk()
if !ok {
d.SetId("")
return nil
}

d.Set("name", disk.Name)
d.Set("size", disk.ProvisionedSize)
d.Set("format", disk.Format)
d.Set("storage_domain_id", disk.StorageDomains.StorageDomain[0].ID)
shareable, _ := strconv.ParseBool(disk.Shareable)
d.Set("shareable", shareable)
sparse, _ := strconv.ParseBool(disk.Sparse)
d.Set("sparse", sparse)
d.Set("name", disk.MustName())
d.Set("size", disk.MustProvisionedSize())
d.Set("format", disk.MustFormat())

if sds, ok := disk.StorageDomains(); ok {
if len(sds.Slice()) > 0 {
d.Set("storage_domain_id", sds.Slice()[0].MustId())
}
}

if shareable, ok := disk.Shareable(); ok {
d.Set("shareable", shareable)
}

if sparse, ok := disk.Sparse(); ok {
d.Set("sparse", sparse)
}

return nil
}

func resourceDiskDelete(d *schema.ResourceData, meta interface{}) error {
con := meta.(*ovirtapi.Connection)
disk, err := con.GetDisk(d.Id())
conn := meta.(*ovirtsdk4.Connection)

_, err := conn.SystemService().DisksService().
DiskService(d.Id()).Remove().Send()
if err != nil {
return nil
return err
}
return disk.Delete()
return nil
}
Loading