powershellwrapper icon indicating copy to clipboard operation
powershellwrapper copied to clipboard

Helper function to return all objects

Open CalebAlbers opened this issue 5 years ago • 2 comments

Is your feature request related to a problem? Please describe. Inspired by #55, it is often times necessary to return all objects of a specific type (i.e. organizations). Currently, the module is a very transparent wrapper on top of the IT Glue api. This is good for flexibility, but not always for usability.

Describe the solution you'd like A helper function that is called to make it easy for returning all values would be beneficial. Instead of looping through a bunch of Get-ITGlueOrganizations -page_number <x> functions inside your code, abstract this with something such as Get-All Get-ITGlueOrganizations <params> and call it a day, similar to how you can call Get-Help Get-ITGlueOrganizations.

Describe alternatives you've considered A current work-around exists:

$page_number = 1
$data = @() # array to hold all returned IT Glue objects
do {
    $current_page = Get-ITGlue__FILL_THIS_IN -page_number $page_number

    $data += $current_page.data

    $page_number++
} while ($current_page.meta.'total-pages' -ne $page_number - 1)

Additional context Care will have to be taken to implement this correctly. A script block parameter could work as input, but perhaps we would be better served doing some sanity checking (i.e. making sure that the function that gets called is actually a valid IT Glue function). Let's figure out our approach prior to implementing it. Additionally, let's review nomenclature, as currently the naming scheme present for functions directly correlates with those in the API documentation.

CalebAlbers avatar Jan 17 '19 16:01 CalebAlbers

Before submitting a pull request please have a look at the following function. https://github.com/brianmccarty/powershellwrapper/blob/helperfunction-1/ITGlueAPI/Internal/ITGlue_Helpers.ps1

Items that likely need to be addressed

  • function name
  • usefulness in having all the GET- functions included
  • alternative of scriptblock to using SWITCH , I was not able to pass a named argument "page_number" through to $function. Initially I've used ValidateSet to limit the allowed functions to be called

Input would be appreciated regarding implementation

brianmccarty avatar Jan 19 '19 00:01 brianmccarty

@brianmccarty, thank you for the follow up:

  • Suggest Get-ITGlueHelperGetAll (remove the '_' underscores) to try to keep closer to the naming style, Building PowerShell Functions: Best Practices may help here too
  • Go for it for now, I can see some sencarios where it may help to have them, no harm that I know of in adding them for now
  • I do not have an answer yet but maybe @CalebAlbers or @ecspresso do?

adrianwells avatar Jan 28 '19 16:01 adrianwells