| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
from .redfishtoolTransport import RfTransport |
| 31 |
import requests |
| 32 |
import json |
| 33 |
import getopt |
| 34 |
import re |
| 35 |
import sys |
| 36 |
from .ServiceRoot import RfServiceRoot |
| 37 |
from urllib.parse import urljoin |
| 38 |
|
| 39 |
class RfAccountServiceMain(): |
| 40 |
def __init__(self): |
| 41 |
|
| 42 |
self.operation=None |
| 43 |
self.args=None |
| 44 |
self.argnum=0 |
| 45 |
self.nonIdCommands=None |
| 46 |
|
| 47 |
def displayUsage(self,rft): |
| 48 |
if(rft.quiet): return(0) |
| 49 |
print(" Usage:") |
| 50 |
print(" {} [OPTNS] AccountService <operation> [<args>] -- perform <operation> on the AccountService ".format(rft.program)) |
| 51 |
|
| 52 |
def displayHelp(self,rft): |
| 53 |
self.displayUsage(rft) |
| 54 |
self.displayOperations(rft) |
| 55 |
print("") |
| 56 |
|
| 57 |
def displayOperations(self,rft): |
| 58 |
print(" <operations>:") |
| 59 |
print(" [get] -- get the AccountService object. ") |
| 60 |
print(" patch {A: B,C: D,...} -- patch the AccountService w/ json-formatted {prop: value...} ") |
| 61 |
print(" Accounts [list] -- get the \"Accounts\" collection, or list \"Id\", username, and Url ") |
| 62 |
print(" Accounts [IDOPTN] -- get the member specified by IDOPTN: -i<Id>, -m<prop>:<val>, -l<link>, -a #all") |
| 63 |
print(" Roles [list] -- get the \"Roles\" collection, or list \"Id\", IsPredefined, and Url ") |
| 64 |
print(" Roles [IDOPTN] -- get the member specified by IDOPTN: -i<Id>, -m<prop>:<val>, -l<link>, -a #all") |
| 65 |
print(" adduser <usernm> <passwd> [<roleId>] -- add a new user to the Accounts collection") |
| 66 |
print(" -- <roleId>:{Administrator | Operator | ReadOnlyUser | <a custom roleId}, dflt=Operator") |
| 67 |
print(" deleteuser <usernm> -- delete an existing user from Accouts collection") |
| 68 |
print(" setpassword <usernm> <passwd> -- set (change) the password of an existing user account") |
| 69 |
print(" useradmin <userName> [enable|disable|unlock|[setRoleId <roleId>]] -- enable|disable|unlock.. a user account") |
| 70 |
print(" setusername <id> <userName> -- set UserName for account with given Id") |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
print(" examples -- example commands with syntax") |
| 75 |
print(" hello -- AccountService hello -- debug command") |
| 76 |
return(0) |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
def runOperation(self,rft): |
| 86 |
|
| 87 |
op=RfAccountServiceOperations() |
| 88 |
|
| 89 |
|
| 90 |
operationTable = { |
| 91 |
"get": op.get, |
| 92 |
"patch": op.patch, |
| 93 |
"Accounts": op.getAccounts, |
| 94 |
"Roles": op.getRoles, |
| 95 |
"adduser": op.addUser, |
| 96 |
"deleteuser": op.deleteUser, |
| 97 |
"setpassword": op.setPassword, |
| 98 |
"useradmin": op.userAdmin, |
| 99 |
"setusername": op.setUsername, |
| 100 |
|
| 101 |
|
| 102 |
"hello": op.hello, |
| 103 |
"examples": op.examples |
| 104 |
} |
| 105 |
|
| 106 |
rft.printVerbose(5,"AccountService:runOperation: operation: {}".format(self.operation)) |
| 107 |
rft.printVerbose(5,"AccountService:runOperation: args: {}".format(self.args)) |
| 108 |
|
| 109 |
if self.operation in operationTable: |
| 110 |
rft.printVerbose(5,"AccountService:runOperation: found Oper: {} in table. executing".format(rft.subcommand)) |
| 111 |
rc,r,j,d=operationTable[self.operation](self, op, rft, cmdTop=True) |
| 112 |
return(rc,r,j,d) |
| 113 |
|
| 114 |
else: |
| 115 |
rft.printErr("AccountService: Invalid operation: {}".format(self.operation)) |
| 116 |
return(2,None,False,None) |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
def AccountServiceMain(self,rft,cmdTop=False): |
| 121 |
rft.printVerbose(4,"AccountServiceMain: subcommand: {}".format(rft.subcommand)) |
| 122 |
|
| 123 |
if( rft.help ): |
| 124 |
self.displayHelp(rft) |
| 125 |
return(0,None,False,None) |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
args=rft.subcommandArgv[0:] |
| 131 |
|
| 132 |
|
| 133 |
if( len(args) < 2 ): |
| 134 |
self.operation="get" |
| 135 |
self.args= None |
| 136 |
else: |
| 137 |
self.operation=args[1] |
| 138 |
self.args = args[1:] |
| 139 |
self.argnum =len(self.args) |
| 140 |
|
| 141 |
rft.printVerbose(5,"AccountService: operation={}, args={}".format(self.operation,self.args)) |
| 142 |
|
| 143 |
|
| 144 |
rc,r,j,d = self.runOperation(rft) |
| 145 |
|
| 146 |
if(rc !=0 ): |
| 147 |
rft.printVerbose(5,"AccountService: operation returned with error: rc={}".format(rc)) |
| 148 |
return(rc,r,False,None) |
| 149 |
|
| 150 |
|
| 151 |
rft.printVerbose(5,"AccountService: operation exited OK") |
| 152 |
return(rc,r,j,d) |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
class RfAccountServiceOperations(): |
| 159 |
def __init__(self): |
| 160 |
self.AccountServicePath=None |
| 161 |
self.AccountServiceCollectionDict=None |
| 162 |
|
| 163 |
|
| 164 |
def hello(self,sc,op,rft,cmdTop=False): |
| 165 |
rft.printVerbose(4,"in hello") |
| 166 |
rft.printVerbose(4," subcmd:{}, operation:{}, args:{}".format(rft.subcommand,sc.operation,sc.args)) |
| 167 |
print("hello world from AccountService") |
| 168 |
return(0,None,False,None) |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
def get(self,sc,op,rft, cmdTop=False, prop=None): |
| 173 |
rft.printVerbose(4,"{}:{}: in operation".format(rft.subcommand,sc.operation)) |
| 174 |
|
| 175 |
|
| 176 |
svcRoot=RfServiceRoot() |
| 177 |
rc,r,j,d = svcRoot.getServiceRoot(rft) |
| 178 |
if( rc != 0 ): |
| 179 |
rft.printErr("get AccountService: Error getting service root, aborting") |
| 180 |
return(rc,r,False,None) |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
if (("AccountService" in d) and ("@odata.id" in d["AccountService"])): |
| 185 |
accountServiceLink=d["AccountService"]["@odata.id"] |
| 186 |
else: |
| 187 |
rft.printErr("Error: root does not have a AccountService link") |
| 188 |
return(4) |
| 189 |
|
| 190 |
rft.printVerbose(4,"AccountService: get AccountService: link is: {}".format(accountServiceLink)) |
| 191 |
|
| 192 |
if cmdTop is True: prop=rft.prop |
| 193 |
|
| 194 |
|
| 195 |
rc,r,j,d=rft.rftSendRecvRequest(rft.AUTHENTICATED_API, 'GET', r.url, relPath=accountServiceLink, prop=prop) |
| 196 |
|
| 197 |
if(rc==0): rft.printVerbose(1," AccountService Resource:",skip1=True, printV12=cmdTop) |
| 198 |
return(rc,r,j,d) |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
def patch(self,sc,op,rft,cmdTop=False, prop=None, patchData=None, r=None): |
| 203 |
rft.printVerbose(4,"{}:{}: in operation".format(rft.subcommand,sc.operation)) |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
if( len( sc.args) == 2): |
| 208 |
|
| 209 |
try: |
| 210 |
patchData=json.loads(sc.args[1]) |
| 211 |
except ValueError: |
| 212 |
rft.printErr("Patch: invalid Json input data:{}".format(sc.args[1])) |
| 213 |
return(5,None,False,None) |
| 214 |
|
| 215 |
else: |
| 216 |
rft.printErr("Patch: error: invalid argument format") |
| 217 |
rft.printErr(" : args={}".format(sc.args)) |
| 218 |
rft.printErr(" : expect: SessionService patch \"{ <prop>: <value> }\"") |
| 219 |
return(4,None,False,None) |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
rc,r,j,d=op.get(sc,op,rft) |
| 226 |
if( rc != 0): |
| 227 |
return(rc,r,False,None) |
| 228 |
|
| 229 |
|
| 230 |
rc,r,j,d=rft.patchResource(rft, r, patchData) |
| 231 |
|
| 232 |
if(rc==0): rft.printVerbose(1," AccountService Patch:",skip1=True, printV12=cmdTop) |
| 233 |
return(rc,r,j,d) |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
def getAccounts(self,sc,op, rft, cmdTop=False, prop=None): |
| 239 |
rft.printVerbose(4,"{}:{}: in operation: getAccounts collection".format(rft.subcommand,sc.operation)) |
| 240 |
|
| 241 |
|
| 242 |
rc,r,j,d=op.get(sc,op, rft) |
| 243 |
if( rc != 0): return(rc,r,False,None) |
| 244 |
|
| 245 |
collName="Accounts" |
| 246 |
|
| 247 |
if ((collName in d) and ("@odata.id" in d[collName])): |
| 248 |
accountsLink=d[collName]["@odata.id"] |
| 249 |
else: |
| 250 |
rft.printErr("Error: AccountsService resource does not have a {} link".format(collName)) |
| 251 |
return(6,None,False,None) |
| 252 |
|
| 253 |
if cmdTop is True: prop=rft.prop |
| 254 |
|
| 255 |
|
| 256 |
if( sc.argnum > 1 and sc.args[1] == 'list' ): |
| 257 |
|
| 258 |
rc,r,j,d=rft.rftSendRecvRequest(rft.AUTHENTICATED_API, 'GET', r.url, relPath=accountsLink) |
| 259 |
|
| 260 |
rc,r,j,d=rft.listCollection(rft, r, d, prop="UserName") |
| 261 |
if(rc==0): |
| 262 |
rft.printVerbose(1," list {} Collection member info: Id, URI, UserName".format(collName,skip1=True, printV12=cmdTop)) |
| 263 |
|
| 264 |
|
| 265 |
elif(rft.IdLevel2OptnCount==0): |
| 266 |
rc,r,j,d=rft.rftSendRecvRequest(rft.AUTHENTICATED_API, 'GET', r.url, relPath=accountsLink, prop=prop) |
| 267 |
if(rc==0): |
| 268 |
rft.printVerbose(1," {} Collection ".format(collName,skip1=True, printV12=cmdTop)) |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
elif( rft.allOptn is not True ): |
| 273 |
|
| 274 |
rc,r,j,d=rft.rftSendRecvRequest(rft.AUTHENTICATED_API, 'GET', r.url, relPath=accountsLink, prop=prop) |
| 275 |
collUrl=r.url |
| 276 |
|
| 277 |
|
| 278 |
path2,rc,r,j,d=rft.getLevel2ResourceById(rft,r,d) |
| 279 |
if(rc!=0): |
| 280 |
return(rc,r,j,d) |
| 281 |
|
| 282 |
|
| 283 |
if( (r is not None) and (prop is not None) ): |
| 284 |
rc,r,j,d=rft.getPropFromDict(rft,r,d,prop) |
| 285 |
|
| 286 |
|
| 287 |
elif( r is None ): |
| 288 |
rc,r,j,d=rft.rftSendRecvRequest(rft.AUTHENTICATED_API, 'GET', collUrl, relPath=path2, prop=prop) |
| 289 |
if(rc==0): |
| 290 |
rft.printVerbose(1," {} Collection Member ".format(collName,skip1=True, printV12=cmdTop)) |
| 291 |
|
| 292 |
|
| 293 |
else: |
| 294 |
rft.printVerbose(4,"getting expanded Accounts Collection") |
| 295 |
rc,r,j,d=rft.getAllCollectionMembers(rft, r.url, relPath=accountsLink) |
| 296 |
if(rc==0): |
| 297 |
rft.printVerbose(1," Get ALL {} Collection Members".format(collName,skip1=True, printV12=cmdTop)) |
| 298 |
|
| 299 |
return(rc,r,j,d) |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
def getRoles(self,sc,op, rft, cmdTop=False, prop=None): |
| 304 |
rft.printVerbose(4,"{}:{}: in operation: getRoles collection".format(rft.subcommand,sc.operation)) |
| 305 |
|
| 306 |
|
| 307 |
rc,r,j,d=op.get(sc,op, rft) |
| 308 |
if( rc != 0): return(rc,r,False,None) |
| 309 |
|
| 310 |
collName="Roles" |
| 311 |
|
| 312 |
if ((collName in d) and ("@odata.id" in d[collName])): |
| 313 |
rolesLink=d[collName]["@odata.id"] |
| 314 |
else: |
| 315 |
rft.printErr("Error: AccountsService resource does not have a {} link".format(collName)) |
| 316 |
return(6,None,False,None) |
| 317 |
|
| 318 |
if cmdTop is True: prop=rft.prop |
| 319 |
|
| 320 |
|
| 321 |
if( sc.argnum > 1 and sc.args[1] == 'list' ): |
| 322 |
|
| 323 |
rc,r,j,d=rft.rftSendRecvRequest(rft.AUTHENTICATED_API, 'GET', r.url, relPath=rolesLink) |
| 324 |
|
| 325 |
rc,r,j,d=rft.listCollection(rft, r, d, prop="IsPredefined") |
| 326 |
if(rc==0): |
| 327 |
rft.printVerbose(1," list {} Collection member info: Id, URI, IsPredefined".format(collName,skip1=True, printV12=cmdTop)) |
| 328 |
|
| 329 |
|
| 330 |
elif(rft.IdLevel2OptnCount==0): |
| 331 |
rc,r,j,d=rft.rftSendRecvRequest(rft.AUTHENTICATED_API, 'GET', r.url, relPath=rolesLink, prop=prop) |
| 332 |
if(rc==0): |
| 333 |
rft.printVerbose(1," {} Collection ".format(collName,skip1=True, printV12=cmdTop)) |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
elif( rft.allOptn is not True ): |
| 338 |
|
| 339 |
rc,r,j,d=rft.rftSendRecvRequest(rft.AUTHENTICATED_API, 'GET', r.url, relPath=rolesLink, prop=prop) |
| 340 |
collUrl=r.url |
| 341 |
|
| 342 |
|
| 343 |
path2,rc,r,j,d=rft.getLevel2ResourceById(rft,r,d) |
| 344 |
if(rc!=0): |
| 345 |
return(rc,r,j,d) |
| 346 |
|
| 347 |
|
| 348 |
if( (r is not None) and (prop is not None) ): |
| 349 |
rc,r,j,d=rft.getPropFromDict(rft,r,d,prop) |
| 350 |
|
| 351 |
|
| 352 |
elif( r is None ): |
| 353 |
rc,r,j,d=rft.rftSendRecvRequest(rft.AUTHENTICATED_API, 'GET', collUrl, relPath=path2, prop=prop) |
| 354 |
if(rc==0): |
| 355 |
rft.printVerbose(1," {} Collection Member ".format(collName,skip1=True, printV12=cmdTop)) |
| 356 |
|
| 357 |
|
| 358 |
else: |
| 359 |
rft.printVerbose(4,"getting expanded Roles Collection") |
| 360 |
rc,r,j,d=rft.getAllCollectionMembers(rft, r.url, relPath=rolesLink) |
| 361 |
if(rc==0): |
| 362 |
rft.printVerbose(1," Get ALL {} Collection Members".format(collName,skip1=True, printV12=cmdTop)) |
| 363 |
|
| 364 |
return(rc,r,j,d) |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
def addUser(self,sc,op,rft,cmdTop=False, prop=None): |
| 371 |
rft.printVerbose(4,"{}:{}: in operation".format(rft.subcommand,sc.operation)) |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
if(len(sc.args) < 3 ): |
| 383 |
rft.printErr("Error, adduser: invalid number of arguments") |
| 384 |
rft.printErr("Syntax: {} AccountService adduser <username> <passwd> [<roleId>]".format(rft.program)) |
| 385 |
rft.printErr(" default <roleId> is Operator") |
| 386 |
return(8,None,False,None) |
| 387 |
|
| 388 |
|
| 389 |
userName=sc.args[1] |
| 390 |
password=sc.args[2] |
| 391 |
|
| 392 |
|
| 393 |
if(len(sc.args) > 3 ): |
| 394 |
roleVal=sc.args[3] |
| 395 |
else: |
| 396 |
roleVal="Operator" |
| 397 |
|
| 398 |
|
| 399 |
rc,r,j,d=op.getAccounts(sc,op, rft) |
| 400 |
if( rc != 0): return(rc,r,False,None) |
| 401 |
accountsUrl=r.url |
| 402 |
|
| 403 |
|
| 404 |
rft.gotMatchLevel2Optn=True |
| 405 |
rft.matchLevel2Prop="UserName" |
| 406 |
rft.matchLevel2Value=userName |
| 407 |
path,rc,r,j,d=rft.getLevel2ResourceById(rft,r,d) |
| 408 |
if( rc == 0): |
| 409 |
rft.printErr("Error: username {} already exists".format(userName)) |
| 410 |
return(9,None,False,None) |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
rc,r,j,d=op.getRoles(sc,op, rft) |
| 415 |
if( rc != 0): return(rc,r,False,None) |
| 416 |
|
| 417 |
rft.gotMatchLevel2Optn=True |
| 418 |
rft.matchLevel2Prop="Id" |
| 419 |
rft.matchLevel2Value=roleVal |
| 420 |
path,rc,r,j,d=rft.getLevel2ResourceById(rft,r,d) |
| 421 |
if( rc != 0): |
| 422 |
rft.printErr("Error: roleId: {} is not supported on remote service".format(roleVal)) |
| 423 |
return(rc,None,False,None) |
| 424 |
|
| 425 |
|
| 426 |
postData={"UserName": userName, "Password": password, "RoleId": roleVal} |
| 427 |
|
| 428 |
|
| 429 |
reqPostData=json.dumps(postData) |
| 430 |
rc,r,j,d=rft.rftSendRecvRequest(rft.AUTHENTICATED_API, 'POST', accountsUrl, reqData=reqPostData) |
| 431 |
if(rc==0): |
| 432 |
rft.printVerbose(1," Add User: {} (POST to Accounts collection): ".format(userName), skip1=True, printV12=cmdTop) |
| 433 |
respData=d |
| 434 |
return(rc,r,j,respData) |
| 435 |
else: return(rc,r,False,None) |
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
def deleteUser(self,sc,op,rft,cmdTop=False, prop=None): |
| 442 |
rft.printVerbose(4,"{}:{}: in operation".format(rft.subcommand,sc.operation)) |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
if(len(sc.args) < 2 ): |
| 451 |
rft.printErr("Error, deleteuser: invalid number of arguments") |
| 452 |
rft.printErr("Syntax: {} AccountService deleteuser <username>".format(rft.program)) |
| 453 |
return(8,None,False,None) |
| 454 |
|
| 455 |
|
| 456 |
userName=sc.args[1] |
| 457 |
|
| 458 |
|
| 459 |
rc,r,j,d=op.getAccounts(sc,op, rft) |
| 460 |
if( rc != 0): return(rc,r,False,None) |
| 461 |
accountsUrl=r.url |
| 462 |
|
| 463 |
|
| 464 |
rft.gotMatchLevel2Optn=True |
| 465 |
rft.matchLevel2Prop="UserName" |
| 466 |
rft.matchLevel2Value=userName |
| 467 |
accountPath,rc,r,j,d=rft.getLevel2ResourceById(rft,r,d) |
| 468 |
if( rc != 0): |
| 469 |
rft.printErr("Error: username {} does not exists".format(userName)) |
| 470 |
return(9,None,False,None) |
| 471 |
|
| 472 |
|
| 473 |
rc,r,j,d=rft.rftSendRecvRequest(rft.AUTHENTICATED_API, 'DELETE', accountsUrl, relPath=accountPath) |
| 474 |
if(rc==0): |
| 475 |
rft.printVerbose(1," Delete User: {} (DELETE to Account entry): ".format(userName), skip1=True, printV12=cmdTop) |
| 476 |
return(rc,r,False,None) |
| 477 |
else: return(rc,r,False,None) |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
def userAdmin(self,sc,op,rft,cmdTop=False, prop=None): |
| 484 |
rft.printVerbose(4,"{}:{}: in operation".format(rft.subcommand,sc.operation)) |
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
if(len(sc.args) < 3 ): |
| 496 |
rft.printErr("Error, userAdmin: invalid number of arguments") |
| 497 |
rc=self.userAdminUsage(rft) |
| 498 |
return(rc,None,False,None) |
| 499 |
|
| 500 |
|
| 501 |
userName=sc.args[1] |
| 502 |
action=sc.args[2] |
| 503 |
roleVal="" |
| 504 |
|
| 505 |
|
| 506 |
if( action=="enable"): |
| 507 |
prop="Enabled" |
| 508 |
patchData={prop: True} |
| 509 |
elif( action=="disable"): |
| 510 |
prop="Enabled" |
| 511 |
patchData={prop: False} |
| 512 |
elif( action=="unlock"): |
| 513 |
prop="Locked" |
| 514 |
patchData={prop: False} |
| 515 |
elif( action=="setRoleId"): |
| 516 |
if(len(sc.args) < 4): |
| 517 |
rft.printErr("Error, userAdmin setRoleId: no <roleId> argument") |
| 518 |
rc=self.userAdminUsage(rft) |
| 519 |
return(rc,None,False,None) |
| 520 |
else: |
| 521 |
prop="RoleId" |
| 522 |
roleVal=sc.args[3] |
| 523 |
patchData={prop: roleVal} |
| 524 |
else: |
| 525 |
rft.printErr("Error: userAdmin: specified <action> not valid") |
| 526 |
rc=self.userAdminUsage(rft) |
| 527 |
return(rc,None,False,None) |
| 528 |
|
| 529 |
|
| 530 |
rc,r,j,d=op.getAccounts(sc,op, rft) |
| 531 |
if( rc != 0): return(rc,r,False,None) |
| 532 |
|
| 533 |
|
| 534 |
rft.gotMatchLevel2Optn=True |
| 535 |
rft.matchLevel2Prop="UserName" |
| 536 |
rft.matchLevel2Value=userName |
| 537 |
path,rc,r,j,d=rft.getLevel2ResourceById(rft,r,d) |
| 538 |
if( rc != 0): |
| 539 |
rft.printErr("Error: username {} is not valid user".format(userName)) |
| 540 |
return(rc,None,False,None) |
| 541 |
accountResponse=r |
| 542 |
|
| 543 |
|
| 544 |
if( not prop in d ): |
| 545 |
rft.printErr("Error: userAdmin: property {} not in the remote service user account resource".format(prop)) |
| 546 |
return(8,None,False,None) |
| 547 |
|
| 548 |
|
| 549 |
if( action=="setRoleId"): |
| 550 |
|
| 551 |
rc,r,j,d=op.getRoles(sc,op, rft) |
| 552 |
if( rc != 0): return(rc,r,False,None) |
| 553 |
|
| 554 |
rft.gotMatchLevel2Optn=True |
| 555 |
rft.matchLevel2Prop="Id" |
| 556 |
rft.matchLevel2Value=roleVal |
| 557 |
path,rc,r,j,d=rft.getLevel2ResourceById(rft,r,d) |
| 558 |
if( rc != 0): |
| 559 |
rft.printErr("Error: roleId: {} is not supported on remote service".format(roleVal)) |
| 560 |
return(rc,None,False,None) |
| 561 |
|
| 562 |
|
| 563 |
rc,r,j,d=rft.patchResource(rft, accountResponse, patchData) |
| 564 |
if(rc==0): |
| 565 |
rft.printVerbose(1," AccountService userAdmin {} {} :".format(action,roleVal),skip1=True, printV12=cmdTop) |
| 566 |
respData={prop: d[prop]} |
| 567 |
return(rc,r,j,respData) |
| 568 |
else: return(rc,r,False,None) |
| 569 |
|
| 570 |
def userAdminUsage(self,rft): |
| 571 |
rft.printErr("Syntax: {} useradmin <username> {{enable|disable|unlock|{{setRoleId <roleId>}} }}".format(rft.program)) |
| 572 |
return(8) |
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
def setUsername(self, sc, op, rft, cmdTop=False, prop=None): |
| 577 |
rft.printVerbose(4, "{}:{}: in operation".format(rft.subcommand, sc.operation)) |
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
if (len(sc.args) != 3): |
| 589 |
rft.printErr("Error, setusername: invalid number of arguments") |
| 590 |
rc = self.setUsernameUsage(rft) |
| 591 |
return (rc, None, False, None) |
| 592 |
|
| 593 |
|
| 594 |
idVal = sc.args[1] |
| 595 |
userName = sc.args[2] |
| 596 |
|
| 597 |
|
| 598 |
prop = "UserName" |
| 599 |
patchData = {prop: userName} |
| 600 |
|
| 601 |
|
| 602 |
rc, r, j, d = op.getAccounts(sc, op, rft) |
| 603 |
if (rc != 0): return (rc, r, False, None) |
| 604 |
|
| 605 |
|
| 606 |
rft.gotMatchLevel2Optn = True |
| 607 |
rft.matchLevel2Prop = "Id" |
| 608 |
rft.matchLevel2Value = idVal |
| 609 |
path, rc, r, j, d = rft.getLevel2ResourceById(rft, r, |
| 610 |
d) |
| 611 |
if (rc != 0): |
| 612 |
rft.printErr("Error: Id {} is not a valid account Id".format(idVal)) |
| 613 |
return (rc, None, False, None) |
| 614 |
accountResponse = r |
| 615 |
|
| 616 |
|
| 617 |
if (not prop in d): |
| 618 |
rft.printErr("Error: setusername: property {} not in the remote service user account resource".format(prop)) |
| 619 |
return (8, None, False, None) |
| 620 |
|
| 621 |
|
| 622 |
rc, r, j, d = rft.patchResource(rft, accountResponse, patchData) |
| 623 |
if (rc == 0): |
| 624 |
rft.printVerbose(1, " AccountService setusername {} {} :".format(idVal, userName), skip1=True, |
| 625 |
printV12=cmdTop) |
| 626 |
respData = {prop: d[prop]} |
| 627 |
return (rc, r, j, respData) |
| 628 |
else: |
| 629 |
return (rc, r, False, None) |
| 630 |
|
| 631 |
def setUsernameUsage(self, rft): |
| 632 |
rft.printErr( |
| 633 |
"Syntax: {} setusername <id> <username>".format(rft.program)) |
| 634 |
return (8) |
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
def setPassword(self,sc,op,rft,cmdTop=False, prop=None): |
| 639 |
rft.printVerbose(4,"{}:{}: in operation".format(rft.subcommand,sc.operation)) |
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
if(len(sc.args) < 3 ): |
| 649 |
rft.printErr("Error, setPassword: invalid number of arguments") |
| 650 |
rft.printErr("Syntax: {} AccountService setPassword setpassword <username> <password>".format(rft.program)) |
| 651 |
return(8,None,False,None) |
| 652 |
|
| 653 |
|
| 654 |
prop="Password" |
| 655 |
userName=sc.args[1] |
| 656 |
password=sc.args[2] |
| 657 |
patchData={prop: password} |
| 658 |
|
| 659 |
|
| 660 |
rc,r,j,d=op.getAccounts(sc,op, rft) |
| 661 |
if( rc != 0): return(rc,r,False,None) |
| 662 |
|
| 663 |
|
| 664 |
rft.gotMatchLevel2Optn=True |
| 665 |
rft.matchLevel2Prop="UserName" |
| 666 |
rft.matchLevel2Value=userName |
| 667 |
path,rc,r,j,d=rft.getLevel2ResourceById(rft,r,d) |
| 668 |
if( rc != 0): |
| 669 |
rft.printErr("Error: username {} is not valid user".format(userName)) |
| 670 |
return(rc,None,False,None) |
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
rc,r,j,d=rft.patchResource(rft, r, patchData, getResponseAfterPatch=False ) |
| 676 |
if(rc==0): |
| 677 |
rft.printVerbose(1," AccountService setPassword: successful",skip1=True, printV12=cmdTop) |
| 678 |
respData=d |
| 679 |
return(rc,r,j,respData) |
| 680 |
else: return(rc,r,False,None) |
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
def examples(self,sc,op,rft,cmdTop=False,prop=None): |
| 699 |
rft.printVerbose(4,"{}:{}: in operation".format(rft.subcommand,sc.operation)) |
| 700 |
print(" {} -r<ip> AccountService # gets the AccountService".format(rft.program)) |
| 701 |
print(" {} -r<ip> AccountService patch {{ \"AccountLockoutThreshold\": 5 }} ]# set failed login lockout threshold".format(rft.program)) |
| 702 |
print(" {} -r<ip> AccountService Accounts # gets Accounts collection".format(rft.program)) |
| 703 |
print(" {} -r<ip> AccountService Accounts list # list Accounts to get Id, username, url for each account".format(rft.program)) |
| 704 |
print(" {} -r<ip> AccountService Accounts -mUserName:john # gets the Accounts member with username: john".format(rft.program)) |
| 705 |
print(" {} -r<ip> AccountService Roles list # list Roles collection to get RoleId, IsPredefined, & url for each role".format(rft.program)) |
| 706 |
print(" {} -r<ip> AccountService Roles -iAdministrator # gets the Roles member with RoleId=Administrator".format(rft.program)) |
| 707 |
print(" {} -r<ip> AccountService adduser john 12345 Administrator # add new user (john) w/ passwd \"12345\" and role: Administrator".format(rft.program)) |
| 708 |
print(" {} -r<ip> AccountService deleteuser john # delete user \"john\"s account".format(rft.program)) |
| 709 |
print(" {} -r<ip> AccountService useradmin john disable # disable user \"john\"s account".format(rft.program)) |
| 710 |
print(" {} -r<ip> AccountService useradmin john unlock # unlock user \"john\"s account".format(rft.program)) |
| 711 |
print(" {} -r<ip> AccountService setusername 3 alice # set username for account with id=3 to \"alice\"".format(rft.program)) |
| 712 |
return(0,None,False,None) |
| 713 |
|
| 714 |
|
| 715 |
''' |
| 716 |
TODO: |
| 717 |
1. add "addRole" and "deleteRole" commands |
| 718 |
2. |
| 719 |
|
| 720 |
|
| 721 |
''' |
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|