---

- name: "Get all meta_data"
  delegate_to: 127.0.0.1
  become: false
  uri:
    url: "{{ harbor_external_url }}/api/v2.0/projects/{{ project_name }}/metadatas/{{ meta_data_elem.key }}"
    user: '{{ harbor_admin_username }}'
    password: '{{ harbor_admin_password }}'
    method: GET
    body_format: json
    force_basic_auth: yes
    headers:
      Content-Type: application/json
    status_code: [200]
  register: all_metadata
  delay: 10
  retries: 3

- name: "Set fact"
  set_fact:
    body_content: "{ \"{{ meta_data_elem.key }}\":\"{{ meta_data_elem.value }}\" }"

- name: "Add meta_data: <<{{ meta_data_elem.key }}>>"
  delegate_to: 127.0.0.1
  become: false
  uri:
    url: "{{ harbor_external_url }}/api/v2.0/projects/{{ project_name }}/metadatas"
    user: '{{ harbor_admin_username }}'
    password: '{{ harbor_admin_password }}'
    method: POST
    body_format: json
    body: '{{ body_content }}'
    force_basic_auth: yes
    headers:
      Content-Type: application/json
    status_code: [200,201]
  register: create_metadata
  delay: 10
  retries: 3
  until: create_metadata.status in [200,201]
  when: 
    - meta_data_elem.key not in all_metadata.json

- name: "Update meta_data: <<{{ meta_data_elem.key }}>>"
  delegate_to: 127.0.0.1
  become: false
  uri:
    url: "{{ harbor_external_url }}/api/v2.0/projects/{{ project_name }}/metadatas/{{ meta_data_elem.key }}"
    user: '{{ harbor_admin_username }}'
    password: '{{ harbor_admin_password }}'
    method: PUT
    body_format: json
    body: '{{ body_content }}'
    force_basic_auth: yes
    headers:
      Content-Type: application/json
    status_code: [200,201]
  register: update_metadata
  delay: 10
  retries: 3
  until: update_metadata.status in [200,201]
  when: 
    - meta_data_elem.key in all_metadata.json

# DELETION currently out-of-scope
