diff --git a/.changelog/1453.txt b/.changelog/1453.txt new file mode 100644 index 0000000000..a62324269d --- /dev/null +++ b/.changelog/1453.txt @@ -0,0 +1,3 @@ +```release-note:new-data-source +cloudflare_devices +``` \ No newline at end of file diff --git a/cloudflare/data_source_devices.go b/cloudflare/data_source_devices.go new file mode 100644 index 0000000000..16509609fd --- /dev/null +++ b/cloudflare/data_source_devices.go @@ -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 +} diff --git a/cloudflare/provider.go b/cloudflare/provider.go index 652b0664d6..d052a68bd5 100644 --- a/cloudflare/provider.go +++ b/cloudflare/provider.go @@ -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(), diff --git a/cloudflare/schema_cloudflare_devices.go b/cloudflare/schema_cloudflare_devices.go new file mode 100644 index 0000000000..ee576e9aaa --- /dev/null +++ b/cloudflare/schema_cloudflare_devices.go @@ -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.", + }, + }, + }, + }, + } +} diff --git a/website/cloudflare.erb b/website/cloudflare.erb index d59ece8506..f5b0026c49 100644 --- a/website/cloudflare.erb +++ b/website/cloudflare.erb @@ -34,6 +34,9 @@