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

Add dynamic data source #1103

Merged
merged 4 commits into from
Jun 11, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
121 changes: 121 additions & 0 deletions vsphere/data_source_vsphere_dynamic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package vsphere

import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/vapi/tags"
"log"
"regexp"
)

func dataSourceVSphereDynamic() *schema.Resource {
return &schema.Resource{
Read: dataSourceVSphereDynamicRead,

Schema: map[string]*schema.Schema{
"filter": {
Type: schema.TypeSet,
Required: true,
Description: "List of tag IDs to match target.",
Elem: &schema.Schema{Type: schema.TypeString},
},
"name_regex": {
Type: schema.TypeString,
Optional: true,
Description: "A regular expression used to match against managed object names.",
},
"type": {
Type: schema.TypeString,
Optional: true,
Description: "The type of managed object to return.",
},
},
}
}

func dataSourceVSphereDynamicRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] dataSourceDynamic: Beggining dynamic data source read.")
tm, err := meta.(*VSphereClient).TagsManager()
if err != nil {
return err
}
tagIds := d.Get("filter").(*schema.Set).List()
matches, err := filterObjectsByTag(tm, tagIds)
if err != nil {
return err
}
filtered, err := filterObjectsByName(d, meta, matches)
if err != nil {
return err
}
switch {
case len(filtered) < 1:
return fmt.Errorf("no matcheing resources found")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return fmt.Errorf("no matcheing resources found")
return fmt.Errorf("no matching resources found")

case len(filtered) > 1:
log.Printf("dataSourceVSphereDynamic: Multiple matches found: %v", filtered)
return fmt.Errorf("multiple objecst match the supplied criteria")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return fmt.Errorf("multiple objecst match the supplied criteria")
return fmt.Errorf("multiple objects match the supplied criteria")

}
d.SetId(filtered[0])
log.Printf("[DEBUG] dataSourceDynamic: Read complete. Resource located: %s", filtered[0])
return nil
}

func filterObjectsByName(d *schema.ResourceData, meta interface{}, matches []tags.AttachedObjects) ([]string, error) {
log.Printf("[DEBUG] dataSourceDynamic: Filtering objects by name.")
var filtered []string
re, err := regexp.Compile(d.Get("name_regex").(string))
if err != nil {
return nil, err
}
for _, match := range matches[0].ObjectIDs {
mtype := d.Get("type").(string)
if mtype != "" && match.Reference().Type != mtype {
// Skip this object because the type does not match
continue
}
attachedObject := object.NewCommon(meta.(*VSphereClient).vimClient.Client, match.Reference())
name, err := attachedObject.ObjectName(context.TODO())
if err != nil {
return nil, err
}
if re.Match([]byte(name)) {
log.Printf("[DEBUG] dataSourceDynamic: Match found: %s", name)
filtered = append(filtered, match.Reference().Value)
}
}
return filtered, nil
}

func filterObjectsByTag(tm *tags.Manager, t []interface{}) ([]tags.AttachedObjects, error) {
log.Printf("[DEBUG] dataSourceDynamic: Filtering objects by tags.")
var tagIds []string
for _, ti := range t {
tagIds = append(tagIds, ti.(string))
}
matches, err := tm.GetAttachedObjectsOnTags(context.TODO(), tagIds)
if err != nil {
return nil, err
}
for _, match := range matches {
matches[0] = attachedObjectsIntersection(matches[0], match)
}
if len(matches[0].ObjectIDs) < 1 {
return nil, fmt.Errorf("no resources match filter")
}
log.Printf("[DEBUG] dataSourceDynamic: Objects filtered.")
return matches, nil
}

func attachedObjectsIntersection(a, b tags.AttachedObjects) tags.AttachedObjects {
var inter tags.AttachedObjects
for _, aval := range a.ObjectIDs {
for _, bval := range b.ObjectIDs {
if aval == bval {
inter.ObjectIDs = append(inter.ObjectIDs, aval)
}
}
}
return inter
}
182 changes: 182 additions & 0 deletions vsphere/data_source_vsphere_dynamic_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package vsphere

import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"github.com/terraform-providers/terraform-provider-vsphere/vsphere/internal/helper/testhelper"
"regexp"
"testing"
)

func TestAccDataSourceVSphereDynamic_regexAndTag(t *testing.T) {
t.Cleanup(RunSweepers)
resource.Test(t, resource.TestCase{
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceVSphereDynamicConfigBase(),
},
{
Config: testAccDataSourceVSphereConfigRegexAndTag(),
Check: resource.ComposeTestCheckFunc(
testMatchDatacenterIds("vsphere_datacenter.dc2", "data.vsphere_dynamic.dyn1"),
),
},
{
Config: testAccDataSourceVSphereDynamicConfigBase(),
},
},
})
}

func TestAccDataSourceVSphereDynamic_multiTag(t *testing.T) {
t.Cleanup(RunSweepers)
resource.Test(t, resource.TestCase{
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceVSphereDynamicConfigBase(),
},
{
Config: testAccDataSourceVSphereConfigMultiTag(),
Check: resource.ComposeTestCheckFunc(
testMatchDatacenterIds("vsphere_datacenter.dc1", "data.vsphere_dynamic.dyn2"),
),
},
{
Config: testAccDataSourceVSphereDynamicConfigBase(),
},
},
})
}

func TestAccDataSourceVSphereDynamic_multiResult(t *testing.T) {
t.Cleanup(RunSweepers)
resource.Test(t, resource.TestCase{
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceVSphereDynamicConfigBase(),
},
{
Config: testAccDataSourceVSphereConfigMultiMatch(),
ExpectError: regexp.MustCompile("multiple object match the supplied criteria"),
},
},
})
}

func TestAccDataSourceVSphereDynamic_typeFilter(t *testing.T) {
t.Cleanup(RunSweepers)
resource.Test(t, resource.TestCase{
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceVSphereDynamicConfigBase(),
},
{
Config: testAccDataSourceVSphereConfigType(),
Check: resource.ComposeTestCheckFunc(
testMatchDatacenterIds("vsphere_datacenter.dc1", "data.vsphere_dynamic.dyn4"),
),
},
{
Config: testAccDataSourceVSphereDynamicConfigBase(),
},
},
})
}

func testMatchDatacenterIds(a, b string) resource.TestCheckFunc {
return func(s *terraform.State) error {
ida := s.RootModule().Resources[a].Primary.Attributes["moid"]
idb := s.RootModule().Resources[b].Primary.ID
if ida != idb {
return fmt.Errorf("unexpected ID. Expected: %s, Got: %s", idb, ida)
}
return nil
}
}

func init() {
resource.AddTestSweepers("tags", &resource.Sweeper{
Name: "tag_cleanup",
Dependencies: nil,
F: tagSweep,
})
resource.AddTestSweepers("datacenters", &resource.Sweeper{
Name: "datacenter_cleanup",
Dependencies: nil,
F: dcSweep,
})
}

func testAccDataSourceVSphereDynamicConfigBase() string {
return testhelper.CombineConfigs(
testhelper.ConfigResDC1(),
testhelper.ConfigResDC2(),
testhelper.ConfigResTagCat1(),
testhelper.ConfigResTagCat2(),
testhelper.ConfigResTag1(),
testhelper.ConfigResTag2(),
testhelper.ConfigResTag3(),
)
}

func testAccDataSourceVSphereConfigRegexAndTag() string {
conf := fmt.Sprintf(`
data "vsphere_dynamic" "dyn1" {
filter = [ vsphere_tag.tag1.id ]
name_regex = "dc2"
}
`)
return testhelper.CombineConfigs(
testAccDataSourceVSphereDynamicConfigBase(),
conf,
testhelper.ConfigDataDC2(),
)
}

func testAccDataSourceVSphereConfigMultiTag() string {
conf := fmt.Sprintf(`
data "vsphere_dynamic" "dyn2" {
filter = [ vsphere_tag.tag1.id, vsphere_tag.tag2.id ]
name_regex = ""
}
`)
return testhelper.CombineConfigs(
testAccDataSourceVSphereDynamicConfigBase(),
conf,
testhelper.ConfigDataDC1(),
)
}

func testAccDataSourceVSphereConfigMultiMatch() string {
conf := fmt.Sprintf(`
data "vsphere_dynamic" "dyn3" {
filter = [ vsphere_tag.tag1.id ]
name_regex = ""
}
`)
return testhelper.CombineConfigs(
testAccDataSourceVSphereDynamicConfigBase(),
conf,
testhelper.ConfigDataDC1(),
)
}

func testAccDataSourceVSphereConfigType() string {
conf := fmt.Sprintf(`
data "vsphere_dynamic" "dyn4" {
filter = [ vsphere_tag.tag1.id, vsphere_tag.tag2.id ]
name_regex = ""
type = "Datacenter"
}
`)
return testhelper.CombineConfigs(
testAccDataSourceVSphereDynamicConfigBase(),
conf,
testhelper.ConfigDataDC1(),
)
}
6 changes: 3 additions & 3 deletions vsphere/data_source_vsphere_vmfs_disks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ output "found" {
value = "${length(data.vsphere_vmfs_disks.available.disks) >= 1 ? "true" : "false" }"
}
`,
os.Getenv("TF_VAR_VSPHERE_DATACENTER"),
os.Getenv("TF_VAR_VSPHERE_ESXI1"),
os.Getenv("TF_VAR_VSPHERE_DATACENTER"),
os.Getenv("TF_VAR_VSPHERE_ESXI1"),
)
}
}
20 changes: 19 additions & 1 deletion vsphere/datacenter_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package vsphere
import (
"context"
"fmt"

"github.com/vmware/govmomi"
"github.com/vmware/govmomi/find"
"github.com/vmware/govmomi/object"
Expand Down Expand Up @@ -58,3 +57,22 @@ func datacenterCustomAttributes(dc *object.Datacenter) (*mo.Datacenter, error) {
}
return &props, nil
}

func listDatacenters(client *govmomi.Client) ([]*object.Datacenter, error) {
finder := find.NewFinder(client.Client, true)
l, err := finder.ManagedObjectListChildren(context.TODO(), "/")
if err != nil {
return nil, nil
}
var dcs []*object.Datacenter
for _, item := range l {
if item.Object.Reference().Type == "Datacenter" {
dc, err := getDatacenter(client, item.Path)
if err != nil {
return nil, err
}
dcs = append(dcs, dc)
}
}
return dcs, nil
}
Loading