Skip to content

Conversation

@JaviCerveraIngram
Copy link
Collaborator

Support for Usage API.

@codecov-io
Copy link

codecov-io commented Apr 2, 2019

Codecov Report

Merging #24 into master will increase coverage by 1.14%.
The diff coverage is 96.18%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #24      +/-   ##
==========================================
+ Coverage   92.95%   94.09%   +1.14%     
==========================================
  Files          26       28       +2     
  Lines         809     1084     +275     
==========================================
+ Hits          752     1020     +268     
- Misses         57       64       +7
Impacted Files Coverage Δ
connect/resource/template.py 60% <0%> (+1.17%) ⬆️
connect/resource/__init__.py 100% <100%> (ø) ⬆️
connect/models/product.py 100% <100%> (ø) ⬆️
connect/resource/automation.py 91.42% <100%> (-0.47%) ⬇️
connect/models/exception.py 93.22% <100%> (+4.64%) ⬆️
connect/resource/tier_config_automation.py 100% <100%> (ø) ⬆️
connect/resource/fulfillment_automation.py 46.15% <75%> (-0.19%) ⬇️
connect/resource/usage_automation.py 93.85% <93.85%> (ø)
connect/resource/usage_file_automation.py 96.15% <96.15%> (ø)
connect/resource/base.py 92.22% <97.87%> (+2.34%) ⬆️
... and 4 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7db1965...2215257. Read the comment docs.

return self._load_schema(response, many=False)

@staticmethod
def create_spreadsheet(usage_records):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably, better to create spreadsheet on base of template downloaded with API instead of hardcoding structure right here?

Copy link
Contributor

@marcserrat marcserrat Apr 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently there is way to download template, but limitations it implies are quite big. First one is that will limit PaaS platform configurations, requieres volume with R/W and the way connectors works, they don't need it at all (is library limitation).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it ok like this then?

def _get_usage_template_download_location(self, product_id):
# type: (str) -> str
try:
response = self.api.get(self.urljoin(self.config.api_url,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it worth to use path= instead of url= for api get,
all these urljoin's looks not really good

should be like:
response = self.api.get(path= '/usage/products/')
response = self.api.get(path=f '/usage/products/{product_id}')

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the path argument officially supported by requests? Could not find it in the documentation. I can add it to our API client though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably - good moment to invent that, for both requests and usages

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

location = self._get_usage_template_download_location(product.id)
contents = self._retrieve_usage_template(location) if location else None
if not location or contents is None:
msg = 'Error obtaining template usage file from `{}`'.format(location)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

truly speaking failure to find template location and no failure to download template itself - different errors.

and error like
"Error obtaining template usage file from None"
will looks like strange ...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. Will split this in two different errors.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

book = self.create_spreadsheet(usage_records)
self.upload_spreadsheet(usage_file, book)

def get_usage_template(self, product):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning string looks strange ... binary blob in a string ...
probably operate with file object?
How to use it - need example, like:

with open("template.xlsx", "w") as ssf:
ssf.write(UsageAutomation().get_usage_template('PRD-xxx-yyy-zzz'))

Copy link
Collaborator Author

@JaviCerveraIngram JaviCerveraIngram Apr 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have checked this and you are right. A "bytes" object should be returned.

It works like this:

  • The API '/usage/products/{product_id}/template/' is used to retrieve the link to the usage template (I just spotted a bug in the code here, the '/template/' part is missing; will be fixed in next commit).
  • We try to read the contents of the location provided. The contents of the file are returned as text, but since it will be an xslx file, it makes more sense to use the "content" field of the response rather than the "text" field, and obtain it as a bytes object.

Apart from using requests to obtain the file contents, a simple file read is also attempted, in case that the file is stored locally. I think that this should actually only happen during testing, and since there are other alternatives to achieve this during testing (by mocking the requests.get function to return the contents of a file), I didn't come up with the best solution here. I will remove the part where the file is loaded and use requests only.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done,

… Before, the URL sent contained the path '/usage/files/usage/products/' instead of '/usage/files/'.
@vgrebenschikov vgrebenschikov merged commit 18c21c1 into cloudblue:master Apr 5, 2019
@JaviCerveraIngram JaviCerveraIngram deleted the usage branch April 8, 2019 14:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants