| 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 |
|
| 33 |
from urllib.parse import urljoin, urlparse, urlunparse |
| 34 |
|
| 35 |
class RfRawMain(): |
| 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] raw <method> <path> ".format(rft.program)) |
| 47 |
print("") |
| 48 |
print(" {} raw -h # for help".format(rft.program)) |
| 49 |
print(" {} raw examples #for example commands".format(rft.program)) |
| 50 |
print("") |
| 51 |
print(" <method> is one of: GET, PATCH, POST, DELETE, HEAD, PUT") |
| 52 |
print(" <path> is full URI path to a redfish resource--the full path following <ipaddr:port>, starting with forward slash /") |
| 53 |
print("") |
| 54 |
print(" Common OPTNS:") |
| 55 |
print(" -u <user>, --user=<usernm> -- username used for remote redfish authentication") |
| 56 |
print(" -p <passwd>, --password=<passwd> -- password used for remote redfish authentication") |
| 57 |
print(" -t <token>, --token=<token> - redfish auth session token-for sessions across multiple calls") |
| 58 |
print("") |
| 59 |
print(" -r <rhost>, --rhost=<rhost> -- remote redfish service hostname or IP:port") |
| 60 |
print(" -X <method> --request=<method> -- the http method to use. <method>={GET,PATCH,POST,DELETE,HEAD,PUT}. Default=GET") |
| 61 |
print(" -d <data> --data=<data> -- the http request \"data\" to send on PATCH,POST,or PUT requests") |
| 62 |
print(" -H <hdrs>, --Headers=<hdrs> -- Specify the request header list--overrides defaults. Format \"{ A:B, C:D...}\" ") |
| 63 |
print(" -S <Secure>, --Secure=<Secure> -- When to use https: (Note: doesn't stop rhost from redirect http to https)") |
| 64 |
|
| 65 |
|
| 66 |
def displayHelp(self,rft): |
| 67 |
self.displayUsage(rft) |
| 68 |
self.displayOperations(rft) |
| 69 |
print("") |
| 70 |
|
| 71 |
def displayOperations(self,rft): |
| 72 |
print(" <operations / methods>:") |
| 73 |
print(" GET -- HTTP GET method") |
| 74 |
print(" PATCH -- HTTP PATCH method") |
| 75 |
print(" POST -- HTTP POST method") |
| 76 |
print(" DELETE -- HTTP DELETE method") |
| 77 |
print(" HEAD -- HTTP HEAD method") |
| 78 |
print(" PUT -- HTTP PUT method") |
| 79 |
print(" examples -- example raw commands with syntax") |
| 80 |
print(" hello -- raw hello -- debug command") |
| 81 |
return(0) |
| 82 |
|
| 83 |
|
| 84 |
def runOperation(self,rft): |
| 85 |
|
| 86 |
op=RfRawOperations() |
| 87 |
|
| 88 |
|
| 89 |
operationTable = { |
| 90 |
"GET": op.httpGet, |
| 91 |
"PATCH": op.httpPatch, |
| 92 |
"POST": op.httpPost, |
| 93 |
"DELETE": op.httpDelete, |
| 94 |
"HEAD": op.httpHead, |
| 95 |
"PUT": op.httpPut, |
| 96 |
"hello": op.hello, |
| 97 |
"examples": op.examples |
| 98 |
} |
| 99 |
|
| 100 |
rft.printVerbose(5,"raw: runOperation: operation: {}".format(self.operation)) |
| 101 |
rft.printVerbose(5,"raw:runOperation: args: {}".format(self.args)) |
| 102 |
|
| 103 |
if self.operation in operationTable: |
| 104 |
rft.printVerbose(5,"raw:runOperation: found Oper: {} in table. executing".format(rft.subcommand)) |
| 105 |
rc,r,j,d=operationTable[self.operation](self, op, rft, cmdTop=True) |
| 106 |
return(rc,r,j,d) |
| 107 |
|
| 108 |
else: |
| 109 |
rft.printErr("raw: Invalid operation: {}".format(self.operation)) |
| 110 |
return(2,None,False,None) |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
def RawMain(self,rft,cmdTop=False): |
| 115 |
rft.printVerbose(4,"RawMain: subcommand: {}".format(rft.subcommand)) |
| 116 |
|
| 117 |
if( rft.help ): |
| 118 |
self.displayHelp(rft) |
| 119 |
return(2,None,False,None) |
| 120 |
|
| 121 |
args=rft.subcommandArgv[0:] |
| 122 |
|
| 123 |
|
| 124 |
nonMethodOperations=("hello","examples") |
| 125 |
if( len(args)==2): |
| 126 |
nonMethodOp=args[1] |
| 127 |
if( not nonMethodOp in nonMethodOperations): |
| 128 |
rft.printErr("Syntax error: \"raw\" subcommands require 2 arguments\n") |
| 129 |
rft.printErr("") |
| 130 |
self.displayHelp(rft) |
| 131 |
return(2,None,False,None) |
| 132 |
|
| 133 |
elif( len(args) < 3 ): |
| 134 |
rft.printErr("Syntax error: \"raw\" subcommands require 2 arguments\n") |
| 135 |
self.displayHelp(rft) |
| 136 |
return(2,None,False,None) |
| 137 |
|
| 138 |
self.operation=args[1] |
| 139 |
self.args = args[1:] |
| 140 |
self.argnum =len(self.args) |
| 141 |
|
| 142 |
rft.printVerbose(5,"raw: operation={}, args={}".format(self.operation,self.args)) |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
rc,r,j,d = self.runOperation(rft) |
| 147 |
|
| 148 |
if(rc !=0 ): |
| 149 |
rft.printVerbose(5,"raw: operation returned with error: rc={}".format(rc)) |
| 150 |
return(rc,r,False,None) |
| 151 |
|
| 152 |
|
| 153 |
rft.printVerbose(5,"raw: operation exited OK") |
| 154 |
return(rc,r,j,d) |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
class RfRawOperations(): |
| 161 |
def __init__(self): |
| 162 |
self.systemsPath=None |
| 163 |
self.systemsCollectionDict=None |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
def getApiType(self,rft,uri): |
| 169 |
unauthenticatedAPIs=("/redfish", "/redfish/v1", "/redfish/v1/", "/redfish/v1/odata", "/redfish/v1/$metadata") |
| 170 |
if not uri in unauthenticatedAPIs: |
| 171 |
return( rft.AUTHENTICATED_API ) |
| 172 |
else: |
| 173 |
return( rft.UNAUTHENTICATED_API ) |
| 174 |
|
| 175 |
|
| 176 |
def hello(self,sc,op,rft,cmdTop=False): |
| 177 |
rft.printVerbose(4,"in hello") |
| 178 |
rft.printVerbose(4," subcmd:{}, operation:{}, args:{}".format(rft.subcommand,sc.operation,sc.args)) |
| 179 |
print("hello world from raw subcommand") |
| 180 |
return(0,None,False,None) |
| 181 |
|
| 182 |
|
| 183 |
def httpGet(self,sc,op,rft,cmdTop=False, prop=None): |
| 184 |
rft.printVerbose(4,"{}:{}: in raw".format(rft.subcommand,sc.operation)) |
| 185 |
|
| 186 |
|
| 187 |
path=sc.args[1] |
| 188 |
method="GET" |
| 189 |
rft.printVerbose(4,"raw: GET: method:{} path:{}".format(method,path)) |
| 190 |
|
| 191 |
apiType=self.getApiType(rft,path) |
| 192 |
|
| 193 |
|
| 194 |
scheme=rft.getApiScheme(apiType) |
| 195 |
scheme_tuple=[scheme, rft.rhost, "/redfish", "","",""] |
| 196 |
rootUrl=urlunparse(scheme_tuple) |
| 197 |
|
| 198 |
if cmdTop is True: prop=rft.prop |
| 199 |
jsonData=True |
| 200 |
if (path=="/redfish/v1/$metadata"): jsonData=False |
| 201 |
rc,r,j,d=rft.rftSendRecvRequest(apiType, method, rootUrl, relPath=path, prop=prop,jsonData=jsonData) |
| 202 |
if(rc==0): |
| 203 |
rft.printVerbose(1," raw GET:",skip1=True, printV12=cmdTop) |
| 204 |
return(rc,r,j,d) |
| 205 |
else: |
| 206 |
rft.printErr("raw: Error getting response") |
| 207 |
return(rc,r,False,None) |
| 208 |
|
| 209 |
def httpHead(self,sc,op,rft,cmdTop=False, prop=None): |
| 210 |
rft.printVerbose(4,"{}:{}: in raw".format(rft.subcommand,sc.operation)) |
| 211 |
|
| 212 |
|
| 213 |
path=sc.args[1] |
| 214 |
method="HEAD" |
| 215 |
rft.printVerbose(4,"raw: HEAD: method:{} path:{}".format(method,path)) |
| 216 |
|
| 217 |
apiType=self.getApiType(rft,path) |
| 218 |
|
| 219 |
|
| 220 |
scheme=rft.getApiScheme(apiType) |
| 221 |
scheme_tuple=[scheme, rft.rhost, "/redfish", "","",""] |
| 222 |
rootUrl=urlunparse(scheme_tuple) |
| 223 |
|
| 224 |
rc,r,j,d=rft.rftSendRecvRequest(apiType, method, rootUrl, relPath=path) |
| 225 |
if(rc==0): |
| 226 |
rft.printVerbose(1," raw HEAD:",skip1=True, printV12=cmdTop) |
| 227 |
return(rc,r,False,None) |
| 228 |
else: |
| 229 |
rft.printErr("raw: Error getting response") |
| 230 |
return(rc,r,False,None) |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
def httpPatch(self,sc,op,rft,cmdTop=False, prop=None): |
| 236 |
rft.printVerbose(4,"{}:{}: in raw".format(rft.subcommand,sc.operation)) |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
try: |
| 241 |
patchData=json.loads(rft.requestData) |
| 242 |
except ValueError: |
| 243 |
rft.printErr("Patch: invalid Json input data:{}".format(rft.requestData)) |
| 244 |
return(5,None,False,None) |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
path=sc.args[1] |
| 250 |
method="PATCH" |
| 251 |
rft.printVerbose(4,"raw: PATCH: method:{} path:{}".format(method,path)) |
| 252 |
|
| 253 |
apiType=self.getApiType(rft,path) |
| 254 |
|
| 255 |
|
| 256 |
scheme=rft.getApiScheme(apiType) |
| 257 |
scheme_tuple=[scheme, rft.rhost, "/redfish", "","",""] |
| 258 |
rootUrl=urlunparse(scheme_tuple) |
| 259 |
|
| 260 |
|
| 261 |
rc,r,j,d=rft.rftSendRecvRequest(apiType, "GET", rootUrl, relPath=path) |
| 262 |
if(rc!=0): |
| 263 |
rft.printErr("raw: Error getting resource prior to patching it, aborting") |
| 264 |
return(rc,r,False,None) |
| 265 |
|
| 266 |
|
| 267 |
rc,r,j,d=rft.patchResource(rft, r, patchData) |
| 268 |
|
| 269 |
if(rc==0): rft.printVerbose(1," Systems Patch:",skip1=True, printV12=cmdTop) |
| 270 |
return(rc,r,j,d) |
| 271 |
|
| 272 |
|
| 273 |
def httpPost(self,sc,op,rft,cmdTop=False, prop=None): |
| 274 |
rft.printVerbose(4,"{}:{}: in raw".format(rft.subcommand,sc.operation)) |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
try: |
| 279 |
postData=json.loads(rft.requestData) |
| 280 |
except ValueError: |
| 281 |
rft.printErr("Post: invalid Json input data:{}".format(rft.requestData)) |
| 282 |
return(5,None,False,None) |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
path=sc.args[1] |
| 287 |
method="POST" |
| 288 |
rft.printVerbose(4,"raw: POST: method:{} path:{}".format(method,path)) |
| 289 |
|
| 290 |
apiType=self.getApiType(rft,path) |
| 291 |
|
| 292 |
|
| 293 |
scheme=rft.getApiScheme(apiType) |
| 294 |
scheme_tuple=[scheme, rft.rhost, "/redfish", "","",""] |
| 295 |
rootUrl=urlunparse(scheme_tuple) |
| 296 |
|
| 297 |
|
| 298 |
reqPostData=json.dumps(postData) |
| 299 |
|
| 300 |
rc,r,j,d=rft.rftSendRecvRequest(apiType, method, rootUrl, relPath=path, reqData=reqPostData) |
| 301 |
if(rc!=0): |
| 302 |
rft.printErr("raw: Error sending POST to resource, aborting") |
| 303 |
return(rc,r,False,None) |
| 304 |
|
| 305 |
|
| 306 |
if(rc==0): rft.printVerbose(1," raw POST:",skip1=True, printV12=cmdTop) |
| 307 |
return(rc,r,j,d) |
| 308 |
|
| 309 |
|
| 310 |
def httpPut(self,sc,op,rft,cmdTop=False, prop=None): |
| 311 |
rft.printVerbose(4,"{}:{}: in raw".format(rft.subcommand,sc.operation)) |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
try: |
| 316 |
putData=json.loads(rft.requestData) |
| 317 |
except ValueError: |
| 318 |
rft.printErr("Put: invalid Json input data:{}".format(rft.requestData)) |
| 319 |
return(5,None,False,None) |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
path=sc.args[1] |
| 324 |
method="PUT" |
| 325 |
rft.printVerbose(4,"raw: POST: method:{} path:{}".format(method,path)) |
| 326 |
|
| 327 |
apiType=self.getApiType(rft,path) |
| 328 |
|
| 329 |
|
| 330 |
scheme=rft.getApiScheme(apiType) |
| 331 |
scheme_tuple=[scheme, rft.rhost, "/redfish", "","",""] |
| 332 |
rootUrl=urlunparse(scheme_tuple) |
| 333 |
|
| 334 |
|
| 335 |
reqPutData=json.dumps(putData) |
| 336 |
|
| 337 |
rc,r,j,d=rft.rftSendRecvRequest(apiType, method, rootUrl, relPath=path, reqData=reqPutData) |
| 338 |
if(rc!=0): |
| 339 |
rft.printErr("raw: Error sending PUT to resource, aborting") |
| 340 |
return(rc,r,False,None) |
| 341 |
|
| 342 |
|
| 343 |
if(rc==0): rft.printVerbose(1," raw PUT:",skip1=True, printV12=cmdTop) |
| 344 |
return(rc,r,j,d) |
| 345 |
|
| 346 |
|
| 347 |
def httpDelete(self,sc,op,rft,cmdTop=False, prop=None): |
| 348 |
rft.printVerbose(4,"{}:{}: in raw".format(rft.subcommand,sc.operation)) |
| 349 |
|
| 350 |
|
| 351 |
path=sc.args[1] |
| 352 |
method="DELETE" |
| 353 |
rft.printVerbose(4,"raw: DELETE: method:{} path:{}".format(method,path)) |
| 354 |
|
| 355 |
apiType=self.getApiType(rft,path) |
| 356 |
|
| 357 |
|
| 358 |
scheme=rft.getApiScheme(apiType) |
| 359 |
scheme_tuple=[scheme, rft.rhost, "/redfish", "","",""] |
| 360 |
rootUrl=urlunparse(scheme_tuple) |
| 361 |
|
| 362 |
|
| 363 |
rc,r,j,d=rft.rftSendRecvRequest(apiType, method, rootUrl, relPath=path) |
| 364 |
if(rc!=0): |
| 365 |
rft.printErr("raw: Error sending DELETE to resource, aborting") |
| 366 |
return(rc,r,False,None) |
| 367 |
|
| 368 |
|
| 369 |
if(rc==0): rft.printVerbose(1," raw DELETE:",skip1=True, printV12=cmdTop) |
| 370 |
return(rc,r,j,d) |
| 371 |
|
| 372 |
|
| 373 |
def examples(self,sc,op,rft,cmdTop=False,prop=None): |
| 374 |
rft.printVerbose(4,"{}:{}: in operation".format(rft.subcommand,sc.operation)) |
| 375 |
print(" {} -r<ip> raw GET /redfish/v1/ # returns the root collection".format(rft.program)) |
| 376 |
|
| 377 |
return(0,None,False,None) |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
''' |
| 383 |
TODO: |
| 384 |
1. need to handle case where no patch or post data (no -d) |
| 385 |
2. add raw PUT |
| 386 |
CHANGES: |
| 387 |
0.9.2: no longer call /redfish and /redfish/v1 before executing the raw api |
| 388 |
''' |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|