powershellwrapper icon indicating copy to clipboard operation
powershellwrapper copied to clipboard

Helper function to return all objects

Open CalebAlbers opened this issue 7 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

Great feature. I personally think adding an -All parameter to the existing cmdlets is a better design than Get-All ....., but either way, I would love to see this implemented.

davidhaymond avatar Nov 30 '22 17:11 davidhaymond

adding an -All parameter to the existing cmdlets

Yeah, I don't know what I was on about nearly four years ago, but a -all param is definitely the play here 😅

CalebAlbers avatar Dec 01 '22 05:12 CalebAlbers

Wouldn't this mean multiple calls to the API? I suggest that we add a switch as well with some kind of message, to make the user confirm their action. A [Switch] for example can be turned off with :$false for those who want to automate.

ecspresso avatar Dec 01 '22 07:12 ecspresso

Wouldn't this mean multiple calls to the API? I suggest that we add a switch as well with some kind of message, to make the user confirm their action. A [Switch] for example can be turned off with :$false for those who want to automate.

Yes, it would typically require multiple calls to the API. I don't think a confirmation is necessary, however, because it's a non-destructive action, and the user must specify an extra parameter to enable it. It might be good to name the parameter something like -AutoPaginate to make it clear that multiple API calls may occur.

davidhaymond avatar Dec 01 '22 17:12 davidhaymond

I had created a function to do this. I'm sure there is a more elegant way

function get-allflexassets {
    $page_number = 1
    $data = @()
    $total_pages = Get-ITGlueFlexibleAssets -filter_flexible_asset_type_id $FilterID.id -filter_organization_id $org.id -page_size 1000 -page_number $page_number | Select-Object -ExpandProperty meta | Select-Object -ExpandProperty total-count 
    if ($total_pages -eq 0) { $data = (Get-ITGlueFlexibleAssets -filter_flexible_asset_type_id $FilterID.id -filter_organization_id $org.id -page_size 1000 -page_number $page_number).data }
    else {
        do {
            $current_page = Get-ITGlueFlexibleAssets -filter_flexible_asset_type_id $FilterID.id -filter_organization_id $org.id -page_size 1000 -page_number $page_number
            $data += $current_page.data
            $page_number++
        } 
        while ($current_page.meta.'total-pages' -ne $page_number - 1)
        $data
    }
}

Where: $FlexAssetName = "(Name of Flexible Asset)" $FilterID = (Get-ITGlueFlexibleAssetTypes -filter_name $FlexAssetName).data

marcoby avatar Jan 03 '23 21:01 marcoby

Implemented by #163.

davidhaymond avatar Jan 04 '24 19:01 davidhaymond