Repository navigation
Fix #16, use standard redfish headers #58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -124,10 +124,9 @@ | |
| standard_library.install_aliases() | ||
| from builtins import object | ||
|
|
||
|
|
||
|
|
||
| import json | ||
| from urllib.parse import urlparse, urljoin | ||
| import socket | ||
| from urllib.parse import urlparse, urljoin, urlunparse | ||
| import requests | ||
| from . import config | ||
| from . import types | ||
|
|
@@ -207,13 +206,13 @@ def __init__(self, | |
| "this is insecure and can allow" + | ||
| " a man in the middle attack") | ||
|
|
||
| # Show redfish standard headers | ||
| config.logger.debug(self.connection_parameters.headers) | ||
|
|
||
| config.logger.debug("Root url : %s", | ||
| self.connection_parameters.rooturl) | ||
| self.Root = types.Root(self.connection_parameters.rooturl, | ||
| self.connection_parameters) | ||
| #self.api_url = tortilla.wrap(self.connection_parameters.rooturl, | ||
| # debug=TORTILLADEBUG) | ||
| #self.root = self.api_url.get(verify=self.connection_parameters.verify_cert) | ||
|
|
||
| config.logger.info("API Version : %s", self.get_api_version()) | ||
| mapping.redfish_version = self.get_api_version() | ||
|
|
@@ -292,9 +291,10 @@ def login(self): | |
| url = urljoin(url, "Sessions") | ||
|
|
||
| # Craft request body and header | ||
| requestBody = {"UserName": self.connection_parameters.user_name , "Password": self.connection_parameters.password} | ||
| requestBody = {"UserName": self.connection_parameters.user_name, | ||
| "Password": self.connection_parameters.password} | ||
| config.logger.debug(requestBody) | ||
| header = {'Content-type': 'application/json'} | ||
| headers = self.connection_parameters.headers | ||
| # ======================================================================= | ||
| # Tortilla seems not able to provide the header of a post request answer. | ||
| # However this is required by redfish standard to get X-Auth-Token. | ||
|
|
@@ -305,38 +305,40 @@ def login(self): | |
| # sessions = sessionsUrl.post(verify=self.verify_cert, data=requestBody) | ||
| auth = requests.post(url, | ||
| data=json.dumps(requestBody), | ||
| headers=header, | ||
| verify=self.connection_parameters.verify_cert | ||
| ) | ||
| headers=headers, | ||
| verify=self.connection_parameters.verify_cert) | ||
|
|
||
| # ======================================================================= | ||
| # TODO : Manage exception with a class. | ||
| # ======================================================================= | ||
| if auth.status_code != 201: | ||
| try: | ||
| answer=auth.json() | ||
| except ValueError as e: | ||
| answer = auth.json() | ||
| except ValueError: | ||
| answer = "" | ||
| raise exception.AuthenticationFailureException("Login request return an invalid status code ", code=auth.status_code, queryAnswer=answer) | ||
| raise exception.AuthenticationFailureException( | ||
| "Login request return an invalid status code ", | ||
| code=auth.status_code, queryAnswer=answer) | ||
|
|
||
| self.connection_parameters.auth_token = auth.headers.get("x-auth-token") | ||
| self.connection_parameters.auth_token = auth.headers.get( | ||
| "x-auth-token") | ||
| self.connection_parameters.user_uri = auth.headers.get("location") | ||
| config.logger.debug("x-auth-token : %s", self.connection_parameters.auth_token) | ||
| config.logger.debug("user session : %s", self.connection_parameters.user_uri) | ||
| config.logger.debug("x-auth-token : %s", | ||
| self.connection_parameters.auth_token) | ||
| config.logger.debug("user session : %s", | ||
| self.connection_parameters.user_uri) | ||
| return True | ||
|
|
||
| def logout(self): | ||
| # Craft full url | ||
| url = self.connection_parameters.user_uri | ||
|
|
||
| # Craft request header | ||
| header = {"Content-type": "application/json", | ||
| "x-auth-token": self.connection_parameters.auth_token | ||
| } | ||
| headers = self.connection_parameters.headers | ||
|
|
||
| logout = requests.delete(url, headers=header, | ||
| verify=self.connection_parameters.verify_cert | ||
| ) | ||
| logout = requests.delete(url, | ||
| headers=headers, | ||
| verify=self.connection_parameters.verify_cert) | ||
|
|
||
| if logout.status_code == 200: | ||
| config.logger.info("Logout successful") | ||
|
|
@@ -406,3 +408,17 @@ def user_uri(self): | |
| @user_uri.setter | ||
| def user_uri(self, user_uri): | ||
| self.__user_uri = user_uri | ||
|
|
||
| @property | ||
| def headers(self): | ||
| # Host header is set by request or tortilla | ||
| url = urlparse(self.__rooturl) | ||
| origin = urlunparse((url.scheme, url.netloc, '', '', '', '')) | ||
| headers = {'OData-Version': '4.0', | ||
| 'Content-type': 'application/json', | ||
| 'Accept': 'application/json', | ||
| 'Origin': origin, | ||
| 'User-Agent': 'python-redfish'} | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could also add the version here |
||
| if self.auth_token: | ||
| headers.update({'x-auth-token': self.auth_token}) | ||
| return headers | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does that require a change in the package deps ? If yes, could you create a tickt for that please ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, urllib is part of core modules.
By the way I try to sort the modules in the following way:
1- buildin
2- Common external module (request, tortilla)
3- Our modules