| 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 |
from .redfishtoolTransport import RfTransport |
| 27 |
import requests |
| 28 |
import json |
| 29 |
import getopt |
| 30 |
import re |
| 31 |
import sys |
| 32 |
from .ServiceRoot import RfServiceRoot |
| 33 |
from urllib.parse import urljoin |
| 34 |
|
| 35 |
class RfSessionServiceMain(): |
| 36 |
def __init__(self): |
| 37 |
|
| 38 |
self.operation=None |
| 39 |
self.args=None |
| 40 |
self.argnum=0 |
| 41 |
self.nonIdCommands=None |
| 42 |
|
| 43 |
def displayUsage(self,rft): |
| 44 |
if(rft.quiet): return(0) |
| 45 |
print(" Usage:") |
| 46 |
print(" {} [OPTNS] SessionService <operation> [<args>] -- perform <operation> on the SessionService ".format(rft.program)) |
| 47 |
|
| 48 |
def displayHelp(self,rft): |
| 49 |
self.displayUsage(rft) |
| 50 |
self.displayOperations(rft) |
| 51 |
print("") |
| 52 |
|
| 53 |
def displayOperations(self,rft): |
| 54 |
print(" <operations>:") |
| 55 |
print(" [get] -- get the sessionService object. ") |
| 56 |
print(" patch {A: B,C: D,...} -- patch the sessionService w/ json-formatted {prop: value...} ") |
| 57 |
print(" setSessionTimeout <timeout> -- patches the SessionTimeout property w/ etag support ") |
| 58 |
print(" Sessions [list] -- get the \"Sessions\" collection, or list \"Id\", username, and Url ") |
| 59 |
print(" Sessions [IDOPTN] -- get the member specified by IDOPTN: -i<Id>, -m<prop>:<val>, -l<link>, -a #all") |
| 60 |
print(" login -- sessionLogin. post to Sessions collection to create a session") |
| 61 |
print(" the user is -u<user>, password is -p<password>") |
| 62 |
print(" logout -- logout or delete the session by identified by -i<SessionId> or -l<link>") |
| 63 |
print(" where <link> is the session path returned in Location from login") |
| 64 |
print(" examples -- example commands with syntax") |
| 65 |
print(" hello -- Systems hello -- debug command") |
| 66 |
return(0) |
| 67 |
|
| 68 |
|
| 69 |
def runOperation(self,rft): |
| 70 |
|
| 71 |
op=RfSessionServiceOperations() |
| 72 |
|
| 73 |
|
| 74 |
operationTable = { |
| 75 |
"get": op.get, |
| 76 |
"patch": op.patch, |
| 77 |
"setSessionTimeout": op.setSessionTimeout, |
| 78 |
"Sessions": op.getSessions, |
| 79 |
"login": op.sessionLogin, |
| 80 |
"logout": op.sessionLogout, |
| 81 |
"hello": op.hello, |
| 82 |
"examples": op.examples |
| 83 |
} |
| 84 |
|
| 85 |
rft.printVerbose(5,"SessionService:runOperation: operation: {}".format(self.operation)) |
| 86 |
rft.printVerbose(5,"SessionService:runOperation: args: {}".format(self.args)) |
| 87 |
|
| 88 |
if self.operation in operationTable: |
| 89 |
rft.printVerbose(5,"SessionService:runOperation: found Oper: {} in table. executing".format(rft.subcommand)) |
| 90 |
rc,r,j,d=operationTable[self.operation](self, op, rft, cmdTop=True) |
| 91 |
return(rc,r,j,d) |
| 92 |
|
| 93 |
else: |
| 94 |
rft.printErr("SessionService: Invalid operation: {}".format(self.operation)) |
| 95 |
return(2,None,False,None) |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
def SessionServiceMain(self,rft,cmdTop=False): |
| 100 |
rft.printVerbose(4,"SessionServiceMain: subcommand: {}".format(rft.subcommand)) |
| 101 |
|
| 102 |
if( rft.help ): |
| 103 |
self.displayHelp(rft) |
| 104 |
return(0,None,False,None) |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
args=rft.subcommandArgv[0:] |
| 110 |
|
| 111 |
|
| 112 |
if( len(args) < 2 ): |
| 113 |
self.operation="get" |
| 114 |
self.args= None |
| 115 |
else: |
| 116 |
self.operation=args[1] |
| 117 |
self.args = args[1:] |
| 118 |
self.argnum =len(self.args) |
| 119 |
|
| 120 |
rft.printVerbose(5,"SessionService: operation={}, args={}".format(self.operation,self.args)) |
| 121 |
|
| 122 |
|
| 123 |
rc,r,j,d = self.runOperation(rft) |
| 124 |
|
| 125 |
if(rc !=0 ): |
| 126 |
rft.printVerbose(5,"SessionService: operation returned with error: rc={}".format(rc)) |
| 127 |
return(rc,r,False,None) |
| 128 |
|
| 129 |
|
| 130 |
rft.printVerbose(5,"SessionService: operation exited OK") |
| 131 |
return(rc,r,j,d) |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
class RfSessionServiceOperations(): |
| 138 |
def __init__(self): |
| 139 |
self.SessionServicePath=None |
| 140 |
self.SessionServiceCollectionDict=None |
| 141 |
|
| 142 |
|
| 143 |
def hello(self,sc,op,rft,cmdTop=False): |
| 144 |
rft.printVerbose(4,"in hello") |
| 145 |
rft.printVerbose(4," subcmd:{}, operation:{}, args:{}".format(rft.subcommand,sc.operation,sc.args)) |
| 146 |
print("hello world from SessionService") |
| 147 |
return(0,None,False,None) |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
def get(self,sc,op,rft, cmdTop=False, prop=None): |
| 152 |
rft.printVerbose(4,"{}:{}: in operation".format(rft.subcommand,sc.operation)) |
| 153 |
|
| 154 |
|
| 155 |
svcRoot=RfServiceRoot() |
| 156 |
rc,r,j,d = svcRoot.getServiceRoot(rft) |
| 157 |
if( rc != 0 ): |
| 158 |
rft.printErr("get SessionService: Error getting service root, aborting") |
| 159 |
return(rc,r,False,None) |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
if (("SessionService" in d) and ("@odata.id" in d["SessionService"])): |
| 164 |
sessionServiceLink=d["SessionService"]["@odata.id"] |
| 165 |
else: |
| 166 |
rft.printErr("Error: root does not have a SessionService link") |
| 167 |
return(4) |
| 168 |
|
| 169 |
rft.printVerbose(4,"SessionService: get SessionService: link is: {}".format(sessionServiceLink)) |
| 170 |
|
| 171 |
if cmdTop is True: prop=rft.prop |
| 172 |
|
| 173 |
|
| 174 |
rc,r,j,d=rft.rftSendRecvRequest(rft.AUTHENTICATED_API, 'GET', r.url, relPath=sessionServiceLink, prop=prop) |
| 175 |
|
| 176 |
if(rc==0): rft.printVerbose(1," SessionService Resource:",skip1=True, printV12=cmdTop) |
| 177 |
return(rc,r,j,d) |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
def patch(self,sc,op,rft,cmdTop=False, prop=None, patchData=None, r=None): |
| 182 |
rft.printVerbose(4,"{}:{}: in operation".format(rft.subcommand,sc.operation)) |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
if( len( sc.args) == 2): |
| 187 |
|
| 188 |
try: |
| 189 |
patchData=json.loads(sc.args[1]) |
| 190 |
except ValueError: |
| 191 |
rft.printErr("Patch: invalid Json input data:{}".format(sc.args[1])) |
| 192 |
return(5,None,False,None) |
| 193 |
|
| 194 |
else: |
| 195 |
rft.printErr("Patch: error: invalid argument format") |
| 196 |
rft.printErr(" : args={}".format(sc.args)) |
| 197 |
rft.printErr(" : expect: SessionService patch \"{ <prop>: <value> }\"") |
| 198 |
return(4,None,False,None) |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
rc,r,j,d=op.get(sc,op,rft) |
| 205 |
if( rc != 0): |
| 206 |
return(rc,r,False,None) |
| 207 |
|
| 208 |
|
| 209 |
rc,r,j,d=rft.patchResource(rft, r, patchData) |
| 210 |
|
| 211 |
if(rc==0): rft.printVerbose(1," SessionService Patch:",skip1=True, printV12=cmdTop) |
| 212 |
return(rc,r,j,d) |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
def setSessionTimeout(self,sc,op,rft,cmdTop=False, prop=None): |
| 217 |
rft.printVerbose(4,"{}:{}: in operation".format(rft.subcommand,sc.operation)) |
| 218 |
|
| 219 |
propName="SessionTimeout" |
| 220 |
|
| 221 |
|
| 222 |
if(len(sc.args) < 2 ): |
| 223 |
rft.printErr("Error, no SessionTimeout value specified") |
| 224 |
rft.printErr("Syntax: {} [options] SessionService setSessionTimeout <timeoutInt> ".format(rft.program)) |
| 225 |
return(8,None,False,None) |
| 226 |
sessTimeout=int(sc.args[1],0) |
| 227 |
patchData={propName: sessTimeout} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
rc,r,j,d=op.get(sc,op,rft) |
| 232 |
if( rc != 0): return(rc,r,False,None) |
| 233 |
if( not propName in d ): |
| 234 |
rft.printErr("SessionService resource does not have a {} property.".format(propName)) |
| 235 |
return(8,r,False,None) |
| 236 |
|
| 237 |
rc,r,j,d=rft.patchResource(rft, r, patchData) |
| 238 |
if(rc==0): |
| 239 |
rft.printVerbose(1," SessionService setSessionTimeout:",skip1=True, printV12=cmdTop) |
| 240 |
assetTag={propName: d[propName]} |
| 241 |
return(rc,r,j,assetTag) |
| 242 |
else: return(rc,r,False,None) |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
def getSessions(self,sc,op, rft, cmdTop=False, prop=None): |
| 247 |
rft.printVerbose(4,"{}:{}: in operation: getSessions collection".format(rft.subcommand,sc.operation)) |
| 248 |
|
| 249 |
|
| 250 |
rc,r,j,d=op.get(sc,op, rft) |
| 251 |
if( rc != 0): return(rc,r,False,None) |
| 252 |
|
| 253 |
collName="Sessions" |
| 254 |
|
| 255 |
if ((collName in d) and ("@odata.id" in d[collName])): |
| 256 |
sessionsLink=d[collName]["@odata.id"] |
| 257 |
else: |
| 258 |
rft.printErr("Error: SessionService resource does not have a {} link".format(collName)) |
| 259 |
return(6,None,False,None) |
| 260 |
|
| 261 |
if cmdTop is True: prop=rft.prop |
| 262 |
|
| 263 |
|
| 264 |
if( sc.argnum > 1 and sc.args[1] == 'list' ): |
| 265 |
|
| 266 |
rc,r,j,d=rft.rftSendRecvRequest(rft.AUTHENTICATED_API, 'GET', r.url, relPath=sessionsLink) |
| 267 |
|
| 268 |
rc,r,j,d=rft.listCollection(rft, r, d, prop="UserName") |
| 269 |
if(rc==0): |
| 270 |
rft.printVerbose(1," list {} Collection member info: Id, URI, Socket".format(collName,skip1=True, printV12=cmdTop)) |
| 271 |
|
| 272 |
|
| 273 |
elif(rft.IdLevel2OptnCount==0): |
| 274 |
rc,r,j,d=rft.rftSendRecvRequest(rft.AUTHENTICATED_API, 'GET', r.url, relPath=sessionsLink, prop=prop) |
| 275 |
if(rc==0): |
| 276 |
rft.printVerbose(1," {} Collection ".format(collName,skip1=True, printV12=cmdTop)) |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
elif( rft.allOptn is not True ): |
| 281 |
|
| 282 |
rc,r,j,d=rft.rftSendRecvRequest(rft.AUTHENTICATED_API, 'GET', r.url, relPath=sessionsLink, prop=prop) |
| 283 |
collUrl=r.url |
| 284 |
|
| 285 |
|
| 286 |
path2,rc,r,j,d=rft.getLevel2ResourceById(rft,r,d) |
| 287 |
if(rc!=0): |
| 288 |
return(rc,r,j,d) |
| 289 |
|
| 290 |
|
| 291 |
if( (r is not None) and (prop is not None) ): |
| 292 |
rc,r,j,d=rft.getPropFromDict(rft,r,d,prop) |
| 293 |
|
| 294 |
|
| 295 |
elif( r is None ): |
| 296 |
rc,r,j,d=rft.rftSendRecvRequest(rft.AUTHENTICATED_API, 'GET', collUrl, relPath=path2, prop=prop) |
| 297 |
if(rc==0): |
| 298 |
rft.printVerbose(1," {} Collection Member ".format(collName,skip1=True, printV12=cmdTop)) |
| 299 |
|
| 300 |
|
| 301 |
else: |
| 302 |
rft.printVerbose(4,"getting expanded Sessions Collection") |
| 303 |
rc,r,j,d=rft.getAllCollectionMembers(rft, r.url, relPath=sessionsLink) |
| 304 |
if(rc==0): |
| 305 |
rft.printVerbose(1," Get ALL {} Collection Members".format(collName,skip1=True, printV12=cmdTop)) |
| 306 |
|
| 307 |
return(rc,r,j,d) |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
def sessionLogin(self,sc,op,rft,cmdTop=False, prop=None): |
| 314 |
rft.printVerbose(4,"{}:{}: in operation".format(rft.subcommand,sc.operation)) |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
svcRoot=RfServiceRoot() |
| 321 |
rc,r,j,d = svcRoot.getServiceRoot(rft) |
| 322 |
if( rc != 0 ): |
| 323 |
rft.printErr("SessionService: login: Error getting service root, aborting") |
| 324 |
return(rc,r,False,None) |
| 325 |
|
| 326 |
|
| 327 |
rc,r,j,d=rft.rfSessionLogin(rft,cleanupOnExit=False) |
| 328 |
if(rc == 0): |
| 329 |
|
| 330 |
respData={"SessionId": rft.sessionId, "SessionLocation": rft.sessionLink, "X-Auth-Token": rft.authToken} |
| 331 |
rft.printVerbose(1," SessionLogin: {}".format(respData, skip1=True, printV12=cmdTop)) |
| 332 |
return(rc,r,j,respData) |
| 333 |
else: |
| 334 |
return(rc,r,j,d) |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
def sessionLogout(self,sc,op,rft,cmdTop=False, prop=None): |
| 339 |
rft.printVerbose(4,"{}:{}: in operation".format(rft.subcommand,sc.operation)) |
| 340 |
|
| 341 |
if( (rft.linkLevel2 is None) and( rft.IdLevel2 is None) ): |
| 342 |
rft.printErr("Error, logout: no sessionId or sessionLink specified") |
| 343 |
rft.printErr("Syntax: {} [options] [SessionService] logout [-i<sessionId> | -l <sessionLink>]".format(rft.program)) |
| 344 |
return(8,None,False,None) |
| 345 |
|
| 346 |
|
| 347 |
svcRoot=RfServiceRoot() |
| 348 |
rc,r,j,d = svcRoot.getServiceRoot(rft) |
| 349 |
if( rc != 0 ): |
| 350 |
rft.printErr("SessionService: login: Error getting service root, aborting") |
| 351 |
return(rc,r,False,None) |
| 352 |
|
| 353 |
|
| 354 |
if( ("Links" in d) and ("Sessions" in d["Links"]) and ("@odata.id" in d["Links"]["Sessions"]) ): |
| 355 |
sessionsLink=rft.rootResponseDict["Links"]["Sessions"]["@odata.id"] |
| 356 |
|
| 357 |
else: |
| 358 |
rft.printErr("Error: the rootService response does not have a login link: \"Links\":\"Sessions\":{{\"@odata.id\": <uri>}") |
| 359 |
return(4,None,False,None) |
| 360 |
|
| 361 |
|
| 362 |
rc,r,j,d=rft.rftSendRecvRequest(rft.AUTHENTICATED_API, 'GET', r.url, relPath=sessionsLink, prop=prop) |
| 363 |
if(rc != 0): |
| 364 |
rft.printErr("Error: logout: cant read sessions collection") |
| 365 |
return(6,None,False,None) |
| 366 |
|
| 367 |
path2,rc,r,j,d=rft.getLevel2ResourceById(rft,r,d) |
| 368 |
if(rc!=0): |
| 369 |
rft.printErr("Error: the specified sessionId or sessionLink was not a valid Sessions collection entry") |
| 370 |
return(rc,r,j,d) |
| 371 |
|
| 372 |
|
| 373 |
rc,r,j,d=rft.rfSessionDelete(rft,sessionLink=path2) |
| 374 |
if(rc!=0): |
| 375 |
rft.printErr("Error: Logout: the session delete failed") |
| 376 |
return(rc,r,j,d) |
| 377 |
else: |
| 378 |
rft.printVerbose(1," SessionLogout successful".format(skip1=True, printV12=cmdTop)) |
| 379 |
return(rc,r,j,d) |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
def examples(self,sc,op,rft,cmdTop=False,prop=None): |
| 385 |
rft.printVerbose(4,"{}:{}: in operation".format(rft.subcommand,sc.operation)) |
| 386 |
print(" {} -r<ip> SessionService # gets the sessionService".format(rft.program)) |
| 387 |
print(" {} -r<ip> SessionService setSessionTimeout <timeout> # sets the session timeout property".format(rft.program)) |
| 388 |
print(" {} -r<ip> SessionService Sessions # gets Sessions collection".format(rft.program)) |
| 389 |
print(" {} -r<ip> SessionService Sessions -l<sessUrl> # gets the session at URI=<sessUrl".format(rft.program)) |
| 390 |
print(" {} -r<ip> SessionService Sessions -i<sessId> # gets the session with session Id <sessId>".format(rft.program)) |
| 391 |
print(" {} -r<ip> SessionService patch {{A: B,C: D,...}} # patch the json-formatted {{prop: value...}} data to the sessionService object".format(rft.program)) |
| 392 |
print(" {} -r<ip> SessionService login <usernm> <passwd> # login (create session)".format(rft.program)) |
| 393 |
print(" {} -r<ip> SessionService logout <sessionId> # logout (delete session <sessId>".format(rft.program)) |
| 394 |
return(0,None,False,None) |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
''' |
| 399 |
TODO: |
| 400 |
1. e |
| 401 |
|
| 402 |
''' |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|