Skip to content

Commit 4cac15e

Browse files
committed
Update Blog “bulk-onboarding-of-users-in-hpe-greenlake-edge-to-cloud-platform”
1 parent 6fe36da commit 4cac15e

File tree

1 file changed

+260
-1
lines changed

1 file changed

+260
-1
lines changed

content/blog/bulk-onboarding-of-users-in-hpe-greenlake-edge-to-cloud-platform.md

Lines changed: 260 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ date: 2024-04-24T13:44:40.533Z
44
author: Didier Lalli
55
authorimage: /img/didier-lalli.png
66
disable: false
7+
tags:
8+
- API
9+
- hpe-greenlake
10+
- hpe-greenlake-platform
711
---
812
## HPE GreenLake API to the rescue
913

@@ -29,4 +33,259 @@ In this blog post, I will focus on one specific API call, part of Identity Manag
2933

3034
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.
3135

32-
So, my workspace contains the following 2 sheets:
36+
So, my Excel file contains the following 2 sheets:
37+
38+
![Users tab in Excel](/img/bulkimport-blog-picture-1.png "Users tab in Excel")
39+
40+
![Workspaces tab in Excel](/img/bulkimport-blog-picture-2.png "Workspaces tab in Excel")
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.
80+
81+
```
82+
$tokens =@{}
83+
$invited=@{}
84+
85+
if ($XLFile)
86+
{
87+
$users_excel = import-excel -path $XLFile -dataonly -worksheetname Users
88+
$workspaces_excel = Import-Excel -path $XLFile -dataonly -worksheetname Workspaces
89+
90+
```
91+
92+
*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
103+
$workspace_name = $workspace.'Workspace Name'
104+
$client_id = $workspace.'Client Id'
105+
106+
$secClientSecret = read-host "Enter HPE GreenLake Client Secret for Workspace $workspace_name" -AsSecureString
107+
$bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($secClientSecret)
108+
$secret = [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($bstr)
109+
110+
# use Client Id and Client Secret to retrieve a token
111+
$body = "grant_type=client_credentials&client_id=" + $client_id + "&client_secret=" + $secret
112+
$headers = @{}
113+
$headers["Content-Type"] = "application/x-www-form-urlencoded"
114+
115+
try {
116+
$response = Invoke-webrequest "https://sso.common.cloud.hpe.com/as/token.oauth2" -Method POST -Headers $headers -Body $body
117+
# store the token for future use
118+
$AccessToken = ($response.Content | Convertfrom-Json).access_token
119+
$tokens.Add($client_id,$AccessToken)
120+
}
121+
catch {
122+
Write-Host "Error retrieving access token for workspace $workspace_name!" -ForegroundColor Red
123+
exit
124+
}
125+
}
126+
}
127+
128+
```
129+
130+
Note that, at the end of this loop, I have a hash table of tokens indexed by Client Id, which I will use to call the API in the next section
131+
132+
### Step 4 – Iterating over Users sheet to invite each of them.
133+
134+
```
135+
# Now walk the list of users to add
136+
$invited.Add($client_id,0)
137+
foreach ($user in $users_excel ) {
138+
$workspace_name = $user.'Workspace Name'
139+
# Get client id from workspace name
140+
$result = $workspaces_excel | Where-Object { $_.'Workspace Name' -eq $workspace_name }
141+
if ($result.Count -eq 0)
142+
{
143+
Write-Host "Workspace not found for user " $user.email -ForegroundColor Red
144+
exit
145+
}
146+
$client_id = $result[0].'Client Id'
147+
148+
Write-Host "Inviting user" $user.email "to workspace./" $workspace_name
149+
$AccessToken = $tokens[$client_id]
150+
151+
# Create header for next API calls
152+
$headers = @{}
153+
$headers["Authorization"] = "Bearer $AccessToken"
154+
$headers["Accept"] = "application/json"
155+
$headers["Content-Type"] = "application/json"
156+
157+
# Build body for next API call
158+
$_body = @{
159+
"email" = $user.email
160+
"sendWelcomeEmail" = $true
161+
}
162+
163+
$Body = $_body | ConvertTo-Json
164+
165+
# Call GLP API to invite user
166+
try {
167+
$response = Invoke-webrequest -Uri "https://global.api.greenlake.hpe.com/identity/v1/users" -Method POST -Headers $headers -Body $Body
168+
$invited[$client_id]++
169+
}
170+
catch {
171+
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
215+
216+
$_list = $response.Content | ConvertFrom-Json
217+
if ($null -ne $_list)
218+
{
219+
$_users_list = [System.Collections.ArrayList]::new()
220+
221+
foreach ($_u in $_list.Items)
222+
{
223+
224+
$_users_list += @{
225+
'Username' = $_u.Username
226+
'Status' = $_u.userStatus
227+
'id' = $_u.Id
228+
}
229+
}
230+
231+
}
232+
233+
$_users_list | select Username, Status | ft -AutoSize
234+
235+
}
236+
237+
```
238+
239+
## Try it!
240+
241+
Let’s run this script, making sure to reference the right Excel spreadsheet:
242+
243+
```
244+
PS /Volumes/Dev/GreenLake/GLP-API-Tooling/Scripts> ./bulk_invite.ps1 -XLfile userlist.xlsx
245+
Enter HPE GreenLake Client Secret for Workspace HPEDEV -GLCP- Hackshack: ********************************
246+
Enter HPE GreenLake Client Secret for Workspace Super Awesome Company: ********************************
247+
Inviting user [email protected] to workspace HPEDEV -GLCP- Hackshack
248+
Inviting user [email protected] to workspace HPEDEV -GLCP- Hackshack
249+
Error sending invite for [email protected] ! Already onboarded?
250+
Inviting user [email protected] to workspace Super Awesome Company
251+
Inviting user [email protected] to workspace Super Awesome Company
252+
Error sending invite for [email protected] ! Already onboarded?
253+
Done processing Excel file userlist.xlsx!
254+
255+
1 user(s) invited to workspace HPEDEV -GLCP- Hackshack
256+
List of users in workspace: HPEDEV -GLCP- Hackshack
257+
258+
Username Status
259+
-------- ------
260+
<email> VERIFIED
261+
262+
263+
264+
265+
266+
<email> VERIFIED
267+
268+
1 user(s) invited to workspace Super Awesome Company
269+
List of users in workspace: Super Awesome Company
270+
271+
Username Status
272+
-------- ------
273+
<email> VERIFIED
274+
275+
276+
277+
278+
279+
<email> VERIFIED
280+
281+
```
282+
283+
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

Comments
 (0)