Merge pull request #8016 from john-westcott-iv/inventory_insights

Adding insights credential to tower_inventory

Reviewed-by: https://github.com/apps/softwarefactory-project-zuul
This commit is contained in:
softwarefactory-project-zuul[bot]
2020-08-27 16:14:37 +00:00
committed by GitHub
4 changed files with 138 additions and 86 deletions

View File

@@ -48,7 +48,11 @@ options:
type: str type: str
host_filter: host_filter:
description: description:
- The host_filter field. Only useful when C(kind=smart). - The host_filter field. Only useful when C(kind=smart).
type: str
insights_credential:
description:
- Credentials to be used by hosts belonging to this inventory when accessing Red Hat Insights API.
type: str type: str
state: state:
description: description:
@@ -84,6 +88,7 @@ def main():
variables=dict(type='dict'), variables=dict(type='dict'),
kind=dict(choices=['', 'smart'], default=''), kind=dict(choices=['', 'smart'], default=''),
host_filter=dict(), host_filter=dict(),
insights_credential=dict(),
state=dict(choices=['present', 'absent'], default='present'), state=dict(choices=['present', 'absent'], default='present'),
) )
@@ -98,6 +103,7 @@ def main():
state = module.params.get('state') state = module.params.get('state')
kind = module.params.get('kind') kind = module.params.get('kind')
host_filter = module.params.get('host_filter') host_filter = module.params.get('host_filter')
insights_credential = module.params.get('insights_credential')
# Attempt to look up the related items the user specified (these will fail the module if not found) # Attempt to look up the related items the user specified (these will fail the module if not found)
org_id = module.resolve_name_to_id('organizations', organization) org_id = module.resolve_name_to_id('organizations', organization)
@@ -125,6 +131,8 @@ def main():
inventory_fields['description'] = description inventory_fields['description'] = description
if variables is not None: if variables is not None:
inventory_fields['variables'] = json.dumps(variables) inventory_fields['variables'] = json.dumps(variables)
if insights_credential is not None:
inventory_fields['insights_credential'] = module.resolve_name_to_id('credentials', insights_credential)
# We need to perform a check to make sure you are not trying to convert a regular inventory into a smart one. # We need to perform a check to make sure you are not trying to convert a regular inventory into a smart one.
if inventory and inventory['kind'] == '' and inventory_fields['kind'] == 'smart': if inventory and inventory['kind'] == '' and inventory_fields['kind'] == 'smart':

View File

@@ -58,7 +58,6 @@ needs_development = [
] ]
needs_param_development = { needs_param_development = {
'tower_host': ['instance_id'], 'tower_host': ['instance_id'],
'tower_inventory': ['insights_credential'],
} }
# ----------------------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------------------

View File

@@ -3,20 +3,26 @@ __metaclass__ = type
import pytest import pytest
from awx.main.models import Inventory from awx.main.models import Inventory, Credential
from awx.main.tests.functional.conftest import insights_credential, credentialtype_insights
@pytest.mark.django_db @pytest.mark.django_db
def test_inventory_create(run_module, admin_user, organization): def test_inventory_create(run_module, admin_user, organization, insights_credential):
# Create an insights credential
result = run_module('tower_inventory', { result = run_module('tower_inventory', {
'name': 'foo-inventory', 'name': 'foo-inventory',
'organization': organization.name, 'organization': organization.name,
'variables': {'foo': 'bar', 'another-foo': {'barz': 'bar2'}}, 'variables': {'foo': 'bar', 'another-foo': {'barz': 'bar2'}},
'insights_credential': insights_credential.name,
'state': 'present' 'state': 'present'
}, admin_user) }, admin_user)
assert not result.get('failed', False), result.get('msg', result)
inv = Inventory.objects.get(name='foo-inventory') inv = Inventory.objects.get(name='foo-inventory')
assert inv.variables == '{"foo": "bar", "another-foo": {"barz": "bar2"}}' assert inv.variables == '{"foo": "bar", "another-foo": {"barz": "bar2"}}'
assert inv.insights_credential.name == insights_credential.name
result.pop('module_args', None) result.pop('module_args', None)
result.pop('invocation', None) result.pop('invocation', None)

View File

@@ -1,101 +1,140 @@
--- ---
- name: Generate a test ID
set_fact:
test_id: "{{ lookup('password', '/dev/null chars=ascii_letters length=16') }}"
- name: Generate names - name: Generate names
set_fact: set_fact:
inv_name1: "AWX-Collection-tests-tower_inventory-inv1-{{ lookup('password', '/dev/null chars=ascii_letters length=16') }}" inv_name1: "AWX-Collection-tests-tower_inventory-inv1-{{ test_id }}"
inv_name2: "AWX-Collection-tests-tower_inventory-inv2-{{ lookup('password', '/dev/null chars=ascii_letters length=16') }}" inv_name2: "AWX-Collection-tests-tower_inventory-inv2-{{ test_id }}"
cred_name1: "AWX-Collection-tests-tower_inventory-cred1-{{ test_id }}"
- name: Create an Inventory - block:
tower_inventory: - name: Create an Insights Credential
name: "{{ inv_name1 }}" tower_credential:
organization: Default name: "{{ cred_name1 }}"
state: present organization: Default
register: result kind: insights
inputs:
username: joe
password: secret
state: present
register: result
- assert: - assert:
that: that:
- "result is changed" - "result is changed"
- name: Test Inventory module idempotency - name: Create an Inventory
tower_inventory: tower_inventory:
name: "{{ inv_name1 }}" name: "{{ inv_name1 }}"
organization: Default organization: Default
state: present insights_credential: "{{ cred_name1 }}"
register: result state: present
register: result
- assert: - assert:
that: that:
- "result is not changed" - "result is changed"
- name: Fail Change Regular to Smart - name: Test Inventory module idempotency
tower_inventory: tower_inventory:
name: "{{ inv_name1 }}" name: "{{ inv_name1 }}"
organization: Default organization: Default
kind: smart insights_credential: "{{ cred_name1 }}"
register: result state: present
ignore_errors: true register: result
- assert: - assert:
that: that:
- "result is failed" - "result is not changed"
- name: Create a smart inventory - name: Fail Change Regular to Smart
tower_inventory: tower_inventory:
name: "{{ inv_name2 }}" name: "{{ inv_name1 }}"
organization: Default organization: Default
kind: smart kind: smart
host_filter: name=foo register: result
register: result ignore_errors: true
- assert: - assert:
that: that:
- "result is changed" - "result is failed"
- name: Delete a smart inventory - name: Create a smart inventory
tower_inventory: tower_inventory:
name: "{{ inv_name2 }}" name: "{{ inv_name2 }}"
organization: Default organization: Default
kind: smart kind: smart
host_filter: name=foo host_filter: name=foo
state: absent register: result
register: result
- assert: - assert:
that: that:
- "result is changed" - "result is changed"
- name: Delete an Inventory - name: Delete a smart inventory
tower_inventory: tower_inventory:
name: "{{ inv_name1 }}" name: "{{ inv_name2 }}"
organization: Default organization: Default
state: absent kind: smart
register: result host_filter: name=foo
state: absent
register: result
- assert: - assert:
that: that:
- "result is changed" - "result is changed"
- name: Delete a Non-Existent Inventory - name: Delete an Inventory
tower_inventory: tower_inventory:
name: "{{ inv_name1 }}" name: "{{ inv_name1 }}"
organization: Default organization: Default
state: absent state: absent
register: result register: result
- assert: - assert:
that: that:
- "result is not changed" - "result is changed"
- name: Check module fails with correct msg - name: Delete a Non-Existent Inventory
tower_inventory: tower_inventory:
name: test-inventory name: "{{ inv_name1 }}"
description: Inventory Description organization: Default
organization: test-non-existing-org state: absent
state: present register: result
register: result
ignore_errors: true
- assert: - assert:
that: that:
- "result is not changed" - "result is not changed"
- "result.msg =='Failed to update inventory, organization not found: The requested object could not be found.'
or result.msg =='The organizations test-non-existing-org was not found on the Tower server'" - name: Check module fails with correct msg
tower_inventory:
name: test-inventory
description: Inventory Description
organization: test-non-existing-org
state: present
register: result
ignore_errors: true
- assert:
that:
- "result is not changed"
- "result.msg =='Failed to update inventory, organization not found: The requested object could not be found.'
or result.msg =='The organizations test-non-existing-org was not found on the Tower server'"
always:
- name: Delete Inventories
tower_inventory:
name: "{{ item }}"
organization: Default
state: absent
loop:
- "{{ inv_name1 }}"
- "{{ inv_name2 }}"
- name: Delete Insights Credential
tower_credential:
name: "{{ cred_name1 }}"
organization: "Default"
kind: insights
state: absent