Private/Get-AtwsParameterDefinition.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222

<#
    .COPYRIGHT
    Copyright (c) ECIT Solutions AS. All rights reserved. Licensed under the MIT license.
    See https://github.com/ecitsolutions/Autotask/blob/master/LICENSE.md for license information.
#>

Function Get-AtwsParameterDefinition {
    <#
        .SYNOPSIS
            This function returns a complete parameter set for a given entity and verb.
        .DESCRIPTION
            This function takes an entity as EntityInfo, an array of FieldInfo and a verb.
            It creates a set of default parameters and then loops through all fields in
            FieldInfo and adds them as parameters with validation sets for picklists,
            correct type and help texts.
        .INPUTS
            Autotask.EntityInfo
            Autotask.FieldInfo[]
            String, validateset get, set, new, remove
        .OUTPUTS
            Text
        .EXAMPLE
            Get-AtwsParameterDefinition -Entity $EntityInfo -FieldInfo $FieldInfo -Verb Get
 
        .NOTES
            NAME: Get-AtwsParameterDefinition
        .LINK
            Get-AtwsPSParameter
  #>

    [CmdLetBinding()]
    Param
    (   
        [Parameter(Mandatory)]
        [PSObject]
        $Entity,
        
        [Parameter(Mandatory)]
        [ValidateSet('Get', 'Set', 'New', 'Remove')]
        [string]
        $Verb,
        
        [Parameter(Mandatory)]
        [Autotask.Field[]]
        $FieldInfo
    )
    
    begin {

        Write-Debug ('{0}: Begin of function' -F $MyInvocation.MyCommand.Name)
        
        $Mandatory = @{ }
        $parameterSet = @{ }
    
        # Add Default PSParameter info to Fields
        foreach ($field in $fieldInfo) {
            $Mandatory[$field.Name] = $field.IsRequired
            $parameterSet[$field.Name] = @('By_parameters')
        }
    }

    process { 
        $TypeName = 'Autotask.{0}' -F $Entity.Name
      
        if ($Verb -eq 'Get') {
            # -Filter
            $Comment = 'A filter that limits the number of objects that is returned from the API'
            Get-AtwsPSParameter -Name 'Filter' -SetName 'Filter' -Type 'string' -Mandatory -Remaining -NotNull  -Array -Comment $Comment
            $ReferenceFields = $fieldInfo.Where( { $_.IsReference }).Name | Sort-Object
            # -GetReferenceEntityById, -GetRef
            $Comment = 'Follow this external ID and return any external objects'            
            Get-AtwsPSParameter -Name 'GetReferenceEntityById' -Alias 'GetRef' -SetName 'Filter', 'By_parameters' -Type 'string' -NotNull -ValidateSet $ReferenceFields -Comment $Comment
            # -GetExternalEntityByThisEntityId, -External
            $IncomingReferenceEntities = Get-AtwsFieldInfo -Entity $Entity.Name -ReferencingEntity | Sort-Object
            $Comment = 'Return entities of selected type that are referencing to this entity.'
            Get-AtwsPSParameter -Name 'GetExternalEntityByThisEntityId' -Alias 'External' -SetName 'Filter', 'By_parameters' -Type 'string' -NotNull -ValidateSet $IncomingReferenceEntities -Comment $Comment
            # -All
            $Comment = 'Return all objects in one query'    
            Get-AtwsPSParameter -Name 'All' -SetName 'Get_all' -Type 'switch' -Comment $Comment
            if ($Entity.HasUserDefinedFields) {
                # -UserDefinedField
                $Comment = 'A single user defined field can be used pr query'
                Get-AtwsPSParameter -Name 'UserDefinedField' -Alias 'UDF' -SetName 'By_parameters' -Type 'Autotask.UserDefinedField' -NotNull -Comment $Comment
            }
        }    
        elseif ($Verb -eq 'Set') {
            # -InputObject
            $Comment = 'An object that will be modified by any parameters and updated in Autotask'
            Get-AtwsPSParameter -Name 'InputObject' -SetName 'Input_Object' -Type $TypeName -Mandatory -Pipeline -NotNull -Array -Comment $Comment
            # -Id
            $field = $fieldInfo.Where( { $_.Name -eq 'Id' })
            $Comment = 'The object.ids of objects that should be modified by any parameters and updated in Autotask'
            Get-AtwsPSParameter -Name 'Id' -SetName 'By_Id' -Type $field.Type -Mandatory -NotNull -Array -Comment $Comment
            # -PassThru
            $Comment = 'Return any updated objects through the pipeline'
            Get-AtwsPSParameter -Name 'PassThru' -SetName 'Input_Object', 'By_parameters' -Type 'switch' -Comment $Comment
            if ($Entity.HasUserDefinedFields) {
                # -UserDefinedFields
                $Comment = 'User defined fields already setup i Autotask'
                Get-AtwsPSParameter -Name 'UserDefinedFields' -Alias 'UDF' -SetName 'Input_Object', 'By_parameters' -Type 'Autotask.UserDefinedField' -Array -Comment $Comment
            }
        }
        elseif ($Verb -in 'New') {
            # -InputObject
            $Comment = 'An array of objects to create'          
            Get-AtwsPSParameter -Name 'InputObject' -SetName 'Input_Object' -Type $TypeName -Mandatory -Pipeline -NotNull -Array -Comment $Comment
            if ($Entity.HasUserDefinedFields) {
                # -UserDefinedFields
                $Comment = 'User defined fields already setup i Autotask'
                Get-AtwsPSParameter -Name 'UserDefinedFields' -Alias 'UDF' -SetName 'By_parameters' -Type 'Autotask.UserDefinedField' -NotNull -Array -Comment $Comment
            }
        }
        elseif ($Verb -eq 'Remove') {
            # -InputObject
            $Comment = 'Any objects that should be deleted'          
            Get-AtwsPSParameter -Name 'InputObject' -SetName 'Input_Object' -Type $TypeName -Mandatory -Pipeline -NotNull -Array -Comment $Comment
            # -Id
            $field = $fieldInfo.Where( { $_.Name -eq 'Id' })
            $Comment = 'The unique id of an object to delete'
            Get-AtwsPSParameter -Name 'Id' -SetName 'By_parameters' -Type $field.Type -Mandatory  -NotNull -Array -Comment $Comment
        }
    

        switch ($Verb) {
            'Get' { 
                [array]$fields = $fieldInfo.Where{ $_.IsQueryable } | ForEach-Object {
                    $Mandatory[$_.Name] = $false
                    $_
                }
            }
            'Set' { 
                [array]$fields = $fieldInfo.Where{ -Not $_.IsReadOnly } | ForEach-Object {
                    $parameterSet[$_.Name] = @('Input_Object', 'By_parameters', 'By_Id')
                    $_
                }
            }
            'New' {
                [array]$fields = $fieldInfo.Where{
                    $_.Name -ne 'Id'
                }
            }
            default {
                return
            }

        }
    
        # Add Name alias for EntityName parameters
        $entityNameParameter = '{0}Name' -f $Entity.Name
        foreach ($field in $fields ) {
            # Start with native field type
            $Type = $field.Type

            # Fieldtype for picklists
            if ($field.IsPickList) {
                $Type = 'string'
                $ValidateLength = 0
            }
            else {
                $ValidateLength = $field.Length
            }

      
            $Alias = @() 
            if ($field.Name -eq $entityNameParameter) {
                $Alias += 'Name'
            }

            $parameterOptions = @{
                Mandatory              = $Mandatory[$field.Name]
                ParameterSetName       = $parameterSet[$field.Name]
                ValidateNotNullOrEmpty = $field.IsRequired
                ValidateLength         = $ValidateLength
                ValidateSet            = $field.PickListValues | Where-Object { $_.IsActive } | Select-Object -ExpandProperty Label | Sort-Object -Unique
                Array                  = $(($Verb -eq 'Get'))
                Name                   = $field.Name
                Alias                  = $Alias
                Type                   = $Type
                Comment                = $field.Label
                Nullable               = $Verb -ne 'New' -and $Type -ne 'string'
            }

            Get-AtwsPSParameter @ParameterOptions
        }
    
    
        # Make modifying operators possible
        if ($Verb -eq 'Get') {
            # These operators work for all fields (add quote characters here)
            [array]$Labels = $fields | Select-Object -ExpandProperty Name
            if ($Entity.HasUserDefinedFields) { $Labels += 'UserDefinedField' }
            foreach ($Operator in 'NotEquals', 'IsNull', 'IsNotNull') {
                Get-AtwsPSParameter -Name $Operator -SetName 'By_parameters' -Type 'string' -Array -ValidateSet $Labels
            }

            # These operators work for all fields except boolean (add quote characters here)
            [array]$Labels = $fields | Where-Object { $_.Type -ne 'boolean' } | Select-Object -ExpandProperty Name
            if ($Entity.HasUserDefinedFields) { $Labels += 'UserDefinedField' }
            foreach ($Operator in 'GreaterThan', 'GreaterThanOrEquals', 'LessThan', 'LessThanOrEquals') {
                Get-AtwsPSParameter -Name $Operator -SetName 'By_parameters' -Type 'string' -Array -ValidateSet $Labels
            }

            # These operators only work for strings (add quote characters here)
            [array]$Labels = $fields | Where-Object { $_.Type -eq 'string' } | Select-Object -ExpandProperty Name
            if ($Entity.HasUserDefinedFields) { $Labels += 'UserDefinedField' }
            foreach ($Operator in 'Like', 'NotLike', 'BeginsWith', 'EndsWith', 'Contains') {
                Get-AtwsPSParameter -Name $Operator -SetName 'By_parameters' -Type 'string' -Array -ValidateSet $Labels
            }
      
            # This operator only work for datetime (add quote characters here)
            [array]$Labels = $fields | Where-Object { $_.Type -eq 'datetime' } | Select-Object -ExpandProperty Name
            if ($Entity.HasUserDefinedFields) { $Labels += 'UserDefinedField' }
            foreach ($Operator in 'IsThisDay') {
                Get-AtwsPSParameter -Name $Operator -SetName 'By_parameters' -Type 'string' -Array -ValidateSet $Labels
            }
        }
    }

    end {
        return
    }
}