#!/bin/bash

set -e
set -x

export PATH=$PATH:/var/vcap/packages/cf-cli/bin

CF_API_URL='<%= p("cf.api_url") %>'
CF_ADMIN_USERNAME='<%= p("cf.admin_username") %>'
CF_ADMIN_PASSWORD='<%= p("cf.admin_password") %>'
BROKER_NAME='<%= p("broker.name") %>'
BROKER_URL='<%= p("broker.protocol") %>://<%= p("broker.host") %>'
BROKER_USERNAME='<%= p("broker.username") %>'
BROKER_PASSWORD='<%= p("broker.password") %>'

cf --version
cf api --skip-ssl-validation $CF_API_URL
cf auth $CF_ADMIN_USERNAME $CF_ADMIN_PASSWORD

# Get all service brokers before we register this service
EXISTING_SERVICE_BROKERS="$(cf service-brokers)"

cf create-service-broker $BROKER_NAME $BROKER_USERNAME $BROKER_PASSWORD $BROKER_URL || cf update-service-broker $BROKER_NAME $BROKER_USERNAME $BROKER_PASSWORD $BROKER_URL

<% if p("broker-registrar.orgs").empty? %>

  # If the broker has already been registered, don't do service enablement
  while read line; do
    if [[ "$line" == "$BROKER_NAME "* ]]; then
      echo "Service previously enabled, exiting successfully to avoid overwriting previous org"
      exit 0
    fi
  done <<< "$EXISTING_SERVICE_BROKERS"

  cf enable-service-access $BROKER_NAME
<% else %>
  <% p("broker-registrar.orgs").each do |org| %>
    cf enable-service-access $BROKER_NAME -o <%= org %>
  <% end %>
<% end %>

#!/bin/bash

set -e
set -x

export PATH=$PATH:/var/vcap/packages/cf-cli/bin

CF_API_URL='<%= p("cf.api_url") %>'
CF_ADMIN_USERNAME='<%= p("cf.admin_username") %>'
CF_ADMIN_PASSWORD='<%= p("cf.admin_password") %>'
BROKER_NAME='<%= p("broker.name") %>'
BROKER_URL='<%= p("broker.protocol") %>://<%= p("broker.host") %>'
BROKER_USERNAME='<%= p("broker.username") %>'
BROKER_PASSWORD='<%= p("broker.password") %>'

cf --version
cf api --skip-ssl-validation $CF_API_URL
cf auth $CF_ADMIN_USERNAME $CF_ADMIN_PASSWORD
cf create-service-broker $BROKER_NAME $BROKER_USERNAME $BROKER_PASSWORD $BROKER_URL || cf update-service-broker $BROKER_NAME $BROKER_USERNAME $BROKER_PASSWORD $BROKER_URL

<% if p("broker-registrar.orgs").empty? %>
  cf enable-service-access $BROKER_NAME
<% else %>
  <% p("broker-registrar.orgs").each do |org| %>
    cf enable-service-access $BROKER_NAME -o <%= org %>
  <% end %>
<% end %>

Revert "Don't enable service in orgs if already registered"
Revert "Don't enable service in orgs if already registered"

This reverts commit 1b818ae620168ec8df2f9ee5fa32c1bc8beb689c.
