lwhitelock / HuduAPI

A powershell API for Hudu Documentation
MIT License
53 stars 45 forks source link

Set-HuduAsset doesn't allow moving asset to another company #50

Closed fowlerr closed 4 months ago

fowlerr commented 8 months ago

Set-HuduAsset doesn't allow moving an asset between companies as there is only one company_id parameter. The function parameter $CompanyId is replaced by $object.company_id from the get-huduasset inside the Set-HuduAsset function.

If I use the HUDU API directly for the Put action on Assets /companies/{existing_company_id}/assets/id I can move the asset between companies. { "company_id": new_company_id }

mrmattipants commented 7 months ago

I would just use "Get-HuduAssets" separately to retrieve the Asset Data using the CompanyID from Company A, then use "Set-HuduAsset" with the CompanyID for Company B.

It should be noted that since "Set-HuduAsset" is meant to Update/Replace the Field Data in relation to an existing Asset, the "Get-HuduAssets" Cmdlet that runs inside of the "Set-HuduAsset" Cmdlet, is simply used to obtain the Fields for the existing Asset, so that they can be updated.

I hope that helps. Let me know if you need an example, as I just wrote a Script to Copy Asset Fields from one asset to another, the other day. I'll be happy to update it, so it can Copy Asset Fields between Companies and post it.

fowlerr commented 7 months ago

I think this is an issue because the function as written discards the user provided $CompanyId.

The Hudu API allows an asset to move to another company by setting company_id in the body of the asset. The old company_id must be passed in the url of the PUT endpoint.

Scenario: I want to move asset with id 9883 from companyid 29 to companyid 31.

Action: set-huduAsset -companyId 31 -id 9883

Result: The asset IS NOT moved.

In Set-HuduAsset.ps1 line 75 it will retrieve the asset from the $id which will in the body contain the companyId of 29 not 31 $Object = get-HuduAssets -id 9883

In Set-HuduAsset.ps1 line 77 the parameter $CompanyId (which I set as 31) is replaced with 29 from downloading the asset $CompanyId = $Object.company_id (29 again)

In Set-HuduAsset.ps1 line 114 Both the $CompanyId in the Resource parameter as well as $JSON are both at 29 not 31. Invoke-HuduRequest -Method put -Resource "/api/v1/companies/$CompanyId/assets/$Id" -Body $JSON

My workaround: Replace line 77 ($CompanyId = $Object.company_id) with below or something to compare and then store new vs old company as the old company is required in the URL and the new company needs to be in the body:

if ($CompanyId -and $Object.company_id -ne $CompanyId){ $oldCompany = $Object.company_id $Object.company_id = $CompanyId $CompanyId = $oldCompany } else { $CompanyId = $Object.company_id }

On Fri, Mar 22, 2024 at 12:25 AM Matti Pedersen @.***> wrote:

I would just use "Get-HuduAssets" separately to retrieve the Asset Data using the CompanyID from Company A, then use "Set-HuduAsset" with the CompanyID for Company B.

It should be noted that since "Set-HuduAsset" is meant to Update/Replace the Field Data in relation to an existing Asset, the "Get-HuduAssets" Cmdlet that runs inside of the "Set-HuduAsset" Cmdlet, is simply used to obtain the Fields for the existing Asset, so that they can be updated.

I hope that helps.

— Reply to this email directly, view it on GitHub https://github.com/lwhitelock/HuduAPI/issues/50#issuecomment-2014382145, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFB23ESRNVUFKEILCBVMMZLYZO6FXAVCNFSM6AAAAABEMTP3ECVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAMJUGM4DEMJUGU . You are receiving this because you authored the thread.Message ID: @.***>

--

Ron Fowler

Network Analyst

Lawrence Public Schools

110 McDonald Drive

Lawrence, KS

(785)330-1984

mrmattipants commented 7 months ago

These types of issues explain why I prefer to write my Scripts, in a way, where I do Not have to deal with any ID Numbers, etc.

That being said, I spent some time, last night, Updating my original Script, (which I mentioned in my previous Reply), to allow the Asset Fields, specific to the "Wireless APs" Asset Type, be Copied between Companies.

https://github.com/mrmattipants/HuduApiScripts/tree/main/AccessPoints

You will also find some additional examples, that have been re-written, so that they are not tied to any specific Asset Type, at the Link/URL below.

https://github.com/mrmattipants/HuduApiScripts

I have thoroughly Tested All of these Scripts and therefore, I am completely positive that they work, as advertised.

Of course, you will need to Update the necessary values (Company Names, Asset Types, Asset Names, Asset Fields, etc.), so that they match the Asset you are attempting to Copy/Move. Obviously, this also applies to the BaseURL and ApiKeys (as those in my script are simply placeholders).

Let me know if you have any questions, as I'll be happy to assist.

fowlerr commented 7 months ago

I still don't understand why Set-Asset.ps1 discards the user provided $CompanyId parameter.

Why does the function even have the $CompanyId parameter if it isn't used?

greenlighttec commented 4 months ago

Sorry for the deay in responding to this, the Hudu API requires that the Company ID be set when updating an Asset, the Set-HuduAsset grabs the existing Company ID from the existing asset so that you don't have to specify it. This is intended behavior of the function because the API requires that the parameter of the existing company ID be used.

If you want to move the company you'll need to create a copy of the asset. We may add a helper function to do this in the future.