Papercuts #16

Merged
merged 9 commits into from Jan 26, 2016

Conversation

Projects
None yet
4 participants
Member

AdamIsrael commented Jan 26, 2016

Fixes a few papercuts:

config_changed hook
fix status_set after successful config_changed
consistent use of status_set

reactive/vpe_router.py
@@ -35,8 +35,10 @@ def validate_config():
except Exception as e:
remove_state('vpe.configured')
set_state('blocked', 'validation failed: %s' % e)
- else:
@chuckbutler

chuckbutler Jan 26, 2016

Contributor

nice catch here

@marcoceppi

marcoceppi Jan 26, 2016

Owner

Not a nice catch. There should be a finally and else. Else means no exception raised. Finally means everything after including exception. With this change vpe.configured will be set always

@johnsca

johnsca Jan 26, 2016

Owner

It would probably be less confusing to just move the else: code inside the try block at the bottom.

Edit: The main reason to use the else clause with try is if you expect the code in the else clause to raise an exception that would be caught by the except which you don't want to catch. In this case, your except clause is too broad and you should catch something more specific than Exception (both of those could reasonably be ValueErrors, or, even better, you could create a ValidationError subclass), at which point it would be reasonable to move the code from the else block into the try.

@marcoceppi

marcoceppi Jan 26, 2016

Owner

It's a good point, moving the set_state inside of the try block is probably best

reactive/vpe_router.py
@@ -35,8 +35,10 @@ def validate_config():
except Exception as e:
remove_state('vpe.configured')
set_state('blocked', 'validation failed: %s' % e)
- else:
+ finally:
+ remove_state('blocked')
@marcoceppi

marcoceppi Jan 26, 2016

Owner

What is this? We never set a blocked state?

@AdamIsrael

AdamIsrael Jan 26, 2016

Member

Line 37 set the state. I found setting the config values never cleared the state.

@johnsca

johnsca Jan 26, 2016

Owner

Should line 37 be status_set instead of set_state?

@AdamIsrael

AdamIsrael Jan 26, 2016

Member

Good catch, I think you're right.

Member

AdamIsrael commented Jan 26, 2016

Fixed the else and state/status switch. I redeployed and confirmed that it's working as expected now.

Contributor

chuckbutler commented Jan 26, 2016

I'm +1 on this PR

As my initial glance was interrupted and i got completely schooled on some python :) I'll reserve the actual merging to @marcoceppi

Owner

marcoceppi commented Jan 26, 2016

Thanks for cleaning up my typos with set_state and status_set. My only real comment is I really prefer all lower case status messages since all of the tabular format is lowercase. It's not enough to block this but if someone feels like going through and updating this...

reactive/vpe_router.py
@@ -25,7 +25,7 @@
@hook('config-changed')
def validate_config():
try:
- if ('pass', 'vpe-router', 'user') not in cfg:
+ if not cfg.keys() & {'pass', 'vpe-router', 'user'}:
@marcoceppi

marcoceppi Jan 26, 2016

Owner

This syntax is odd to me. maybe if ['pass', 'vpe-router', 'user'] not in cfg.keys() instead

@AdamIsrael

AdamIsrael Jan 26, 2016

Member

I tried that but it didn't work for me.

@marcoceppi

marcoceppi Jan 26, 2016

Owner

yeah, I see that. I'd prefer this instead:

>>> cfg
{'vpe-router': None, 'obama': None, 'user': None, 'pass': None}
>>> all(k in cfg for k in ['pass', 'vpe-router', 'user'])
True
>>> del cfg['pass']
>>> cfg
{'vpe-router': None, 'obama': None, 'user': None}
>>> all(k in cfg for k in ['pass', 'vpe-router', 'user'])
False

so

if all(k in cfg for k in ['pass', 'vpe-router', 'user']):
Owner

marcoceppi commented Jan 26, 2016

⛵️

marcoceppi added a commit that referenced this pull request Jan 26, 2016

@marcoceppi marcoceppi merged commit c49eda3 into juju-solutions:master Jan 26, 2016

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