Skip to content

Commit

Permalink
Fix consul_token usage without accessor_id. (#8091)
Browse files Browse the repository at this point in the history
* Fix consul_token usage without accessor_id.

* Update changelogs/fragments/8091-consul-token-fixes.yaml

Co-authored-by: Felix Fontein <[email protected]>

* Update plugins/modules/consul_token.py

Co-authored-by: Felix Fontein <[email protected]>

---------

Co-authored-by: Felix Fontein <[email protected]>
  • Loading branch information
apollo13 and felixfontein authored Mar 14, 2024
1 parent 2262075 commit 67736d7
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
2 changes: 2 additions & 0 deletions changelogs/fragments/8091-consul-token-fixes.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- "consul_token - fix token creation without ``accessor_id`` (https://github.com/ansible-collections/community.general/pull/8091)."
2 changes: 1 addition & 1 deletion plugins/module_utils/consul.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ def _request(self, method, url_parts, data=None, params=None):
headers["X-Consul-Token"] = token

try:
if data:
if data is not None:
data = json.dumps(data)
headers["Content-Type"] = "application/json"
if params:
Expand Down
6 changes: 6 additions & 0 deletions plugins/modules/consul_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,12 @@ class ConsulTokenModule(_ConsulModule):

create_only_fields = {"expiration_ttl"}

def read_object(self):
# if `accessor_id` is not supplied we can only create objects and are not idempotent
if not self.params.get(self.unique_identifier):
return None
return super(ConsulTokenModule, self).read_object()

def needs_update(self, api_obj, module_obj):
# SecretID is usually not supplied
if "SecretID" not in module_obj and "SecretID" in api_obj:
Expand Down
11 changes: 11 additions & 0 deletions tests/integration/targets/consul/tasks/consul_token.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@
- foo-access
- foo-access2

- name: Create token without accessor
community.general.consul_token:
state: present
register: simple_create_result

- assert:
that:
- simple_create_result is changed
- simple_create_result.token.AccessorID
- simple_create_result.operation == 'create'

- name: Create token
community.general.consul_token:
state: present
Expand Down

0 comments on commit 67736d7

Please sign in to comment.