Skip to content

Commit

Permalink
Merge pull request #163 from imjoey/add_resourceid_parser
Browse files Browse the repository at this point in the history
Add general purpose function to parse resource ID
imjoey authored Jul 11, 2019

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents f8d2bb0 + e9d2289 commit b78f491
Showing 8 changed files with 92 additions and 42 deletions.
19 changes: 6 additions & 13 deletions ovirt/resource_ovirt_disk_attachment.go
Original file line number Diff line number Diff line change
@@ -7,9 +7,7 @@
package ovirt

import (
"fmt"
"log"
"strings"
"time"

"github.com/hashicorp/terraform/helper/resource"
@@ -134,10 +132,11 @@ func resourceOvirtDiskAttachmentCreate(d *schema.ResourceData, meta interface{})
func resourceOvirtDiskAttachmentUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*ovirtsdk4.Connection)

vmID, diskID, err := getVMIDAndDiskID(d.Id())
parts, err := parseResourceID(d.Id(), 2)
if err != nil {
return err
}
vmID, diskID := parts[0], parts[1]

paramDiskAttachment := ovirtsdk4.NewDiskAttachmentBuilder()
attributeUpdate := false
@@ -173,10 +172,11 @@ func resourceOvirtDiskAttachmentRead(d *schema.ResourceData, meta interface{}) e
conn := meta.(*ovirtsdk4.Connection)
// Disk ID is equals to its relevant Disk Attachment ID
// Sess: https://github.com/oVirt/ovirt-engine/blob/68753f46f09419ddcdbb632453501273697d1a20/backend/manager/modules/restapi/types/src/main/java/org/ovirt/engine/api/restapi/types/DiskAttachmentMapper.java
vmID, diskID, err := getVMIDAndDiskID(d.Id())
parts, err := parseResourceID(d.Id(), 2)
if err != nil {
return err
}
vmID, diskID := parts[0], parts[1]
d.Set("vm_id", vmID)
d.Set("disk_id", diskID)

@@ -211,10 +211,11 @@ func resourceOvirtDiskAttachmentRead(d *schema.ResourceData, meta interface{}) e
func resourceOvirtDiskAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*ovirtsdk4.Connection)

vmID, diskID, err := getVMIDAndDiskID(d.Id())
parts, err := parseResourceID(d.Id(), 2)
if err != nil {
return err
}
vmID, diskID := parts[0], parts[1]

diskAttachmentService := conn.SystemService().
VmsService().
@@ -267,14 +268,6 @@ func resourceOvirtDiskAttachmentDelete(d *schema.ResourceData, meta interface{})
return nil
}

func getVMIDAndDiskID(rsID string) (string, string, error) {
parts := strings.Split(rsID, ":")
if len(parts) != 2 {
return "", "", fmt.Errorf("Invalid resource id")
}
return parts[0], parts[1], nil
}

// DiskAttachmentStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
// an oVirt DiskAttachment.
func DiskAttachmentStateRefreshFunc(conn *ovirtsdk4.Connection, vmID, diskID string) resource.StateRefreshFunc {
6 changes: 4 additions & 2 deletions ovirt/resource_ovirt_disk_attachment_test.go
Original file line number Diff line number Diff line change
@@ -55,10 +55,11 @@ func testAccCheckDiskAttachmentDestroy(s *terraform.State) error {
continue
}

vmID, diskID, err := getVMIDAndDiskID(rs.Primary.ID)
parts, err := parseResourceID(rs.Primary.ID, 2)
if err != nil {
return err
}
vmID, diskID := parts[0], parts[1]

getResp, err := conn.SystemService().VmsService().
VmService(vmID).
@@ -89,10 +90,11 @@ func testAccCheckOvirtDiskAttachmentExists(n string, diskAttachment *ovirtsdk4.D
return fmt.Errorf("No Disk attachment ID is set")
}

vmID, diskID, err := getVMIDAndDiskID(rs.Primary.ID)
parts, err := parseResourceID(rs.Primary.ID, 2)
if err != nil {
return err
}
vmID, diskID := parts[0], parts[1]

conn := testAccProvider.Meta().(*ovirtsdk4.Connection)
getResp, err := conn.SystemService().VmsService().
16 changes: 5 additions & 11 deletions ovirt/resource_ovirt_snapshot.go
Original file line number Diff line number Diff line change
@@ -3,7 +3,6 @@ package ovirt
import (
"fmt"
"log"
"strings"
"time"

"github.com/hashicorp/terraform/helper/resource"
@@ -103,10 +102,11 @@ func resourceOvirtSnapshotCreate(d *schema.ResourceData, meta interface{}) error

func resourceOvirtSnapshotRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*ovirtsdk4.Connection)
vmID, snapshotID, err := getVMIDAndSnapshotID(d.Id())
parts, err := parseResourceID(d.Id(), 2)
if err != nil {
return err
}
vmID, snapshotID := parts[0], parts[1]

d.Set("vm_id", vmID)

@@ -138,10 +138,12 @@ func resourceOvirtSnapshotRead(d *schema.ResourceData, meta interface{}) error {

func resourceOvirtSnapshotDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*ovirtsdk4.Connection)
vmID, snapshotID, err := getVMIDAndSnapshotID(d.Id())
parts, err := parseResourceID(d.Id(), 2)
if err != nil {
return err
}
vmID, snapshotID := parts[0], parts[1]

snapshotService := conn.SystemService().
VmsService().
VmService(vmID).
@@ -185,11 +187,3 @@ func SnapshotStateRefreshFunc(conn *ovirtsdk4.Connection, vmID, snapshotID strin
return r.MustSnapshot, string(r.MustSnapshot().MustSnapshotStatus()), nil
}
}

func getVMIDAndSnapshotID(rsID string) (string, string, error) {
parts := strings.Split(rsID, ":")
if len(parts) != 2 {
return "", "", fmt.Errorf("Invalid Snapshot ID: %s", rsID)
}
return parts[0], parts[1], nil
}
6 changes: 4 additions & 2 deletions ovirt/resource_ovirt_snapshot_test.go
Original file line number Diff line number Diff line change
@@ -42,10 +42,11 @@ func testAccCheckSnapshotDestroy(s *terraform.State) error {
continue
}

vmID, snapshotID, err := getVMIDAndSnapshotID(rs.Primary.ID)
parts, err := parseResourceID(rs.Primary.ID, 2)
if err != nil {
return err
}
vmID, snapshotID := parts[0], parts[1]

getResp, err := conn.SystemService().
VmsService().
@@ -78,10 +79,11 @@ func testAccCheckOvirtSnapshotExists(n string, v *ovirtsdk4.Snapshot) resource.T
return fmt.Errorf("No Snapshot ID is set")
}

vmID, snapshotID, err := getVMIDAndSnapshotID(rs.Primary.ID)
parts, err := parseResourceID(rs.Primary.ID, 2)
if err != nil {
return err
}
vmID, snapshotID := parts[0], parts[1]

conn := testAccProvider.Meta().(*ovirtsdk4.Connection)
getResp, err := conn.SystemService().
15 changes: 4 additions & 11 deletions ovirt/resource_ovirt_vnic.go
Original file line number Diff line number Diff line change
@@ -9,7 +9,6 @@ package ovirt
import (
"fmt"
"log"
"strings"

"github.com/hashicorp/terraform/helper/schema"
ovirtsdk4 "github.com/ovirt/go-ovirt"
@@ -75,10 +74,11 @@ func resourceOvirtVnicCreate(d *schema.ResourceData, meta interface{}) error {

func resourceOvirtVnicRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*ovirtsdk4.Connection)
vmID, vnicID, err := getVMIDAndNicID(d.Id())
parts, err := parseResourceID(d.Id(), 2)
if err != nil {
return err
}
vmID, vnicID := parts[0], parts[1]
d.Set("vm_id", vmID)

getVnicResp, err := conn.SystemService().
@@ -104,10 +104,11 @@ func resourceOvirtVnicRead(d *schema.ResourceData, meta interface{}) error {

func resourceOvirtVnicDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*ovirtsdk4.Connection)
vmID, vnicID, err := getVMIDAndNicID(d.Id())
parts, err := parseResourceID(d.Id(), 2)
if err != nil {
return err
}
vmID, vnicID := parts[0], parts[1]

nicService := conn.SystemService().
VmsService().
@@ -134,11 +135,3 @@ func resourceOvirtVnicDelete(d *schema.ResourceData, meta interface{}) error {
}
return nil
}

func getVMIDAndNicID(id string) (string, string, error) {
parts := strings.Split(id, ":")
if len(parts) != 2 {
return "", "", fmt.Errorf("Invalid resource id")
}
return parts[0], parts[1], nil
}
7 changes: 4 additions & 3 deletions ovirt/resource_ovirt_vnic_test.go
Original file line number Diff line number Diff line change
@@ -51,11 +51,11 @@ func testAccCheckVnicDestroy(s *terraform.State) error {
if rs.Type != "ovirt_vnic" {
continue
}

vmID, nicID, err := getVMIDAndNicID(rs.Primary.ID)
parts, err := parseResourceID(rs.Primary.ID, 2)
if err != nil {
return err
}
vmID, nicID := parts[0], parts[1]

getResp, err := conn.SystemService().VmsService().
VmService(vmID).
@@ -86,10 +86,11 @@ func testAccCheckOvirtVnicExists(n string, v *ovirtsdk4.Nic) resource.TestCheckF
return fmt.Errorf("No Vnic ID is set")
}

vmID, nicID, err := getVMIDAndNicID(rs.Primary.ID)
parts, err := parseResourceID(rs.Primary.ID, 2)
if err != nil {
return err
}
vmID, nicID := parts[0], parts[1]

conn := testAccProvider.Meta().(*ovirtsdk4.Connection)
getResp, err := conn.SystemService().VmsService().
9 changes: 9 additions & 0 deletions ovirt/utils.go
Original file line number Diff line number Diff line change
@@ -55,3 +55,12 @@ func macRange() schema.SchemaValidateFunc {

}
}

func parseResourceID(id string, count int) ([]string, error) {
parts := strings.Split(id, ":")

if len(parts) != count {
return nil, fmt.Errorf("Invalid Resource ID %s, expected %d parts, got %d", id, count, len(parts))
}
return parts, nil
}
56 changes: 56 additions & 0 deletions ovirt/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package ovirt

import "testing"

func TestParseResourceID(t *testing.T) {
validResourceID := []struct {
ResourceID string
Count int
}{
{
"08f9d3ec-5768-479f-aa16-d2d9934b356a:8daea363-7535-4af3-a80f-e0f6c02666e0",
2,
},
{
"24252dc8-2a5a-4871-9601-7486a775d0e3:f59d3de3-512e-4eb3-9f4b-0e2395297b7c:8daea363-7535-4af3-a80f-e0f6c02666e0",
3,
},
{
"df18a81a-02eb-476e-bd42-bf87cbd5b948:615bdbb1-4d2a-443d-8ede-4f15b141603c:4104093f-5ba8-4d9f-849b-e4ad75143be7:7e8f03c7-0ac0-40b8-9497-9c64d4001f54",
4,
},
}

for _, v := range validResourceID {
_, err := parseResourceID(v.ResourceID, v.Count)
if err != nil {
t.Fatalf("%s should be a valid Resource ID: %s", v.ResourceID, err)
}
}

invalidResourceID := []struct {
ResourceID string
Count int
}{
{
"08f9d3ec-5768-479f-aa16-d2d9934b356a:8daea363-7535-4af3-a80f-e0f6c02666e0",
3,
},
{
"24252dc8-2a5a-4871-9601-7486a775d0e3:f59d3de3-512e-4eb3-9f4b-0e2395297b7c:8daea363-7535-4af3-a80f-e0f6c02666e0",
2,
},
{
"df18a81a-02eb-476e-bd42-bf87cbd5b948:615bdbb1-4d2a-443d-8ede-4f15b141603c:4104093f-5ba8-4d9f-849b-e4ad75143be7:7e8f03c7-0ac0-40b8-9497-9c64d4001f54",
5,
},
}

for _, v := range invalidResourceID {
_, err := parseResourceID(v.ResourceID, v.Count)
if err == nil {
t.Fatalf("%s should be an invalid Resource ID: %s", v.ResourceID, err)
}
}

}

0 comments on commit b78f491

Please sign in to comment.