|
| 1 | +#SkipTest:FunctionTest:Resolves the cloud ID from the public tenant_info endpoint; covered by integration tests. |
| 2 | +function ConvertTo-ConfluenceCloudId { |
| 3 | + <# |
| 4 | + .SYNOPSIS |
| 5 | + Resolve a Confluence Cloud site to its cloud ID. |
| 6 | +
|
| 7 | + .DESCRIPTION |
| 8 | + Looks up the cloud ID for an Atlassian Confluence Cloud site from the |
| 9 | + public `/_edge/tenant_info` endpoint, so a caller can supply a site name or |
| 10 | + URL instead of the cloud ID. Accepts a bare subdomain (`myorg`), a host |
| 11 | + (`myorg.atlassian.net`), or any URL on the site |
| 12 | + (`https://myorg.atlassian.net/wiki/...`). No authentication is required. |
| 13 | +
|
| 14 | + .EXAMPLE |
| 15 | + ```powershell |
| 16 | + ConvertTo-ConfluenceCloudId -Site 'msxorg' |
| 17 | + ``` |
| 18 | +
|
| 19 | + Returns the cloud ID for `https://msxorg.atlassian.net`. |
| 20 | +
|
| 21 | + .EXAMPLE |
| 22 | + ```powershell |
| 23 | + 'https://msxorg.atlassian.net/wiki/spaces/DOCS' | ConvertTo-ConfluenceCloudId |
| 24 | + ``` |
| 25 | +
|
| 26 | + Resolves the cloud ID from a full site URL supplied through the pipeline. |
| 27 | +
|
| 28 | + .EXAMPLE |
| 29 | + ```powershell |
| 30 | + $cloudId = ConvertTo-ConfluenceCloudId -Site 'msxorg' |
| 31 | + Connect-Confluence -ApiBaseUri "https://api.atlassian.com/ex/confluence/$cloudId" -Username $user -Token $token |
| 32 | + ``` |
| 33 | +
|
| 34 | + Uses the resolved cloud ID to build the API-gateway base URI for `Connect-Confluence`, |
| 35 | + so the caller only needs the site name. |
| 36 | +
|
| 37 | + .OUTPUTS |
| 38 | + System.String |
| 39 | +
|
| 40 | + .LINK |
| 41 | + https://psmodule.io/Confluence/Functions/Site/ConvertTo-ConfluenceCloudId/ |
| 42 | +
|
| 43 | + .LINK |
| 44 | + https://developer.atlassian.com/cloud/confluence/rest/v2/intro/#about |
| 45 | + #> |
| 46 | + [CmdletBinding()] |
| 47 | + [OutputType([string])] |
| 48 | + param( |
| 49 | + # The site to resolve: a subdomain (`myorg`), a host (`myorg.atlassian.net`), |
| 50 | + # or any URL on the site (`https://myorg.atlassian.net/...`). |
| 51 | + [Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)] |
| 52 | + [Alias('Url', 'Uri', 'Name')] |
| 53 | + [string]$Site |
| 54 | + ) |
| 55 | + |
| 56 | + process { |
| 57 | + $trimmed = $Site.Trim() |
| 58 | + if ([string]::IsNullOrWhiteSpace($trimmed)) { |
| 59 | + throw 'The -Site value cannot be empty.' |
| 60 | + } |
| 61 | + |
| 62 | + # Derive the host whether the caller passed a bare subdomain, a host, or a URL. |
| 63 | + $siteHost = if ($trimmed -match '^[A-Za-z][A-Za-z0-9+.-]*://') { |
| 64 | + ([uri]$trimmed).Host |
| 65 | + } elseif ($trimmed -match '[/.]') { |
| 66 | + ([uri]"https://$trimmed").Host |
| 67 | + } else { |
| 68 | + "$trimmed.atlassian.net" |
| 69 | + } |
| 70 | + |
| 71 | + if ([string]::IsNullOrWhiteSpace($siteHost)) { |
| 72 | + throw "Could not determine the site host from '$Site'." |
| 73 | + } |
| 74 | + |
| 75 | + $tenantInfoUri = "https://$siteHost/_edge/tenant_info" |
| 76 | + Write-Verbose "Resolving cloud ID from [$tenantInfoUri]." |
| 77 | + |
| 78 | + try { |
| 79 | + $tenantInfo = Invoke-RestMethod -Uri $tenantInfoUri -Method Get -ErrorAction Stop |
| 80 | + } catch { |
| 81 | + throw "Failed to resolve the cloud ID for '$siteHost' from [$tenantInfoUri]: $($_.Exception.Message)" |
| 82 | + } |
| 83 | + |
| 84 | + if ([string]::IsNullOrWhiteSpace($tenantInfo.cloudId)) { |
| 85 | + throw "The endpoint [$tenantInfoUri] did not return a cloud ID for '$siteHost'." |
| 86 | + } |
| 87 | + |
| 88 | + $tenantInfo.cloudId |
| 89 | + } |
| 90 | +} |
0 commit comments