Functions/Get-IAConfigurationDataModel.ps1

Function Get-IAConfigurationDataModel{
    <#
        .SYNOPSIS
            Returns a Data Model for Widget Configuration.
        .DESCRIPTION
            Configuration Data Models are used to set-up the Widget. They contain all of the different selections and variables nessessary for a Widget.
            A Configuration Data Model is just like the Data Source that you use in the IA website to configure a widget.
        .OUTPUTS
            The Configuraiton Data Model as a PSObject.
        .EXAMPLE
            Get-IAConfigurationDataModel -Interactive
    #>

    [CmdletBinding(DefaultParameterSetName='Name')]
    Param(
        [Parameter(ParameterSetName = 'Interactive')]
        [Switch]$Interactive,
        [Parameter(ParameterSetName = 'WidgetTemplateId')]
        [Guid]$WidgetTemplateId
    )
    DynamicParam
    {
        try{
            # Name
            $Bucket = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameterDictionary
            $AttributeList = New-Object -TypeName System.Collections.ObjectModel.Collection[System.Attribute]
            
            # Find which connectors are available
            Try{
                $connectorsAvailable = @{}
                $connectorsEnabled = Get-IAConnector -All -ExpandSpecific 'ConnectorType' -Filter 'Enabled eq true'

                foreach($con in $connectorsEnabled){
                    $connectorType = $con.ConnectorType
                    $assemblyName = $connectorType.AssemblyName
                    $NamespaceName = $assemblyName.Split(".")[-1]

                    if(![String]::IsNullOrEmpty($connectorType.ServiceRoute)){
                        $properties = @{
                            "ServiceRoute" = $connectorType.ServiceRoute
                            "NamespaceName" = $NamespaceName
                            "ConnectorTypeId" = $connectorType.Id
                        }

                        $connectorsAvailable.Add($con.Name,$properties)
                    }
                }
            }
            Catch{
                $CurrentError = $_
                throw [System.ArgumentException]::New('Failed to find the connector query urls!', $currentError.Exception) 
            }

            foreach($item in $connectorsAvailable.Keys){
               $Values += (([XML](Invoke-IAQuery -QueryUrl "$($connectorsAvailable.Item($item).ServiceRoute)/`$metadata" -Method Get)).edmx.DataServices.schema | 
                        Where-Object -Property Namespace -EQ "IA_Backend.Entities.DataDefinitions.$($connectorsAvailable.Item($item).NamespaceName)").EntityType.Name
            }

            $AttribValidateSet = New-Object System.Management.Automation.ValidateSetAttribute($Values)
            $AttributeList.Add($AttribValidateSet)
    
            $AttribParameter = New-Object System.Management.Automation.ParameterAttribute
            $AttribParameter.Mandatory = $true
            $AttribParameter.ParameterSetName = 'Name'
            $AttribParameter.Position = 0
            $AttributeList.Add($AttribParameter)
            $ParameterName = 'Name'
            $Parameter = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameter($ParameterName, [String], $AttributeList)
            $Bucket.Add($ParameterName, $Parameter)
            #Name
        }
        catch [System.Net.WebException] {
            $CurrentError = $_
            throw [System.Net.WebException]::New('Make sure TLS1.2 is allowed!', $currentError.Exception)
        }
        catch {
            $CurrentError = $_

            if($CurrentError.Exception.Message -like '*https:///api*'){
                throw [System.ArgumentException]::New('Make sure that you are connected to the environment!', $currentError.Exception)
            }
        }
    }
    End
    {
        Foreach ($key in $PSBoundParameters.Keys)
        {
            if ($MyInvocation.MyCommand.Parameters.$key.isDynamic)
            {
                Set-Variable -Name $key -Value $PSBoundParameters.$key
            }
        }

        foreach($item in $connectorsAvailable.Keys){
            $Values = $null
            if($WidgetTemplateName){
                $Values = (Get-IAWidgetTemplate -All -ExpandSpecific 'ConnectorModuleType') | 
                    Where-Object {$_.ConnectorModuleType.ConnectorTypeId -eq $($connectorsAvailable.Item($item).ConnectorTypeId)} |
                    Select-Object -ExpandProperty Name
            }
            elseif($WidgetTemplateId){
                
                $Values = (Get-IAWidgetTemplate -All -ExpandSpecific 'ConnectorModuleType') | 
                    Where-Object {$_.ConnectorModuleType.ConnectorTypeId -eq $($connectorsAvailable.Item($item).ConnectorTypeId) -and $_.Id -eq $WidgetTemplateId} |
                    Select-Object -ExpandProperty Id
            }

            if($Values -ne $null){
                if((($Name -ne $null-and $Values.Contains($Name)) -or ($Values.Contains($WidgetTemplateId)))){
                    break;
                }
            }
        }

        [XML]$XML = Invoke-IAQuery -QueryUrl "$($connectorsAvailable.Item($item).ServiceRoute)/`$metadata" -Method Get
        $XMLBase = $XML.edmx.DataServices.schema

        $ConfigurationDataModels = $XMLBase | 
            Where-Object -Property Namespace -EQ "IA_Backend.Entities.DataDefinitions.$($connectorsAvailable.Item($item).NamespaceName)" | 
            Select-Object -ExpandProperty EntityType

        if($Interactive){
            $Name = $ConfigurationDataModels | 
                Select-Object -ExpandProperty Name | 
                Out-GridView -PassThru
        }
        elseif($WidgetTemplateId){

            $WidgetTemplate = Get-IAWidgetTemplate -Filter "Id eq $WidgetTemplateId"
            $ConnectorModule = Get-IAConnectorModule -All -Filter "ConnectorModuleTypeId eq $($WidgetTemplate.ConnectorModuleTypeId)"

            if($LicenseId) { $ConnectorModule = $ConnectorModule | Where-Object -Property ConnectorId -eq $LicenseId }
            if($ConnectorModule.Count -gt 1) { $ConnectorModule = $ConnectorModule | Select-Object -First 1 }

            $Name = $ConnectorModule.Name + 'Configuration'
        }

        $DataModel = $ConfigurationDataModels | Where-Object -Property Name -Eq $Name
        $CompleteDataModel = New-Object -TypeName psobject

        foreach($property in $DataModel.Property){
            $CompleteDataModel | Add-Member -MemberType NoteProperty -Name $property.Name -Value ""
        }

        $ReturnObject = New-Object -TypeName PSObject
        $ReturnObject | Add-Member -MemberType NoteProperty -Name 'Name' -Value $Name
        $ReturnObject | Add-Member -MemberType NoteProperty -Name 'DataModel' -Value $CompleteDataModel

        return $ReturnObject
    }
}