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

Added devices datasource #1453

Merged
merged 2 commits into from
Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions .changelog/1453.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
cloudflare_devices
```
55 changes: 55 additions & 0 deletions cloudflare/data_source_devices.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package cloudflare

import (
"context"
"fmt"

cloudflare "github.com/cloudflare/cloudflare-go"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceCloudflareDevices() *schema.Resource {
return &schema.Resource{
Schema: resoureceCloudflareDevicesSchema(),
Read: dataResourceCloudflareDevicesRead,
}
}

func dataResourceCloudflareDevicesRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.API)
accountID := d.Get("account_id").(string)
d.SetId(accountID)

devices, err := client.ListTeamsDevices(context.Background(), accountID)

if err != nil {
return fmt.Errorf("error finding devices in account %q: %w", accountID, err)
}

deviceDetails := make([]interface{}, 0)

for _, device := range devices {
deviceDetails = append(deviceDetails, map[string]interface{}{
"id": device.ID,
"key": device.Key,
"device_type": device.DeviceType,
"name": device.Name,
"version": device.Version,
"updated": device.Updated,
"created": device.Created,
"last_seen": device.LastSeen,
"model": device.Model,
"os_version": device.OSVersion,
"ip": device.IP,
"user_id": device.User.ID,
"user_email": device.User.Email,
"user_name": device.User.Name,
})
}

if err = d.Set("devices", deviceDetails); err != nil {
return fmt.Errorf("error setting device details: %w", err)
}

return nil
}
1 change: 1 addition & 0 deletions cloudflare/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func Provider() *schema.Provider {
"cloudflare_access_identity_provider": dataSourceCloudflareAccessIdentityProvider(),
"cloudflare_account_roles": dataSourceCloudflareAccountRoles(),
"cloudflare_api_token_permission_groups": dataSourceCloudflareApiTokenPermissionGroups(),
"cloudflare_devices": dataSourceCloudflareDevices(),
"cloudflare_ip_ranges": dataSourceCloudflareIPRanges(),
"cloudflare_origin_ca_root_certificate": dataSourceCloudflareOriginCARootCertificate(),
"cloudflare_waf_groups": dataSourceCloudflareWAFGroups(),
Expand Down
92 changes: 92 additions & 0 deletions cloudflare/schema_cloudflare_devices.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package cloudflare

import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func resoureceCloudflareDevicesSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"account_id": {
Type: schema.TypeString,
Required: true,
},
"devices": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Optional: true,
Description: "Device ID.",
suhrit-cf marked this conversation as resolved.
Show resolved Hide resolved
},
"key": {
Type: schema.TypeString,
Optional: true,
Description: "The device's public key.",
},
"device_type": {
Type: schema.TypeString,
Optional: true,
Description: "The type of the device.",
},
"name": {
Type: schema.TypeString,
Optional: true,
Description: "The device name.",
},
"version": {
Type: schema.TypeString,
Optional: true,
Description: "The WARP client version.",
},
"model": {
Type: schema.TypeString,
Optional: true,
Description: "The device model name.",
},
"os_version": {
Type: schema.TypeString,
Optional: true,
Description: "The operating system version.",
},
"ip": {
Type: schema.TypeString,
Optional: true,
Description: "IPv4 or IPv6 address.",
},
"last_seen": {
Type: schema.TypeString,
Optional: true,
Description: "When the device was last seen.",
},
"created": {
Type: schema.TypeString,
Optional: true,
Description: "When the device was created.",
},
"updated": {
Type: schema.TypeString,
Optional: true,
Description: "When the device was updated.",
},
"user_id": {
Type: schema.TypeString,
Optional: true,
Description: "User's ID.",
},
"user_email": {
Type: schema.TypeString,
Optional: true,
Description: "User's email.",
},
"user_name": {
Type: schema.TypeString,
Optional: true,
Description: "User's Name.",
},
},
},
},
}
}
3 changes: 3 additions & 0 deletions website/cloudflare.erb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
<li<%= sidebar_current("docs-cloudflare-api-token-permission-groups") %>>
<a href="/docs/providers/cloudflare/d/api_token_permission_groups.html">cloudflare_api_token_permission_groups</a>
</li>
<li<%= sidebar_current("docs-cloudflare-datasource-devices") %>>
<a href="/docs/providers/cloudflare/r/devices.html">cloudflare_devices</a>
</li>
<li<%= sidebar_current("docs-cloudflare-datasource-ip-ranges") %>>
<a href="/docs/providers/cloudflare/d/ip_ranges.html">cloudflare_ip_ranges</a>
</li>
Expand Down
45 changes: 45 additions & 0 deletions website/docs/d/devices.html.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
suhrit-cf marked this conversation as resolved.
Show resolved Hide resolved
layout: "cloudflare"
page_title: "Cloudflare: cloudflare_devices"
sidebar_current: "docs-cloudflare-datasource-devices"
description: Get information on Cloudflare Devices.
---

# cloudflare_devices

Use this data source to lookup [Devices][1].

## Example usage

```hcl
data "cloudflare_devices" "devices" {
account_id = "c68973221045fe805dfb9aa520153148"
}
```

## Argument Reference

* `account_id` - (Required) The account for which to list the devices.

## Attributes Reference

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

**devices**

- `id` - Device ID.
- `key` - The device's public key.
- `device_type` - The type of the device.
- `name` - The device name.
- `version` - The WARP client version.
- `model` - The device model name.
- `os_version` - The operating system version.
- `ip` - IPv4 or IPv6 address.
- `last_seen` - When the device was last seen.
- `created` - When the device was created.
- `updated` - When the device was updated.
- `user_id` - User's ID.
- `user_email` - User's email.
- `user_name` - User's Name.

[1]: https://api.cloudflare.com/#devices-list-devices