Skip to content

Classes and data types #38

@agowa

Description

@agowa

Please add data types for simpler handling and error checking.
Having types like below will allow:

  • PowerShell to perform syntax checks and show useful error messages
  • Allow to do type validation
  • Provide self documenting code
  • Better interoperability between the New, Get and Set functions
  • Better error handling by strictly typing variables.
<#
class NetboxVirtualMachineCustomFields {
    [ValidateNotNull()][String]$customField000 = ''
    [ValidateNotNull()][String]$customField001 = ''
}
class NetboxTenantCustomFields {
    [ValidateNotNull()][String]$tenant_customField000
}
class NetboxVirtualMachineObject {
    [ValidateNotNullOrEmpty()][String]$name
    [ValidateNotNull()][Int64]$device_role = $nbDeviceRole.Id
    [ValidateNotNullOrEmpty()][Int64]$cluster
    [ValidateNotNull()][ValidateSet('active')][String]$status = 'active'
    [ValidateNotNull()][Int64]$tenant
    [ValidateNotNull()][Int64]$platform = $nbPlatform.Id
    [ValidateNotNull()][Int64]$vcpus = 0
    [ValidateNotNull()][Int64]$memory = 0
    [ValidateNotNull()][Int64]$disk = 0
    [ValidateNotNull()][String]$comment = ''
    [ValidateNotNull()][String[]]$tags = [String[]]@('')
    [ValidateNotNullOrEmpty()][NetboxVirtualMachineCustomFields]$custom_fields
}
class NetboxClusterObject {
    [ValidateNotNull()][String]$name
    [ValidateNotNull()][Int64]$type = $nbClusterType.id
}
class NetboxTenantObject {
    [ValidateNotNull()][String]$name
    [ValidateNotNull()][String]$slug
    [ValidateNotNull()][Int64]$group
    [ValidateNotNull()][NetboxTenantCustomFields]$custom_fields
}
#>

My example is currently static, but it could be generated dynamically too. Just overwriting the new method and calling Add-Member before returning the instantiated object.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions