-
Notifications
You must be signed in to change notification settings - Fork 21
LOG-9289 apikeys and live tail supports #42
Conversation
…ad of managemement/query url in config file
lecli/api_key/api.py
Outdated
|
||
def create(payload): | ||
""" | ||
Delete an api key with the provided ID |
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.
Think this was a copy-paste error!
lecli/api_key/api.py
Outdated
sys.stderr.write('Create api key failed, status code: %d' % response.status_code) | ||
sys.exit(1) | ||
elif response.status_code == 201: | ||
sys.stdout.write('Created api key with payload: %s\n' % payload) |
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.
Are the response body & the payload here identical?
If not, we should probably print out the body.
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.
we are printing the response body on the next line. I will remove printing the payload though, I think I put this in for debugging purposes. :)
Handle get api key response | ||
""" | ||
if response_utils.response_error(response): | ||
sys.exit(1) |
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.
Not printing out an error message here?
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.
response_utils does
lecli/api_key/api.py
Outdated
try: | ||
response = requests.delete(url, headers=headers) | ||
if response_utils.response_error(response): | ||
sys.stderr.write('Deleting api key failed, status code: %d' % response.status_code) |
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.
How come we're printing this here? Does response_utils not print the status code?
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.
nice catch, response_utils does print the status code so I will remove it from here.
lecli/api_key/api.py
Outdated
sys.stderr.write('Deleting api key failed, status code: %d' % response.status_code) | ||
sys.exit(1) | ||
elif response.status_code == 204: | ||
sys.stdout.write('Deleted api key with id: %s \n' % api_key_id) |
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.
What happens when this branch is false, and it's not a 204? If there's no possibility for another status code we can just have an else here and maybe a comment to say the status is 204
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.
Same can be applied to all of the functions here
response = requests.get(url, headers=headers) | ||
handle_api_key_response(response) | ||
except requests.exceptions.RequestException as error: | ||
sys.stderr.write(error) |
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.
These logging statements can be refactored to use the logging module built into python.
Might take a bit of time, so not a blocker.
Much nicer to have log.error(error) 🙂
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.
This was done in this way for a reason -- possibly a feedback from a reviewer before. @ellynch-r7 do you remember why?
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.
actualy a better way of doing this is with click.echo(err=True) as logging will require adopting logging for this as well + there are not really logs but information that has to be printed on screen.
I have used click.echo for team api, hopefully in the future we can use it all around the project. For the moment I have reached my refactoring limits :)
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 click.echo provide the option to add logging handlers? i.e. directing it to a file or a webhook? With python logging module we can just add le_python as a dependency and provide a log token
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.
to a file yes not sure about logging handlers.
lecli/api_key/commands.py
Outdated
with open(filename) as json_data: | ||
try: | ||
params = json.load(json_data) | ||
print params |
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.
print statement here
lecli/api_key/commands.py
Outdated
params = json.load(json_data) | ||
print params | ||
api.create(params) | ||
except ValueError as error: |
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.
This should only be wrapped around the one line, params = json.load(json_data)
right?
I don't believe api.create will throw that exception, or will it?
lecli/api_utils.py
Outdated
else: | ||
print_config_error_and_exit(URL_SECTION, 'Log Query URL(%s)' % config_key) | ||
except (ConfigParser.NoOptionError, ConfigParser.NoSectionError): | ||
return 'https://rest.logentries.com/query' |
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.
This should be extracted as a constant. So it's easier to modify later. As well as the other default urls in the file
@cli.group() | ||
def tail(): | ||
"""Tail logs""" | ||
pass |
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.
Should we bother committing this if it's incomplete?
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.
this isn't incomplete actually, its there for registering that group with click
context
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.
Ah ok
lecli/query/api.py
Outdated
next_url = response.json()['links'][0]['href'] | ||
time.sleep(poll_interval) | ||
response = fetch_results(next_url) | ||
|
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.
What happens here if there is no links field in the response?
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.
there has to be a link in the response as its live tail
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.
Then should we print an error? It will just end silently if something weird comes back
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.
will do
tests/test_apikey_api.py
Outdated
api.list() | ||
|
||
out, err = capsys.readouterr() | ||
assert not err |
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.
Can we check that something has been printed to out
?
If someone creates a branch with no error output, it will still pass
tests/test_apikey_api.py
Outdated
|
||
out, err = capsys.readouterr() | ||
assert not err | ||
assert 'Disabled api key with id: %s' % api_key_id in out |
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.
There's no guarantee from this that we've sent the correct body. Can we check what calls are made to the mocked http client?
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.
yeah can check that i guess with httpretty.last_request 👍
1st commit: Introduced apikeys and live tail supports to lecli
2nd commit: (No functional change here) There has been a major cleanup in UTs such that I have removed dummy urls and response examples that was not helping to test lecli functionality. Only left the ones that helps to verify lecli implementation
3rd commit: (Only functional change is around config file in this commit) I have tried to simplify and standardise url building logic among different api implementations. I have also removed management and query URLs from config file and introduced "api url" which is supposed to be the base url for REST API
4th commit: Owner support for listing api keys to show the owner api key itself and documentation for new APIs(live tail and api key management)