Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MGMT-14094: Manage case when no resources are found in OCI #2272

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ansible_files/roles/oci/cleanup_resources/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ excluded_types:
- oci_load_balancer_backend
- oci_load_balancer_backend_set
- oci_load_balancer_listener
- oci_network_load_balancer_backend
- oci_network_load_balancer_backend_set
- oci_network_load_balancer_listener
expired_after_hours: 6
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
- name: Load terraform state
ansible.builtin.command:
cmd: terraform show -json
chdir: "{{ terraform_working_dir }}"
register: terraform_state
check_mode: false
changed_when: false

- name: Convert terraform state to dict
ansible.builtin.set_fact:
terraform_resources: "{{ (terraform_state.stdout | from_json)['values']['root_module']['resources'] | default([]) }}"
excluded_resources: []

- name: Exclude types from terraform state
ansible.builtin.set_fact:
excluded_resources: "{{ excluded_resources + [item] }}"
loop: "{{ terraform_resources }}"
when: item.type in excluded_types

- name: Exclude recently created resources from terraform state
ansible.builtin.set_fact:
excluded_resources: "{{ excluded_resources + [item] }}"
loop: "{{ terraform_resources }}"
when:
- item["values"]["defined_tags"]["Oracle-Tags.CreatedOn"] is defined
- >-
(
now(utc=true).replace(tzinfo=None)
-
(item["values"]["defined_tags"]["Oracle-Tags.CreatedOn"] | ansible.builtin.to_datetime("%Y-%m-%dT%H:%M:%S.%fZ")).replace(tzinfo=None)
).total_seconds() / 3600 < expired_after_hours

- name: Remove excluded resources from terraform state
ansible.builtin.command:
cmd: terraform state rm {{ excluded_resources | json_query('[].address') | unique | join(" ") }}
chdir: "{{ terraform_working_dir }}"
when: excluded_resources | length > 0
changed_when: true

- name: Destroy expired resources
community.general.terraform:
project_path: "{{ terraform_working_dir }}"
state: absent
register: result
until: "result is not failed"
retries: 10
delay: 10
56 changes: 9 additions & 47 deletions ansible_files/roles/oci/cleanup_resources/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,50 +35,12 @@
chdir: "{{ terraform_working_dir }}"
check_mode: false

- name: Load terraform state
ansible.builtin.command:
cmd: terraform show -json
chdir: "{{ terraform_working_dir }}"
register: terraform_state
check_mode: false
changed_when: false

- name: Convert terraform state to dict
ansible.builtin.set_fact:
terraform_resources: "{{ (terraform_state.stdout | from_json)['values']['root_module']['resources'] | default([]) }}"
excluded_resources: []

- name: Exclude types from terraform state
ansible.builtin.set_fact:
excluded_resources: "{{ excluded_resources + [item] }}"
loop: "{{ terraform_resources }}"
when: item.type in excluded_types

- name: Exclude recently created resources from terraform state
ansible.builtin.set_fact:
excluded_resources: "{{ excluded_resources + [item] }}"
loop: "{{ terraform_resources }}"
when:
- item["values"]["defined_tags"]["Oracle-Tags.CreatedOn"] is defined
- >-
(
now(utc=true).replace(tzinfo=None)
-
(item["values"]["defined_tags"]["Oracle-Tags.CreatedOn"] | ansible.builtin.to_datetime("%Y-%m-%dT%H:%M:%S.%fZ")).replace(tzinfo=None)
).total_seconds() / 3600 < expired_after_hours

- name: Remove excluded resources from terraform state
ansible.builtin.command:
cmd: terraform state rm {{ excluded_resources | json_query('[].address') | unique | join(" ") }}
chdir: "{{ terraform_working_dir }}"
when: excluded_resources | length > 0
changed_when: true

- name: Destroy expired resources
community.general.terraform:
project_path: "{{ terraform_working_dir }}"
state: absent
register: result
until: "result is not failed"
retries: 10
delay: 10
- name: Check terraform state file
ansible.builtin.stat:
path: "{{ [terraform_working_dir, 'terraform.tfstate'] | path_join }}"
register: terraform_state_file_result

- name: Cleanup resources when a terraform state file exists
ansible.builtin.include_tasks:
file: cleanup_resources.yml
when: terraform_state_file_result.stat.exists