You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/blog/bulk-onboarding-of-users-in-hpe-greenlake-edge-to-cloud-platform.md
+260-1Lines changed: 260 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,6 +4,10 @@ date: 2024-04-24T13:44:40.533Z
4
4
author: Didier Lalli
5
5
authorimage: /img/didier-lalli.png
6
6
disable: false
7
+
tags:
8
+
- API
9
+
- hpe-greenlake
10
+
- hpe-greenlake-platform
7
11
---
8
12
## HPE GreenLake API to the rescue
9
13
@@ -29,4 +33,259 @@ In this blog post, I will focus on one specific API call, part of Identity Manag
29
33
30
34
Before writing any code, it’s important to understand what data is required to invite a user. According to the [API reference](https://developer.greenlake.hpe.com/docs/greenlake/services/iam/workspaces/public/openapi/workspaces-v1/operation/invite_user_to_account_identity_v1_users_post/), one simply needs the email address of the invited user. That’s easy! In the documentation, you can also see that there is no way to select a workspace to invite the user to. The reason for this is that the API credentials used to make the call is workspace specific, so it implicitly provides the workspace to which the user will be invited to. This means that one needs to collect API access credentials for every workspace that to the users are added to. For the script I am writing here, in a Workspace tab, I have stored the Client Id corresponding to API Access of a given Workspace. Because I don’t want to save Client Secrets, I will prompt for them and store them in memory.
31
35
32
-
So, my workspace contains the following 2 sheets:
36
+
So, my Excel file contains the following 2 sheets:
37
+
38
+

39
+
40
+

41
+
42
+
43
+
44
+
## High-level algorithm
45
+
46
+
47
+
Let’s look at the steps necessary to invite users from my spreadsheets:
48
+
49
+
1. Read command parameters to get the Excel filename
50
+
2. Open spreadsheet to retrieve data
51
+
3. For each workspace in Workspaces sheet
52
+
- Prompt for Client Secret that matches the Client Id
53
+
- Retrieve a session token using those credentials
54
+
4. For each user in Users sheet
55
+
- Lookup Client Id using workspace name
56
+
- Call POST /identity/v1/users for user using email
57
+
- Increase counter of invited users
58
+
5. Display list of users invited in each workspace
59
+
60
+
## Putting things together in PowerShell
61
+
62
+
I decided to use PowerShell to write this script because it provides easy native access to Excel spreadsheets.
63
+
64
+
### Step 1 – Reading the parameter from the command line.
65
+
66
+
```
67
+
Param($XLFile)
68
+
69
+
if ($Null -eq $XLFile)
70
+
{
71
+
if ($env:XLFile -eq $Null)
72
+
{
73
+
$XLFile = read-host "Enter name of the Excel file"
74
+
}
75
+
}
76
+
77
+
```
78
+
79
+
### Step 2 – Importing data from the 2 sheets of my spreadsheet.
*Note that I initialized 2 hash tables, one called $tokens that will store the token for a given Client Id (i.e Workspace) and another called $invited for storing the number of invited users for a given Client Id.*
93
+
94
+
### Step 3 – Iterating over the Workspaces sheet to collect client secrets, and retrieve access tokens.
95
+
96
+
```
97
+
# Ask for client_Secret of each workspace in Excel file
98
+
foreach ($workspace in $workspaces_excel ) {
99
+
$client_id = $workspace.'Client Id'
100
+
if ($tokens[$client_id] -eq $null) {
101
+
# We don't have a token for this client_id yet
102
+
# We need to ask the Client secret for this workspace
Write-Host "Error sending invite for" $user.Email"! Already onboarded?" -ForegroundColor Red
172
+
Write-Host $Error[0] -ForegroundColor Red
173
+
continue
174
+
}
175
+
sleep 15
176
+
}
177
+
}
178
+
179
+
```
180
+
181
+
Note that before the loop, I initialized to zero the count of invited users for a given workspace. Also note the sleep 15 (seconds) at the end of the loop to avoid issues with rate limiting constraints which might raise a status code 429.
182
+
183
+
*Note: Rate Limiting is a mechanism employed to control and restrict the rate at which requests or interactions are permitted to occur between clients and a service.*
184
+
185
+
### Step 5: Displaying list of users invited in each workspace.
186
+
187
+
```
188
+
else
189
+
{
190
+
write-host 'Mailing list file not provided nor found....'
191
+
exit
192
+
}
193
+
Write-host "Done processing Excel file $XLFile!"
194
+
195
+
# ------------------------ Query GL to get list of users for each workspace ------------------------
196
+
foreach ($workspace in $workspaces_excel ) {
197
+
$workspace_name = $workspace.'Workspace Name'
198
+
$client_id = $workspace.'Client Id'
199
+
# Create header for next API calls
200
+
$headers = @{}
201
+
$AccessToken = $tokens[$client_id]
202
+
$headers["Authorization"] = "Bearer $AccessToken"
203
+
$headers["Accept"] = "application/json"
204
+
$headers["Content-Type"] = "application/json"
205
+
try {
206
+
$response = Invoke-webrequest "https://global.api.greenlake.hpe.com/identity/v1/users?filter=&limit=300&offset=0" -Method GET -Headers $headers
207
+
}
208
+
catch {
209
+
Write-Host "Cannot get list of users!!"
210
+
exit
211
+
}
212
+
$invited_users=$invited[$client_id]
213
+
Write-Host $invited_users "user(s) invited to workspace" $workspace_name
214
+
Write-Host "List of users in workspace:" $workspace_name
As you can see, the script has invited 1 user in each workspace, the second email being already a member of the workspace (thus no action is necessary).
284
+
285
+
## What’s next?
286
+
287
+
Through this post, I have shown you how it is possible to integrate with HPE GreenLake platform using the most popular scripting languages, such as PowerShell. You can get the source code for these scripts from [our community tooling repository](https://github.com/hpe-dev-incubator/GLP-API-Tooling).
288
+
289
+
If you’re interested in trying out what I just discussed, you might first want to check out one of our hands-on Workshops-on-Demand that lets you play with the HPE GreenLake APIs mentioned in this blog post. The workshops are free, available 24/7, and very easy to use. They give you a real-world experience without any risk. Check out our [catalog of workshops](https://developer.hpe.com/hackshack/workshops), register for the one you’re interested in and go! It’s as simple as that.
290
+
291
+
If you still have any questions regarding the HPE GreenLake platform APIs, join the [HPE Developer Community Slack Workspace](https://developer.hpe.com/slack-signup/) and start a discussion on our [\#hpe-greenlake-api](https://hpedev.slack.com/archives/C02EG5XFK8Q) channel. We are always here to help.
0 commit comments