Skip to content

Commit

Permalink
fix schema + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobbednarz committed Sep 27, 2022
1 parent 5a4590e commit 924f1e8
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 79 deletions.
26 changes: 25 additions & 1 deletion docs/data-sources/devices.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,31 @@ data "cloudflare_devices" "devices" {

- `devices` - A list of device object. See below for nested attributes.

**devices**
- `devices` (List of Object) (see [below for nested schema](#nestedatt--devices))
- `id` (String) The ID of this resource.

<a id="nestedatt--devices"></a>

### Nested Schema for `devices`

Read-Only:

- `created` (String)
- `device_type` (String)
- `id` (String)
- `ip` (String)
- `key` (String)
- `last_seen` (String)
- `model` (String)
- `name` (String)
- `os_distro_name` (String)
- `os_distro_revision` (String)
- `os_version` (String)
- `updated` (String)
- `user_email` (String)
- `user_id` (String)
- `user_name` (String)
- `version` (String)

- `id` - Device ID.
- `key` - The device's public key.
Expand Down
4 changes: 2 additions & 2 deletions docs/resources/device_posture_rule.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Provides a Cloudflare Device Posture Rule resource. Device posture rules configu
resource "cloudflare_device_posture_rule" "eaxmple" {
account_id = "f037e56e89293a057740de681ac9abbe"
name = "Corporate devices posture rule"
type = "serial_number"
type = "os_version"
description = "Device posture rule for corporate devices."
schedule = "24h"
expiration = "24h"
Expand All @@ -39,7 +39,7 @@ resource "cloudflare_device_posture_rule" "eaxmple" {
### Required

- `account_id` (String) The account identifier to target for the resource.
- `type` (String) The device posture rule type. Available values: `serial_number`, `file`, `application`, `gateway`, `warp`, `domain_joined`, `os_version`, `os_distro_name`, `os_distro_revision`, `disk_encryption`, `firewall`, `workspace_one`.
- `type` (String) The device posture rule type. Available values: `serial_number`, `file`, `application`, `gateway`, `warp`, `domain_joined`, `os_version`, `disk_encryption`, `firewall`, `workspace_one`.

### Optional

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
resource "cloudflare_device_posture_rule" "eaxmple" {
account_id = "f037e56e89293a057740de681ac9abbe"
name = "Corporate devices posture rule"
type = "serial_number"
type = "os_version"
description = "Device posture rule for corporate devices."
schedule = "24h"
expiration = "24h"
Expand Down
34 changes: 21 additions & 13 deletions internal/provider/resource_cloudflare_device_posture_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,12 @@ func setDevicePostureRuleInput(rule *cloudflare.DevicePostureRule, d *schema.Res
if connectionID, ok := d.GetOk("input.0.connection_id"); ok {
input.ConnectionID = connectionID.(string)
}
if osDistroName, ok := d.GetOk("input.0.os_distro_name"); ok {
input.OsDistroName = osDistroName.(string)
}
if osDistroRevision, ok := d.GetOk("input.0.os_distro_revision"); ok {
input.OsDistroRevision = osDistroRevision.(string)
}
rule.Input = input
}
}
Expand Down Expand Up @@ -234,19 +240,21 @@ func convertMatchToSchema(matches []cloudflare.DevicePostureRuleMatch) []map[str

func convertInputToSchema(input cloudflare.DevicePostureRuleInput) []map[string]interface{} {
m := map[string]interface{}{
"id": input.ID,
"path": input.Path,
"exists": input.Exists,
"thumbprint": input.Thumbprint,
"sha256": input.Sha256,
"running": input.Running,
"require_all": input.RequireAll,
"enabled": input.Enabled,
"version": input.Version,
"operator": input.Operator,
"domain": input.Domain,
"compliance_status": input.ComplianceStatus,
"connection_id": input.ConnectionID,
"id": input.ID,
"path": input.Path,
"exists": input.Exists,
"thumbprint": input.Thumbprint,
"sha256": input.Sha256,
"running": input.Running,
"require_all": input.RequireAll,
"enabled": input.Enabled,
"version": input.Version,
"os_distro_name": input.OsDistroName,
"os_distro_revision": input.OsDistroRevision,
"operator": input.Operator,
"domain": input.Domain,
"compliance_status": input.ComplianceStatus,
"connection_id": input.ConnectionID,
}

return []map[string]interface{}{m}
Expand Down
69 changes: 9 additions & 60 deletions internal/provider/resource_cloudflare_device_posture_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func TestAccCloudflareDevicePostureRule_OsVersion(t *testing.T) {
})
}

func TestAccCloudflareDevicePostureRule_OsDistroName(t *testing.T) {
func TestAccCloudflareDevicePostureRule_LinuxOsDistro(t *testing.T) {
// Temporarily unset CLOUDFLARE_API_TOKEN if it is set as the Access
// service does not yet support the API tokens and it results in
// misleading state error messages.
Expand All @@ -109,51 +109,16 @@ func TestAccCloudflareDevicePostureRule_OsDistroName(t *testing.T) {
CheckDestroy: testAccCheckCloudflareDevicePostureRuleDestroy,
Steps: []resource.TestStep{
{
Config: testAccCloudflareDevicePostureRuleConfigOsDistroName(rnd, accountID),
Config: testAccCloudflareDevicePostureRuleConfigLinuxDistro(rnd, accountID),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(name, "account_id", accountID),
resource.TestCheckResourceAttr(name, "name", rnd),
resource.TestCheckResourceAttr(name, "type", "os_distro_name"),
resource.TestCheckResourceAttr(name, "type", "os_version"),
resource.TestCheckResourceAttr(name, "description", "My description"),
resource.TestCheckResourceAttr(name, "match.0.platform", "linux"),
resource.TestCheckResourceAttr(name, "input.0.version", "1.0.0"),
resource.TestCheckResourceAttr(name, "input.0.os_distro_name", "ubuntu"),
),
},
},
})
}

func TestAccCloudflareDevicePostureRule_OsDistroRevision(t *testing.T) {
// Temporarily unset CLOUDFLARE_API_TOKEN if it is set as the Access
// service does not yet support the API tokens and it results in
// misleading state error messages.
if os.Getenv("CLOUDFLARE_API_TOKEN") != "" {
defer func(apiToken string) {
os.Setenv("CLOUDFLARE_API_TOKEN", apiToken)
}(os.Getenv("CLOUDFLARE_API_TOKEN"))
os.Setenv("CLOUDFLARE_API_TOKEN", "")
}

rnd := generateRandomResourceName()
name := fmt.Sprintf("cloudflare_device_posture_rule.%s", rnd)

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccessAccPreCheck(t)
},
ProviderFactories: providerFactories,
CheckDestroy: testAccCheckCloudflareDevicePostureRuleDestroy,
Steps: []resource.TestStep{
{
Config: testAccCloudflareDevicePostureRuleConfigOsDistroRevision(rnd, accountID),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(name, "account_id", accountID),
resource.TestCheckResourceAttr(name, "name", rnd),
resource.TestCheckResourceAttr(name, "type", "os_distro_revision"),
resource.TestCheckResourceAttr(name, "description", "My description"),
resource.TestCheckResourceAttr(name, "match.0.platform", "linux"),
resource.TestCheckResourceAttr(name, "input.0.os_distro_revision", "os_distro_revision"),
resource.TestCheckResourceAttr(name, "input.0.os_distro_revision", "1.0.0"),
),
},
},
Expand Down Expand Up @@ -314,38 +279,22 @@ resource "cloudflare_device_posture_rule" "%[1]s" {
`, rnd, accountID)
}

func testAccCloudflareDevicePostureRuleConfigOsDistroName(rnd, accountID string) string {
func testAccCloudflareDevicePostureRuleConfigLinuxDistro(rnd, accountID string) string {
return fmt.Sprintf(`
resource "cloudflare_device_posture_rule" "%[1]s" {
account_id = "%[2]s"
name = "%[1]s"
type = "os_distro_name"
type = "os_version"
description = "My description"
schedule = "24h"
expiration = "24h"
match {
platform = "linux"
}
input {
version = "1.0.0"
operator = "<"
os_distro_name = "ubuntu"
}
}
`, rnd, accountID)
}

func testAccCloudflareDevicePostureRuleConfigOsDistroRevision(rnd, accountID string) string {
return fmt.Sprintf(`
resource "cloudflare_device_posture_rule" "%[1]s" {
account_id = "%[2]s"
name = "%[1]s"
type = "os_distro_revision"
description = "My description"
schedule = "24h"
expiration = "24h"
match {
platform = "linux"
}
input {
os_distro_revision = "1.0.0"
}
}
Expand Down
4 changes: 2 additions & 2 deletions internal/provider/schema_cloudflare_device_posture_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ func resourceCloudflareDevicePostureRuleSchema() map[string]*schema.Schema {
"type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{"serial_number", "file", "application", "gateway", "warp", "domain_joined", "os_version", "os_distro_name", "os_distro_revision", "disk_encryption", "firewall", "workspace_one"}, false),
Description: fmt.Sprintf("The device posture rule type. %s", renderAvailableDocumentationValuesStringSlice([]string{"serial_number", "file", "application", "gateway", "warp", "domain_joined", "os_version", "os_distro_name", "os_distro_revision", "disk_encryption", "firewall", "workspace_one"})),
ValidateFunc: validation.StringInSlice([]string{"serial_number", "file", "application", "gateway", "warp", "domain_joined", "os_version", "disk_encryption", "firewall", "workspace_one"}, false),
Description: fmt.Sprintf("The device posture rule type. %s", renderAvailableDocumentationValuesStringSlice([]string{"serial_number", "file", "application", "gateway", "warp", "domain_joined", "os_version", "disk_encryption", "firewall", "workspace_one"})),
},
"name": {
Type: schema.TypeString,
Expand Down

0 comments on commit 924f1e8

Please sign in to comment.