Skip to content

Commit

Permalink
fix: Fixed devices data source
Browse files Browse the repository at this point in the history
  • Loading branch information
willguibr committed May 23, 2024
1 parent 7d8e44b commit f8a93f9
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 21 deletions.
6 changes: 3 additions & 3 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 ..."
Expand Down
26 changes: 23 additions & 3 deletions zia/data_source_zia_devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
},
},
}
}
Expand Down Expand Up @@ -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
Expand Down
96 changes: 81 additions & 15 deletions zia/data_source_zia_devices_test.go
Original file line number Diff line number Diff line change
@@ -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" {}
`
*/

0 comments on commit f8a93f9

Please sign in to comment.