---
## The purpose of this task file is to take in a list of configmap files provided
##  in the variable configmap_file_names, which correspond to the data sections
##  within a configmap. We iterate over each of these files and create a patch
##  from the diff between current_file and new_file to try to maintain any custom
##  changes that a user may have made to a currently deployed configmap while
##  trying to idempotently update with any role provided files.

## The following variables are expected to be provided when including this task:
# configmap_name        -- This is the name of the configmap that the files exist in
# configmap_namespace   -- The namespace that the configmap lives in
# configmap_file_names  -- This is expected to be passed in as a dict
#   current_file        -- The name of the data entry within the configmap
#   new_file            -- The file path to the file we are comparing to current_file
#   protected_lines     -- List of variables whose line will be excluded when creating a diff

- oc_configmap:
    name: "{{ configmap_name }}"
    state: list
    namespace: "{{ configmap_namespace }}"
  register: __configmap_output

- when: __configmap_output.module_results.stderr is undefined
  include_tasks: patch_configmap_file.yaml
  vars:
    configmap_current_file: "{{ configmap_files.current_file }}"
    configmap_new_file: "{{ configmap_files.new_file }}"
    configmap_protected_lines: "{{ configmap_files.protected_lines | default([]) }}"
  with_items: "{{ configmap_file_names }}"
  loop_control:
    loop_var: configmap_files
