diff --git a/CHANGELOG.rst b/CHANGELOG.rst index ee3253e80e..e9b99d6c39 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -79,6 +79,12 @@ Fixed Reported by Carlos. * Fix action-alias execute response to show execution id and matching action-alias #3231 (bug fix) Reported by Carlos. +* Fix ``st2 apikey load`` command to update an existing entry if items in input file contain ``id`` + attribute and item already exists on the server. This way the behavior is consistent with + ``st2 key load`` command and the command is idempotent if each item contains ``id`` attribute. + #3748 #3786 + + Reported by Christopher Baklid. 2.4.1 - September 12, 2017 -------------------------- diff --git a/st2client/st2client/commands/auth.py b/st2client/st2client/commands/auth.py index f6dfe531b4..a26c4161cf 100644 --- a/st2client/st2client/commands/auth.py +++ b/st2client/st2client/commands/auth.py @@ -17,7 +17,9 @@ import json import logging +import requests from six.moves.configparser import ConfigParser +from six.moves import http_client from st2client.base import BaseCLIApp from st2client import config_parser @@ -339,14 +341,29 @@ def run(self, args, **kwargs): instances = [] for res in resources: # pick only the meaningful properties. - instance = { + data = { 'user': res['user'], # required 'key_hash': res['key_hash'], # required 'metadata': res.get('metadata', {}), 'enabled': res.get('enabled', False) } - instance = self.resource.deserialize(instance) - instances.append(self.manager.create(instance, **kwargs)) + + if 'id' in res: + data['id'] = res['id'] + + instance = self.resource.deserialize(data) + + try: + result = self.manager.update(instance, **kwargs) + except requests.exceptions.HTTPError as e: + if e.response.status_code == http_client.NOT_FOUND: + instance = self.resource.deserialize(data) + # Key doesn't exist yet, create it instead + result = self.manager.create(instance, **kwargs) + else: + raise e + + instances.append(result) return instances def run_and_print(self, args, **kwargs):