#
# SPDX-License-Identifier: Apache-2.0
#
---
- name: Install and approve chaincode
  hosts: localhost
  vars:
    wallet: "/_cfg"
  vars_files:
    - /_cfg/asset-transfer-chaincode-vars.yml
    - /_cfg/fabric-common-vars.yml
    - /_cfg/fabric-org2-vars.yml
    - /_cfg/auth-vars.yml
  tasks:
    - name: Install the chaincode on the peer
      ibm.blockchain_platform.installed_chaincode:
        api_endpoint: "{{ api_endpoint }}"
        api_authtype: "{{ api_authtype }}"
        api_key: "{{ api_key }}"
        api_secret: "{{ api_secret | default(omit) }}"
        api_token_endpoint: "{{ api_token_endpoint | default(omit) }}"
        peer: "{{ org2_peer_name }}"
        identity: "{{ wallet }}/{{ org2_name }} Admin.json"
        msp_id: "{{ org2_msp_id }}"
        path: "{{ wallet }}/{{ smart_contract_package }}"
      register: result

    - name: Approve the chaincode on the channel
      ibm.blockchain_platform.approved_chaincode:
        api_endpoint: "{{ api_endpoint }}"
        api_authtype: "{{ api_authtype }}"
        api_key: "{{ api_key }}"
        api_secret: "{{ api_secret | default(omit) }}"
        api_token_endpoint: "{{ api_token_endpoint | default(omit) }}"
        peer: "{{ org2_peer_name }}"
        identity: "{{ wallet }}/{{ org2_name }} Admin.json"
        msp_id: "{{ org2_msp_id }}"
        channel: "{{ channel_name }}"
        name: "{{ smart_contract_name }}"
        version: "{{ smart_contract_version }}"
        package_id: "{{ result.installed_chaincode.package_id }}"
        sequence: "{{ smart_contract_sequence }}"
        endorsement_policy: "{{ smart_contract_endorsement_policy | default(omit) }}"
        collections_config: "{{ smart_contract_collections_file | default(omit) }}"
