PSDigitalOcean.psm1
#Region '.\Classes\1.class1.ps1' -1 class Class1 { [string]$Name = 'Class1' Class1() { #default Constructor } [String] ToString() { # Typo "calss" is intentional return ( 'This calss is {0}' -f $this.Name) } } #EndRegion '.\Classes\1.class1.ps1' 16 #Region '.\Classes\2.class2.ps1' -1 class Class2 { [string]$Name = 'Class2' Class2() { #default constructor } [String] ToString() { return ( 'This calss is {0}' -f $this.Name) } } #EndRegion '.\Classes\2.class2.ps1' 15 #Region '.\Classes\3.class11.ps1' -1 class Class11 : Class1 { [string]$Name = 'Class11' Class11 () { } [String] ToString() { return ( 'This calss is {0}:{1}' -f $this.Name,'class1') } } #EndRegion '.\Classes\3.class11.ps1' 14 #Region '.\Classes\4.class12.ps1' -1 class Class12 : Class1 { [string]$Name = 'Class12' Class12 () { } [String] ToString() { return ( 'This calss is {0}:{1}' -f $this.Name,'class1') } } #EndRegion '.\Classes\4.class12.ps1' 14 #Region '.\Classes\DigitalOceanAccount.ps1' -1 class Team { [string]$uuid [string]$name Team([string]$uuid, [string]$name) { $this.uuid = $uuid $this.name = $name } } class Account { [int]$droplet_limit [int]$floating_ip_limit [string]$email [string]$name [string]$uuid [bool]$email_verified [string]$status [string]$status_message [Team]$team Account( [int]$droplet_limit, [int]$floating_ip_limit, [string]$email, [string]$name, [string]$uuid, [bool]$email_verified, [string]$status, [string]$status_message, [Team]$team ) { $this.droplet_limit = $droplet_limit $this.floating_ip_limit = $floating_ip_limit $this.email = $email $this.name = $name $this.uuid = $uuid $this.email_verified = $email_verified $this.status = $status $this.status_message = $status_message $this.team = $team } } class Root { [Account]$account Root([Account]$account) { $this.account = $account } } #EndRegion '.\Classes\DigitalOceanAccount.ps1' 58 #Region '.\Private\Get-DigitalOceanAPIAuthorizationBearerToken.ps1' -1 function Get-DigitalOceanAPIAuthorizationBearerToken { <# .SYNOPSIS Get-DigitalOceanAPIAuthorizationBearerToken .DESCRIPTION Get Digital Ocean API Authorization Bearer Token. .EXAMPLE Get-DigitalOceanAPIAuthorizationBearerToken .OUTPUTS [System.String] #> [CmdletBinding()] [OutputType([System.String])] param() begin { #Content } process { [Environment]::GetEnvironmentVariable("DIGITALOCEAN_TOKEN", [System.EnvironmentVariableTarget]::User) } end { #Content } } #EndRegion '.\Private\Get-DigitalOceanAPIAuthorizationBearerToken.ps1' 39 #Region '.\Private\Invoke-DigitalOceanAPI.ps1' -1 function Invoke-DigitalOceanAPI { <# .SYNOPSIS This private function invokes the DigitalOcean API. .DESCRIPTION This function is used to make API calls to DigitalOcean. It requires an API token to authenticate the request. .PARAMETER APIPath The API path to call, such as 'account', 'droplets', 'tags', etc. .PARAMETER APIVersion The API version to use, currently defaults to 'v2'. If you need to use a different version, you can specify it here. .PARAMETER Method The HTTP method to use. Valid values are 'GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE', and 'PATCH'. Defaults to 'GET'. .PARAMETER Parameters The query parameters to include in the request. This should be a hashtable where the keys are parameter names and the values are parameter values. .EXAMPLE Invoke-DigitalOceanAPI -APIPath 'account' -APIVersion 'v2' -Method 'GET' -Parameters @{ page = 1; per_page = 10 } This example retrieves the account information from the DigitalOcean API. #> [CmdletBinding()] param ( [Parameter(Mandatory = $true)] [String] $APIPath, [Parameter()] [String] $APIVersion = 'v2', [Parameter()] [ValidateSet('GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE', 'PATCH')] $Method = 'GET', [Parameter()] [hashtable] $Parameters ) $Token = Get-DigitalOceanAPIAuthorizationBearerToken if ([string]::IsNullOrEmpty($Token)) { throw "DigitalOcean API token is not set. Please set the DIGITALOCEAN_TOKEN environment variable." } $Headers = @{ "Content-Type" = "application/json" "Authorization" = "Bearer $($Token)" } if ($null -ne $Parameters -and $Parameters.Count -gt 0) { $query = ($Parameters.GetEnumerator() | ForEach-Object { "$([uri]::EscapeDataString($_.Key))=$([uri]::EscapeDataString($_.Value.ToString()))" }) -join "&" } if (-not [string]::IsNullOrEmpty($query)) { $URI = "https://api.digitalocean.com/$($APIVersion)/$($APIPath)?$($query)" } else { $URI = "https://api.digitalocean.com/$($APIVersion)/$($APIPath)" } Write-Verbose "about to run $($URI)" $Response = Invoke-RestMethod -Method $Method -Headers $Headers -Uri $URI $Response } #EndRegion '.\Private\Invoke-DigitalOceanAPI.ps1' 76 #Region '.\Public\Get-DigitalOceanAccount.ps1' -1 function Get-DigitalOceanAccount { <# .SYNOPSIS Get-DigitalOceanAccount .DESCRIPTION Retrieves Digital Ocean account information including account details, limits, and verification status. Supports pagination to retrieve multiple accounts or use the -All parameter to get all accounts at once. .PARAMETER Page Which 'page' of paginated results to return. .PARAMETER Limit Number of items returned per page. .PARAMETER All If you want to get all the images and not the Page / Limit. .EXAMPLE Get-DigitalOceanAccount -Page 1 -Limit 20 .EXAMPLE Get-DigitalOceanAccount -All .LINK https://docs.digitalocean.com/reference/api/digitalocean/#tag/Account .OUTPUTS DigitalOcean.Account #> [CmdletBinding(DefaultParameterSetName = "Limit")] [OutputType('DigitalOcean.Account')] param ( [int] [ValidateRange(1, 1000)] [Parameter(ParameterSetName = "Limit")] $Page = 1, [int] [ValidateRange(20, 200)] [Parameter(ParameterSetName = "Limit")] $Limit = 20, [Switch] [Parameter(ParameterSetName = "All")] $All ) if ($All.IsPresent) { $Parameters = @{ page = 1 per_page = 20 } $AllArray = @() Write-Verbose "about to get all account from DigitalOcean" Write-Verbose "Page: 1, PerPage: 20" $response = Invoke-DigitalOceanAPI -APIPath account -Parameters $Parameters $Total = $response.meta.total Write-Verbose "DigitalOcean total account is $($Total)" $AllArray = $response.account do { # Check if there's a next page URL if ($response.links.pages.PSObject.Properties.Name -contains 'next' -and $null -ne $response.links.pages.next -and $response.links.pages.next -ne "") { $Split = $response.links.pages.next.Split('?')[1] if ($Split -match 'page=(\d+)&per_page=(\d+)') { $Parameters = @{ page = $matches[1] per_page = $matches[2] } Write-Verbose "Page: $($matches[1]), PerPage: $($matches[2])" Write-Verbose "the next url is $($response.links.pages.next)" $response = Invoke-DigitalOceanAPI -APIPath account -Parameters $Parameters $AllArray += $response.account } else { # URL doesn't match pattern, break the loop break } } else { # No next page, break the loop break } } while ($AllArray.Count -lt $Total) Write-Verbose "finished getting all sizes" # Convert API response objects to PowerShell class objects $ConvertedArray = @() foreach ($obj in $AllArray) { # Create Team object if team data exists $teamObject = $null if ($obj.team) { $teamObject = [Team]::new( $(if ($null -ne $obj.team.uuid) { $obj.team.uuid } else { "" }), $(if ($null -ne $obj.team.name) { $obj.team.name } else { "" }) ) } # Create Account object with all properties, providing defaults for missing values $accountObject = [Account]::new( $(if ($null -ne $obj.droplet_limit) { $obj.droplet_limit } else { 0 }), $(if ($null -ne $obj.floating_ip_limit) { $obj.floating_ip_limit } else { 0 }), $(if ($null -ne $obj.email) { $obj.email } else { "" }), $(if ($null -ne $obj.name) { $obj.name } else { "" }), $(if ($null -ne $obj.uuid) { $obj.uuid } else { "" }), $(if ($null -ne $obj.email_verified) { $obj.email_verified } else { $false }), $(if ($null -ne $obj.status) { $obj.status } else { "" }), $(if ($null -ne $obj.status_message) { $obj.status_message } else { "" }), $teamObject ) $ConvertedArray += $accountObject } $ConvertedArray } else { $Parameters = @{ page = $Page per_page = $Limit } $AllArray = @() $response = Invoke-DigitalOceanAPI -APIPath account -Parameters $Parameters $AllArray = $response.account # Convert API response objects to PowerShell class objects $ConvertedArray = @() foreach ($obj in $AllArray) { # Create Team object if team data exists $teamObject = $null if ($obj.team) { $teamObject = [Team]::new( $(if ($null -ne $obj.team.uuid) { $obj.team.uuid } else { "" }), $(if ($null -ne $obj.team.name) { $obj.team.name } else { "" }) ) } # Create Account object with all properties, providing defaults for missing values $accountObject = [Account]::new( $(if ($null -ne $obj.droplet_limit) { $obj.droplet_limit } else { 0 }), $(if ($null -ne $obj.floating_ip_limit) { $obj.floating_ip_limit } else { 0 }), $(if ($null -ne $obj.email) { $obj.email } else { "" }), $(if ($null -ne $obj.name) { $obj.name } else { "" }), $(if ($null -ne $obj.uuid) { $obj.uuid } else { "" }), $(if ($null -ne $obj.email_verified) { $obj.email_verified } else { $false }), $(if ($null -ne $obj.status) { $obj.status } else { "" }), $(if ($null -ne $obj.status_message) { $obj.status_message } else { "" }), $teamObject ) $ConvertedArray += $accountObject } $ConvertedArray } } #EndRegion '.\Public\Get-DigitalOceanAccount.ps1' 181 |