Public/Metadata.ps1



function Get-HidMetadata {
    <#
    .SYNOPSIS
        Retrieves HelloId Metadata from objects
    .DESCRIPTION
        Get-HidMetadata will the metadata from objects, or all metadata objects
    .PARAMETER Id
        Specifies the Guid of a metadata object
    .PARAMETER ObjectId
        The GUID of the HelloID object associated with this metadata.
    .PARAMETER ObjectType
        The type of the HelloID object associated with this metadata.
    .PARAMETER Key
        A key of up to 128 characters.
    .PARAMETER CompanyName
        The companyname that's used in the helloId URL to know which HelloID tenant to talk to. Required if not connected with Connect-HelloId.
    .PARAMETER ApiKey
        The Apikey to use for the api call. Required if not connected with Connect-HelloId.
    .PARAMETER ApiSecret
        The Apisecret belonging to the apikey, has to be a securestring. Required if not connected with Connect-HelloId.
    .EXAMPLE
        Get-HidDataSource -CompanyName "MyCompany" -ApiKey "myapikey" -ApiSecret (ConvertTo-SecureString -AsPlainText -String "password" -Force)
         
        Returns all datasources
    .EXAMPLE
        Get-HidDataSource
 
        Returns all datasources in the tenant
    .EXAMPLE
        Get-HidDatasource -DataSourceGuid "812302af-c3db-4a66-c39d-08d7d4a10f72"
         
        Returns the datasource with guid 812302af-c3db-4a66-c39d-08d7d4a10f72
    .EXAMPLE
        "AD-group-generate-table" | Get-HidDataSource
 
        Returns the datasource with name AD-group-generate-table. The name can be piped
    .INPUTS
        You can pipe a string that contains the id to Get-HidMetadata
    .OUTPUTS
        Returns an object for each datasource that it gets.
    #>


    [CmdletBinding(DefaultParameterSetName = 'Default',PositionalBinding = $false)]
    [Alias()]
    [OutputType([String])]
    Param (
        # the Id of a metadata object
        <#
        [Parameter(Mandatory = $false,
            ValueFromPipelineByPropertyName = $true,
            ParameterSetName = "Id")]
        [ValidateNotNullOrEmpty()]
        [guid[]]$Id,
        #>


        # The GUID of the HelloID object associated with this metadata.
        [Parameter(Mandatory = $true,
        ValueFromPipeline = $true,
        ValueFromPipelineByPropertyName = $true,
        ParameterSetName = "ObjectId")]
        [ValidateNotNullOrEmpty()]
        [guid]$ObjectId,

        # The type of the HelloID object associated with this metadata.
        [Parameter(Mandatory = $true,
        ValueFromPipeline = $true,
        ValueFromPipelineByPropertyName = $true,
        ParameterSetName = "ObjectType")]
        [ValidateNotNullOrEmpty()]
        [ValidateSet("DelegatedForm","DynamicForm","DataSource","Product")]
        [string]$ObjectType,

        # A key of up to 128 characters.
        [Parameter(Mandatory = $false,
        ValueFromPipeline = $true,
        ValueFromPipelineByPropertyName = $true)]
        [ValidateNotNullOrEmpty()]
        [string]$Key,

        # Company name used in the URL
        [Parameter(Mandatory= $false)]
        [ValidateNotNullOrEmpty()]
        [string]$CompanyName,
        
        # Api key
        [Parameter(Mandatory= $false)]
        [ValidateNotNullOrEmpty()]
        [string]$ApiKey,

        # Api secret
        [Parameter(Mandatory= $false)]
        [ValidateNotNullOrEmpty()]
        [securestring]$ApiSecret,

        # For preview tenants
        [Parameter(Mandatory = $false)]
        [switch]$IsPreviewTenant
    )
    
    begin {

        if ($PSBoundParameters.ContainsKey("CompanyName") -AND $PSBoundParameters.ContainsKey("ApiKey") -AND $PSBoundParameters.ContainsKey("ApiSecret") ){
            Write-Verbose -Message "Using connectioninfo and credentials from parameter"
            #Create credential object for authentication
            $Cred = New-Object System.Management.Automation.PSCredential ($ApiKey, $ApiSecret)
            if ($IsPreviewTenant){
                $BaseUrl = "https://$CompanyName.preview-helloid.com"
            }
            else {
                $BaseUrl = "https://$CompanyName.helloid.com"
            }
        }
        elseif ($Global:HelloIdConnection.ApiCredentials) {
            Write-Verbose -Message "Using Global connectioninfo and credentials from Connect-HelloId "
            $Cred = $Global:HelloIdConnection.ApiCredentials
            $CompanyName = $Global:HelloIdConnection.CompanyName
            $BaseUrl = $Global:HelloIdConnection.BaseUrl
        }
        else {            
            throw "Error finding connectioninfo. Connect using Connect-HelloId, or specifie CompanyName, ApiKey and ApiSecret"
        }

        #Headers
        $headers = @{
            "Content-Type" = "application/json"
        }

        #Variables
        [array]$resultArray = @()
        $skip = 0
        $take = 100
        
    } #End begin
    process {

        do {
            $URI = "$BaseUrl/api/v1/metadata"

            $queryToAppend = "skip=$skip&take=$take"
            $URI = Add-QueryToUri -Uri $URI -Query $queryToAppend

            if ($PSBoundParameters.ContainsKey("ObjectId")){            
                $queryToAppend = "objectId=$ObjectId"
                $URI = Add-QueryToUri -Uri $URI -Query $queryToAppend
            }
            if ($PSBoundParameters.ContainsKey("ObjectType")){            
                $queryToAppend = "ObjectType=$ObjectType"
                $URI = Add-QueryToUri -Uri $URI -Query $queryToAppend
            }
            if ($PSBoundParameters.ContainsKey("Key")){            
                $queryToAppend = "key=$Key"
                $URI = Add-QueryToUri -Uri $URI -Query $queryToAppend
            }
    
            Write-Verbose "Invoking restmethod with uri: $URI"
            $Result = Invoke-RestMethod -Uri $URI -Method "GET" -Headers $headers -Credential $Cred -UseBasicParsing               
            
            $resultArray += $Result

            # Update skip and take with new values
            $skip = $resultArray.Count
            
        } while ( $Result.Count -ge $take )

        
    } #End process
    end {
        return $resultArray
    } #End end
} #End function

function Set-HidMetadata {
    [CmdletBinding(DefaultParameterSetName = 'Name',PositionalBinding = $false)]
    [Alias()]
    [OutputType([String])]
    Param (
        # The GUID of a metadata object. You should only send this property when you're updating existing metadata.
        [Parameter(Mandatory = $false,
            ValueFromPipelineByPropertyName = $true)]
        [ValidateNotNullOrEmpty()]
        [guid]$Id,

        # The GUID of the HelloID object associated with this metadata.
        [Parameter(Mandatory = $true,
        ValueFromPipeline = $true,
        ValueFromPipelineByPropertyName = $true)]
        [ValidateNotNullOrEmpty()]
        [guid]$ObjectId,

        # The type of the HelloID object associated with this metadata.
        [Parameter(Mandatory = $true,
        ValueFromPipeline = $true,
        ValueFromPipelineByPropertyName = $true)]
        [ValidateNotNullOrEmpty()]
        [ValidateSet("DelegatedForm","DynamicForm","DataSource","Product")]
        [string]$ObjectType,

        # A key of up to 128 characters.
        [Parameter(Mandatory = $true,
        ValueFromPipeline = $true,
        ValueFromPipelineByPropertyName = $true)]
        [ValidateNotNullOrEmpty()]
        [string]$Key,

        # A value of up to 255 characters // string or null
        [Parameter(Mandatory = $true,
        ValueFromPipeline = $true,
        ValueFromPipelineByPropertyName = $true)]
        [string]$Value,

        # Company name used in the URL
        [Parameter(Mandatory= $false)]
        [ValidateNotNullOrEmpty()]
        [string]$CompanyName,
        
        # Api key
        [Parameter(Mandatory= $false)]
        [ValidateNotNullOrEmpty()]
        [string]$ApiKey,

        # Api secret
        [Parameter(Mandatory= $false)]
        [ValidateNotNullOrEmpty()]
        [securestring]$ApiSecret,

        # For preview tenants
        [Parameter(Mandatory = $false)]
        [switch]$IsPreviewTenant
    )
    
    begin {

        if ($PSBoundParameters.ContainsKey("CompanyName") -AND $PSBoundParameters.ContainsKey("ApiKey") -AND $PSBoundParameters.ContainsKey("ApiSecret") ){
            Write-Verbose -Message "Using connectioninfo and credentials from parameter"
            #Create credential object for authentication
            $Cred = New-Object System.Management.Automation.PSCredential ($ApiKey, $ApiSecret)
            if ($IsPreviewTenant){
                $BaseUrl = "https://$CompanyName.preview-helloid.com"
            }
            else {
                $BaseUrl = "https://$CompanyName.helloid.com"
            }
        }
        elseif ($Global:HelloIdConnection.ApiCredentials) {
            Write-Verbose -Message "Using Global connectioninfo and credentials from Connect-HelloId "
            $Cred = $Global:HelloIdConnection.ApiCredentials
            $CompanyName = $Global:HelloIdConnection.CompanyName
            $BaseUrl = $Global:HelloIdConnection.BaseUrl
        }
        else {            
            throw "Error finding connectioninfo. Connect using Connect-HelloId, or specifie CompanyName, ApiKey and ApiSecret"
        }

        #Headers
        $headers = @{
            "Content-Type" = "application/json"
        }
        
    } #End begin
    process {        

        $URI = "$BaseUrl/api/v1/metadata"

        $body = @{
            objectId = $ObjectId
            objectType = $ObjectType
            key = $Key
            value = $Value
        }
        if ($PSBoundParameters.ContainsKey("Id")){            
            $body.Add("Id",$Id)
        }
        
        $body = $body | ConvertTo-Json

        $output = Invoke-RestMethod -Uri $URI -Method "POST" -Headers $headers -Body $body -Credential $Cred -UseBasicParsing
        $output
    } #End process
    end {        
    } #End end
} #End function

function Remove-HidMetadata {
    [CmdletBinding(DefaultParameterSetName = 'MetadataGuid',PositionalBinding = $false)]
    [Alias()]
    [OutputType([String])]
    Param (
        # A HelloID metadata object GUID.
        [Parameter(Mandatory = $true,
            ValueFromPipelineByPropertyName = $true,
            ParameterSetName = "MetadataGuid")]
        [Alias("MetadataGuid")]
        [ValidateNotNullOrEmpty()]
        [guid[]]$Id,

        # The type of the HelloID object associated with a metadata object.
        [Parameter(Mandatory = $true,
        ValueFromPipeline = $false,
        ValueFromPipelineByPropertyName = $false,
        ParameterSetName = "ObjectType")]
        [ValidateNotNullOrEmpty()]
        [ValidateSet("DelegatedForm","DynamicForm","DataSource","Product")]
        [string]$ObjectType,

        # The GUID of a HelloID object associated with a metadata object.
        [Parameter(Mandatory = $true,
        ValueFromPipeline = $false,
        ValueFromPipelineByPropertyName = $false,
        ParameterSetName = "ObjectType")]
        [ValidateNotNullOrEmpty()]
        [guid]$ObjectId,

        # The key of a helloID metadata object
        [Parameter(Mandatory = $false,
        ValueFromPipeline = $false,
        ValueFromPipelineByPropertyName = $false,
        ParameterSetName = "ObjectType")]
        [ValidateNotNullOrEmpty()]
        [string]$Key,

        # Company name used in the URL
        [Parameter(Mandatory= $false)]
        [ValidateNotNullOrEmpty()]
        [string]$CompanyName,
        
        # Api key
        [Parameter(Mandatory= $false)]
        [ValidateNotNullOrEmpty()]
        [string]$ApiKey,

        # Api secret
        [Parameter(Mandatory= $false)]
        [ValidateNotNullOrEmpty()]
        [securestring]$ApiSecret,

        # For preview tenants
        [Parameter(Mandatory = $false)]
        [switch]$IsPreviewTenant
    )
    
    begin {

        if ($PSBoundParameters.ContainsKey("CompanyName") -AND $PSBoundParameters.ContainsKey("ApiKey") -AND $PSBoundParameters.ContainsKey("ApiSecret") ){
            Write-Verbose -Message "Using connectioninfo and credentials from parameter"
            #Create credential object for authentication
            $Cred = New-Object System.Management.Automation.PSCredential ($ApiKey, $ApiSecret)
            if ($IsPreviewTenant){
                $BaseUrl = "https://$CompanyName.preview-helloid.com"
            }
            else {
                $BaseUrl = "https://$CompanyName.helloid.com"
            }
        }
        elseif ($Global:HelloIdConnection.ApiCredentials) {
            Write-Verbose -Message "Using Global connectioninfo and credentials from Connect-HelloId "
            $Cred = $Global:HelloIdConnection.ApiCredentials
            $CompanyName = $Global:HelloIdConnection.CompanyName
            $BaseUrl = $Global:HelloIdConnection.BaseUrl
        }
        else {            
            throw "Error finding connectioninfo. Connect using Connect-HelloId, or specifie CompanyName, ApiKey and ApiSecret"
        }

        #Headers
        $headers = @{
            "Content-Type" = "application/json"
        }
        
    } #End begin
    process {

        $URI = "$BaseUrl/api/v1/metadata"

        if ($PSBoundParameters.ContainsKey("Id")){
            foreach ($guid in $Id){
                $DeleteURI = $URI + "/$Id"

                Write-Verbose "Invoking restmethod with uri: $DeleteURI"
                $output = Invoke-RestMethod -Uri $DeleteURI -Method "DELETE" -Headers $headers -Credential $Cred -UseBasicParsing               
                $output
            }
        }
        else {
            if ($PSBoundParameters.ContainsKey("ObjectType")){            
                $queryToAppend = "objectType=$ObjectType"
                $URI = Add-QueryToUri -Uri $URI -Query $queryToAppend
            }
            if ($PSBoundParameters.ContainsKey("ObjectId")){            
                $queryToAppend = "objectId=$ObjectId"
                $URI = Add-QueryToUri -Uri $URI -Query $queryToAppend
            }
            if ($PSBoundParameters.ContainsKey("Key")){            
                $queryToAppend = "key=$Key"
                $URI = Add-QueryToUri -Uri $URI -Query $queryToAppend
            }

            Write-Verbose "Invoking restmethod with uri: $URI"
            $output = Invoke-RestMethod -Uri $URI -Method "DELETE" -Headers $headers -Credential $Cred -UseBasicParsing               
            $output
        }

    } #End process
    end {        
    } #End end
} #End function