Skip to content
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

Added 4 more methods #10

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open

Added 4 more methods #10

wants to merge 10 commits into from

Conversation

me-sosa
Copy link

@me-sosa me-sosa commented Feb 13, 2019

  1. show_tags_per_device: gets all the devices with the specified app_id
  2. show_device_env_vars: Shows overriden ENV VARS in each device
  3. show_app_env_vars: Shows application ENV VARS with default value
    4. set_device_env_vars: Override ENV VARS with new values taken from the tags

Tested on schAlpha with two devices and different test cases.

Tested on schAlpha with two devices and different test cases.
all_devices = self.models.device.get_all_by_application_id(app_info['id'])
tags_per_device = []
for device in all_devices:
tags_per_device.append({'uuid':device['uuid'],
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe you can use something like this, so you don't have that many api calls, https://github.com/mobilityhouse/resin-release-tool/blob/master/resin_release_tool/releaser.py#L18

@Roger
Copy link
Contributor

Roger commented Feb 14, 2019

You are setting every tag to env and should be get all the tags of the devices in a dict and set it to RESIN_TAG_LIST, using json.dumps

@me-sosa
Copy link
Author

me-sosa commented Feb 14, 2019

@Roger

You are setting every tag to env and should be get all the tags of the devices in a dict and set it to RESIN_TAG_LIST, using json.dumps

So RESIN_TAG_LIST should look like: {'STATUS': 'not commissioned', 'COLOR';'orange', etc} right?
Now my question is, which method will receive this?

@@ -21,6 +23,69 @@ def get_canaries(self):
if tag['tag_key'] == 'CANARY']
return canaries

def get_tags_per_device(self):

Choose a reason for hiding this comment

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

maybe it makes it clearer to have a short docstring here?

Copy link
Author

Choose a reason for hiding this comment

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

maybe

…a string containing all the tags.

NOTE: During this bug fix we've found that  variable names starting with "BALENA_" or "RESIN_" are forbidden.
@me-sosa
Copy link
Author

me-sosa commented Feb 14, 2019

g

@Roger please check the latest commit. I already tested it on schAlpha and it works.

resin_release_tool/cli.py Outdated Show resolved Hide resolved

def get_app_env_vars(self):
allvars = self.models.environment_variables.application.get_all(int(self.app_id))
list_of_app_env_vars = [{'id':var['id'], var['name']:var['value']} for var in allvars]
Copy link

Choose a reason for hiding this comment

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

Any particular reason this format was chosen?

If I'm understanding things right, it's:

[{"id": 1234, "something": "its value"}, {"id": 2345, "foobar": "baz"}]

Which seems odd to me, using var['name'] as dictionary key makes it hard to access stuff programmatically (you'd have to iterate over the dictionary's keys and exclude the one named "id" to access the actual key/value pair)

It would make things simpler to either keep the original format returned by get_all() (since the name is reliably in the dictionary key named 'name') or rearrange it into a dictionary like this:

{"something": {"id": 1234, "value": "its value"}, "foobar": {"id": 2345, "value": "baz"}}

So you can just access dict["something"]["value"] without for loops.

Copy link
Author

Choose a reason for hiding this comment

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

It's been refactored now, thanks for your help!

Copy link

Choose a reason for hiding this comment

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

This function looks the same to me though.

list_of_env_vars_per_device = []
for device in self.uuid_list:
allvars = self.models.environment_variables.device.get_all(device)
list_of_vars = [{'id':var['id'], var['name']:var['value']} for var in allvars]
Copy link

Choose a reason for hiding this comment

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

This seems very similar to the code in get_app_env_vars, consider putting that in a separate function.

for device in self.uuid_list:
allvars = self.models.environment_variables.device.get_all(device)
list_of_vars = [{'id':var['id'], var['name']:var['value']} for var in allvars]
list_of_env_vars_per_device.append({device:list_of_vars})
Copy link

Choose a reason for hiding this comment

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

Pretty much the comment on structure as above: Any particular reason to choose this...

[{"some_device_id": [...list...]}, {"other_device_id": [...list...]}]

over just this?

{"some_device_id": [...list...], "other_device_id": [...list...]}

value = json.dumps(device['tags'])
self.models.environment_variables.device.create(device['uuid'], 'DEVICE_TAG_LIST', value)
print("creating/overriding var: ", device['uuid'], 'DEVICE_TAG_LIST', value)
except:
Copy link

Choose a reason for hiding this comment

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

This "except" is too broad and could hide other problems.

This is something pylint would point out, I think this project is missing some integration there.

Choose a reason for hiding this comment

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

I agree

Copy link
Author

Choose a reason for hiding this comment

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

Noted, for sure it will require some more refactoring and integrations in the future but for now I think we can merge it since we need to provide the tags to the ops team.
Thanks @dequis @OrangeTux for the review!

Copy link

Choose a reason for hiding this comment

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

Yeah the pylint stuff is out of scope.

Copy link

Choose a reason for hiding this comment

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

But this isn't resolved, the except is still too broad

resin_release_tool/releaser.py Outdated Show resolved Hide resolved
resin_release_tool/releaser.py Outdated Show resolved Hide resolved
resin_release_tool/releaser.py Outdated Show resolved Hide resolved
resin_release_tool/releaser.py Outdated Show resolved Hide resolved
resin_release_tool/releaser.py Outdated Show resolved Hide resolved
Copy link

@OrangeTux OrangeTux left a comment

Choose a reason for hiding this comment

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

I've some minor comments. Also look at @dequis his comments.

resin_release_tool/releaser.py Outdated Show resolved Hide resolved
resin_release_tool/releaser.py Outdated Show resolved Hide resolved
value = json.dumps(device['tags'])
self.models.environment_variables.device.create(device['uuid'], 'DEVICE_TAG_LIST', value)
print("creating/overriding var: ", device['uuid'], 'DEVICE_TAG_LIST', value)
except:

Choose a reason for hiding this comment

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

I agree

OrangeTux
OrangeTux previously approved these changes Feb 15, 2019
Copy link

@dequis dequis left a comment

Choose a reason for hiding this comment

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

Some things are still not addressed (that self.uuid_list is still there)

@Roger
Copy link
Contributor

Roger commented Feb 15, 2019

please squash before merge! and thanks for the changes :)

…a string containing all the tags.

NOTE: During this bug fix we've found that  variable names starting with "BALENA_" or "RESIN_" are forbidden.
Refactored "get_tags_per_device", now it is cleaner and shorter.
Solved empty uuid list and got rid of "self.uuid_list"
…-tool into env_vars_mod

Squashed last 5 commits

def get_app_env_vars(self):
all_vars = self.models.environment_variables.application.get_all(int(self.app_id))
return [{'id':var['id'], var['name']:var['value']} for var in all_vars]
Copy link

Choose a reason for hiding this comment

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

Oh the original line got refactored but #10 (comment) still applies

if uuid in elem:
list_of_env_vars = elem[uuid]
for var in list_of_env_vars:
if 'DEVICE_TAG_LIST' in var:
Copy link

Choose a reason for hiding this comment

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

Remember to refactor this whole loop after get_app_env_vars returns a plain dict.

@sonarcloud
Copy link

sonarcloud bot commented Mar 24, 2022

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 2 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

5 participants