From f8a93f973c14a6fb2e53a570a0455a8a3db3c219 Mon Sep 17 00:00:00 2001 From: William Guilherme Date: Thu, 23 May 2024 14:16:35 -0700 Subject: [PATCH] fix: Fixed devices data source --- GNUmakefile | 6 +- zia/data_source_zia_devices.go | 26 +++++++- zia/data_source_zia_devices_test.go | 96 ++++++++++++++++++++++++----- 3 files changed, 107 insertions(+), 21 deletions(-) diff --git a/GNUmakefile b/GNUmakefile index f189f49d..63cae233 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -194,14 +194,14 @@ test\:integration\:zscalertwo: build13: GOOS=$(shell go env GOOS) build13: GOARCH=$(shell go env GOARCH) ifeq ($(OS),Windows_NT) # is Windows_NT on XP, 2000, 7, Vista, 10... -build13: DESTINATION=$(APPDATA)/terraform.d/plugins/$(ZIA_PROVIDER_NAMESPACE)/2.9.0/$(GOOS)_$(GOARCH) +build13: DESTINATION=$(APPDATA)/terraform.d/plugins/$(ZIA_PROVIDER_NAMESPACE)/2.9.1/$(GOOS)_$(GOARCH) else -build13: DESTINATION=$(HOME)/.terraform.d/plugins/$(ZIA_PROVIDER_NAMESPACE)/2.9.0/$(GOOS)_$(GOARCH) +build13: DESTINATION=$(HOME)/.terraform.d/plugins/$(ZIA_PROVIDER_NAMESPACE)/2.9.1/$(GOOS)_$(GOARCH) endif build13: fmtcheck @echo "==> Installing plugin to $(DESTINATION)" @mkdir -p $(DESTINATION) - go build -o $(DESTINATION)/terraform-provider-zia_v2.9.0 + go build -o $(DESTINATION)/terraform-provider-zia_v2.9.1 coverage: test @echo "✓ Opening coverage for unit tests ..." diff --git a/zia/data_source_zia_devices.go b/zia/data_source_zia_devices.go index 7e5d179c..fd1d39f7 100644 --- a/zia/data_source_zia_devices.go +++ b/zia/data_source_zia_devices.go @@ -63,6 +63,12 @@ func dataSourceDevices() *schema.Resource { Computed: true, Description: "The device owner's user name.", }, + "hostname": { + Type: schema.TypeString, + Optional: true, + Computed: true, + Description: "The device owner's user name.", + }, }, } } @@ -130,19 +136,33 @@ func dataSourceDevicesRead(d *schema.ResourceData, m interface{}) error { resp = res } + // Check if no attributes are set and return all devices + if resp == nil && name == "" && model == "" && owner == "" && osType == "" && osVersion == "" { + log.Printf("[INFO] No specific attributes provided, getting all devices.") + allDevices, err := zClient.devicegroups.GetAllDevices() + if err != nil { + return err + } + if len(allDevices) > 0 { + resp = &allDevices[0] + } else { + return fmt.Errorf("no devices found") + } + } + if resp != nil { d.SetId(fmt.Sprintf("%d", resp.ID)) _ = d.Set("name", resp.Name) + _ = d.Set("description", resp.Description) _ = d.Set("device_group_type", resp.DeviceGroupType) _ = d.Set("device_model", resp.DeviceModel) _ = d.Set("os_type", resp.OSType) _ = d.Set("os_version", resp.OSVersion) - _ = d.Set("description", resp.Description) _ = d.Set("owner_user_id", resp.OwnerUserId) _ = d.Set("owner_name", resp.OwnerName) - + _ = d.Set("hostname", resp.HostName) } else { - return fmt.Errorf("couldn't find any device name '%s' or id '%d'", name, id) + return fmt.Errorf("couldn't find any device with the provided attributes") } return nil diff --git a/zia/data_source_zia_devices_test.go b/zia/data_source_zia_devices_test.go index 6eb00a62..8eb749ac 100644 --- a/zia/data_source_zia_devices_test.go +++ b/zia/data_source_zia_devices_test.go @@ -1,45 +1,111 @@ package zia -/* import ( + "fmt" + "log" "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" ) func TestAccDataSourceDevices_Basic(t *testing.T) { + var initialDevice map[string]string + resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccCheckDataSourceDevicesConfig_basic, + Config: testAccCheckDataSourceDevicesConfig_all, + Check: resource.ComposeAggregateTestCheckFunc( + func(s *terraform.State) error { + // Capture the initial device information + rs, ok := s.RootModule().Resources["data.zia_devices.all"] + if !ok { + return fmt.Errorf("Not found: data.zia_devices.all") + } + + if rs.Primary.ID == "" { + log.Printf("[INFO] No resource found for data.zia_devices.all; considering this test successful as the tenant might not have this data.") + return nil + } + + initialDevice = map[string]string{ + "id": rs.Primary.Attributes["id"], + "name": rs.Primary.Attributes["name"], + "device_model": rs.Primary.Attributes["device_model"], + "os_type": rs.Primary.Attributes["os_type"], + "os_version": rs.Primary.Attributes["os_version"], + "owner_name": rs.Primary.Attributes["owner_name"], + "device_group_type": rs.Primary.Attributes["device_group_type"], + "description": rs.Primary.Attributes["description"], + "owner_user_id": rs.Primary.Attributes["owner_user_id"], + } + + return nil + }, + ), + }, + { + Config: testAccCheckDataSourceDevicesConfig(initialDevice["device_model"], "device_model"), Check: resource.ComposeTestCheckFunc( testAccDataSourceDevicesCheck("data.zia_devices.device_model"), + ), + }, + { + Config: testAccCheckDataSourceDevicesConfig(initialDevice["os_type"], "os_type"), + Check: resource.ComposeTestCheckFunc( testAccDataSourceDevicesCheck("data.zia_devices.os_type"), + ), + }, + { + Config: testAccCheckDataSourceDevicesConfig(initialDevice["os_version"], "os_version"), + Check: resource.ComposeTestCheckFunc( testAccDataSourceDevicesCheck("data.zia_devices.os_version"), ), }, + { + Config: testAccCheckDataSourceDevicesConfig(initialDevice["name"], "name"), + Check: resource.ComposeTestCheckFunc( + testAccDataSourceDevicesCheck("data.zia_devices.name"), + ), + }, + // { + // Config: testAccCheckDataSourceDevicesConfig(initialDevice["owner_name"], "owner_name"), + // Check: resource.ComposeTestCheckFunc( + // testAccDataSourceDevicesCheck("data.zia_devices.owner"), + // ), + // }, }, }) } func testAccDataSourceDevicesCheck(name string) resource.TestCheckFunc { - return resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrSet(name, "id"), - resource.TestCheckResourceAttrSet(name, "name"), - ) -} + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[name] + if !ok { + return fmt.Errorf("Not found: %s", name) + } + + if rs.Primary.ID == "" { + log.Printf("[INFO] No resource found for %s; considering this test successful as the tenant might not have this data.", name) + return nil + } -var testAccCheckDataSourceDevicesConfig_basic = ` -data "zia_devices" "device_model"{ - device_model = "VMware Virtual Platform" + // Additional checks can go here + return nil + } } -data "zia_devices" "os_type"{ - os_type = "WINDOWS_OS" + +func testAccCheckDataSourceDevicesConfig(value, attribute string) string { + return fmt.Sprintf(` +data "zia_devices" "%s" { + %s = "%s" } -data "zia_devices" "os_version"{ - os_version = "Microsoft Windows 10 Pro;64 bit" +`, attribute, attribute, value) } + +var testAccCheckDataSourceDevicesConfig_all = ` +data "zia_devices" "all" {} ` -*/