Public/Connect-PCOci.ps1

function Connect-PCOci {
    <#
    .SYNOPSIS
        Resolves an identity and reads the tenancy, its home region and its subscribed regions.

    .DESCRIPTION
        The identity is resolved in the order of decision D-008 and never prompted for: values passed here;
        the PowerCraft.Secrets entry 'oci'; a profile in the OCI config file; the OCI_CLI_* variables. Two
        network calls are made, both reads. The context returned names the identity's source, user and key
        file path, never the key or a pass phrase, and is kept as the default for the other verbs.

    .PARAMETER ProfileName
        A profile in the OCI config file. Naming it skips the secret store.

    .PARAMETER ConfigFile
        The OCI config file. Default: ~/.oci/config.

    .PARAMETER Tenancy
        With -User, -Fingerprint, -KeyFile and -Region: the five values passed directly.

    .PARAMETER PassPhrase
        The key file's pass phrase, as a secure string, when it has one.

    .EXAMPLE
        Connect-PCOci -ProfileName READONLY

    .EXAMPLE
        Connect-PCOci -Tenancy $t -User $u -Fingerprint $f -KeyFile ~/.oci/readonly.pem -Region eu-frankfurt-1
    #>

    [CmdletBinding(DefaultParameterSetName = 'Resolve')]
    [OutputType([PowerCraft.OCI.Core.Identity.OciContext])]
    param(
        [Parameter(ParameterSetName = 'Resolve')][string]$ProfileName,
        [Parameter(ParameterSetName = 'Resolve')][string]$ConfigFile,
        [Parameter(ParameterSetName = 'Values', Mandatory)][string]$Tenancy,
        [Parameter(ParameterSetName = 'Values', Mandatory)][string]$User,
        [Parameter(ParameterSetName = 'Values', Mandatory)][string]$Fingerprint,
        [Parameter(ParameterSetName = 'Values', Mandatory)][string]$KeyFile,
        [Parameter(ParameterSetName = 'Values', Mandatory)][string]$Region,
        [Parameter(ParameterSetName = 'Values')][securestring]$PassPhrase
    )

    $identity = if ($PSCmdlet.ParameterSetName -eq 'Values') {
        $values = @{ tenancy = $Tenancy; user = $User; fingerprint = $Fingerprint; keyFile = $KeyFile; region = $Region }
        if ($PassPhrase) { $values.passPhrase = ConvertTo-PCOciPlainText -Secure $PassPhrase }
        Resolve-PCOciIdentity -Values $values
    }
    else {
        $resolveArgs = @{}
        if ($PSBoundParameters.ContainsKey('ProfileName')) { $resolveArgs.ProfileName = $ProfileName }
        if ($PSBoundParameters.ContainsKey('ConfigFile')) { $resolveArgs.ConfigFile = $ConfigFile }
        Resolve-PCOciIdentity @resolveArgs
    }

    Write-Verbose "Identity from $($identity.Source); key file $($identity.KeyFile)"
    $context = [PowerCraft.OCI.Core.Gateways.OciSession]::Connect($identity)
    $script:PCOciContext = $context
    Write-Verbose "Connected to tenancy '$($context.TenancyName)', home region $($context.HomeRegion), $($context.SubscribedRegions.Count) region(s) subscribed"
    $context
}