Private/Types.psm1

<#
SAMPLE CODE NOTICE
 
THIS SAMPLE CODE IS MADE AVAILABLE AS IS. MICROSOFT MAKES NO WARRANTIES, WHETHER EXPRESS OR IMPLIED,
OF FITNESS FOR A PARTICULAR PURPOSE, OF ACCURACY OR COMPLETENESS OF RESPONSES, OF RESULTS, OR CONDITIONS OF MERCHANTABILITY.
THE ENTIRE RISK OF THE USE OR THE RESULTS FROM THE USE OF THIS SAMPLE CODE REMAINS WITH THE USER.
NO TECHNICAL SUPPORT IS PROVIDED. YOU MAY NOT DISTRIBUTE THIS CODE UNLESS YOU HAVE A LICENSE AGREEMENT WITH MICROSOFT THAT ALLOWS YOU TO DO SO.
#>


class VnetInformation{
    [string] $VnetId
    [string] $SubnetName
}

class NetworkUsage{
    [string] $AzureRegion
    [string] $EnvironmentId
    [string] $VnetId
    [string] $SubnetName
    [string] $SubnetIpRange
    [string] $ContainerReservedIpCount
    [string[]] $DnsServers
}

enum PolicyType{
    Encryption
    NetworkInjection
    Identity
}

enum BAPEndpoint{
    unknown # Used for defaulting to this value, not meant to be used.
    tip1
    tip2
    prod
    usgovhigh
    dod
    china
}

enum LinkOperation{
    link
    unlink
}

# Define the types to export with type accelerators.
$ExportableTypes = @(
    [VnetInformation]
    [PolicyType]
    [BAPEndpoint]
    [LinkOperation]
    [NetworkUsage]
)

# Get the internal TypeAccelerators class to use its static methods.
$TypeAcceleratorsClass = [psobject].Assembly.GetType(
    'System.Management.Automation.TypeAccelerators'
)

# Ensure none of the types would clobber an existing type accelerator.
# If a type accelerator with the same name exists, throw an exception.
$ExistingTypeAccelerators = $TypeAcceleratorsClass::Get

foreach ($Type in $ExportableTypes) {
    if($Type.FullName -in $global:ImportedTypes) {
        continue
    }
    if ($Type.FullName -in $ExistingTypeAccelerators.Keys) {
        $Message = @(
            "Unable to register type accelerator '$($Type.FullName)'"
            'Accelerator already exists.'
        ) -join ' - '

        throw [System.Management.Automation.ErrorRecord]::new(
            [System.InvalidOperationException]::new($Message),
            'TypeAcceleratorAlreadyExists',
            [System.Management.Automation.ErrorCategory]::InvalidOperation,
            $Type.FullName
        )
    }
}
# Add type accelerators for every exportable type.
foreach ($Type in $ExportableTypes) {
    if($Type.FullName -in $global:ImportedTypes) {
        continue
    }
    $TypeAcceleratorsClass::Add($Type.FullName, $Type)
    [string[]]$global:ImportedTypes += $Type
}

# Remove type accelerators when the module is removed.
$MyInvocation.MyCommand.ScriptBlock.Module.OnRemove = {
    foreach($Type in $ExportableTypes) {
        $TypeAcceleratorsClass::Remove($Type.FullName)
    }
}.GetNewClosure()