From 1cbb832f4290da96c7c05bef478c9508656c73dd Mon Sep 17 00:00:00 2001 From: Connor Carnes Date: Thu, 14 Dec 2023 17:08:04 -0600 Subject: [PATCH 01/25] Update README.md --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 3a27d85..d50e0d4 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,14 @@ # pwshCloudflare +[![Minimum Supported PowerShell Version](https://img.shields.io/badge/PowerShell-5.1+-purple.svg)](https://github.com/PowerShell/PowerShell) ![Cross Platform](https://img.shields.io/badge/platform-windows%20%7C%20macos%20%7C%20linux-lightgrey) [![License][license-badge]](LICENSE) + +[license-badge]: https://img.shields.io/github/license/connorcarnes/pwshCloudflare + +| Branch | Windows - PowerShell | Windows - pwsh | Linux | MacOS | +| --- | --- | --- | --- | --- | +| main | ![Build Status Windows PowerShell Main](https://github.com/connorcarnes/pwshCloudflare/actions/workflows/wf_WindowsPowerShell.yml/badge.svg?branch=main) | ![Build Status Windows pwsh Main](https://github.com/connorcarnes/pwshCloudflare/actions/workflows/wf_Windows.yml/badge.svg?branch=main) | ![Build Status Linux Main](https://github.com/connorcarnes/pwshCloudflare/actions/workflows/wf_Linux.yml/badge.svg?branch=main) | ![Build Status MacOS dev](https://github.com/connorcarnes/pwshCloudflare/actions/workflows/wf_MacOS.yml/badge.svg?branch=main) | +| dev | ![Build Status Windows PowerShell dev](https://github.com/connorcarnes/pwshCloudflare/actions/workflows/wf_WindowsPowerShell.yml/badge.svg?branch=dev) | ![Build Status Windows pwsh dev](https://github.com/connorcarnes/pwshCloudflare/actions/workflows/wf_Windows.yml/badge.svg?branch=dev) | ![Build Status Linux dev](https://github.com/connorcarnes/pwshCloudflare/actions/workflows/wf_Linux.yml/badge.svg?branch=dev) | ![Build Status MacOS dev](https://github.com/connorcarnes/pwshCloudflare/actions/workflows/wf_MacOS.yml/badge.svg?branch=dev) | + ## Synopsis PowerShell module for interacting with the Cloudflare API. From d3e5a98cdc9af1d9d630dd824bbd5a72ce83105e Mon Sep 17 00:00:00 2001 From: connorcarnes Date: Thu, 14 Dec 2023 18:36:07 -0600 Subject: [PATCH 02/25] Add output type to CFZoneRecord functions --- src/pwshCloudflare/Public/Get-CFZoneRecord.ps1 | 1 + src/pwshCloudflare/Public/New-CFZoneRecord.ps1 | 1 + src/pwshCloudflare/Public/Set-CFZoneRecord.ps1 | 1 + 3 files changed, 3 insertions(+) diff --git a/src/pwshCloudflare/Public/Get-CFZoneRecord.ps1 b/src/pwshCloudflare/Public/Get-CFZoneRecord.ps1 index 2ea124a..1bee149 100644 --- a/src/pwshCloudflare/Public/Get-CFZoneRecord.ps1 +++ b/src/pwshCloudflare/Public/Get-CFZoneRecord.ps1 @@ -20,6 +20,7 @@ #> function Get-CFZoneRecord { [CmdletBinding()] + [OutputType('Cloudflare.ZoneRecord')] param( [Parameter(ParameterSetName = 'ZoneId', Mandatory)] [string]$ZoneId, diff --git a/src/pwshCloudflare/Public/New-CFZoneRecord.ps1 b/src/pwshCloudflare/Public/New-CFZoneRecord.ps1 index 0ad384f..cf68b2d 100644 --- a/src/pwshCloudflare/Public/New-CFZoneRecord.ps1 +++ b/src/pwshCloudflare/Public/New-CFZoneRecord.ps1 @@ -30,6 +30,7 @@ #> function New-CFZoneRecord { [CmdletBinding()] + [OutputType('Cloudflare.ZoneRecord')] param( [Parameter(ParameterSetName = 'ZoneId', Mandatory)] [string]$ZoneId, diff --git a/src/pwshCloudflare/Public/Set-CFZoneRecord.ps1 b/src/pwshCloudflare/Public/Set-CFZoneRecord.ps1 index a99d27e..370efea 100644 --- a/src/pwshCloudflare/Public/Set-CFZoneRecord.ps1 +++ b/src/pwshCloudflare/Public/Set-CFZoneRecord.ps1 @@ -29,6 +29,7 @@ #> function Set-CFZoneRecord { [CmdletBinding()] + [OutputType('Cloudflare.ZoneRecord')] param( [Parameter(Mandatory)] [string]$RecordId, From dc558331b5eb5c66c264293b21bd2862f8a94175 Mon Sep 17 00:00:00 2001 From: connorcarnes Date: Thu, 14 Dec 2023 19:09:00 -0600 Subject: [PATCH 03/25] Add Get-CFAccount, Cloudflare.Account.Typs.ps1xml and update psd1 --- src/pwshCloudflare/Public/Get-CFAccount.ps1 | 44 ++++++++++++ .../Types/Cloudflare.Account.Types.ps1xml | 72 +++++++++++++++++++ src/pwshCloudflare/pwshCloudflare.psd1 | 2 + 3 files changed, 118 insertions(+) create mode 100644 src/pwshCloudflare/Public/Get-CFAccount.ps1 create mode 100644 src/pwshCloudflare/Types/Cloudflare.Account.Types.ps1xml diff --git a/src/pwshCloudflare/Public/Get-CFAccount.ps1 b/src/pwshCloudflare/Public/Get-CFAccount.ps1 new file mode 100644 index 0000000..12994b7 --- /dev/null +++ b/src/pwshCloudflare/Public/Get-CFAccount.ps1 @@ -0,0 +1,44 @@ +<# +.SYNOPSIS + Get Cloudflare account information. +.DESCRIPTION + Get Cloudflare account information. +.PARAMETER AccountId + ID of account to retrieve. If not specified, all accounts will be returned. +.LINK + https://developers.cloudflare.com/api/operations/accounts-list-accounts +.LINK + https://developers.cloudflare.com/api/operations/accounts-account-details +#> +function Get-CFAccount { + [CmdletBinding()] + [OutputType('Cloudflare.Account')] + param( + [Parameter()] + [string]$AccountId + ) + begin { + Write-Verbose "$($MyInvocation.MyCommand.Name) :: BEGIN :: $(Get-Date)" + Write-Verbose "ParameterSetName: $($PSCmdlet.ParameterSetName)" + if (-not $script:cfSession) { + throw 'Cloudflare session not found. Use Set-CloudflareSession to create a session.' + } + } + process { + if ($AccountId) { + $Uri = '{0}/accounts/{1}' -f $Script:cfBaseApiUrl, $AccountId + } + else { + $Uri = '{0}/accounts' -f $Script:cfBaseApiUrl + } + $Splat = @{ + Method = 'GET' + WebSession = $script:cfSession + Uri = $Uri + } + $Result = Invoke-CFRestMethod @Splat + $Result.result | ForEach-Object { $_.PSobject.TypeNames.Insert(0, 'Cloudflare.Account') } + $Result.result + } + end {} +} diff --git a/src/pwshCloudflare/Types/Cloudflare.Account.Types.ps1xml b/src/pwshCloudflare/Types/Cloudflare.Account.Types.ps1xml new file mode 100644 index 0000000..e61b597 --- /dev/null +++ b/src/pwshCloudflare/Types/Cloudflare.Account.Types.ps1xml @@ -0,0 +1,72 @@ + + + Cloudflare.Account + + + AccountId + id + + + AccountName + name + + + LegacyFlags + legacy_flags + + + CreatedOn + created_on + + + EnforceTwoFactor + + $this.settings.enforce_twofactor + + + + ApiAccessEnabled + + $this.settings.api_access_enabled + + + + AccessApprovalExpiry + + $this.settings.access_approval_expiry + + + + UseAccountCustomNameserversByDefault + + $this.settings.use_account_custom_ns_by_default + + + + DefaultNameservers + + $this.settings.default_nameservers + + + + PSStandardMembers + + + DefaultDisplayPropertySet + + AccountId + AccountName + EnforceTwoFactor + ApiAccessEnabled + AccessApprovalExpiry + UseAccountCustomNameserversByDefault + DefaultNameservers + CreatedOn + LegacyFlags + + + + + + + \ No newline at end of file diff --git a/src/pwshCloudflare/pwshCloudflare.psd1 b/src/pwshCloudflare/pwshCloudflare.psd1 index 0f59d69..2083329 100644 --- a/src/pwshCloudflare/pwshCloudflare.psd1 +++ b/src/pwshCloudflare/pwshCloudflare.psd1 @@ -61,6 +61,7 @@ # Type files (.ps1xml) to be loaded when importing this module TypesToProcess = @( + 'Types\Cloudflare.Account.Types.ps1xml' 'Types\Cloudflare.Zone.Types.ps1xml' 'Types\Cloudflare.ZoneRecord.Types.ps1xml' ) @@ -73,6 +74,7 @@ # Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export. FunctionsToExport = @( + 'Get-CFAccount' 'Get-CFZone' 'Get-CFZoneRecord' 'New-CFZoneRecord' From 2c5f8a699c6b99cc2ad10aaf811da9be499ad140 Mon Sep 17 00:00:00 2001 From: connorcarnes Date: Thu, 14 Dec 2023 19:39:13 -0600 Subject: [PATCH 04/25] Add AccountName parameter to Get-CFAccount --- src/pwshCloudflare/Public/Get-CFAccount.ps1 | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/pwshCloudflare/Public/Get-CFAccount.ps1 b/src/pwshCloudflare/Public/Get-CFAccount.ps1 index 12994b7..d2e4a7b 100644 --- a/src/pwshCloudflare/Public/Get-CFAccount.ps1 +++ b/src/pwshCloudflare/Public/Get-CFAccount.ps1 @@ -5,17 +5,21 @@ Get Cloudflare account information. .PARAMETER AccountId ID of account to retrieve. If not specified, all accounts will be returned. +.PARAMETER AccountName + Name of account to retrieve. If not specified, all accounts will be returned. .LINK https://developers.cloudflare.com/api/operations/accounts-list-accounts .LINK https://developers.cloudflare.com/api/operations/accounts-account-details #> function Get-CFAccount { - [CmdletBinding()] + [CmdletBinding(DefaultParameterSetName = 'AccountId')] [OutputType('Cloudflare.Account')] param( - [Parameter()] - [string]$AccountId + [Parameter(ParameterSetName = 'AccountId')] + [string]$AccountId, + [Parameter(ParameterSetName = 'AccountName')] + [string]$AccountName ) begin { Write-Verbose "$($MyInvocation.MyCommand.Name) :: BEGIN :: $(Get-Date)" @@ -25,6 +29,9 @@ function Get-CFAccount { } } process { + if ($AccountName) { + $AccountId = $Script:cfAccountLookupTable[$AccountName] + } if ($AccountId) { $Uri = '{0}/accounts/{1}' -f $Script:cfBaseApiUrl, $AccountId } From f02415105b9bb64d83f1ae9d4b119f19c5dddbed Mon Sep 17 00:00:00 2001 From: connorcarnes Date: Thu, 14 Dec 2023 19:41:08 -0600 Subject: [PATCH 05/25] Update Register-CFArguementCompleter - Use $MyInvocation to get target functions - Add AccountName completer --- .../Private/Register-CFArgumentCompleter.ps1 | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/pwshCloudflare/Private/Register-CFArgumentCompleter.ps1 b/src/pwshCloudflare/Private/Register-CFArgumentCompleter.ps1 index 644cbc0..64c004c 100644 --- a/src/pwshCloudflare/Private/Register-CFArgumentCompleter.ps1 +++ b/src/pwshCloudflare/Private/Register-CFArgumentCompleter.ps1 @@ -15,6 +15,9 @@ function Register-CFArgumentCompleter { param() begin {} process { + # Zone Lookup Table + $Functions = ($MyInvocation.MyCommand.Module.ExportedFunctions.GetEnumerator() | + Where-Object { $_.Value.Parameters.Keys -Contains 'ZoneName' }).Key $Script:cfZoneLookupTable = @{} Get-CFZone | ForEach-Object { $Script:cfZoneLookupTable.Add($_.name, $_.id) @@ -34,6 +37,29 @@ function Register-CFArgumentCompleter { } } Register-ArgumentCompleter @Splat + + # Account Lookup Table + $Functions = ($MyInvocation.MyCommand.Module.ExportedFunctions.GetEnumerator() | + Where-Object { $_.Value.Parameters.Keys -Contains 'AccountName' }).Key + $Script:cfAccountLookupTable = @{} + Get-CFAccount | ForEach-Object { + $Script:cfAccountLookupTable.Add($_.name, $_.id) + } + $Splat = @{ + CommandName = $Functions + ParameterName = 'AccountName' + ScriptBlock = { + param( + $commandName, + $parameterName, + $wordToComplete, + $commandAst, + $fakeBoundParameters + ) + $Script:cfAccountLookupTable.Keys | Where-Object { $_ -like "$WordToComplete*" } + } + } + Register-ArgumentCompleter @Splat } end {} } \ No newline at end of file From 7d4a95bb90db11d2043906c8910d78bfe8377f66 Mon Sep 17 00:00:00 2001 From: connorcarnes Date: Fri, 15 Dec 2023 00:50:06 -0600 Subject: [PATCH 06/25] Add D1 Functions --- .../Private/Find-CFD1Database.ps1 | 26 ++++++ .../Public/Get-CFD1Database.ps1 | 91 +++++++++++++++++++ .../Public/Invoke-CFD1Query.ps1 | 66 ++++++++++++++ .../Public/New-CFD1Database.ps1 | 50 ++++++++++ .../Public/Remove-CFD1Database.ps1 | 50 ++++++++++ 5 files changed, 283 insertions(+) create mode 100644 src/pwshCloudflare/Private/Find-CFD1Database.ps1 create mode 100644 src/pwshCloudflare/Public/Get-CFD1Database.ps1 create mode 100644 src/pwshCloudflare/Public/Invoke-CFD1Query.ps1 create mode 100644 src/pwshCloudflare/Public/New-CFD1Database.ps1 create mode 100644 src/pwshCloudflare/Public/Remove-CFD1Database.ps1 diff --git a/src/pwshCloudflare/Private/Find-CFD1Database.ps1 b/src/pwshCloudflare/Private/Find-CFD1Database.ps1 new file mode 100644 index 0000000..40714a2 --- /dev/null +++ b/src/pwshCloudflare/Private/Find-CFD1Database.ps1 @@ -0,0 +1,26 @@ +function Find-CFD1Database { + [CmdletBinding()] + param( + [Alias('DatabaseName')] + [Parameter()] + [string]$Name, + [Parameter()] + [string]$AccountId + ) + begin { + Write-Verbose "$($MyInvocation.MyCommand.Name) :: BEGIN :: $(Get-Date)" + } + process { + $Uri = '{0}/accounts/{1}/d1/database' -f $Script:cfBaseApiUrl, $AccountId + if ($Name) { + $Uri = $Uri + "?name=$Name" + } + $Splat = @{ + Method = 'Get' + Uri = $Uri + } + $Result = Invoke-CFRestMethod @Splat + $Result.result + } + end {} +} \ No newline at end of file diff --git a/src/pwshCloudflare/Public/Get-CFD1Database.ps1 b/src/pwshCloudflare/Public/Get-CFD1Database.ps1 new file mode 100644 index 0000000..2b111cf --- /dev/null +++ b/src/pwshCloudflare/Public/Get-CFD1Database.ps1 @@ -0,0 +1,91 @@ +<# +.SYNOPSIS + Get Cloudflare account information. +.DESCRIPTION + Get Cloudflare account information. +.PARAMETER AccountId + ID of account to retrieve. If not specified, all accounts will be returned. +.PARAMETER AccountName + Name of account to retrieve. If not specified, all accounts will be returned. +.NOTES + The cloudflare-d1-list-databases API endpoint returns the properties uuid, name, version and created_at. + The cloudflare-d1-get-database API endpoint returns the same properties as well as num_tables, file_size and running_in region. + When listing all databases or getting a database by name this function makes multiple API calls. The first call is to get the database ID(s). The subsequent call(s) use the ID(s) to get the database details. + This is done so that the same output is returned regardless of parameters used. +.LINK + https://developers.cloudflare.com/api/operations/cloudflare-d1-list-databases +.LINK + https://developers.cloudflare.com/api/operations/cloudflare-d1-get-database +#> +function Get-CFD1Database { + [CmdletBinding()] + [OutputType('Cloudflare.D1Database')] + param( + [Parameter(ParameterSetName = 'AccountNameDatabaseName')] + [Parameter(ParameterSetName = 'AccountIdDatabaseName')] + [Alias('DatabaseName')] + [string]$Name, + [Parameter(ParameterSetName = 'AccountNameDatabaseId')] + [Parameter(ParameterSetName = 'AccountIdDatabaseId')] + [Alias('DatabaseId')] + [string]$Id, + [Parameter(ParameterSetName = 'ListAllByAccountId')] + [Parameter(ParameterSetName = 'AccountIdDatabaseName')] + [Parameter(ParameterSetName = 'AccountIdDatabaseId')] + [string]$AccountId, + [Parameter(ParameterSetName = 'ListAllByAccountName')] + [Parameter(ParameterSetName = 'AccountNameDatabaseName')] + [Parameter(ParameterSetName = 'AccountNameDatabaseId')] + [string]$AccountName, + [Parameter(ParameterSetName = 'ListAllByAccountName')] + [Parameter(ParameterSetName = 'ListAllByAccountId')] + [switch]$List + ) + begin { + Write-Verbose "$($MyInvocation.MyCommand.Name) :: BEGIN :: $(Get-Date)" + Write-Verbose "ParameterSetName: $($PSCmdlet.ParameterSetName)" + if (-not $script:cfSession) { + throw 'Cloudflare session not found. Use Set-CloudflareSession to create a session.' + } + } + process { + if ($AccountName) { + $AccountId = $Script:cfAccountLookupTable[$AccountName] + } + $Splat = @{ + Method = 'Get' + WebSession = $script:cfSession + } + if ($List) { + Write-Verbose "Listing all databases for account $AccountId" + $FindResult = Find-CFD1Database -AccountId $AccountId + $Output = [System.Collections.ArrayList]::new() + foreach ($Database in $FindResult) { + Write-Verbose "Getting details of database with id $($Database.uuid) for account $AccountId" + $Uri = '{0}/accounts/{1}/d1/database/{2}' -f $Script:cfBaseApiUrl, $AccountId, $Database.uuid + $Splat.Uri = $Uri + $Result = Invoke-CFRestMethod @Splat + [void]$Output.Add($Result.result) + } + } + elseif ($Name) { + Write-Verbose "Getting database with name $Name for account $AccountId" + $FindResult = Find-CFD1Database -Name $Name -AccountId $AccountId + $Uri = '{0}/accounts/{1}/d1/database/{2}' -f $Script:cfBaseApiUrl, $AccountId, $FindResult.uuid + $Splat.Uri = $Uri + Write-Verbose "Getting details of database with name $Name for account $AccountId" + $Result = Invoke-CFRestMethod @Splat + $Output = $Result.result + } + else { + Write-Verbose "Getting details of database with $Id for account $AccountId" + $Uri = '{0}/accounts/{1}/d1/database/{2}' -f $Script:cfBaseApiUrl, $AccountId, $Id + $Splat.Uri = $Uri + $Result = Invoke-CFRestMethod @Splat + $Output = $Result.result + } + $Output | ForEach-Object { $_.PSobject.TypeNames.Insert(0, 'Cloudflare.D1Database') } + $Output + } + end {} +} diff --git a/src/pwshCloudflare/Public/Invoke-CFD1Query.ps1 b/src/pwshCloudflare/Public/Invoke-CFD1Query.ps1 new file mode 100644 index 0000000..69d7754 --- /dev/null +++ b/src/pwshCloudflare/Public/Invoke-CFD1Query.ps1 @@ -0,0 +1,66 @@ +<# +.SYNOPSIS + Get Cloudflare account information. +.DESCRIPTION + Get Cloudflare account information. +.PARAMETER AccountId + ID of account to retrieve. If not specified, all accounts will be returned. +.PARAMETER AccountName + Name of account to retrieve. If not specified, all accounts will be returned. +.LINK + https://developers.cloudflare.com/api/operations/cloudflare-d1-query-database +.LINK + https://developers.cloudflare.com/d1/platform/client-api +.LINK + https://developers.cloudflare.com/d1/learning/querying-json/ +#> +function Invoke-CFD1Query { + [CmdletBinding()] + #[OutputType('Cloudflare.D1Database')] + param( + [Alias('DatabaseName')] + [Parameter()] + [string]$Name, + [Alias('DatabaseId')] + [Parameter()] + [string]$Id, + [Parameter(ParameterSetName = 'AccountId')] + [string]$AccountId, + [Parameter(ParameterSetName = 'AccountName')] + [string]$AccountName + ) + begin { + Write-Verbose "$($MyInvocation.MyCommand.Name) :: BEGIN :: $(Get-Date)" + Write-Verbose "ParameterSetName: $($PSCmdlet.ParameterSetName)" + if (-not $script:cfSession) { + throw 'Cloudflare session not found. Use Set-CloudflareSession to create a session.' + } + } + process { + if ($AccountName) { + $AccountId = $Script:cfAccountLookupTable[$AccountName] + } + if ($Name) { + $Id = (Find-CFD1Database -Name $Name -AccountId $AccountId).uuid + } + $Sql = 'CREATE TABLE IF NOT EXISTS users (id integer PRIMARY KEY AUTOINCREMENT, userName text NOT NULL);' + $Sql2 = "INSERT INTO users (userName) VALUES ('JohnDoe');" + $Sql3 = 'SELECT ?1 FROM users;' + $QueryParams = @() + $QueryParams += 'JohnDoe' + $Body = [PSCustomObject]@{ + sql = $Sql3 + params = $QueryParams + } + $Splat = @{ + Body = $Body | ConvertTo-Json + Method = 'POST' + WebSession = $script:cfSession + Uri = '{0}/accounts/{1}/d1/database/{2}/query' -f $Script:cfBaseApiUrl, $AccountId, $Id + } + $Result = Invoke-CFRestMethod @Splat + #$Result.result | ForEach-Object { $_.PSobject.TypeNames.Insert(0, 'Cloudflare.D1Database') } + $Result.result + } + end {} +} diff --git a/src/pwshCloudflare/Public/New-CFD1Database.ps1 b/src/pwshCloudflare/Public/New-CFD1Database.ps1 new file mode 100644 index 0000000..d28a2a7 --- /dev/null +++ b/src/pwshCloudflare/Public/New-CFD1Database.ps1 @@ -0,0 +1,50 @@ +<# +.SYNOPSIS + Get Cloudflare account information. +.DESCRIPTION + Get Cloudflare account information. +.PARAMETER AccountId + ID of account to retrieve. If not specified, all accounts will be returned. +.PARAMETER AccountName + Name of account to retrieve. If not specified, all accounts will be returned. +.LINK + https://developers.cloudflare.com/api/operations/cloudflare-d1-create-database +#> +function New-CFD1Database { + [CmdletBinding()] + [OutputType('Cloudflare.D1Database')] + param( + [Alias('DatabaseName')] + [Parameter()] + [string]$Name, + [Parameter(ParameterSetName = 'AccountId')] + [string]$AccountId, + [Parameter(ParameterSetName = 'AccountName')] + [string]$AccountName + ) + begin { + Write-Verbose "$($MyInvocation.MyCommand.Name) :: BEGIN :: $(Get-Date)" + Write-Verbose "ParameterSetName: $($PSCmdlet.ParameterSetName)" + if (-not $script:cfSession) { + throw 'Cloudflare session not found. Use Set-CloudflareSession to create a session.' + } + } + process { + if ($AccountName) { + $AccountId = $Script:cfAccountLookupTable[$AccountName] + } + $Body = [PSCustomObject]@{ + name = $Name + } + $Splat = @{ + Body = $Body | ConvertTo-Json + Method = 'POST' + WebSession = $script:cfSession + Uri = '{0}/accounts/{1}/d1/database' -f $Script:cfBaseApiUrl, $AccountId + } + $Result = Invoke-CFRestMethod @Splat + $Result.result | ForEach-Object { $_.PSobject.TypeNames.Insert(0, 'Cloudflare.D1Database') } + $Result.result + } + end {} +} diff --git a/src/pwshCloudflare/Public/Remove-CFD1Database.ps1 b/src/pwshCloudflare/Public/Remove-CFD1Database.ps1 new file mode 100644 index 0000000..654b6a1 --- /dev/null +++ b/src/pwshCloudflare/Public/Remove-CFD1Database.ps1 @@ -0,0 +1,50 @@ +<# +.SYNOPSIS + Deletes a Cloudflare D1 database. +.DESCRIPTION + Get Cloudflare account information. +.PARAMETER AccountId + ID of account to retrieve. If not specified, all accounts will be returned. +.PARAMETER AccountName + Name of account to retrieve. If not specified, all accounts will be returned. +.LINK + https://developers.cloudflare.com/api/operations/cloudflare-d1-delete-database +#> +function Remove-CFD1Database { + [CmdletBinding()] + [OutputType('Cloudflare.D1Database')] + param( + [Alias('DatabaseName')] + [Parameter()] + [string]$Name, + [Alias('DatabaseId')] + [Parameter()] + [string]$Id, + [Parameter(ParameterSetName = 'AccountId')] + [string]$AccountId, + [Parameter(ParameterSetName = 'AccountName')] + [string]$AccountName + ) + begin { + Write-Verbose "$($MyInvocation.MyCommand.Name) :: BEGIN :: $(Get-Date)" + Write-Verbose "ParameterSetName: $($PSCmdlet.ParameterSetName)" + if (-not $script:cfSession) { + throw 'Cloudflare session not found. Use Set-CloudflareSession to create a session.' + } + } + process { + if ($AccountName) { + $AccountId = $Script:cfAccountLookupTable[$AccountName] + } + if ($Name) { + $Id = (Find-CFD1Database -Name $Name -AccountId $AccountId).uuid + } + $Splat = @{ + Method = 'DELETE' + Uri = '{0}/accounts/{1}/d1/database/{2}' -f $Script:cfBaseApiUrl, $AccountId, $Id + } + + $null = Invoke-CFRestMethod @Splat + } + end {} +} From 672292cc78603562b66cd3f8b6bdb22e01bed007 Mon Sep 17 00:00:00 2001 From: connorcarnes Date: Fri, 15 Dec 2023 00:51:55 -0600 Subject: [PATCH 07/25] Update Invoke-CFRestMethod with default -WebSession value --- src/pwshCloudflare/Private/Invoke-CFRestMethod.ps1 | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/pwshCloudflare/Private/Invoke-CFRestMethod.ps1 b/src/pwshCloudflare/Private/Invoke-CFRestMethod.ps1 index 91f3488..6feecb2 100644 --- a/src/pwshCloudflare/Private/Invoke-CFRestMethod.ps1 +++ b/src/pwshCloudflare/Private/Invoke-CFRestMethod.ps1 @@ -29,9 +29,15 @@ function Invoke-CFRestMethod { [object]$Body ) try { + if (-not $PSBoundParameters['WebSession']) { + $PSBoundParameters.Add('WebSession', $script:cfSession) + } Invoke-RestMethod @PSBoundParameters } catch { + if ($_.ErrorDetails.Message -eq 'page not found') { + throw $_ + } $ApiError = $_.ErrorDetails.Message | ConvertFrom-Json if ($ApiError.errors.count -eq 1) { throw "Cloudflare API returned error code $($ApiError.errors.code) with message: $($ApiError.errors.message)" From ff9ec08e0ecdc0d1fb3853af9414ff3fcbce8db2 Mon Sep 17 00:00:00 2001 From: connorcarnes Date: Fri, 15 Dec 2023 00:53:20 -0600 Subject: [PATCH 08/25] Update Register-CFArgumentCompleters to wrap strings in quotes --- .../Private/Register-CFArgumentCompleter.ps1 | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/pwshCloudflare/Private/Register-CFArgumentCompleter.ps1 b/src/pwshCloudflare/Private/Register-CFArgumentCompleter.ps1 index 64c004c..1d4244a 100644 --- a/src/pwshCloudflare/Private/Register-CFArgumentCompleter.ps1 +++ b/src/pwshCloudflare/Private/Register-CFArgumentCompleter.ps1 @@ -33,7 +33,9 @@ function Register-CFArgumentCompleter { $commandAst, $fakeBoundParameters ) - $Script:cfZoneLookupTable.Keys | Where-Object { $_ -like "$WordToComplete*" } + $Script:cfZoneLookupTable.Keys | + Where-Object { $_ -like "$WordToComplete*" } | + ForEach-Object { "'$_'" } } } Register-ArgumentCompleter @Splat @@ -56,7 +58,9 @@ function Register-CFArgumentCompleter { $commandAst, $fakeBoundParameters ) - $Script:cfAccountLookupTable.Keys | Where-Object { $_ -like "$WordToComplete*" } + $Script:cfAccountLookupTable.Keys | + Where-Object { $_ -like "$WordToComplete*" } | + ForEach-Object { "'$_'" } } } Register-ArgumentCompleter @Splat From 2a1e16ca9e959a1803e9b0409e4a1cdd8b52da9e Mon Sep 17 00:00:00 2001 From: connorcarnes Date: Fri, 15 Dec 2023 00:54:01 -0600 Subject: [PATCH 09/25] Add Cloudflare.D1Database.Types.ps1xml --- .../Types/Cloudflare.D1Database.Types.ps1xml | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/pwshCloudflare/Types/Cloudflare.D1Database.Types.ps1xml diff --git a/src/pwshCloudflare/Types/Cloudflare.D1Database.Types.ps1xml b/src/pwshCloudflare/Types/Cloudflare.D1Database.Types.ps1xml new file mode 100644 index 0000000..4f3c903 --- /dev/null +++ b/src/pwshCloudflare/Types/Cloudflare.D1Database.Types.ps1xml @@ -0,0 +1,47 @@ + + + Cloudflare.D1Database + + + DatabaseId + uuid + + + DatabaseName + name + + + CreatedAt + created_at + + + NumTables + num_tables + + + FileSize + file_size + + + Region + running_in_region + + + PSStandardMembers + + + DefaultDisplayPropertySet + + DatabaseId + DatabaseName + CreatedAt + NumTables + FileSize + Region + + + + + + + \ No newline at end of file From 57188d39dc169364de8b01fe22529614e5516b0e Mon Sep 17 00:00:00 2001 From: connorcarnes Date: Fri, 15 Dec 2023 00:55:18 -0600 Subject: [PATCH 10/25] Update psd1 with new functions and types --- src/pwshCloudflare/pwshCloudflare.psd1 | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/pwshCloudflare/pwshCloudflare.psd1 b/src/pwshCloudflare/pwshCloudflare.psd1 index 2083329..c357d1b 100644 --- a/src/pwshCloudflare/pwshCloudflare.psd1 +++ b/src/pwshCloudflare/pwshCloudflare.psd1 @@ -62,6 +62,7 @@ # Type files (.ps1xml) to be loaded when importing this module TypesToProcess = @( 'Types\Cloudflare.Account.Types.ps1xml' + 'Types\Cloudflare.D1Database.Types.ps1xml' 'Types\Cloudflare.Zone.Types.ps1xml' 'Types\Cloudflare.ZoneRecord.Types.ps1xml' ) @@ -75,10 +76,14 @@ # Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export. FunctionsToExport = @( 'Get-CFAccount' + 'Get-CFD1Database' 'Get-CFZone' 'Get-CFZoneRecord' + 'Invoke-CFD1Query' + 'New-CFD1Database' 'New-CFZoneRecord' 'Remove-CFZoneRecord' + 'Remove-CFD1Database' 'Set-CFZoneRecord' 'Set-CloudflareSession' 'Test-CloudflareSession' From 95ed0be75506ae3da3bbbbf6eaa83805b17e308a Mon Sep 17 00:00:00 2001 From: connorcarnes Date: Fri, 15 Dec 2023 01:04:39 -0600 Subject: [PATCH 11/25] remove readthedocs.yml --- .readthedocs.yaml | 25 ------------------------- 1 file changed, 25 deletions(-) delete mode 100644 .readthedocs.yaml diff --git a/.readthedocs.yaml b/.readthedocs.yaml deleted file mode 100644 index 76d1c23..0000000 --- a/.readthedocs.yaml +++ /dev/null @@ -1,25 +0,0 @@ -# https://docs.readthedocs.io/en/stable/config-file/index.html - -# .readthedocs.yaml -# Read the Docs configuration file -# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details - -# Required -version: 2 - -build: - os: ubuntu-22.04 - tools: - python: "3.11" - -mkdocs: - configuration: mkdocs.yml - -python: - install: - - requirements: docs/requirements.txt - -# # Build PDF & ePub -formats: all -# - epub -# - pdf From 63e6eb27aa97691e2332a40a469b9cdf294d4761 Mon Sep 17 00:00:00 2001 From: connorcarnes Date: Fri, 15 Dec 2023 15:53:43 -0600 Subject: [PATCH 12/25] Update Invoke-CFD1Query and add D1QueryResult.Types.ps1xml --- README.md | 2 +- .../Private/Invoke-CFRestMethod.ps1 | 3 + .../Public/Invoke-CFD1Query.ps1 | 52 ++++++++---- .../Cloudflare.D1QueryResult.Types.ps1xml | 81 +++++++++++++++++++ src/pwshCloudflare/pwshCloudflare.psd1 | 1 + 5 files changed, 123 insertions(+), 16 deletions(-) create mode 100644 src/pwshCloudflare/Types/Cloudflare.D1QueryResult.Types.ps1xml diff --git a/README.md b/README.md index d50e0d4..2ae32e7 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ PowerShell module for interacting with the Cloudflare API. ## Why -The existing Cloudflare modules in the PSGallery are limited in scope. This project aims to support as many [Cloudflare products](https://developers.cloudflare.com/products/) as possible including R2, D1, Pages, Workers, Workers KV, Workers AI, Images, Stream, Access, Tunnel, Durable Objects, Queues, etc. +The existing Cloudflare modules in the PSGallery are limited in scope. This project aims to support a broad range of [Cloudflare products](https://developers.cloudflare.com/products/) including R2, D1, Pages, Workers, Workers KV, Workers AI, Images, Stream, Access, Tunnel, Durable Objects, Queues, etc. ## Getting Started diff --git a/src/pwshCloudflare/Private/Invoke-CFRestMethod.ps1 b/src/pwshCloudflare/Private/Invoke-CFRestMethod.ps1 index 6feecb2..b0bed17 100644 --- a/src/pwshCloudflare/Private/Invoke-CFRestMethod.ps1 +++ b/src/pwshCloudflare/Private/Invoke-CFRestMethod.ps1 @@ -36,6 +36,9 @@ function Invoke-CFRestMethod { } catch { if ($_.ErrorDetails.Message -eq 'page not found') { + # TODO: This could be that the resource doesn't exists, for example if the user input + # the wrong database name. Update this error handling to dump $PsBoundParameters to + # give the user more information about what went wrong. throw $_ } $ApiError = $_.ErrorDetails.Message | ConvertFrom-Json diff --git a/src/pwshCloudflare/Public/Invoke-CFD1Query.ps1 b/src/pwshCloudflare/Public/Invoke-CFD1Query.ps1 index 69d7754..6bda043 100644 --- a/src/pwshCloudflare/Public/Invoke-CFD1Query.ps1 +++ b/src/pwshCloudflare/Public/Invoke-CFD1Query.ps1 @@ -1,12 +1,33 @@ <# .SYNOPSIS - Get Cloudflare account information. + Executes a SQL query against a Cloudflare D1 database. .DESCRIPTION - Get Cloudflare account information. + Executes a SQL query against a Cloudflare D1 database. Must include a query as well as the account name or id as well as the database name or id. +.PARAMETER Name + The name of the database to query. +.PARAMETER Id + The ID of the database to query. .PARAMETER AccountId - ID of account to retrieve. If not specified, all accounts will be returned. + The ID of the account that owns the database. .PARAMETER AccountName - Name of account to retrieve. If not specified, all accounts will be returned. + The name of the account that owns the database. +.PARAMETER Query + The SQL query to execute. +.PARAMETER QueryParams + The parameters to pass to the query. +.EXAMPLE + $Query = 'CREATE TABLE IF NOT EXISTS users (id integer PRIMARY KEY AUTOINCREMENT, userName text NOT NULL);' + Invoke-CFD1Query -AccountName 'myAccount' -Name 'myDb' -Query $Query + Creates a 'users' table in the 'myDb' database. +.EXAMPLE + $Query = "INSERT INTO users (userName) VALUES ('JohnDoe');" + Invoke-CFD1Query -AccountName 'myAccount' -Name 'myDb' -Query $Query + Inserts a user with username 'JohnDoe' into the 'users' table of the 'myDb' database. +.EXAMPLE + $Query = 'SELECT ?1 FROM users;' + $QueryParams = @('JohnDoe') + Invoke-CFD1Query -AccountName 'myAccount' -Name 'myDb' -Query $Query -QueryParams $QueryParams + Returns the user with username 'JohnDoe' from the 'users' table of the 'myDb' database. .LINK https://developers.cloudflare.com/api/operations/cloudflare-d1-query-database .LINK @@ -16,7 +37,7 @@ #> function Invoke-CFD1Query { [CmdletBinding()] - #[OutputType('Cloudflare.D1Database')] + [OutputType('Cloudflare.D1QueryResult')] param( [Alias('DatabaseName')] [Parameter()] @@ -27,7 +48,11 @@ function Invoke-CFD1Query { [Parameter(ParameterSetName = 'AccountId')] [string]$AccountId, [Parameter(ParameterSetName = 'AccountName')] - [string]$AccountName + [string]$AccountName, + [Parameter(Mandatory = $true)] + [string]$Query, + [Parameter()] + [string[]]$QueryParams ) begin { Write-Verbose "$($MyInvocation.MyCommand.Name) :: BEGIN :: $(Get-Date)" @@ -43,14 +68,11 @@ function Invoke-CFD1Query { if ($Name) { $Id = (Find-CFD1Database -Name $Name -AccountId $AccountId).uuid } - $Sql = 'CREATE TABLE IF NOT EXISTS users (id integer PRIMARY KEY AUTOINCREMENT, userName text NOT NULL);' - $Sql2 = "INSERT INTO users (userName) VALUES ('JohnDoe');" - $Sql3 = 'SELECT ?1 FROM users;' - $QueryParams = @() - $QueryParams += 'JohnDoe' $Body = [PSCustomObject]@{ - sql = $Sql3 - params = $QueryParams + sql = $Query + } + if ($QueryParams) { + $Body | Add-Member -MemberType NoteProperty -Name 'params' -Value $QueryParams } $Splat = @{ Body = $Body | ConvertTo-Json @@ -59,8 +81,8 @@ function Invoke-CFD1Query { Uri = '{0}/accounts/{1}/d1/database/{2}/query' -f $Script:cfBaseApiUrl, $AccountId, $Id } $Result = Invoke-CFRestMethod @Splat - #$Result.result | ForEach-Object { $_.PSobject.TypeNames.Insert(0, 'Cloudflare.D1Database') } - $Result.result + $Result.Result | ForEach-Object { $_.PSobject.TypeNames.Insert(0, 'Cloudflare.D1QueryResult') } + $Result.Result } end {} } diff --git a/src/pwshCloudflare/Types/Cloudflare.D1QueryResult.Types.ps1xml b/src/pwshCloudflare/Types/Cloudflare.D1QueryResult.Types.ps1xml new file mode 100644 index 0000000..35595d5 --- /dev/null +++ b/src/pwshCloudflare/Types/Cloudflare.D1QueryResult.Types.ps1xml @@ -0,0 +1,81 @@ + + + Cloudflare.D1QueryResult + + + Results + + $this.results + + + + ServedBy + + $this.meta.served_by + + + + Duration + + $this.meta.duration + + + + Changes + + $this.meta.changes + + + + LastRowId + + $this.meta.last_row_id + + + + ChangedDb + + $this.meta.changed_db + + + + SizeAfter + + $this.meta.size_after + + + + RowsRead + + $this.meta.rows_read + + + + RowsWritten + + $this.meta.rows_written + + + + PSStandardMembers + + + DefaultDisplayPropertySet + + Success + Results + ServedBy + Duration + Changes + LastRowId + ChangedDb + SizeAfter + RowsRead + RowsWritten + + + + + + + \ No newline at end of file diff --git a/src/pwshCloudflare/pwshCloudflare.psd1 b/src/pwshCloudflare/pwshCloudflare.psd1 index c357d1b..e063ff6 100644 --- a/src/pwshCloudflare/pwshCloudflare.psd1 +++ b/src/pwshCloudflare/pwshCloudflare.psd1 @@ -63,6 +63,7 @@ TypesToProcess = @( 'Types\Cloudflare.Account.Types.ps1xml' 'Types\Cloudflare.D1Database.Types.ps1xml' + 'Types\Cloudflare.D1QueryResult.Types.ps1xml' 'Types\Cloudflare.Zone.Types.ps1xml' 'Types\Cloudflare.ZoneRecord.Types.ps1xml' ) From 64e810a3737e4335a648dfddaa755c059b81e1f4 Mon Sep 17 00:00:00 2001 From: connorcarnes Date: Wed, 20 Dec 2023 17:41:09 -0600 Subject: [PATCH 13/25] Update Get-CFD1Database and add help --- .../Public/Get-CFD1Database.ps1 | 47 +++++++++++-------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/src/pwshCloudflare/Public/Get-CFD1Database.ps1 b/src/pwshCloudflare/Public/Get-CFD1Database.ps1 index 2b111cf..9d51db0 100644 --- a/src/pwshCloudflare/Public/Get-CFD1Database.ps1 +++ b/src/pwshCloudflare/Public/Get-CFD1Database.ps1 @@ -1,12 +1,25 @@ <# .SYNOPSIS - Get Cloudflare account information. + Gets Cloudflare D1 database(s). .DESCRIPTION - Get Cloudflare account information. + Gets Cloudflare D1 database(s). Must provide exactly one of AccountId or AccountName. If Name or Id is not specified, all databases will be returned. +.PARAMETER Name + Name of database to retrieve. If not specified, all databases will be returned. +.PARAMETER Id + ID of database to retrieve. If not specified, all databases will be returned. .PARAMETER AccountId ID of account to retrieve. If not specified, all accounts will be returned. .PARAMETER AccountName Name of account to retrieve. If not specified, all accounts will be returned. +.EXAMPLE + Get-CFD1Database -AccountId '12345' + Gets all databases for account with ID 12345. +.EXAMPLE + Get-CFD1Database -AccountName 'My Account' -Name 'myDb' + Gets databsse 'myDb' for account 'My Account'. +.EXAMPLE + Get-CFD1Database -AccountName 'My Account' -Id '12345' + Gets database with ID 12345 for account 'My Account'. .NOTES The cloudflare-d1-list-databases API endpoint returns the properties uuid, name, version and created_at. The cloudflare-d1-get-database API endpoint returns the same properties as well as num_tables, file_size and running_in region. @@ -21,31 +34,27 @@ function Get-CFD1Database { [CmdletBinding()] [OutputType('Cloudflare.D1Database')] param( - [Parameter(ParameterSetName = 'AccountNameDatabaseName')] - [Parameter(ParameterSetName = 'AccountIdDatabaseName')] + [Parameter(ParameterSetName = 'AccountName')] + [Parameter(ParameterSetName = 'AccountId')] [Alias('DatabaseName')] [string]$Name, - [Parameter(ParameterSetName = 'AccountNameDatabaseId')] - [Parameter(ParameterSetName = 'AccountIdDatabaseId')] + [Parameter(ParameterSetName = 'AccountName')] + [Parameter(ParameterSetName = 'AccountId')] [Alias('DatabaseId')] [string]$Id, - [Parameter(ParameterSetName = 'ListAllByAccountId')] - [Parameter(ParameterSetName = 'AccountIdDatabaseName')] - [Parameter(ParameterSetName = 'AccountIdDatabaseId')] + [Parameter(Mandatory, ParameterSetName = 'AccountId')] [string]$AccountId, - [Parameter(ParameterSetName = 'ListAllByAccountName')] - [Parameter(ParameterSetName = 'AccountNameDatabaseName')] - [Parameter(ParameterSetName = 'AccountNameDatabaseId')] - [string]$AccountName, - [Parameter(ParameterSetName = 'ListAllByAccountName')] - [Parameter(ParameterSetName = 'ListAllByAccountId')] - [switch]$List + [Parameter(Mandatory, ParameterSetName = 'AccountName')] + [string]$AccountName ) begin { Write-Verbose "$($MyInvocation.MyCommand.Name) :: BEGIN :: $(Get-Date)" Write-Verbose "ParameterSetName: $($PSCmdlet.ParameterSetName)" - if (-not $script:cfSession) { - throw 'Cloudflare session not found. Use Set-CloudflareSession to create a session.' + if (($AccountId -and $AccountName) -or + (-not $AccountId -and -not $AccountName) -or + ($Name -and $Id) + ) { + throw 'Must provide exactly one of AccountId or AccountName and exactly one or none of Name or Id' } } process { @@ -56,7 +65,7 @@ function Get-CFD1Database { Method = 'Get' WebSession = $script:cfSession } - if ($List) { + if (-not $Id -and -not $Name) { Write-Verbose "Listing all databases for account $AccountId" $FindResult = Find-CFD1Database -AccountId $AccountId $Output = [System.Collections.ArrayList]::new() From 2fdce41e3e5ccad9048a22993394cb51fb8840ae Mon Sep 17 00:00:00 2001 From: connorcarnes Date: Wed, 20 Dec 2023 17:41:52 -0600 Subject: [PATCH 14/25] add function unit tests --- .../Private/Invoke-CFRestMethod.Tests.ps1 | 54 ++++++++++++++++++ src/Tests/Unit/Public/Get-CFAccount.Tests.ps1 | 44 +++++++++++++++ .../Unit/Public/Get-CFD1Database.Tests.ps1 | 55 +++++++++++++++++++ .../Unit/Public/Invoke-CFD1Query.Tests.ps1 | 35 ++++++++++++ .../Unit/Public/New-CFD1Database.Tests.ps1 | 43 +++++++++++++++ .../Unit/Public/Remove-CFD1Database.Tests.ps1 | 48 ++++++++++++++++ 6 files changed, 279 insertions(+) create mode 100644 src/Tests/Unit/Private/Invoke-CFRestMethod.Tests.ps1 create mode 100644 src/Tests/Unit/Public/Get-CFAccount.Tests.ps1 create mode 100644 src/Tests/Unit/Public/Get-CFD1Database.Tests.ps1 create mode 100644 src/Tests/Unit/Public/Invoke-CFD1Query.Tests.ps1 create mode 100644 src/Tests/Unit/Public/New-CFD1Database.Tests.ps1 create mode 100644 src/Tests/Unit/Public/Remove-CFD1Database.Tests.ps1 diff --git a/src/Tests/Unit/Private/Invoke-CFRestMethod.Tests.ps1 b/src/Tests/Unit/Private/Invoke-CFRestMethod.Tests.ps1 new file mode 100644 index 0000000..705c6a0 --- /dev/null +++ b/src/Tests/Unit/Private/Invoke-CFRestMethod.Tests.ps1 @@ -0,0 +1,54 @@ +#------------------------------------------------------------------------- +Set-Location -Path $PSScriptRoot +#------------------------------------------------------------------------- +$ModuleName = 'pwshCloudflare' +$PathToManifest = [System.IO.Path]::Combine('..', '..', '..', $ModuleName, "$ModuleName.psd1") +#------------------------------------------------------------------------- +if (Get-Module -Name $ModuleName -ErrorAction 'SilentlyContinue') { + #if the module is already in memory, remove it + Remove-Module -Name $ModuleName -Force +} +Import-Module $PathToManifest -Force +#------------------------------------------------------------------------- + +InModuleScope 'pwshCloudflare' { + Describe 'Invoke-CFRestMethod Function Tests' -Tag Unit { + BeforeAll { + $WarningPreference = 'SilentlyContinue' + $ErrorActionPreference = 'SilentlyContinue' + # Mock the dependent cmdlets and variables + Mock Invoke-RestMethod + $script:cfBaseApiUrl = 'https://api.cloudflare.com/client/v4' + $script:cfSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession + } + Context 'Success' { + It 'should call Invoke-RestMethod with ' -TestCases @( + @{ Method = 'GET' }, + @{ Method = 'POST' }, + @{ Method = 'PUT' }, + @{ Method = 'PATCH' }, + @{ Method = 'DELETE' } + ) { + param($Method) + + Invoke-CFRestMethod -Method $Method -Uri 'https://api.cloudflare.com/client/v4/zones' + Assert-MockCalled Invoke-RestMethod -ParameterFilter { $Method -eq $Method } -Times 1 -Exactly + } + It 'should successfully invoke the method with valid parameters' { + $uri = 'https://api.cloudflare.com/client/v4/zones' + Invoke-CFRestMethod -Method 'GET' -Uri $uri + Assert-MockCalled Invoke-RestMethod -ParameterFilter { $Uri -eq $uri } -Times 1 -Exactly + } + } + Context 'Error' { + It 'should throw an error if Cloudflare session is not found' { + $ZoneName = 'example.com' + $script:cfSession = $null + { Invoke-CFRestMethod -ZoneName $ZoneName } | Should -Throw + } + } + AfterAll { + $script:cfSession | Remove-Variable + } + } +} \ No newline at end of file diff --git a/src/Tests/Unit/Public/Get-CFAccount.Tests.ps1 b/src/Tests/Unit/Public/Get-CFAccount.Tests.ps1 new file mode 100644 index 0000000..74223cc --- /dev/null +++ b/src/Tests/Unit/Public/Get-CFAccount.Tests.ps1 @@ -0,0 +1,44 @@ +#------------------------------------------------------------------------- +Set-Location -Path $PSScriptRoot +#------------------------------------------------------------------------- +$ModuleName = 'pwshCloudflare' +$PathToManifest = [System.IO.Path]::Combine('..', '..', '..', $ModuleName, "$ModuleName.psd1") +#------------------------------------------------------------------------- +if (Get-Module -Name $ModuleName -ErrorAction 'SilentlyContinue') { + #if the module is already in memory, remove it + Remove-Module -Name $ModuleName -Force +} +Import-Module $PathToManifest -Force +#------------------------------------------------------------------------- + +InModuleScope 'pwshCloudflare' { + Describe 'Get-CFAccount Function Tests' -Tag Unit { + BeforeAll { + $WarningPreference = 'SilentlyContinue' + $ErrorActionPreference = 'SilentlyContinue' + # Mock the dependent cmdlets and variables + $script:cfBaseApiUrl = 'https://api.cloudflare.com/client/v4' + Mock Invoke-CFRestMethod { return [PSCustomObject]@{ result = [PSCustomObject]@{ AccountName = 'myAcct' } } } + } + # Context 'Error' { + # } + Context 'Success' { + It 'Should get account by name' { + $Result = Get-CFAccount -AccountName 'myAcct' + $Result.AccountName | Should -Be 'myAcct' + } + It 'Should get account by id' { + $Result = Get-CFAccount -AccountId '12345' + $Result.AccountName | Should -Be 'myAcct' + } + It 'Should get all accounts' { + $Result = Get-CFAccount + $Result.AccountName | Should -Be 'myAcct' + } + It 'Should return object of type Cloudflare.Account' { + $Result = Get-CFAccount + $result.PSObject.TypeNames[0] | Should -BeExactly 'Cloudflare.Account' + } + } + } +} \ No newline at end of file diff --git a/src/Tests/Unit/Public/Get-CFD1Database.Tests.ps1 b/src/Tests/Unit/Public/Get-CFD1Database.Tests.ps1 new file mode 100644 index 0000000..38bfecc --- /dev/null +++ b/src/Tests/Unit/Public/Get-CFD1Database.Tests.ps1 @@ -0,0 +1,55 @@ +#------------------------------------------------------------------------- +Set-Location -Path $PSScriptRoot +#------------------------------------------------------------------------- +$ModuleName = 'pwshCloudflare' +$PathToManifest = [System.IO.Path]::Combine('..', '..', '..', $ModuleName, "$ModuleName.psd1") +#------------------------------------------------------------------------- +if (Get-Module -Name $ModuleName -ErrorAction 'SilentlyContinue') { + #if the module is already in memory, remove it + Remove-Module -Name $ModuleName -Force +} +Import-Module $PathToManifest -Force +#------------------------------------------------------------------------- + +InModuleScope 'pwshCloudflare' { + Describe 'Get-CFD1Database Function Tests' -Tag Unit { + BeforeAll { + $WarningPreference = 'SilentlyContinue' + $ErrorActionPreference = 'SilentlyContinue' + # Mock the dependent cmdlets and variables + $script:cfBaseApiUrl = 'https://api.cloudflare.com/client/v4' + $script:cfSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession + Mock Invoke-CFRestMethod { return [PSCustomObject]@{ result = [PSCustomObject]@{ DatabaseName = 'myDb' } } } + Mock Find-CFD1Database { return [PSCustomObject]@{ result = [PSCustomObject]@{ DatabaseName = 'myDb' } } } + } + Context 'Error' { + It 'should throw if account name and id are both provided' { + { Get-CFD1Database -AccountName 'myAccount' -AccountId '12345' -Name 'myDb' } | Should -Throw + } + It 'should throw if neither account name nor id are provided' { + { Get-CFD1Database -Name 'myDb' } | Should -Throw + } + It 'should throw if database name and id are both provided' { + { Get-CFD1Database -AccountName 'myAccount' -Name 'myDb' -Id 'db123' } | Should -Throw + } + } + Context 'Success' { + It 'should list all databases if name or id are not provided' { + $Result = Get-CFD1Database -AccountName 'myAccount' + $Result.DatabaseName | Should -Be 'myDb' + } + It 'should get database by name' { + $Result = Get-CFD1Database -AccountName 'myAccount' -Name 'myDb' + $Result.DatabaseName | Should -Be 'myDb' + } + It 'should get database by id' { + $Result = Get-CFD1Database -AccountName 'myAccount' -Id 'db123' + $Result.DatabaseName | Should -Be 'myDb' + } + It 'returns objects of type Cloudflare.D1Database' { + $Result = Get-CFD1Database -AccountName 'myAccount' -Name 'myDb' + $Result.PSObject.TypeNames[0] | Should -BeExactly 'Cloudflare.D1Database' + } + } + } +} \ No newline at end of file diff --git a/src/Tests/Unit/Public/Invoke-CFD1Query.Tests.ps1 b/src/Tests/Unit/Public/Invoke-CFD1Query.Tests.ps1 new file mode 100644 index 0000000..8f3f20c --- /dev/null +++ b/src/Tests/Unit/Public/Invoke-CFD1Query.Tests.ps1 @@ -0,0 +1,35 @@ +#------------------------------------------------------------------------- +Set-Location -Path $PSScriptRoot +#------------------------------------------------------------------------- +$ModuleName = 'pwshCloudflare' +$PathToManifest = [System.IO.Path]::Combine('..', '..', '..', $ModuleName, "$ModuleName.psd1") +#------------------------------------------------------------------------- +if (Get-Module -Name $ModuleName -ErrorAction 'SilentlyContinue') { + #if the module is already in memory, remove it + Remove-Module -Name $ModuleName -Force +} +Import-Module $PathToManifest -Force +#------------------------------------------------------------------------- + +InModuleScope 'pwshCloudflare' { + Describe 'Invoke-CFD1Query Function Tests' -Tag Unit { + BeforeAll { + $WarningPreference = 'SilentlyContinue' + $ErrorActionPreference = 'SilentlyContinue' + # Mock the dependent cmdlets and variables + Mock Invoke-CFRestMethod + Mock Find-CFD1Database + $script:cfSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession + $script:cfBaseApiUrl = 'https://api.cloudflare.com/client/v4' + $script:cfAccountLookupTable = @{'myAccount' = '12345' } + } + Context 'Error' { + It 'should throw an error if required parameters are missing' { + # Assuming AccountId or AccountName is required + { Invoke-CFD1Query -Query 'SELECT * FROM users;' -Name 'myDb' } | Should -Throw + } + } + # Context 'Success' { + # } + } +} \ No newline at end of file diff --git a/src/Tests/Unit/Public/New-CFD1Database.Tests.ps1 b/src/Tests/Unit/Public/New-CFD1Database.Tests.ps1 new file mode 100644 index 0000000..0e53b28 --- /dev/null +++ b/src/Tests/Unit/Public/New-CFD1Database.Tests.ps1 @@ -0,0 +1,43 @@ +#------------------------------------------------------------------------- +Set-Location -Path $PSScriptRoot +#------------------------------------------------------------------------- +$ModuleName = 'pwshCloudflare' +$PathToManifest = [System.IO.Path]::Combine('..', '..', '..', $ModuleName, "$ModuleName.psd1") +#------------------------------------------------------------------------- +if (Get-Module -Name $ModuleName -ErrorAction 'SilentlyContinue') { + #if the module is already in memory, remove it + Remove-Module -Name $ModuleName -Force +} +Import-Module $PathToManifest -Force +#------------------------------------------------------------------------- + +InModuleScope 'pwshCloudflare' { + Describe 'New-CFD1Database Function Tests' -Tag Unit { + BeforeAll { + $WarningPreference = 'SilentlyContinue' + $ErrorActionPreference = 'SilentlyContinue' + # Mock the dependent cmdlets and variables + $script:cfBaseApiUrl = 'https://api.cloudflare.com/client/v4' + Mock Invoke-CFRestMethod { return [PSCustomObject]@{ result = [PSCustomObject]@{ DatabaseName = 'myDb' } } } + Mock Find-CFD1Database { return [PSCustomObject]@{ result = [PSCustomObject]@{ DatabaseName = 'myDb' } } } + } + Context 'Error' { + It 'should throw an error if required parameters are missing' { + { New-CFD1Database -Name 'myDb' } | Should -Throw + } + } + Context 'Success' { + It 'should successfully create a database with AccountName' { + $Result = New-CFD1Database -AccountName 'myAccount' -Name 'myDb' + $Result.DatabaseName | Should -Be 'myDb' + Assert-MockCalled Invoke-CFRestMethod -Times 1 -Exactly + } + + It 'should successfully create a database with AccountId' { + $Result = New-CFD1Database -AccountId '12345' -Name 'myDb' + $Result.DatabaseName | Should -Be 'myDb' + Assert-MockCalled Invoke-CFRestMethod -Times 1 -Exactly + } + } + } +} \ No newline at end of file diff --git a/src/Tests/Unit/Public/Remove-CFD1Database.Tests.ps1 b/src/Tests/Unit/Public/Remove-CFD1Database.Tests.ps1 new file mode 100644 index 0000000..55d1351 --- /dev/null +++ b/src/Tests/Unit/Public/Remove-CFD1Database.Tests.ps1 @@ -0,0 +1,48 @@ +#------------------------------------------------------------------------- +Set-Location -Path $PSScriptRoot +#------------------------------------------------------------------------- +$ModuleName = 'pwshCloudflare' +$PathToManifest = [System.IO.Path]::Combine('..', '..', '..', $ModuleName, "$ModuleName.psd1") +#------------------------------------------------------------------------- +if (Get-Module -Name $ModuleName -ErrorAction 'SilentlyContinue') { + #if the module is already in memory, remove it + Remove-Module -Name $ModuleName -Force +} +Import-Module $PathToManifest -Force +#------------------------------------------------------------------------- + +InModuleScope 'pwshCloudflare' { + Describe 'Remove-CFD1Query Function Tests' -Tag Unit { + BeforeAll { + $WarningPreference = 'SilentlyContinue' + $ErrorActionPreference = 'SilentlyContinue' + # Mock the dependent cmdlets and variables + Mock Invoke-CFRestMethod { return @{ uuid = 'db123' } } + Mock Find-CFD1Database { return @{ uuid = 'db123' } } + $script:cfSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession + $script:cfBaseApiUrl = 'https://api.cloudflare.com/client/v4' + $script:cfAccountLookupTable = @{'myAccount' = '12345' } + } + Context 'Error' { + It 'should throw an error if required parameters are missing' { + # Assuming AccountId or AccountName is required + { Remove-CFD1Database -Name 'myDb' } | Should -Throw + } + } + Context 'Success' { + It 'should successfully delete the database with AccountName and Database Name' { + Remove-CFD1Database -AccountName 'myAccount' -Name 'myDb' + + Assert-MockCalled Invoke-CFRestMethod -Times 1 -Exactly + Assert-MockCalled Find-CFD1Database -Times 1 -Exactly + } + + It 'should handle deletion with AccountId and Database Id' { + Remove-CFD1Database -AccountId '12345' -Id 'db123' + + Assert-MockCalled Invoke-CFRestMethod -Times 1 -Exactly + Assert-MockCalled Find-CFD1Database -Times 0 -Exactly # Since direct IDs are used + } + } + } +} \ No newline at end of file From ba9225ab06ea09c1cceaf576253be305ddbec8a7 Mon Sep 17 00:00:00 2001 From: connorcarnes Date: Wed, 20 Dec 2023 17:42:23 -0600 Subject: [PATCH 15/25] add comment based help for functions --- docs/Get-CFAccount.md | 92 ++++++++++ docs/Get-CFD1Database.md | 138 +++++++++++++++ docs/Get-CFZoneRecord.md | 1 + docs/Invoke-CFD1Query.md | 165 ++++++++++++++++++ docs/New-CFD1Database.md | 105 +++++++++++ docs/New-CFZoneRecord.md | 1 + docs/Remove-CFD1Database.md | 120 +++++++++++++ docs/Set-CFZoneRecord.md | 21 ++- docs/pwshCloudflare.md | 15 ++ src/pwshCloudflare/Public/Get-CFAccount.ps1 | 6 + .../Public/New-CFD1Database.ps1 | 21 ++- .../Public/Remove-CFD1Database.ps1 | 10 ++ 12 files changed, 685 insertions(+), 10 deletions(-) create mode 100644 docs/Get-CFAccount.md create mode 100644 docs/Get-CFD1Database.md create mode 100644 docs/Invoke-CFD1Query.md create mode 100644 docs/New-CFD1Database.md create mode 100644 docs/Remove-CFD1Database.md diff --git a/docs/Get-CFAccount.md b/docs/Get-CFAccount.md new file mode 100644 index 0000000..6865222 --- /dev/null +++ b/docs/Get-CFAccount.md @@ -0,0 +1,92 @@ +--- +external help file: pwshCloudflare-help.xml +Module Name: pwshCloudflare +online version: https://developers.cloudflare.com/api/operations/accounts-list-accounts +schema: 2.0.0 +--- + +# Get-CFAccount + +## SYNOPSIS +Get Cloudflare account information. + +## SYNTAX + +### AccountId (Default) +``` +Get-CFAccount [-AccountId ] [] +``` + +### AccountName +``` +Get-CFAccount [-AccountName ] [] +``` + +## DESCRIPTION +Get Cloudflare account information. + +## EXAMPLES + +### EXAMPLE 1 +``` +Get-CFAccount -AccountId '12345' +Gets account with ID 12345. +``` + +### EXAMPLE 2 +``` +Get-CFAccount +Lists accounts available to current user. +``` + +## PARAMETERS + +### -AccountId +ID of account to retrieve. +If not specified, all accounts will be returned. + +```yaml +Type: String +Parameter Sets: AccountId +Aliases: + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -AccountName +Name of account to retrieve. +If not specified, all accounts will be returned. + +```yaml +Type: String +Parameter Sets: AccountName +Aliases: + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### CommonParameters +This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. +For more information, see about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216). + +## INPUTS + +## OUTPUTS + +### Cloudflare.Account +## NOTES + +## RELATED LINKS + +[https://developers.cloudflare.com/api/operations/accounts-list-accounts](https://developers.cloudflare.com/api/operations/accounts-list-accounts) + +[https://developers.cloudflare.com/api/operations/accounts-account-details](https://developers.cloudflare.com/api/operations/accounts-account-details) + diff --git a/docs/Get-CFD1Database.md b/docs/Get-CFD1Database.md new file mode 100644 index 0000000..2215eb8 --- /dev/null +++ b/docs/Get-CFD1Database.md @@ -0,0 +1,138 @@ +--- +external help file: pwshCloudflare-help.xml +Module Name: pwshCloudflare +online version: https://developers.cloudflare.com/api/operations/cloudflare-d1-list-databases +schema: 2.0.0 +--- + +# Get-CFD1Database + +## SYNOPSIS +Gets Cloudflare D1 database(s). + +## SYNTAX + +### AccountId +``` +Get-CFD1Database [-Name ] [-Id ] -AccountId [] +``` + +### AccountName +``` +Get-CFD1Database [-Name ] [-Id ] -AccountName [] +``` + +## DESCRIPTION +Gets Cloudflare D1 database(s). +Must provide exactly one of AccountId or AccountName. +If Name or Id is not specified, all databases will be returned. + +## EXAMPLES + +### EXAMPLE 1 +``` +Get-CFD1Database -AccountId '12345' +Gets all databases for account with ID 12345. +``` + +### EXAMPLE 2 +``` +Get-CFD1Database -AccountName 'My Account' -Name 'myDb' +Gets databsse 'myDb' for account 'My Account'. +``` + +### EXAMPLE 3 +``` +Get-CFD1Database -AccountName 'My Account' -Id '12345' +Gets database with ID 12345 for account 'My Account'. +``` + +## PARAMETERS + +### -Name +Name of database to retrieve. +If not specified, all databases will be returned. + +```yaml +Type: String +Parameter Sets: (All) +Aliases: DatabaseName + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -Id +ID of database to retrieve. +If not specified, all databases will be returned. + +```yaml +Type: String +Parameter Sets: (All) +Aliases: DatabaseId + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -AccountId +ID of account to retrieve. +If not specified, all accounts will be returned. + +```yaml +Type: String +Parameter Sets: AccountId +Aliases: + +Required: True +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -AccountName +Name of account to retrieve. +If not specified, all accounts will be returned. + +```yaml +Type: String +Parameter Sets: AccountName +Aliases: + +Required: True +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### CommonParameters +This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. +For more information, see about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216). + +## INPUTS + +## OUTPUTS + +### Cloudflare.D1Database +## NOTES +The cloudflare-d1-list-databases API endpoint returns the properties uuid, name, version and created_at. +The cloudflare-d1-get-database API endpoint returns the same properties as well as num_tables, file_size and running_in region. +When listing all databases or getting a database by name this function makes multiple API calls. +The first call is to get the database ID(s). +The subsequent call(s) use the ID(s) to get the database details. +This is done so that the same output is returned regardless of parameters used. + +## RELATED LINKS + +[https://developers.cloudflare.com/api/operations/cloudflare-d1-list-databases](https://developers.cloudflare.com/api/operations/cloudflare-d1-list-databases) + +[https://developers.cloudflare.com/api/operations/cloudflare-d1-get-database](https://developers.cloudflare.com/api/operations/cloudflare-d1-get-database) + diff --git a/docs/Get-CFZoneRecord.md b/docs/Get-CFZoneRecord.md index 8dc1b95..13450a6 100644 --- a/docs/Get-CFZoneRecord.md +++ b/docs/Get-CFZoneRecord.md @@ -83,6 +83,7 @@ For more information, see about_CommonParameters (http://go.microsoft.com/fwlink ### None. ## OUTPUTS +### Cloudflare.ZoneRecord ## NOTES ## RELATED LINKS diff --git a/docs/Invoke-CFD1Query.md b/docs/Invoke-CFD1Query.md new file mode 100644 index 0000000..de5ffcd --- /dev/null +++ b/docs/Invoke-CFD1Query.md @@ -0,0 +1,165 @@ +--- +external help file: pwshCloudflare-help.xml +Module Name: pwshCloudflare +online version: https://developers.cloudflare.com/api/operations/cloudflare-d1-query-database +schema: 2.0.0 +--- + +# Invoke-CFD1Query + +## SYNOPSIS +Executes a SQL query against a Cloudflare D1 database. + +## SYNTAX + +### AccountId +``` +Invoke-CFD1Query [-Name ] [-Id ] [-AccountId ] -Query + [-QueryParams ] [] +``` + +### AccountName +``` +Invoke-CFD1Query [-Name ] [-Id ] [-AccountName ] -Query + [-QueryParams ] [] +``` + +## DESCRIPTION +Executes a SQL query against a Cloudflare D1 database. +Must include a query as well as the account name or id as well as the database name or id. + +## EXAMPLES + +### EXAMPLE 1 +``` +$Query = 'CREATE TABLE IF NOT EXISTS users (id integer PRIMARY KEY AUTOINCREMENT, userName text NOT NULL);' +Invoke-CFD1Query -AccountName 'myAccount' -Name 'myDb' -Query $Query +Creates a 'users' table in the 'myDb' database. +``` + +### EXAMPLE 2 +``` +$Query = "INSERT INTO users (userName) VALUES ('JohnDoe');" +Invoke-CFD1Query -AccountName 'myAccount' -Name 'myDb' -Query $Query +Inserts a user with username 'JohnDoe' into the 'users' table of the 'myDb' database. +``` + +### EXAMPLE 3 +``` +$Query = 'SELECT ?1 FROM users;' +$QueryParams = @('JohnDoe') +Invoke-CFD1Query -AccountName 'myAccount' -Name 'myDb' -Query $Query -QueryParams $QueryParams +Returns the user with username 'JohnDoe' from the 'users' table of the 'myDb' database. +``` + +## PARAMETERS + +### -Name +The name of the database to query. + +```yaml +Type: String +Parameter Sets: (All) +Aliases: DatabaseName + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -Id +The ID of the database to query. + +```yaml +Type: String +Parameter Sets: (All) +Aliases: DatabaseId + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -AccountId +The ID of the account that owns the database. + +```yaml +Type: String +Parameter Sets: AccountId +Aliases: + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -AccountName +The name of the account that owns the database. + +```yaml +Type: String +Parameter Sets: AccountName +Aliases: + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -Query +The SQL query to execute. + +```yaml +Type: String +Parameter Sets: (All) +Aliases: + +Required: True +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -QueryParams +The parameters to pass to the query. + +```yaml +Type: String[] +Parameter Sets: (All) +Aliases: + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### CommonParameters +This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. +For more information, see about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216). + +## INPUTS + +## OUTPUTS + +### Cloudflare.D1QueryResult +## NOTES + +## RELATED LINKS + +[https://developers.cloudflare.com/api/operations/cloudflare-d1-query-database](https://developers.cloudflare.com/api/operations/cloudflare-d1-query-database) + +[https://developers.cloudflare.com/d1/platform/client-api](https://developers.cloudflare.com/d1/platform/client-api) + +[https://developers.cloudflare.com/d1/learning/querying-json/](https://developers.cloudflare.com/d1/learning/querying-json/) + diff --git a/docs/New-CFD1Database.md b/docs/New-CFD1Database.md new file mode 100644 index 0000000..901dd77 --- /dev/null +++ b/docs/New-CFD1Database.md @@ -0,0 +1,105 @@ +--- +external help file: pwshCloudflare-help.xml +Module Name: pwshCloudflare +online version: https://developers.cloudflare.com/api/operations/cloudflare-d1-create-database +schema: 2.0.0 +--- + +# New-CFD1Database + +## SYNOPSIS +Creates Cloudflare D1 database. + +## SYNTAX + +### AccountId +``` +New-CFD1Database -Name -AccountId [] +``` + +### AccountName +``` +New-CFD1Database -Name -AccountName [] +``` + +## DESCRIPTION +Creates Cloudflare D1 database. + +## EXAMPLES + +### EXAMPLE 1 +``` +New-CFD1Database -Name 'myDb' -AccountId '12345' +Creates database 'myDb' for account with ID 12345. +``` + +### EXAMPLE 2 +``` +New-CFD1Database -Name 'myDb' -AccountName 'My Account' +Creates database 'myDb' for account 'My Account'. +``` + +## PARAMETERS + +### -Name +Name of database to create. + +```yaml +Type: String +Parameter Sets: (All) +Aliases: DatabaseName + +Required: True +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -AccountId +ID of account to retrieve. +If not specified, all accounts will be returned. + +```yaml +Type: String +Parameter Sets: AccountId +Aliases: + +Required: True +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -AccountName +Name of account to retrieve. +If not specified, all accounts will be returned. + +```yaml +Type: String +Parameter Sets: AccountName +Aliases: + +Required: True +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### CommonParameters +This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. +For more information, see about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216). + +## INPUTS + +## OUTPUTS + +### Cloudflare.D1Database +## NOTES + +## RELATED LINKS + +[https://developers.cloudflare.com/api/operations/cloudflare-d1-create-database](https://developers.cloudflare.com/api/operations/cloudflare-d1-create-database) + diff --git a/docs/New-CFZoneRecord.md b/docs/New-CFZoneRecord.md index a80c165..adaa63c 100644 --- a/docs/New-CFZoneRecord.md +++ b/docs/New-CFZoneRecord.md @@ -164,6 +164,7 @@ For more information, see about_CommonParameters (http://go.microsoft.com/fwlink ## OUTPUTS +### Cloudflare.ZoneRecord ## NOTES ## RELATED LINKS diff --git a/docs/Remove-CFD1Database.md b/docs/Remove-CFD1Database.md new file mode 100644 index 0000000..4d040ca --- /dev/null +++ b/docs/Remove-CFD1Database.md @@ -0,0 +1,120 @@ +--- +external help file: pwshCloudflare-help.xml +Module Name: pwshCloudflare +online version: https://developers.cloudflare.com/api/operations/cloudflare-d1-delete-database +schema: 2.0.0 +--- + +# Remove-CFD1Database + +## SYNOPSIS +Deletes a Cloudflare D1 database. + +## SYNTAX + +### AccountId +``` +Remove-CFD1Database [-Name ] [-Id ] [-AccountId ] [] +``` + +### AccountName +``` +Remove-CFD1Database [-Name ] [-Id ] [-AccountName ] [] +``` + +## DESCRIPTION +Get Cloudflare account information. + +## EXAMPLES + +### EXAMPLE 1 +``` +Remove-CFD1Database -Name 'myDb' -AccountId '12345' +Deletes database 'myDb' for account with ID 12345. +``` + +### EXAMPLE 2 +``` +Remove-CFD1Database -Name 'myDb' -AccountName 'My Account' +Deletes database 'myDb' for account 'My Account'. +``` + +## PARAMETERS + +### -Name +Name of database to delete. + +```yaml +Type: String +Parameter Sets: (All) +Aliases: DatabaseName + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -Id +ID of database to delete. + +```yaml +Type: String +Parameter Sets: (All) +Aliases: DatabaseId + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -AccountId +ID of account to retrieve. +If not specified, all accounts will be returned. + +```yaml +Type: String +Parameter Sets: AccountId +Aliases: + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -AccountName +Name of account to retrieve. +If not specified, all accounts will be returned. + +```yaml +Type: String +Parameter Sets: AccountName +Aliases: + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### CommonParameters +This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. +For more information, see about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216). + +## INPUTS + +## OUTPUTS + +### Cloudflare.D1Database +## NOTES + +## RELATED LINKS + +[https://developers.cloudflare.com/api/operations/cloudflare-d1-delete-database](https://developers.cloudflare.com/api/operations/cloudflare-d1-delete-database) + diff --git a/docs/Set-CFZoneRecord.md b/docs/Set-CFZoneRecord.md index ffcdb97..2a3e419 100644 --- a/docs/Set-CFZoneRecord.md +++ b/docs/Set-CFZoneRecord.md @@ -15,13 +15,13 @@ Updates a DNS record for a Cloudflare zone. ### ZoneId ``` Set-CFZoneRecord -RecordId -ZoneId [-Content ] [-Name ] [-Type ] - [-TTL ] [-Proxied ] [] + [-TTL ] [-Proxied ] [-Priority ] [] ``` ### ZoneName ``` Set-CFZoneRecord -RecordId -ZoneName [-Content ] [-Name ] [-Type ] - [-TTL ] [-Proxied ] [] + [-TTL ] [-Proxied ] [-Priority ] [] ``` ## DESCRIPTION @@ -160,6 +160,22 @@ Accept pipeline input: False Accept wildcard characters: False ``` +### -Priority +Required for MX, SRV, and URI records; unused by other record types. +Records with lower priorities are preferred. + +```yaml +Type: Int32 +Parameter Sets: (All) +Aliases: + +Required: False +Position: Named +Default value: 0 +Accept pipeline input: False +Accept wildcard characters: False +``` + ### CommonParameters This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216). @@ -168,6 +184,7 @@ For more information, see about_CommonParameters (http://go.microsoft.com/fwlink ## OUTPUTS +### Cloudflare.ZoneRecord ## NOTES ## RELATED LINKS diff --git a/docs/pwshCloudflare.md b/docs/pwshCloudflare.md index 476ae40..9e06883 100644 --- a/docs/pwshCloudflare.md +++ b/docs/pwshCloudflare.md @@ -11,15 +11,30 @@ Locale: en-US PowerShell module for interacting with the Cloudflare API ## pwshCloudflare Cmdlets +### [Get-CFAccount](Get-CFAccount.md) +Get Cloudflare account information. + +### [Get-CFD1Database](Get-CFD1Database.md) +Gets Cloudflare D1 database(s). + ### [Get-CFZone](Get-CFZone.md) Retrieves information about a Cloudflare zone. ### [Get-CFZoneRecord](Get-CFZoneRecord.md) Retrieves DNS records for a Cloudflare zone. +### [Invoke-CFD1Query](Invoke-CFD1Query.md) +Executes a SQL query against a Cloudflare D1 database. + +### [New-CFD1Database](New-CFD1Database.md) +Creates Cloudflare D1 database. + ### [New-CFZoneRecord](New-CFZoneRecord.md) Creates a new DNS record for a Cloudflare zone. +### [Remove-CFD1Database](Remove-CFD1Database.md) +Deletes a Cloudflare D1 database. + ### [Remove-CFZoneRecord](Remove-CFZoneRecord.md) Removes a DNS record from a Cloudflare zone. diff --git a/src/pwshCloudflare/Public/Get-CFAccount.ps1 b/src/pwshCloudflare/Public/Get-CFAccount.ps1 index d2e4a7b..308d880 100644 --- a/src/pwshCloudflare/Public/Get-CFAccount.ps1 +++ b/src/pwshCloudflare/Public/Get-CFAccount.ps1 @@ -7,6 +7,12 @@ ID of account to retrieve. If not specified, all accounts will be returned. .PARAMETER AccountName Name of account to retrieve. If not specified, all accounts will be returned. +.EXAMPLE + Get-CFAccount -AccountId '12345' + Gets account with ID 12345. +.EXAMPLE + Get-CFAccount + Lists accounts available to current user. .LINK https://developers.cloudflare.com/api/operations/accounts-list-accounts .LINK diff --git a/src/pwshCloudflare/Public/New-CFD1Database.ps1 b/src/pwshCloudflare/Public/New-CFD1Database.ps1 index d28a2a7..a23ec40 100644 --- a/src/pwshCloudflare/Public/New-CFD1Database.ps1 +++ b/src/pwshCloudflare/Public/New-CFD1Database.ps1 @@ -1,12 +1,20 @@ <# .SYNOPSIS - Get Cloudflare account information. + Creates Cloudflare D1 database. .DESCRIPTION - Get Cloudflare account information. + Creates Cloudflare D1 database. +.PARAMETER Name + Name of database to create. .PARAMETER AccountId ID of account to retrieve. If not specified, all accounts will be returned. .PARAMETER AccountName Name of account to retrieve. If not specified, all accounts will be returned. +.EXAMPLE + New-CFD1Database -Name 'myDb' -AccountId '12345' + Creates database 'myDb' for account with ID 12345. +.EXAMPLE + New-CFD1Database -Name 'myDb' -AccountName 'My Account' + Creates database 'myDb' for account 'My Account'. .LINK https://developers.cloudflare.com/api/operations/cloudflare-d1-create-database #> @@ -15,19 +23,16 @@ function New-CFD1Database { [OutputType('Cloudflare.D1Database')] param( [Alias('DatabaseName')] - [Parameter()] + [Parameter(Mandatory)] [string]$Name, - [Parameter(ParameterSetName = 'AccountId')] + [Parameter(Mandatory, ParameterSetName = 'AccountId')] [string]$AccountId, - [Parameter(ParameterSetName = 'AccountName')] + [Parameter(Mandatory, ParameterSetName = 'AccountName')] [string]$AccountName ) begin { Write-Verbose "$($MyInvocation.MyCommand.Name) :: BEGIN :: $(Get-Date)" Write-Verbose "ParameterSetName: $($PSCmdlet.ParameterSetName)" - if (-not $script:cfSession) { - throw 'Cloudflare session not found. Use Set-CloudflareSession to create a session.' - } } process { if ($AccountName) { diff --git a/src/pwshCloudflare/Public/Remove-CFD1Database.ps1 b/src/pwshCloudflare/Public/Remove-CFD1Database.ps1 index 654b6a1..69d4efc 100644 --- a/src/pwshCloudflare/Public/Remove-CFD1Database.ps1 +++ b/src/pwshCloudflare/Public/Remove-CFD1Database.ps1 @@ -3,10 +3,20 @@ Deletes a Cloudflare D1 database. .DESCRIPTION Get Cloudflare account information. +.PARAMETER Name + Name of database to delete. +.PARAMETER Id + ID of database to delete. .PARAMETER AccountId ID of account to retrieve. If not specified, all accounts will be returned. .PARAMETER AccountName Name of account to retrieve. If not specified, all accounts will be returned. +.EXAMPLE + Remove-CFD1Database -Name 'myDb' -AccountId '12345' + Deletes database 'myDb' for account with ID 12345. +.EXAMPLE + Remove-CFD1Database -Name 'myDb' -AccountName 'My Account' + Deletes database 'myDb' for account 'My Account'. .LINK https://developers.cloudflare.com/api/operations/cloudflare-d1-delete-database #> From ddd942bfa7746c6fcc1ed0e347878b75c4de4975 Mon Sep 17 00:00:00 2001 From: connorcarnes Date: Wed, 20 Dec 2023 17:50:58 -0600 Subject: [PATCH 16/25] cfSession check moved to Invoke-CFRestMethod, updated functions and tests to account for this --- src/Tests/Unit/Public/Get-CFZone.Tests.ps1 | 11 ++--------- src/Tests/Unit/Public/Get-CFZoneRecord.Tests.ps1 | 10 ++-------- src/Tests/Unit/Public/New-CFZoneRecord.Tests.ps1 | 7 ------- .../Unit/Public/Remove-CFZoneRecord.Tests.ps1 | 11 ++--------- src/Tests/Unit/Public/Set-CFZoneRecord.Tests.ps1 | 16 ++-------------- src/pwshCloudflare/Public/Get-CFAccount.ps1 | 3 --- src/pwshCloudflare/Public/Get-CFZone.ps1 | 3 --- src/pwshCloudflare/Public/Get-CFZoneRecord.ps1 | 3 --- src/pwshCloudflare/Public/Invoke-CFD1Query.ps1 | 3 --- src/pwshCloudflare/Public/New-CFZoneRecord.ps1 | 3 --- .../Public/Remove-CFD1Database.ps1 | 3 --- .../Public/Remove-CFZoneRecord.ps1 | 3 --- src/pwshCloudflare/Public/Set-CFZoneRecord.ps1 | 3 --- 13 files changed, 8 insertions(+), 71 deletions(-) diff --git a/src/Tests/Unit/Public/Get-CFZone.Tests.ps1 b/src/Tests/Unit/Public/Get-CFZone.Tests.ps1 index 19794fc..c2b7aa1 100644 --- a/src/Tests/Unit/Public/Get-CFZone.Tests.ps1 +++ b/src/Tests/Unit/Public/Get-CFZone.Tests.ps1 @@ -19,17 +19,10 @@ InModuleScope 'pwshCloudflare' { # Mock the dependent cmdlets and variables Mock Invoke-CFRestMethod { return @{ result = @(@{ Name = 'TestZone'; Id = '12345' }) } } $script:cfBaseApiUrl = 'https://api.cloudflare.com/client/v4' - } - BeforeEach { $script:cfSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession } - Context 'Error' { - It 'should throw an error if Cloudflare session is not found' { - $ZoneName = 'example.com' - $script:cfSession = $null - { Get-CFZone -ZoneName $ZoneName } | Should -Throw - } - } + # Context 'Error' { + # } Context 'Success' { It 'Calls Invoke-CFRestMethod with correct parameters for ZoneName' { Get-CFZone -ZoneName 'example.com' diff --git a/src/Tests/Unit/Public/Get-CFZoneRecord.Tests.ps1 b/src/Tests/Unit/Public/Get-CFZoneRecord.Tests.ps1 index 9362d41..13c7af9 100644 --- a/src/Tests/Unit/Public/Get-CFZoneRecord.Tests.ps1 +++ b/src/Tests/Unit/Public/Get-CFZoneRecord.Tests.ps1 @@ -20,16 +20,10 @@ InModuleScope 'pwshCloudflare' { Mock Invoke-CFRestMethod { return @{ result = @(@{ RecordId = '12345'; ZoneName = 'example.com' }) } } $script:cfBaseApiUrl = 'https://api.cloudflare.com/client/v4' $Script:cfZoneLookupTable = @{'example.com' = '23456' } - } - BeforeEach { $script:cfSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession } - Context 'Error' { - It 'should throw an error if Cloudflare session is not found' { - $script:cfSession = $null - { Get-CFZoneRecord -ZoneName 'example.com' } | Should -Throw - } - } + # Context 'Error' { + # } Context 'Success' { It 'Calls Invoke-CFRestMethod with correct parameters for ZoneName' { Get-CFZoneRecord -ZoneName 'example.com' diff --git a/src/Tests/Unit/Public/New-CFZoneRecord.Tests.ps1 b/src/Tests/Unit/Public/New-CFZoneRecord.Tests.ps1 index ebddb3b..ec06f53 100644 --- a/src/Tests/Unit/Public/New-CFZoneRecord.Tests.ps1 +++ b/src/Tests/Unit/Public/New-CFZoneRecord.Tests.ps1 @@ -20,16 +20,9 @@ InModuleScope 'pwshCloudflare' { Mock Invoke-CFRestMethod { return @{ result = @{'id' = 'new_record_id' } } } $script:cfBaseApiUrl = 'https://api.cloudflare.com/client/v4' $Script:cfZoneLookupTable = @{'example.com' = '23456' } - } - BeforeEach { $script:cfSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession } Context 'Error' { - It 'should throw an error if Cloudflare session is not found' { - $ZoneName = 'example.com' - $script:cfSession = $null - { New-CFZoneRecord -ZoneName $ZoneName -Type TXT -Content 'Foo' -Name 'Bar' } | Should -Throw - } It 'should throw an error if required parameters are missing' { { New-CFZoneRecord -Content '192.168.1.1' -Name 'example.com' -Type 'A' -TTL 3600 -Proxied $true } | Should -Throw } diff --git a/src/Tests/Unit/Public/Remove-CFZoneRecord.Tests.ps1 b/src/Tests/Unit/Public/Remove-CFZoneRecord.Tests.ps1 index c97e943..9f424f5 100644 --- a/src/Tests/Unit/Public/Remove-CFZoneRecord.Tests.ps1 +++ b/src/Tests/Unit/Public/Remove-CFZoneRecord.Tests.ps1 @@ -19,17 +19,10 @@ InModuleScope 'pwshCloudflare' { $script:cfBaseApiUrl = 'https://api.cloudflare.com/client/v4' $Script:cfZoneLookupTable = @{'example.com' = '12345' } Mock Invoke-CFRestMethod { return @{ result = @{'id' = '12345' } } } - } - BeforeEach { $script:cfSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession } - Context 'Error' { - It 'should throw an error if Cloudflare session is not found' { - $ZoneName = 'example.com' - $script:cfSession = $null - { Remove-CFZoneRecord -ZoneName $ZoneName -RecordId '12345' } | Should -Throw - } - } + # Context 'Error' { + # } Context 'Success' { It 'Calls Invoke-CFRestMethod with correct parameters for ZoneName' { Remove-CFZoneRecord -ZoneName 'example.com' -RecordId '12345' diff --git a/src/Tests/Unit/Public/Set-CFZoneRecord.Tests.ps1 b/src/Tests/Unit/Public/Set-CFZoneRecord.Tests.ps1 index c4b2eab..0748480 100644 --- a/src/Tests/Unit/Public/Set-CFZoneRecord.Tests.ps1 +++ b/src/Tests/Unit/Public/Set-CFZoneRecord.Tests.ps1 @@ -7,19 +7,10 @@ $script:cfBaseApiUrl = 'https://api.cloudflare.com/client/v4' $script:cfZoneLookupTable = @{ 'example.com' = '12345' } Mock Invoke-CFRestMethod { return @{ result = @{'id' = 'updated_record_id' } } } - } - - BeforeEach { $script:cfSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession } - - Context 'Error Handling' { - It 'should throw an error if Cloudflare session is not found' { - $script:cfSession = $null - { Set-CFZoneRecord -ZoneName 'example.com' -RecordId '123abc456abc' -Content 'UpdatedContent' } | Should -Throw - } - } - + # Context 'Error Handling' { + # } Context 'Success' { It 'Calls Invoke-CFRestMethod with correct parameters for ZoneName' { Set-CFZoneRecord -ZoneName 'example.com' -RecordId '123abc456abc' -Content 'UpdatedContent' @@ -29,7 +20,6 @@ $Body -like '*UpdatedContent*' } } - It 'Calls Invoke-CFRestMethod with correct parameters for ZoneID' { Set-CFZoneRecord -ZoneId '12345' -RecordId '123abc456abc' -Content 'UpdatedContent' Assert-MockCalled Invoke-CFRestMethod -ParameterFilter { @@ -38,13 +28,11 @@ $Body -like '*UpdatedContent*' } } - It 'Returns objects of type Cloudflare.ZoneRecord' { $result = Set-CFZoneRecord -ZoneName 'example.com' -RecordId '123abc456abc' -Content 'UpdatedContent' $result.PSObject.TypeNames -contains 'Cloudflare.ZoneRecord' | Should -Be $true } } - AfterAll { $script:cfSession | Remove-Variable } diff --git a/src/pwshCloudflare/Public/Get-CFAccount.ps1 b/src/pwshCloudflare/Public/Get-CFAccount.ps1 index 308d880..61184cc 100644 --- a/src/pwshCloudflare/Public/Get-CFAccount.ps1 +++ b/src/pwshCloudflare/Public/Get-CFAccount.ps1 @@ -30,9 +30,6 @@ function Get-CFAccount { begin { Write-Verbose "$($MyInvocation.MyCommand.Name) :: BEGIN :: $(Get-Date)" Write-Verbose "ParameterSetName: $($PSCmdlet.ParameterSetName)" - if (-not $script:cfSession) { - throw 'Cloudflare session not found. Use Set-CloudflareSession to create a session.' - } } process { if ($AccountName) { diff --git a/src/pwshCloudflare/Public/Get-CFZone.ps1 b/src/pwshCloudflare/Public/Get-CFZone.ps1 index 3c5ae90..c31ce6e 100644 --- a/src/pwshCloudflare/Public/Get-CFZone.ps1 +++ b/src/pwshCloudflare/Public/Get-CFZone.ps1 @@ -38,9 +38,6 @@ function Get-CFZone { begin { Write-Verbose "$($MyInvocation.MyCommand.Name) :: BEGIN :: $(Get-Date)" Write-Verbose "ParameterSetName: $($PSCmdlet.ParameterSetName)" - if (-not $script:cfSession) { - throw 'Cloudflare session not found. Use Set-CloudflareSession to create a session.' - } } process { if ($ZoneName) { diff --git a/src/pwshCloudflare/Public/Get-CFZoneRecord.ps1 b/src/pwshCloudflare/Public/Get-CFZoneRecord.ps1 index 1bee149..5170d6c 100644 --- a/src/pwshCloudflare/Public/Get-CFZoneRecord.ps1 +++ b/src/pwshCloudflare/Public/Get-CFZoneRecord.ps1 @@ -30,9 +30,6 @@ function Get-CFZoneRecord { begin { Write-Verbose "$($MyInvocation.MyCommand.Name) :: BEGIN :: $(Get-Date)" Write-Verbose "ParameterSetName: $($PSCmdlet.ParameterSetName)" - if (-not $script:cfSession) { - throw 'Cloudflare session not found. Use Set-CloudflareSession to create a session.' - } } process { if ($ZoneName) { diff --git a/src/pwshCloudflare/Public/Invoke-CFD1Query.ps1 b/src/pwshCloudflare/Public/Invoke-CFD1Query.ps1 index 6bda043..ac6d625 100644 --- a/src/pwshCloudflare/Public/Invoke-CFD1Query.ps1 +++ b/src/pwshCloudflare/Public/Invoke-CFD1Query.ps1 @@ -57,9 +57,6 @@ function Invoke-CFD1Query { begin { Write-Verbose "$($MyInvocation.MyCommand.Name) :: BEGIN :: $(Get-Date)" Write-Verbose "ParameterSetName: $($PSCmdlet.ParameterSetName)" - if (-not $script:cfSession) { - throw 'Cloudflare session not found. Use Set-CloudflareSession to create a session.' - } } process { if ($AccountName) { diff --git a/src/pwshCloudflare/Public/New-CFZoneRecord.ps1 b/src/pwshCloudflare/Public/New-CFZoneRecord.ps1 index cf68b2d..2844011 100644 --- a/src/pwshCloudflare/Public/New-CFZoneRecord.ps1 +++ b/src/pwshCloudflare/Public/New-CFZoneRecord.ps1 @@ -68,9 +68,6 @@ function New-CFZoneRecord { begin { Write-Verbose "$($MyInvocation.MyCommand.Name) :: BEGIN :: $(Get-Date)" Write-Verbose "ParameterSetName: $($PSCmdlet.ParameterSetName)" - if (-not $script:cfSession) { - throw 'Cloudflare session not found. Use Set-CloudflareSession to create a session.' - } } process { if ($ZoneName) { diff --git a/src/pwshCloudflare/Public/Remove-CFD1Database.ps1 b/src/pwshCloudflare/Public/Remove-CFD1Database.ps1 index 69d4efc..59ce37f 100644 --- a/src/pwshCloudflare/Public/Remove-CFD1Database.ps1 +++ b/src/pwshCloudflare/Public/Remove-CFD1Database.ps1 @@ -38,9 +38,6 @@ function Remove-CFD1Database { begin { Write-Verbose "$($MyInvocation.MyCommand.Name) :: BEGIN :: $(Get-Date)" Write-Verbose "ParameterSetName: $($PSCmdlet.ParameterSetName)" - if (-not $script:cfSession) { - throw 'Cloudflare session not found. Use Set-CloudflareSession to create a session.' - } } process { if ($AccountName) { diff --git a/src/pwshCloudflare/Public/Remove-CFZoneRecord.ps1 b/src/pwshCloudflare/Public/Remove-CFZoneRecord.ps1 index 8713e37..9a68df2 100644 --- a/src/pwshCloudflare/Public/Remove-CFZoneRecord.ps1 +++ b/src/pwshCloudflare/Public/Remove-CFZoneRecord.ps1 @@ -31,9 +31,6 @@ function Remove-CFZoneRecord { begin { Write-Verbose "$($MyInvocation.MyCommand.Name) :: BEGIN :: $(Get-Date)" Write-Verbose "ParameterSetName: $($PSCmdlet.ParameterSetName)" - if (-not $script:cfSession) { - throw 'Cloudflare session not found. Use Set-CloudflareSession to create a session.' - } } process { if ($ZoneName) { diff --git a/src/pwshCloudflare/Public/Set-CFZoneRecord.ps1 b/src/pwshCloudflare/Public/Set-CFZoneRecord.ps1 index 370efea..a67f931 100644 --- a/src/pwshCloudflare/Public/Set-CFZoneRecord.ps1 +++ b/src/pwshCloudflare/Public/Set-CFZoneRecord.ps1 @@ -55,9 +55,6 @@ function Set-CFZoneRecord { begin { Write-Verbose "$($MyInvocation.MyCommand.Name) :: BEGIN :: $(Get-Date)" Write-Verbose "ParameterSetName: $($PSCmdlet.ParameterSetName)" - if (-not $script:cfSession) { - throw 'Cloudflare session not found. Use Set-CloudflareSession to create a session.' - } } process { if ($ZoneName) { From c10ef4c50b0e5467cd9acf54898190d3b03ece47 Mon Sep 17 00:00:00 2001 From: connorcarnes Date: Wed, 20 Dec 2023 18:02:38 -0600 Subject: [PATCH 17/25] update README.md --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 2ae32e7..1b1b318 100644 --- a/README.md +++ b/README.md @@ -21,9 +21,14 @@ PowerShell module for interacting with the Cloudflare API. | Function | ApiOperation | | ------------------- | -------------------------------------------------------------------------------------------------------------- | +| Get-CFAccount | [Account Details](https://developers.cloudflare.com/api/operations/accounts-account-details) | +| Get-CFD1Database | [Get D1 Database](https://developers.cloudflare.com/api/operations/cloudflare-d1-get-database) | | Get-CFZone | [List Zones](https://developers.cloudflare.com/api/operations/zones-get) | | Get-CFZoneRecord | [List DNS Records](https://developers.cloudflare.com/api/operations/dns-records-for-a-zone-list-dns-records) | +| Invoke-CFD1Query | [Query D1 Database](https://developers.cloudflare.com/api/operations/cloudflare-d1-query-database) | +| New-CFD1Database | [Create D1 Database](https://developers.cloudflare.com/api/operations/cloudflare-d1-create-database) | | New-CFZoneRecord | [Create DNS Record](https://developers.cloudflare.com/api/operations/dns-records-for-a-zone-create-dns-record) | +| Remove-CFD1Database | [Delete D1 Database](https://developers.cloudflare.com/api/operations/cloudflare-d1-delete-database) | | Remove-CFZoneRecord | [Delete DNS Record](https://developers.cloudflare.com/api/operations/dns-records-for-a-zone-delete-dns-record) | | Set-CFZoneRecord | [Patch DNS Record](https://developers.cloudflare.com/api/operations/dns-records-for-a-zone-patch-dns-record) | From 466a6e6fbd88ebbadaa033a38652ad04294a6ad6 Mon Sep 17 00:00:00 2001 From: connorcarnes Date: Wed, 20 Dec 2023 18:10:04 -0600 Subject: [PATCH 18/25] update changelog and bump psd1 version --- docs/CHANGELOG.md | 6 ++++++ src/pwshCloudflare/pwshCloudflare.psd1 | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 6dd7657..18ea3fe 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -26,3 +26,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Add tests for Remove-CFZoneRecord, Set-CFZoneRecord, Set-CloudflareSession, Test-CloudflareSession - Update priority parameter in Set-CFZoneRecord - Remove Update-TypeData from psm1 + +## [0.2.0] + +- Add D1 functions, types and tests +- Add Get-CFAccount function, types and tests +- Updates to arguement completers diff --git a/src/pwshCloudflare/pwshCloudflare.psd1 b/src/pwshCloudflare/pwshCloudflare.psd1 index e063ff6..ca902ce 100644 --- a/src/pwshCloudflare/pwshCloudflare.psd1 +++ b/src/pwshCloudflare/pwshCloudflare.psd1 @@ -12,7 +12,7 @@ RootModule = 'pwshCloudflare.psm1' # Version number of this module. - ModuleVersion = '0.0.1' + ModuleVersion = '0.2.0' # Supported PSEditions # CompatiblePSEditions = @() From 8a749d430e28fa1b29f1163127d77ce061761faa Mon Sep 17 00:00:00 2001 From: connorcarnes Date: Wed, 20 Dec 2023 18:16:59 -0600 Subject: [PATCH 19/25] update README.md --- README.md | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 1b1b318..f5499ff 100644 --- a/README.md +++ b/README.md @@ -21,16 +21,16 @@ PowerShell module for interacting with the Cloudflare API. | Function | ApiOperation | | ------------------- | -------------------------------------------------------------------------------------------------------------- | -| Get-CFAccount | [Account Details](https://developers.cloudflare.com/api/operations/accounts-account-details) | -| Get-CFD1Database | [Get D1 Database](https://developers.cloudflare.com/api/operations/cloudflare-d1-get-database) | -| Get-CFZone | [List Zones](https://developers.cloudflare.com/api/operations/zones-get) | -| Get-CFZoneRecord | [List DNS Records](https://developers.cloudflare.com/api/operations/dns-records-for-a-zone-list-dns-records) | -| Invoke-CFD1Query | [Query D1 Database](https://developers.cloudflare.com/api/operations/cloudflare-d1-query-database) | -| New-CFD1Database | [Create D1 Database](https://developers.cloudflare.com/api/operations/cloudflare-d1-create-database) | -| New-CFZoneRecord | [Create DNS Record](https://developers.cloudflare.com/api/operations/dns-records-for-a-zone-create-dns-record) | -| Remove-CFD1Database | [Delete D1 Database](https://developers.cloudflare.com/api/operations/cloudflare-d1-delete-database) | -| Remove-CFZoneRecord | [Delete DNS Record](https://developers.cloudflare.com/api/operations/dns-records-for-a-zone-delete-dns-record) | -| Set-CFZoneRecord | [Patch DNS Record](https://developers.cloudflare.com/api/operations/dns-records-for-a-zone-patch-dns-record) | +| [Get-CFAccount](docs/Get-CFAccount.md) | [Account Details](https://developers.cloudflare.com/api/operations/accounts-account-details) | +| [Get-CFD1Database](docs/Get-CFD1Database.md) | [Get D1 Database](https://developers.cloudflare.com/api/operations/cloudflare-d1-get-database) | +| [Get-CFZone](docs/Get-CFZone.md) | [List Zones](https://developers.cloudflare.com/api/operations/zones-get) | +| [Get-CFZoneRecord](docs/Get-CFZoneRecord.md) | [List DNS Records](https://developers.cloudflare.com/api/operations/dns-records-for-a-zone-list-dns-records) | +| [Invoke-CFD1Query](docs/Invoke-CFD1Query.md) | [Query D1 Database](https://developers.cloudflare.com/api/operations/cloudflare-d1-query-database) | +| [New-CFD1Database](docs/New-CFD1Database.md) | [Create D1 Database](https://developers.cloudflare.com/api/operations/cloudflare-d1-create-database) | +| [New-CFZoneRecord](docs/New-CFZoneRecord.md) | [Create DNS Record](https://developers.cloudflare.com/api/operations/dns-records-for-a-zone-create-dns-record) | +| [Remove-CFD1Database](docs/New-CFD1Database.md) | [Delete D1 Database](https://developers.cloudflare.com/api/operations/cloudflare-d1-delete-database) | +| [Remove-CFZoneRecord](docs/Remove-CFZoneRecord.md) | [Delete DNS Record](https://developers.cloudflare.com/api/operations/dns-records-for-a-zone-delete-dns-record) | +| [Set-CFZoneRecord](docs/Set-CFZoneRecord.md) | [Patch DNS Record](https://developers.cloudflare.com/api/operations/dns-records-for-a-zone-patch-dns-record) | ## Why From 9efc526cc13a34a363ac66ede34e3a7115a53d29 Mon Sep 17 00:00:00 2001 From: connorcarnes Date: Wed, 20 Dec 2023 18:18:55 -0600 Subject: [PATCH 20/25] update pwshcloudflare.md --- docs/pwshCloudflare.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/pwshCloudflare.md b/docs/pwshCloudflare.md index 9e06883..edce256 100644 --- a/docs/pwshCloudflare.md +++ b/docs/pwshCloudflare.md @@ -2,7 +2,7 @@ Module Name: pwshCloudflare Module Guid: 3d4f9301-da84-4e63-9249-493417c5589c Download Help Link: NA -Help Version: 0.0.1 +Help Version: 0.2.0 Locale: en-US --- From 378e5594b0d6994b185a0795a56f8852be6f0546 Mon Sep 17 00:00:00 2001 From: connorcarnes Date: Wed, 20 Dec 2023 19:35:29 -0600 Subject: [PATCH 21/25] update New-CFD1Database.Tests.ps1 --- src/Tests/Unit/Public/New-CFD1Database.Tests.ps1 | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Tests/Unit/Public/New-CFD1Database.Tests.ps1 b/src/Tests/Unit/Public/New-CFD1Database.Tests.ps1 index 0e53b28..b9bac1d 100644 --- a/src/Tests/Unit/Public/New-CFD1Database.Tests.ps1 +++ b/src/Tests/Unit/Public/New-CFD1Database.Tests.ps1 @@ -19,7 +19,6 @@ InModuleScope 'pwshCloudflare' { # Mock the dependent cmdlets and variables $script:cfBaseApiUrl = 'https://api.cloudflare.com/client/v4' Mock Invoke-CFRestMethod { return [PSCustomObject]@{ result = [PSCustomObject]@{ DatabaseName = 'myDb' } } } - Mock Find-CFD1Database { return [PSCustomObject]@{ result = [PSCustomObject]@{ DatabaseName = 'myDb' } } } } Context 'Error' { It 'should throw an error if required parameters are missing' { From 6b22c360f4cc2e94c53a9af858638160759a89fe Mon Sep 17 00:00:00 2001 From: connorcarnes Date: Wed, 20 Dec 2023 19:39:35 -0600 Subject: [PATCH 22/25] update New-CFD1Database.Tests.ps1 --- src/Tests/Unit/Public/New-CFD1Database.Tests.ps1 | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Tests/Unit/Public/New-CFD1Database.Tests.ps1 b/src/Tests/Unit/Public/New-CFD1Database.Tests.ps1 index b9bac1d..2f488dc 100644 --- a/src/Tests/Unit/Public/New-CFD1Database.Tests.ps1 +++ b/src/Tests/Unit/Public/New-CFD1Database.Tests.ps1 @@ -18,6 +18,7 @@ InModuleScope 'pwshCloudflare' { $ErrorActionPreference = 'SilentlyContinue' # Mock the dependent cmdlets and variables $script:cfBaseApiUrl = 'https://api.cloudflare.com/client/v4' + $script:cfSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession Mock Invoke-CFRestMethod { return [PSCustomObject]@{ result = [PSCustomObject]@{ DatabaseName = 'myDb' } } } } Context 'Error' { From f84a32c6938210d438d6c8f3ef1e7a079031e40a Mon Sep 17 00:00:00 2001 From: connorcarnes Date: Wed, 20 Dec 2023 19:40:15 -0600 Subject: [PATCH 23/25] update New-CFD1Database.Tests.ps1 --- src/Tests/Unit/Public/New-CFD1Database.Tests.ps1 | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Tests/Unit/Public/New-CFD1Database.Tests.ps1 b/src/Tests/Unit/Public/New-CFD1Database.Tests.ps1 index 2f488dc..c0708fe 100644 --- a/src/Tests/Unit/Public/New-CFD1Database.Tests.ps1 +++ b/src/Tests/Unit/Public/New-CFD1Database.Tests.ps1 @@ -19,6 +19,7 @@ InModuleScope 'pwshCloudflare' { # Mock the dependent cmdlets and variables $script:cfBaseApiUrl = 'https://api.cloudflare.com/client/v4' $script:cfSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession + $script:cfAccountLookupTable = @{'myAccount' = '12345' } Mock Invoke-CFRestMethod { return [PSCustomObject]@{ result = [PSCustomObject]@{ DatabaseName = 'myDb' } } } } Context 'Error' { From 19737a28ceb80aab0c657b0b8b6da4e44dd49390 Mon Sep 17 00:00:00 2001 From: connorcarnes Date: Wed, 20 Dec 2023 19:42:22 -0600 Subject: [PATCH 24/25] Update Get-CFD1Database.Tests.ps1 --- src/Tests/Unit/Public/Get-CFD1Database.Tests.ps1 | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Tests/Unit/Public/Get-CFD1Database.Tests.ps1 b/src/Tests/Unit/Public/Get-CFD1Database.Tests.ps1 index 38bfecc..dd8dcfb 100644 --- a/src/Tests/Unit/Public/Get-CFD1Database.Tests.ps1 +++ b/src/Tests/Unit/Public/Get-CFD1Database.Tests.ps1 @@ -19,6 +19,7 @@ InModuleScope 'pwshCloudflare' { # Mock the dependent cmdlets and variables $script:cfBaseApiUrl = 'https://api.cloudflare.com/client/v4' $script:cfSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession + $script:cfAccountLookupTable = @{'myAccount' = '12345' } Mock Invoke-CFRestMethod { return [PSCustomObject]@{ result = [PSCustomObject]@{ DatabaseName = 'myDb' } } } Mock Find-CFD1Database { return [PSCustomObject]@{ result = [PSCustomObject]@{ DatabaseName = 'myDb' } } } } From 0869b7e88f7d4e8350c7996b9147b1498a8370c9 Mon Sep 17 00:00:00 2001 From: connorcarnes Date: Wed, 20 Dec 2023 19:45:24 -0600 Subject: [PATCH 25/25] Update Get-CFAccount.Tests.ps1 --- src/Tests/Unit/Public/Get-CFAccount.Tests.ps1 | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Tests/Unit/Public/Get-CFAccount.Tests.ps1 b/src/Tests/Unit/Public/Get-CFAccount.Tests.ps1 index 74223cc..ad5203a 100644 --- a/src/Tests/Unit/Public/Get-CFAccount.Tests.ps1 +++ b/src/Tests/Unit/Public/Get-CFAccount.Tests.ps1 @@ -18,6 +18,8 @@ InModuleScope 'pwshCloudflare' { $ErrorActionPreference = 'SilentlyContinue' # Mock the dependent cmdlets and variables $script:cfBaseApiUrl = 'https://api.cloudflare.com/client/v4' + $script:cfAccountLookupTable = @{'myAccount' = '12345' } + $script:cfSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession Mock Invoke-CFRestMethod { return [PSCustomObject]@{ result = [PSCustomObject]@{ AccountName = 'myAcct' } } } } # Context 'Error' {