Skip to content

Commit 7582b0f

Browse files
authored
Merge pull request #278 from snazy2000/develop
Support for setting group on user creation and api request throttling Now there's API request throttling support in SnipeitPS . You can specify throttlelimit and period with Connect-SnipeitPS . After that SnipeitPS know when to pause. You can start limit 120 and period 60000 (ms) from hosted snipeit installation. For the fine tune there's throttlemode parameter that can be set to Burst, Constant or Adaptive.
2 parents 98095b6 + 381b6e3 commit 7582b0f

File tree

4 files changed

+115
-1
lines changed

4 files changed

+115
-1
lines changed

SnipeitPS/Private/Invoke-SnipeitMethod.ps1

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,58 @@ function Invoke-SnipeitMethod {
118118

119119
Write-Debug "$($Body | ConvertTo-Json)"
120120

121+
#Check throttle limit
122+
if ($SnipeitPSSession.throttleLimit -gt 0) {
123+
Write-Verbose "Check for request throttling"
124+
Write-debug "ThrottleMode: $($SnipeitPSSession.throttleMode)"
125+
Write-debug "ThrottleLimit: $($SnipeitPSSession.throttleLimit)"
126+
Write-debug "ThrottlePeriod: $($SnipeitPSSession.throttlePeriod)"
127+
Write-debug "ThrottleThreshold: $($SnipeitPSSession.throttleThreshold)"
128+
Write-debug "Current count: $($SnipeitPSSession.throttledRequests.count)"
129+
130+
#current request timestamps in period
131+
$SnipeitPSSession.throttledRequests = ($SnipeitPSSession.throttledRequests).where({$_ -gt (get-date).AddMilliseconds( 0 - $SnipeitPSSession.throttlePeriod).ToFileTime()})
132+
133+
#make sure that we alway have list here
134+
if($null -eq $SnipeitPSSession.throttledRequests) {
135+
$SnipeitPSSession.throttledRequests = [System.Collections.ArrayList]::new()
136+
}
137+
138+
$naptime = 0
139+
switch ($SnipeitPSSession.throttleMode) {
140+
"Burst" {
141+
if ($SnipeitPSSession.throttledRequests.count -ge $SnipeitPSSession.throttleLimit) {
142+
$naptime = [Math]::Round(((get-date).ToFileTime() - ($SnipeitPSSession.throttledRequests[0]))/10000)
143+
}
144+
}
145+
146+
"Constant" {
147+
$prevrequesttime =[Math]::Round(((get-date).ToFileTime() - ($SnipeitPSSession.throttledRequests[$SnipeitPSSession.throttledRequests.count - 1]))/10000)
148+
$naptime = [Math]::Round($SnipeitPSSession.throttlePeriod / $SnipeitPSSession.throttleLimit) - $prevrequesttime
149+
}
150+
151+
"Adaptive" {
152+
$unThrottledRequests = $SnipeitPSSession.throttleLimit * ($SnipeitPSSession.throttleThreshold / 100)
153+
if($SnipeitPSSession.throttledRequests.count -ge $unThrottledRequests) {
154+
#calculate time left in throttlePeriod and devide it for remaining requests
155+
$remaining = $SnipeitPSSession.throttleLimit - $SnipeitPSSession.throttledRequests.count
156+
if ($remaining -lt 1) {
157+
$remaining = 1
158+
}
159+
$naptime = [Math]::Round((((get-date).ToFileTime() - ($SnipeitPSSession.throttledRequests[0]))/ 10000) / $remaining)
160+
}
161+
}
162+
}
163+
164+
#Do we need a nap
165+
if ($naptime -gt 0) {
166+
Write-verbose "Throttling request for $naptime ms"
167+
Start-Sleep -Milliseconds $naptime
168+
}
169+
170+
$SnipeitPSSession.throttledRequests.Add((Get-Date).ToFileTime())
171+
}
172+
121173
# Invoke the API
122174
try {
123175
Write-Verbose "[$($MyInvocation.MyCommand.Name)] Invoking method $Method to URI $URi"

SnipeitPS/Public/Connect-SnipeitPS.ps1

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,22 @@
2020
PSCredential where username shoul be snipe it url and password should be
2121
snipe it apikey.
2222
23+
.PARAMETER throttleLimit
24+
Throttle request rate to nro of requests per throttlePeriod. Defaults to 0 that means no requests are not throttled.
25+
26+
.PARAMETER throttlePeriod
27+
Throttle period time span in milliseconds defaults to 60 milliseconds.
28+
29+
.PARAMETER throttleThreshold
30+
Threshold percentage of used request on period after request are throttled.
31+
32+
.PARAMETER throttleMode
33+
RequestThrottling type. "Burst" allows all requests to be used in ThrottlePeriod without delays and then waits
34+
until there's new requests avalable. With "Contant" mode there always delay between requests. Delay is calculated
35+
by dividing throttlePeriod with throttleLimit. "Adaptive" mode allows throttleThreshold percentage of request to be
36+
used with out delay, after threshold limit is reached next requests are delayded by dividing available requests
37+
over throttlePeriod.
38+
2339
.EXAMPLE
2440
Connect-SnipeitPS -Url $url -apiKey $myapikey
2541
Connect to Snipe it api.
@@ -61,7 +77,28 @@ function Connect-SnipeitPS {
6177
[SecureString]$secureApiKey,
6278

6379
[Parameter(ParameterSetName='Connect with credential',Mandatory=$true)]
64-
[PSCredential]$siteCred
80+
[PSCredential]$siteCred,
81+
82+
[Parameter(ParameterSetName='Connect with url and apikey',Mandatory=$false)]
83+
[Parameter(ParameterSetName='Connect with url and secure apikey',Mandatory=$false)]
84+
[Parameter(ParameterSetName='Connect with credential',Mandatory=$false)]
85+
[int]$throttleLimit,
86+
87+
[Parameter(ParameterSetName='Connect with url and apikey',Mandatory=$false)]
88+
[Parameter(ParameterSetName='Connect with url and secure apikey',Mandatory=$false)]
89+
[Parameter(ParameterSetName='Connect with credential',Mandatory=$false)]
90+
[int]$throttlePeriod,
91+
92+
[Parameter(ParameterSetName='Connect with url and apikey',Mandatory=$false)]
93+
[Parameter(ParameterSetName='Connect with url and secure apikey',Mandatory=$false)]
94+
[Parameter(ParameterSetName='Connect with credential',Mandatory=$false)]
95+
[int]$throttleThreshold,
96+
97+
[Parameter(ParameterSetName='Connect with url and apikey',Mandatory=$false)]
98+
[Parameter(ParameterSetName='Connect with url and secure apikey',Mandatory=$false)]
99+
[Parameter(ParameterSetName='Connect with credential',Mandatory=$false)]
100+
[ValidateSet("Burst","Constant","Adaptive")]
101+
[string]$throttleMode
65102
)
66103

67104

@@ -86,6 +123,21 @@ function Connect-SnipeitPS {
86123
$SnipeitPSSession.apiKey = $siteCred.GetNetworkCredential().SecurePassword
87124
}
88125
}
126+
if($null -eq $throttleLimit) { $throttleLimit = 0}
127+
$SnipeitPSSession.throttleLimit = $throttleLimit
128+
129+
if($throttleThreshold -lt 1) { $throttleThreshold = 90}
130+
$SnipeitPSSession.throttleThreshold = $throttleThreshold
131+
132+
if('' -eq $throttleMode) { $throttleMode = "Burst"}
133+
$SnipeitPSSession.throttleMode = $throttleMode
134+
135+
if ($SnipeitPSSession.throttleLimit -gt 0) {
136+
if($null -eq $throttlePeriod) { $throttlePeriod = 60000}
137+
$SnipeitPSSession.throttlePeriod = $throttlePeriod
138+
139+
$SnipeitPSSession.throttledRequests = [System.Collections.ArrayList]::new()
140+
}
89141

90142
Write-Debug "Site-url $($SnipeitPSSession.url)"
91143
Write-Debug "Site apikey: $($SnipeitPSSession.apiKey)"

SnipeitPS/Public/New-SnipeitUser.ps1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
.PARAMETER manager_id
4545
ID number of manager
4646
47+
.PARAMETER groups
48+
ID numbers of groups
49+
4750
.PARAMETER employee_num
4851
Employeenumber
4952
@@ -103,6 +106,8 @@ function New-SnipeitUser() {
103106

104107
[int]$manager_id,
105108

109+
[int[]]$groups,
110+
106111
[string]$employee_num,
107112

108113
[bool]$ldap_import = $false,

SnipeitPS/Public/Set-SnipeitUser.ps1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@
4747
.PARAMETER manager_id
4848
ID number of manager
4949
50+
.PARAMETER groups
51+
ID numbers of groups
52+
5053
.PARAMETER employee_num
5154
Employeenumber
5255
@@ -110,6 +113,8 @@ function Set-SnipeitUser() {
110113

111114
[Nullable[System.Int32]]$manager_id,
112115

116+
[int[]]$groups,
117+
113118
[string]$employee_num,
114119

115120
[bool]$activated,

0 commit comments

Comments
 (0)