Skip to content
This repository has been archived by the owner on Apr 22, 2020. It is now read-only.

Commit

Permalink
Merge pull request #2 from nabcos/verify-config
Browse files Browse the repository at this point in the history
Set up slightly more helpful messages when the configuration is bad
  • Loading branch information
hjacobs committed Aug 31, 2015
2 parents ee64bb6 + 9fc91ae commit c6cbd2a
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions zmon_cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,25 @@ def get_config_data():
if os.path.exists(fn):
with open(fn) as fd:
data = yaml.safe_load(fd)
else:
raise Exception("Config file not found: ~/.zmon-cli.yaml")

return validate_config(data)


def validate_config(data):
if "user" not in data or "password" not in data:
raise Exception("Config file not found/properly configured: ~/.zmon-cli.yaml with user: and password:")
raise Exception("Config file not properly configured: keys 'user' and 'password' are missing")
if "url" not in data:
raise Exception("Config file not properly configured: key 'url' is missing")

return data


def get(url):
data = get_config_data()
try:
return requests.get(data['url']+url, auth=HTTPBasicAuth(data['user'], data['password']))
return requests.get(data['url'] + url, auth=HTTPBasicAuth(data['user'], data['password']))
except Exception as e:
logging.error(e)

Expand All @@ -159,7 +167,7 @@ def get(url):
def put(url, body):
data = get_config_data()
try:
return requests.put(data['url']+url, data=body, auth=HTTPBasicAuth(data['user'], data['password']),
return requests.put(data['url'] + url, data=body, auth=HTTPBasicAuth(data['user'], data['password']),
headers={'content-type': 'application/json'})
except Exception as e:
logging.error(e)
Expand All @@ -170,7 +178,7 @@ def put(url, body):
def delete(url):
data = get_config_data()
try:
return requests.delete(data['url']+url, auth=HTTPBasicAuth(data['user'], data['password']))
return requests.delete(data['url'] + url, auth=HTTPBasicAuth(data['user'], data['password']))
except Exception as e:
logging.error(e)

Expand Down

0 comments on commit c6cbd2a

Please sign in to comment.