---

# Fact setting and validations
- name: Set default image variables based on deployment type
  include_vars: "{{ item }}"
  with_first_found:
    - "{{ openshift_deployment_type | default(deployment_type) }}.yml"
    - "default_images.yml"

- name: set ansible_service_broker facts
  set_fact:
    ansible_service_broker_image_prefix: "{{ ansible_service_broker_image_prefix | default(__ansible_service_broker_image_prefix) }}"
    ansible_service_broker_image_tag: "{{ ansible_service_broker_image_tag | default(__ansible_service_broker_image_tag) }}"

    ansible_service_broker_etcd_image_prefix: "{{ ansible_service_broker_etcd_image_prefix | default(__ansible_service_broker_etcd_image_prefix) }}"
    ansible_service_broker_etcd_image_tag: "{{ ansible_service_broker_etcd_image_tag | default(__ansible_service_broker_etcd_image_tag) }}"
    ansible_service_broker_etcd_image_etcd_path: "{{ ansible_service_broker_etcd_image_etcd_path | default(__ansible_service_broker_etcd_image_etcd_path) }}"

    ansible_service_broker_registry_type: "{{ ansible_service_broker_registry_type | default(__ansible_service_broker_registry_type) }}"
    ansible_service_broker_registry_url: "{{ ansible_service_broker_registry_url | default(__ansible_service_broker_registry_url) }}"
    ansible_service_broker_registry_user: "{{ ansible_service_broker_registry_user | default(__ansible_service_broker_registry_user) }}"
    ansible_service_broker_registry_password: "{{ ansible_service_broker_registry_password | default(__ansible_service_broker_registry_password) }}"
    ansible_service_broker_registry_organization: "{{ ansible_service_broker_registry_organization | default(__ansible_service_broker_registry_organization) }}"

- name: set ansible-service-broker image facts using set prefix and tag
  set_fact:
    ansible_service_broker_image: "{{ ansible_service_broker_image_prefix }}ansible-service-broker:{{ ansible_service_broker_image_tag }}"
    ansible_service_broker_etcd_image: "{{ ansible_service_broker_etcd_image_prefix }}etcd:{{ ansible_service_broker_etcd_image_tag }}"

- include: validate_facts.yml


# Deployment of ansible-service-broker starts here
- name: create openshift-ansible-service-broker project
  oc_project:
    name: openshift-ansible-service-broker
    state: present

- name: create ansible-service-broker serviceaccount
  oc_serviceaccount:
    name: asb
    namespace: openshift-ansible-service-broker
    state: present

- name: Set SA cluster-role
  oc_adm_policy_user:
    state: present
    namespace: "openshift-ansible-service-broker"
    resource_kind: cluster-role
    resource_name: admin
    user: "system:serviceaccount:openshift-ansible-service-broker:asb"

- name: create ansible-service-broker service
  oc_service:
    name: asb
    namespace: openshift-ansible-service-broker
    state: present
    labels:
      app: openshift-ansible-service-broker
      service: asb
    ports:
      - name: port-1338
        port: 1338
    selector:
      app: openshift-ansible-service-broker
      service: asb

- name: create etcd service
  oc_service:
    name: etcd
    namespace: openshift-ansible-service-broker
    state: present
    ports:
      - name: etcd-advertise
        port: 2379
    selector:
      app: openshift-ansible-service-broker
      service: etcd

- name: create route for ansible-service-broker service
  oc_route:
    name: asb-1338
    namespace: openshift-ansible-service-broker
    state: present
    service_name: asb
    port: 1338
  register: asb_route_out

- name: get ansible-service-broker route name
  set_fact:
    ansible_service_broker_route: "{{ asb_route_out.results.results[0].spec.host }}"

- name: create persistent volume claim for etcd
  oc_obj:
    name: etcd
    namespace: openshift-ansible-service-broker
    state: present
    kind: PersistentVolumeClaim
    content:
      path: /tmp/dcout
      data:
        apiVersion: v1
        kind: PersistentVolumeClaim
        metadata:
          name: etcd
          namespace: openshift-ansible-service-broker
        spec:
          accessModes:
            - ReadWriteOnce
          resources:
            requests:
              storage: 1Gi

- name: create etcd deployment
  oc_obj:
    name: etcd
    namespace: openshift-ansible-service-broker
    state: present
    kind: Deployment
    content:
      path: /tmp/dcout
      data:
        apiVersion: extensions/v1beta1
        kind: Deployment
        metadata:
          name: etcd
          namespace: openshift-ansible-service-broker
          labels:
            app: openshift-ansible-service-broker
            service: etcd
        spec:
          selector:
            matchLabels:
              app: openshift-ansible-service-broker
              service: etcd
          strategy:
            type: RollingUpdate
            rollingUpdate:
              maxSurge: 1
              maxUnavailable: 1
          replicas: 1
          template:
            metadata:
              labels:
                app: openshift-ansible-service-broker
                service: etcd
            spec:
              restartPolicy: Always
              containers:
                - image: "{{ ansible_service_broker_etcd_image }}"
                  name: etcd
                  imagePullPolicy: IfNotPresent
                  terminationMessagePath: /tmp/termination-log
                  workingDir: /etcd
                  args:
                    - '{{ ansible_service_broker_etcd_image_etcd_path }}'
                    - --data-dir=/data
                    - "--listen-client-urls=http://0.0.0.0:2379"
                    - "--advertise-client-urls=http://0.0.0.0:2379"
                  ports:
                    - containerPort: 2379
                      protocol: TCP
                  env:
                    - name: ETCDCTL_API
                      value: "3"
                  volumeMounts:
                    - mountPath: /data
                      name: etcd
              volumes:
                - name: etcd
                  persistentVolumeClaim:
                    claimName: etcd

- name: create ansible-service-broker deployment
  oc_obj:
    name: asb
    namespace: openshift-ansible-service-broker
    state: present
    kind: Deployment
    content:
      path: /tmp/dcout
      data:
        apiVersion: extensions/v1beta1
        kind: Deployment
        metadata:
          name: asb
          namespace: openshift-ansible-service-broker
          labels:
            app: openshift-ansible-service-broker
            service: asb
        spec:
          strategy:
            type: Recreate
          replicas: 1
          template:
            metadata:
              labels:
                app: openshift-ansible-service-broker
                service: asb
            spec:
              serviceAccount: asb
              restartPolicy: Always
              containers:
                - image: "{{ ansible_service_broker_image }}"
                  name: asb
                  imagePullPolicy: IfNotPresent
                  volumeMounts:
                    - name: config-volume
                      mountPath: /etc/ansible-service-broker
                  ports:
                    - containerPort: 1338
                      protocol: TCP
                  env:
                    - name: BROKER_CONFIG
                      value: /etc/ansible-service-broker/config.yaml
                  terminationMessagePath: /tmp/termination-log
              volumes:
                - name: config-volume
                  configMap:
                    name: broker-config
                    items:
                      - key: broker-config
                        path: config.yaml


# TODO: saw a oc_configmap in the library, but didn't understand how to get it to do the following:
- name: Create config map for ansible-service-broker
  oc_obj:
    name: broker-config
    namespace: openshift-ansible-service-broker
    state: present
    kind: ConfigMap
    content:
      path: /tmp/cmout
      data:
        apiVersion: v1
        kind: ConfigMap
        metadata:
          name: broker-config
          namespace: openshift-ansible-service-broker
          labels:
            app: ansible-service-broker
        data:
          broker-config: |
            registry:
              name: "{{ ansible_service_broker_registry_type }}"
              url:  "{{ ansible_service_broker_registry_url }}"
              user: "{{ ansible_service_broker_registry_user }}"
              pass: "{{ ansible_service_broker_registry_password }}"
              org:  "{{ ansible_service_broker_registry_organization }}"
            dao:
              etcd_host: etcd
              etcd_port: 2379
            log:
              logfile: /var/log/ansible-service-broker/asb.log
              stdout: true
              level: "{{ ansible_service_broker_log_level }}"
              color: true
            openshift: {}
            broker:
              dev_broker: {{ ansible_service_broker_dev_broker | bool | lower }}
              launch_apb_on_bind: {{ ansible_service_broker_launch_apb_on_bind | bool | lower }}
              recovery: {{ ansible_service_broker_recovery | bool | lower }}
              output_request: {{ ansible_service_broker_output_request | bool | lower }}
              bootstrap_on_startup: {{ ansible_service_broker_bootstrap_on_startup | bool | lower }}

- name: Create the Broker resource in the catalog
  oc_obj:
    name: ansible-service-broker
    state: present
    kind: Broker
    content:
      path: /tmp/brokerout
      data:
        apiVersion: servicecatalog.k8s.io/v1alpha1
        kind: Broker
        metadata:
          name: ansible-service-broker
        spec:
          url: http://asb.openshift-ansible-service-broker.svc:1338
