Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Classes and data types #38

Open
agowa opened this issue Aug 13, 2020 · 6 comments
Open

Classes and data types #38

agowa opened this issue Aug 13, 2020 · 6 comments

Comments

@agowa
Copy link

agowa commented Aug 13, 2020

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.

@BatmanAMA
Copy link
Owner

Hello. I am currently working on this for basically the reasons you mentioned, plus type backing would help bring the objects into compliance with the backend API while also making them more human readable. Hoping to have this in the next release.

@evilensky
Copy link

Is it possible to follow the progress? I am new to .net and Powershell classes.

@agowa
Copy link
Author

agowa commented Aug 20, 2020

@BatmanAMA
Copy link
Owner

Sorry - I have something I'm cooking up on my local computer. Trying a few different existant modules that will generate stuff from the API spec to see if any of them are reasonable. I have been keeping that in a private repo because anything that touches this repo triggers a beta release and a lot of this literally doesn't run.

@evilensky
Copy link

@BatmanAMA Google sent me down to AutoRest Is that something you are currently using? After typing out a few classes by hand using @agowa338 's example and About_Classes, it makes sense that there has to be "a better way."

@BatmanAMA
Copy link
Owner

I have played with it, and it may be how I generate classes but I don't care for the module part of what it generates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants