Skip to content

Commit

Permalink
Roles import optimatization by modifying the export + 2 minor bugs (#884
Browse files Browse the repository at this point in the history
)

* misc: roles export optimization, fix approval role for users, fix admin roles

* fix: typo

* fix: add missing changelog

* fix: remove new lines

* fix: add missing new line

---------

Co-authored-by: Przemyslaw Kalitowski <[email protected]>
Co-authored-by: Ivan Aragonés Muniesa <[email protected]>
Co-authored-by: David Danielsson <[email protected]>
  • Loading branch information
4 people authored Aug 6, 2024
1 parent 618e677 commit c966615
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
minor_changes:
- filetree_create able export proper approval role (user roles)
- filetree_create able to bulk export role for objects
33 changes: 28 additions & 5 deletions roles/filetree_create/tasks/team_roles.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,32 @@
}}"
no_log: "{{ controller_configuration_filetree_create_secure_logging }}"

- name: "Map objects with roles (block)"
when: team_roles_lookvar | length > 0
block:
- name: "Get role and object types and define object_roles variable"
ansible.builtin.set_fact:
role_types: "{{ team_roles_lookvar | map(attribute='name') | unique }}"
object_types: "{{ team_roles_lookvar | map(attribute='summary_fields.resource_type') | unique }}"
object_roles: []

- name: "Match objects with roles"
when: (team_roles_lookvar | selectattr('name','equalto', item.0) | selectattr('summary_fields.resource_type','equalto', item.1) | map(attribute='summary_fields.resource_name')) | length > 0
ansible.builtin.set_fact:
object_roles: >-
{{ object_roles +
[{ item.0:
{
'resource_names': (team_roles_lookvar |
selectattr('name','equalto', item.0) |
selectattr('summary_fields.resource_type','equalto', item.1) |
map(attribute='summary_fields.resource_name')),
'resource_type': item.1,
}
}]
}}"
loop: "{{ role_types | product(object_types) | list }}"

- name: "Block for to generate flatten output"
when:
- flatten_output is defined
Expand All @@ -28,9 +54,8 @@
marker: ""
block: "{{ lookup('template', 'templates/current_team_roles.j2') }}"
vars:
current_team_roles_asset_value: "{{ team_roles_lookvar }}"
first_team_role: "{{ not team_roles_file.stat.exists }}"
when: team_roles_lookvar | length > 0
when: object_roles | length > 0

- name: "Remove all the blank lines introduced by the last task"
ansible.builtin.lineinfile:
Expand All @@ -52,7 +77,5 @@
src: "templates/current_team_roles.j2"
dest: "{{ output_path }}/team_roles/current_roles_{{ teamname | regex_replace('/', '_') }}.yaml"
mode: '0644'
vars:
current_team_roles_asset_value: "{{ team_roles_lookvar }}"
when: team_roles_lookvar | length > 0
when: object_roles | length > 0
...
33 changes: 28 additions & 5 deletions roles/filetree_create/tasks/user_roles.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,32 @@
}}"
no_log: "{{ controller_configuration_filetree_create_secure_logging }}"

- name: "Map objects with roles (block)"
when: user_roles_lookvar | length > 0
block:
- name: "Get role and object types and define object_roles variable"
ansible.builtin.set_fact:
role_types: "{{ user_roles_lookvar | selectattr('name', 'defined') | map(attribute='name') | unique }}"
object_types: "{{ user_roles_lookvar | selectattr('summary_fields.resource_type', 'defined') | map(attribute='summary_fields.resource_type') | unique }}"
object_roles: []

- name: "Match objects with roles"
when: (user_roles_lookvar | selectattr('name','equalto', item.0) | selectattr('summary_fields.resource_type', 'defined') | selectattr('summary_fields.resource_type','equalto', item.1) | map(attribute='summary_fields.resource_name')) | length > 0
ansible.builtin.set_fact:
object_roles: >-
{{ object_roles +
[{ item.0:
{
'resource_names': (user_roles_lookvar |
selectattr('name','equalto', item.0) |
selectattr('summary_fields.resource_type','equalto', item.1) |
map(attribute='summary_fields.resource_name')),
'resource_type': item.1,
}
}]
}}
loop: "{{ role_types | product(object_types) | list }}"

- name: "Block for to generate flatten output"
when:
- flatten_output is defined
Expand All @@ -32,9 +58,8 @@
marker: ""
block: "{{ lookup('template', 'templates/current_user_roles.j2') }}"
vars:
current_user_roles_asset_value: "{{ user_roles_lookvar }}"
first_user_role: "{{ not user_roles_file.stat.exists }}"
when: user_roles_lookvar | length > 0
when: object_roles | length > 0

- name: "Remove all the blank lines introduced by the last task"
ansible.builtin.lineinfile:
Expand All @@ -56,7 +81,5 @@
src: "templates/current_user_roles.j2"
dest: "{{ output_path }}/user_roles/current_roles_{{ username | regex_replace('/', '_') }}.yaml"
mode: '0644'
vars:
current_user_roles_asset_value: "{{ user_roles_lookvar }}"
when: user_roles_lookvar | length > 0
when: object_roles | length > 0
...
39 changes: 21 additions & 18 deletions roles/filetree_create/templates/current_team_roles.j2
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,30 @@
---
controller_roles:
{% endif %}
{% for role in team_roles_lookvar %}
{% if role.summary_fields.resource_type is defined %}
- team: "{{ teamname }}"
{% if role.summary_fields.resource_type is match('organization') %}
{% for role in object_roles %}
{% if (role|dict2items)[0].value.resource_type is defined %}
- team: "{{ (role|dict2items)[0].value.team_name }}"
{% if (role|dict2items)[0].value.resource_names | length > 0 %}
{% if (role|dict2items)[0].value.resource_type is match('organization') %}
organizations:
- "{{ role.summary_fields.resource_name }}"
{% elif role.summary_fields.resource_type is match('team') %}
{% elif (role|dict2items)[0].value.resource_type is match('team') %}
target_teams:
- "{{ role.summary_fields.resource_name }}"
{% elif role.summary_fields.resource_type is match('job_template') %}
job_template: "{{ role.summary_fields.resource_name }}"
{% elif role.summary_fields.resource_type is match('inventory') %}
inventory: "{{ role.summary_fields.resource_name }}"
{% elif role.summary_fields.resource_type is match('workflow_job_template') %}
workflow_job_template: "{{ role.summary_fields.resource_name }}"
{% elif role.summary_fields.resource_type is match('project') %}
project: "{{ role.summary_fields.resource_name }}"
{% elif role.summary_fields.resource_type is match('credential') %}
credential: "{{ role.summary_fields.resource_name }}"
{% elif (role|dict2items)[0].value.resource_type is match('job_template') %}
job_templates:
{% elif (role|dict2items)[0].value.resource_type is match('inventory') %}
inventories:
{% elif (role|dict2items)[0].value.resource_type is match('workflow_job_template') %}
workflow_job_templates:
{% elif (role|dict2items)[0].value.resource_type is match('project') %}
projects:
{% elif (role|dict2items)[0].value.resource_type is match('credential') %}
credentials:
{% endif %}
role: "{% if role.name | lower == 'approve' %}approval{% else %}{{ role.name | lower }}{% endif %}"
{% for object_name in (role|dict2items)[0].value.resource_names %}
- "{{ object_name }}"
{% endfor %}
{% endif %}
role: "{% if (role|dict2items)[0].key | lower == 'approve' %}approval{% else %}{{ (role|dict2items)[0].key | lower | regex_replace(' ', '_') }}{% endif %}"
{% endif %}
{% endfor %}
{% if last_team_role | default(true) | bool %}
Expand Down
37 changes: 20 additions & 17 deletions roles/filetree_create/templates/current_user_roles.j2
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,30 @@
---
controller_roles:
{% endif %}
{% for role in user_roles_lookvar %}
{% if role.summary_fields.resource_type is defined %}
{% for role in object_roles %}
{% if (role|dict2items)[0].value.resource_type is defined %}
- user: "{{ username }}"
{% if role.summary_fields.resource_type is match('organization') %}
{% if (role|dict2items)[0].value.resource_names | length > 0 %}
{% if (role|dict2items)[0].value.resource_type is match('organization') %}
organizations:
- "{{ role.summary_fields.resource_name }}"
{% elif role.summary_fields.resource_type is match('team') %}
{% elif (role|dict2items)[0].value.resource_type is match('team') %}
target_teams:
- "{{ role.summary_fields.resource_name }}"
{% elif role.summary_fields.resource_type is match('job_template') %}
job_template: "{{ role.summary_fields.resource_name }}"
{% elif role.summary_fields.resource_type is match('inventory') %}
inventory: "{{ role.summary_fields.resource_name }}"
{% elif role.summary_fields.resource_type is match('workflow_job_template') %}
workflow_job_template: "{{ role.summary_fields.resource_name }}"
{% elif role.summary_fields.resource_type is match('project') %}
project: "{{ role.summary_fields.resource_name }}"
{% elif role.summary_fields.resource_type is match('credential') %}
credential: "{{ role.summary_fields.resource_name }}"
{% elif (role|dict2items)[0].value.resource_type is match('job_template') %}
job_templates:
{% elif (role|dict2items)[0].value.resource_type is match('inventory') %}
inventories:
{% elif (role|dict2items)[0].value.resource_type is match('workflow_job_template') %}
workflow_job_templates:
{% elif (role|dict2items)[0].value.resource_type is match('project') %}
projects:
{% elif (role|dict2items)[0].value.resource_type is match('credential') %}
credentials:
{% endif %}
role: "{{ role.name | lower }}"
{% for object_name in (role|dict2items)[0].value.resource_names %}
- "{{ object_name }}"
{% endfor %}
{% endif %}
role: "{% if (role|dict2items)[0].key | lower == 'approve' %}approval{% else %}{{ (role|dict2items)[0].key | lower | regex_replace(' ', '_') }}{% endif %}"
{% endif %}
{% endfor %}
{% if last_user_role | default(true) | bool %}
Expand Down

0 comments on commit c966615

Please sign in to comment.