Skip to content

Commit

Permalink
Merge pull request torizon#183 from microhobby/dev-torizonio
Browse files Browse the repository at this point in the history
scripts: torizonIO: Do not limit the number of packages and fleets
  • Loading branch information
andreriesco authored Mar 6, 2024
2 parents 3d9450e + 5192e7f commit fb7a2df
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions scripts/torizonIO.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ param()

# TODO: we need to work with the offsets and limits

$_VERSION = "0.1.0"
$_VERSION = "0.2.0"

$ErrorActionPreference = "Stop"

Expand All @@ -28,7 +28,7 @@ if ($null -eq $env:PLATFORM_CLIENT_SECRET) {
$_mod = Get-Module -ListAvailable -Name "TorizonPlatformAPI"

if (
-not ($_mod) -or
-not ($_mod) -or
($_mod.Version[0].ToString().Contains($_VERSION) -eq $false)
) {
Install-Module `
Expand All @@ -46,21 +46,21 @@ function _getJonOsterToken () {
$_headers = @{
"Content-Type" = "application/x-www-form-urlencoded"
}

$_payload = @{
"grant_type" = "client_credentials"
"client_id" = "$env:PLATFORM_CLIENT_ID"
"client_secret" = "$env:PLATFORM_CLIENT_SECRET"
}

$_ret = Invoke-RestMethod `
-Method Post `
-Uri "https://kc.torizon.io/auth/realms/ota-users/protocol/openid-connect/token" `
-Headers $_headers `
-Body $_payload `
-ContentType "application/x-www-form-urlencoded" `
-ErrorAction Stop

# and we have the AWESOME JonOster Token 🦪
return $_ret.access_token
}
Expand All @@ -73,7 +73,8 @@ Set-TorizonPlatformAPIConfiguration `
-ErrorAction Stop

function _getTargetByHash ([string] $_hash) {
$_packages = Get-TorizonPlatformAPIPackages
$_packages = `
Get-TorizonPlatformAPIPackages -Limit ([System.Int64]::MaxValue)

foreach ($_package in $_packages.values) {
if ($_hash -eq $_package.hashes.sha256) {
Expand All @@ -85,7 +86,7 @@ function _getTargetByHash ([string] $_hash) {
}

function _getFleetDevices ($_fleetName) {
$_fleets = Get-TorizonPlatformAPIFleets
$_fleets = Get-TorizonPlatformAPIFleets -Limit ([System.Int64]::MaxValue)

$_fleetId = (
$_fleets.values |
Expand All @@ -96,10 +97,11 @@ function _getFleetDevices ($_fleetName) {
throw "Fleet '$_fleetName' not found"
}

$_devices =
$_devices =
Get-TorizonPlatformAPIFleetsFleetidDevices `
-Limit ([System.Int64]::MaxValue) `
-FleetId $_fleetId

if ($_devices.total -eq 0) {
throw "Fleet '$_fleetName' has no devices"
}
Expand All @@ -108,7 +110,7 @@ function _getFleetDevices ($_fleetName) {
}

function _getFleetId ($_fleetName) {
$_fleets = Get-TorizonPlatformAPIFleets
$_fleets = Get-TorizonPlatformAPIFleets -Limit ([System.Int64]::MaxValue)

$_fleetId = (
$_fleets.values |
Expand Down Expand Up @@ -152,7 +154,8 @@ function _resolvePlatformMetadata ([object] $targets, [string] $targetName) {

function package-latest-hash ([string] $packageName) {
$_targetName = $packageName
$_targets = Get-TorizonPlatformAPIPackages
$_targets = `
Get-TorizonPlatformAPIPackages -Limit ([System.Int64]::MaxValue)
$_hash = $null

$_ret = _resolvePlatformMetadata $_targets $_targetName
Expand All @@ -168,7 +171,8 @@ function package-latest-hash ([string] $packageName) {

function package-latest-version ([string] $packageName) {
$_packageName = $packageName
$_packages = Get-TorizonPlatformAPIPackages
$_packages = `
Get-TorizonPlatformAPIPackages -Limit ([System.Int64]::MaxValue)

$_ret = _resolvePlatformMetadata $_packages $_packageName

Expand All @@ -182,19 +186,19 @@ function update-fleet-latest ([string] $targetName, [string] $fleetName) {

$_targetHash = package-latest-hash $_targetName
$_target = _getTargetByHash($_targetHash)

if ($null -eq $_target) {
throw "package $_targetName not found"
}

$_targetId = $_target.packageId
$_fleetId = _getFleetId($_fleetName)

$_updateRequest = Initialize-TorizonPlatformAPIUpdateRequest `
-PackageIds @($_targetId) `
-Fleets @($_fleetId)

$Result =
$Result =
Submit-TorizonPlatformAPIUpdates `
-UpdateRequest $_updateRequest

Expand Down

0 comments on commit fb7a2df

Please sign in to comment.