From aca7ce3a277e25b1af933e3ade95a2bc20910dd0 Mon Sep 17 00:00:00 2001 From: Tomaz Muraus Date: Mon, 16 Oct 2017 12:17:56 +0200 Subject: [PATCH 1/2] Update "st2 apikey load" to update an existing entry if one already exists in the database. This way the command is idempotent and consistent with "st2 key load" command. Keep in mind that update will only work if entries in JSON file contain "id" attribute. --- st2client/st2client/commands/auth.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) 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): From 769f655399224ad33a32db33a856519035b50e99 Mon Sep 17 00:00:00 2001 From: Tomaz Muraus Date: Mon, 16 Oct 2017 12:36:17 +0200 Subject: [PATCH 2/2] Add changelog entry. --- CHANGELOG.rst | 6 ++++++ 1 file changed, 6 insertions(+) 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 --------------------------