|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# This example shows the different aspects of user/team management. |
| 4 | +# |
| 5 | + |
| 6 | +import os |
| 7 | +import sys |
| 8 | +sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..')) |
| 9 | +from sdcclient import SdcClient |
| 10 | +# |
| 11 | +# Parse arguments |
| 12 | +# |
| 13 | +if len(sys.argv) != 4: |
| 14 | + print 'usage: %s <sysdig-token> team-prefix user-name' % sys.argv[0] |
| 15 | + print 'You can find your token at https://app.sysdigcloud.com/#/settings/user' |
| 16 | + sys.exit(1) |
| 17 | + |
| 18 | +sdc_token = sys.argv[1] |
| 19 | + |
| 20 | +# |
| 21 | +# Instantiate the SDC client |
| 22 | +# |
| 23 | +sdclient = SdcClient(sdc_token, sdc_url='https://app.sysdigcloud.com') |
| 24 | + |
| 25 | +team_prefix = sys.argv[2] |
| 26 | + |
| 27 | +user_email_parts = sys.argv[3].split('@') |
| 28 | +user_email_prefix = user_email_parts[0] |
| 29 | +user_email_domain = user_email_parts[1] |
| 30 | + |
| 31 | +# |
| 32 | +# Create test users |
| 33 | +# |
| 34 | +# All users initially are part of default team. |
| 35 | +# |
| 36 | + |
| 37 | +admin = user_email_prefix + '+team_mgmt-admin' + '@' + user_email_domain |
| 38 | +userA = user_email_prefix + '+team_mgmt-a' + '@' + user_email_domain |
| 39 | +userB = user_email_prefix + '+team_mgmt-b' + '@' + user_email_domain |
| 40 | + |
| 41 | +print 'Creating test users...' |
| 42 | + |
| 43 | +res = sdclient.create_user_invite(admin, first_name='TestUser', last_name='Admin', system_role='ROLE_CUSTOMER') |
| 44 | +if res[0] is False: |
| 45 | + print '-- User creation failed:', res[1], '. Exiting.' |
| 46 | + sys.exit(1) |
| 47 | +else: |
| 48 | + print '-- User \'', admin, '\' created successfully.' |
| 49 | + |
| 50 | +res = sdclient.create_user_invite(userA, first_name='TestUser', last_name='Alpha') |
| 51 | +if res[0] is False: |
| 52 | + print '-- User creation failed:', res[1], '. Exiting.' |
| 53 | + sys.exit(1) |
| 54 | +else: |
| 55 | + print '-- User \'', userA, '\' created successfully.' |
| 56 | + |
| 57 | +res = sdclient.create_user_invite(userB, first_name='TestUser', last_name='Beta') |
| 58 | +if res[0] is False: |
| 59 | + print '-- User creation failed:', res[1], '. Exiting.' |
| 60 | + sys.exit(1) |
| 61 | +else: |
| 62 | + print '-- User \'', userB, '\' created successfully.' |
| 63 | + |
| 64 | +# |
| 65 | +# Create test teams |
| 66 | +# |
| 67 | +# Possible failures on Team creation might include having reached the |
| 68 | +# max limit on Teams for this customer account or if the Team by that |
| 69 | +# name already exists. Since a previous successful run of this test |
| 70 | +# would have deleted the Team by the same name, and we need to be able |
| 71 | +# to configure Teams for this test to pass, we'll treat both types of |
| 72 | +# error as a genuine fail of the test. |
| 73 | +# |
| 74 | + |
| 75 | +teamA = team_prefix + 'A' |
| 76 | +teamB = team_prefix + 'B' |
| 77 | + |
| 78 | +print 'Creating test teams...' |
| 79 | + |
| 80 | +res = sdclient.create_team(teamA) |
| 81 | +if res[0] is False: |
| 82 | + print '-- Team creation failed:', res[1], '. Exiting.' |
| 83 | + sys.exit(1) |
| 84 | +else: |
| 85 | + print '-- Team \'', teamA, '\' created successfully.' |
| 86 | + |
| 87 | +res = sdclient.create_team(teamB) |
| 88 | +if res[0] is False: |
| 89 | + print '-- Team creation failed:', res[1], '. Exiting.' |
| 90 | + sys.exit(1) |
| 91 | +else: |
| 92 | + print '-- Team \'', teamB, '\' created successfully.' |
| 93 | + |
| 94 | +# |
| 95 | +# Membership manipulation |
| 96 | +# |
| 97 | +# Admins are part of all teams and their membership cannot be edited. |
| 98 | +# |
| 99 | + |
| 100 | +print 'Membership manipulation...' |
| 101 | + |
| 102 | +res = sdclient.list_memberships(teamA) |
| 103 | +if res[0] is False: |
| 104 | + print '-- Unable to fetch team memberships:', res[1], '. Exiting.' |
| 105 | + sys.exit(1) |
| 106 | +elif admin not in res[1].keys(): |
| 107 | + print '-- Admin should be part of all teams!', 'Exiting.' |
| 108 | + sys.exit(1) |
| 109 | +elif userA in res[1].keys() or userB in res[1].keys(): |
| 110 | + print '-- Users ', userA, ' and ', userB, ' should not be part of team ', teamA, '!', 'Exiting.' |
| 111 | + sys.exit(1) |
| 112 | + |
| 113 | +res = sdclient.list_memberships(teamB) |
| 114 | +if res[0] is False: |
| 115 | + print '-- Unable to fetch team memberships:', res[1], '. Exiting.' |
| 116 | + sys.exit(1) |
| 117 | +elif admin not in res[1].keys(): |
| 118 | + print '-- Admin should be part of all teams!', 'Exiting.' |
| 119 | + sys.exit(1) |
| 120 | +elif userA in res[1].keys() or userB in res[1].keys(): |
| 121 | + print '-- Users ', userA, ' and ', userB, ' should not be part of team ', teamB, '!', 'Exiting.' |
| 122 | + sys.exit(1) |
| 123 | + |
| 124 | +# |
| 125 | +# Create team memberships |
| 126 | +# |
| 127 | + |
| 128 | +print '-- Create team memberships' |
| 129 | + |
| 130 | +# Manipulate with teamA |
| 131 | + |
| 132 | +res = sdclient.save_memberships(teamA, {userA: 'ROLE_TEAM_EDIT'}) |
| 133 | +if res[0] is False: |
| 134 | + print '-- Unable to add ', userA, ' to ', teamA, ' due to: ', res[1], '. Exiting.' |
| 135 | + sys.exit(1) |
| 136 | + |
| 137 | +res = sdclient.list_memberships(teamA) |
| 138 | +if res[0] is False: |
| 139 | + print '-- Unable to fetch team memberships:', res[1], '. Exiting.' |
| 140 | + sys.exit(1) |
| 141 | +elif userA not in res[1].keys() or admin not in res[1].keys(): |
| 142 | + print '-- Users ', userA, ' and ', admin, ' should be part of team ', teamA, '!', 'Exiting.' |
| 143 | + sys.exit(1) |
| 144 | + |
| 145 | +# Manipulate with teamB |
| 146 | + |
| 147 | +res = sdclient.save_memberships(teamB, {userA: 'ROLE_TEAM_MANAGER', userB: 'ROLE_TEAM_READ'}) |
| 148 | +if res[0] is False: |
| 149 | + print '-- Unable to add ', userA, ' and ', userB, ' to ', teamB, ' due to: ', res[1], '. Exiting.' |
| 150 | + sys.exit(1) |
| 151 | + |
| 152 | +res = sdclient.list_memberships(teamB) |
| 153 | +if res[0] is False: |
| 154 | + print '-- Unable to fetch team memberships:', res[1], '. Exiting.' |
| 155 | + sys.exit(1) |
| 156 | +elif userA not in res[1].keys() or userB not in res[1].keys() or admin not in res[1].keys(): |
| 157 | + print '-- Users ', userA, ', ', userB, ' and ', admin, ' should be part of team ', teamB, '!', 'Exiting.' |
| 158 | + sys.exit(1) |
| 159 | + |
| 160 | +# Update team memberships |
| 161 | + |
| 162 | +print '-- Update team memberships' |
| 163 | + |
| 164 | +# Add new or update existing memberships |
| 165 | +res = sdclient.save_memberships(teamA, {userA: 'ROLE_TEAM_READ', userB: 'ROLE_TEAM_EDIT'}) |
| 166 | +if res[0] is False: |
| 167 | + print '-- Unable to modify membership for ', userA, ' and to add ', userB, ' to ', teamA, ' due to: ', res[1], '. Exiting.' |
| 168 | + sys.exit(1) |
| 169 | + |
| 170 | +res = sdclient.list_memberships(teamA) |
| 171 | +if res[0] is False: |
| 172 | + print '-- Unable to fetch team memberships:', res[1], '. Exiting.' |
| 173 | + sys.exit(1) |
| 174 | +elif userA not in res[1].keys() or userB not in res[1].keys() or admin not in res[1].keys(): |
| 175 | + print '-- Users ', userA, ', ', userB, ' and ', admin, ' should be part of team ', teamA, '!', 'Exiting.' |
| 176 | + sys.exit(1) |
| 177 | +elif res[1][userA] != 'ROLE_TEAM_READ' or res[1][userB] != 'ROLE_TEAM_EDIT': |
| 178 | + print '-- Users ', userA, ' and ', userB, ' should have appropriate roles assigned for team ', teamA, '!', 'Exiting.' |
| 179 | + sys.exit(1) |
| 180 | + |
| 181 | +# Remove team memberships |
| 182 | + |
| 183 | +print '-- Remove team memberships' |
| 184 | + |
| 185 | +res = sdclient.remove_memberships(teamA, [userB]) |
| 186 | +if res[0] is False: |
| 187 | + print '-- Unable to remove membership for ', userB, ' from team', teamA, ' due to: ', res[1], '. Exiting.' |
| 188 | + sys.exit(1) |
| 189 | + |
| 190 | +res = sdclient.list_memberships(teamA) |
| 191 | +if res[0] is False: |
| 192 | + print '-- Unable to fetch team memberships:', res[1], '. Exiting.' |
| 193 | + sys.exit(1) |
| 194 | +elif userB in res[1].keys(): |
| 195 | + print '-- User ', userB, ' should not be part of team ', teamA, '!', 'Exiting.' |
| 196 | + sys.exit(1) |
| 197 | + |
| 198 | +# Admin user cannot be removed from any team |
| 199 | +res = sdclient.remove_memberships(teamB, [admin, userA]) |
| 200 | +if res[0] is False: |
| 201 | + print '-- Unable to remove membership for ', userB, ' from team', teamA, ' due to: ', res[1], '. Exiting.' |
| 202 | + sys.exit(1) |
| 203 | + |
| 204 | +res = sdclient.list_memberships(teamB) |
| 205 | +if res[0] is False: |
| 206 | + print '-- Unable to fetch team memberships:', res[1], '. Exiting.' |
| 207 | + sys.exit(1) |
| 208 | +elif userA in res[1].keys(): |
| 209 | + print '-- User ', userA, ' should not be part of team ', teamB, '!', 'Exiting.' |
| 210 | + sys.exit(1) |
| 211 | +elif admin not in res[1].keys(): |
| 212 | + print '-- User ', admin, ' should be always part of all teams!', 'Exiting.' |
| 213 | + sys.exit(1) |
| 214 | + |
| 215 | +# |
| 216 | +# Clean-up |
| 217 | +# |
| 218 | + |
| 219 | +print 'Cleaning up...' |
| 220 | + |
| 221 | +print '-- Deleting test teams.' |
| 222 | + |
| 223 | +res = sdclient.delete_team(teamA) |
| 224 | +if res[0] is False: |
| 225 | + print '-- Team \'', teamA, '\' deletion failed: ', res[1] |
| 226 | + |
| 227 | +res = sdclient.delete_team(teamB) |
| 228 | +if res[0] is False: |
| 229 | + print '-- Team \'', teamB, '\' deletion failed: ', res[1] |
| 230 | + |
| 231 | + |
| 232 | +print '-- Deleting test users.' |
| 233 | + |
| 234 | +res = sdclient.delete_user(admin) |
| 235 | +if res[0] is False: |
| 236 | + print '-- User \'', admin, '\' deletion failed: ', res[1] |
| 237 | + |
| 238 | +res = sdclient.delete_user(userA) |
| 239 | +if res[0] is False: |
| 240 | + print '-- User \'', userA, '\' deletion failed: ', res[1] |
| 241 | + |
| 242 | +res = sdclient.delete_user(userB) |
| 243 | +if res[0] is False: |
| 244 | + print '-- User \'', userB, '\' deletion failed: ', res[1] |
| 245 | + |
| 246 | + |
| 247 | +print 'All done successfully!!!' |
| 248 | + |
| 249 | +sys.exit(0) |
0 commit comments