-
Notifications
You must be signed in to change notification settings - Fork 649
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1453 from suhrit-cf/WDAPI-668-Create-terraform-ca…
…pability-for-all-public-apis Added devices datasource
- Loading branch information
Showing
6 changed files
with
199 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:new-data-source | ||
cloudflare_devices | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.", | ||
}, | ||
"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.", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
--- | ||
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 |