Pamela Dragosh | 0e16acf | 2017-02-14 19:45:48 -0500 | [diff] [blame] | 1 | #! /bin/bash |
| 2 | |
| 3 | ### |
| 4 | # ============LICENSE_START======================================================= |
| 5 | # policy-management |
| 6 | # ================================================================================ |
| 7 | # Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. |
| 8 | # ================================================================================ |
| 9 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | # you may not use this file except in compliance with the License. |
| 11 | # You may obtain a copy of the License at |
| 12 | # |
| 13 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | # |
| 15 | # Unless required by applicable law or agreed to in writing, software |
| 16 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | # See the License for the specific language governing permissions and |
| 19 | # limitations under the License. |
| 20 | # ============LICENSE_END========================================================= |
| 21 | ### |
| 22 | |
| 23 | function usage() { |
| 24 | echo -n "Usage: $(basename $0) " |
| 25 | echo -n "[(-d|--debug)] " |
| 26 | echo -n "(-h|--host) <bus-host> " |
| 27 | echo -n "[(-p|--port) <bus-port>] " |
| 28 | echo -n "[(-k|--key) <api-key>] " |
| 29 | echo -n "[(-s|--secret) <api-secret>] " |
| 30 | echo -n "[(-r|--request-id) <request-id>] " |
| 31 | echo -n "(-c|--controller-name) <controller-name> " |
| 32 | echo -n "(-o|--operation) <create|update|lock|unlock> " |
| 33 | echo -n "[(-g|--group-id) <group-id> " |
| 34 | echo -n "(-a|--artifact-id) <artifact-id> " |
| 35 | echo -n "(-v|--version) <version>] " |
| 36 | echo -n "[(-t|--topic) <topic>] " |
| 37 | echo "" |
| 38 | } |
| 39 | |
| 40 | BUS_PORT=3904 |
| 41 | ENTITY=controller |
| 42 | REQUEST_ID="7f5474ca-16a9-42ac-abc0-d86f62296fbc" |
| 43 | TOPIC="PDPD_CONFIGURATION" |
| 44 | |
| 45 | # command line options parsing |
| 46 | until [[ -z "$1" ]]; do |
| 47 | case $1 in |
| 48 | -d|--debug) set -x |
| 49 | ;; |
| 50 | -h|--host) shift |
| 51 | BUS_HOST=$1 |
| 52 | ;; |
| 53 | -p|--port) shift |
| 54 | BUS_PORT=$1 |
| 55 | ;; |
| 56 | -r|--request-id) shift |
| 57 | REQUEST_ID=$1 |
| 58 | ;; |
| 59 | -k|--key) shift |
| 60 | API_KEY=$1 |
| 61 | ;; |
| 62 | -s|--secret) shift |
| 63 | API_SECRET=$1 |
| 64 | ;; |
| 65 | -c|--controller-name) shift |
| 66 | CONTROLLER_NAME=$1 |
| 67 | ;; |
| 68 | -o|--operation) shift |
| 69 | OPERATION=$1 |
| 70 | ;; |
| 71 | -g|--group-id) shift |
| 72 | GROUP_ID=$1 |
| 73 | ;; |
| 74 | -a|--artifact-id) shift |
| 75 | ARTIFACT_ID=$1 |
| 76 | ;; |
| 77 | -v|--version) shift |
| 78 | VERSION=$1 |
| 79 | ;; |
| 80 | -t|--topic) shift |
| 81 | TOPIC=$1 |
| 82 | ;; |
| 83 | *) usage |
| 84 | exit 1 |
| 85 | ;; |
| 86 | esac |
| 87 | shift |
| 88 | done |
| 89 | |
| 90 | if [[ -z ${BUS_HOST} ]]; then |
| 91 | echo "An UEB/DMAAP server must be provided." |
| 92 | echo |
| 93 | usage |
| 94 | exit 1 |
| 95 | fi |
| 96 | |
| 97 | if [[ -z ${CONTROLLER_NAME} ]]; then |
| 98 | echo "The controller-name must be provided." |
| 99 | usage |
| 100 | exit 2 |
| 101 | fi |
| 102 | |
| 103 | if [[ -z ${OPERATION} ]]; then |
| 104 | echo "The operation must be provided: create|update|lock|unlock" |
| 105 | usage |
| 106 | exit 3 |
| 107 | fi |
| 108 | |
| 109 | if [[ ${OPERATION} == "create" ]] || [[ ${OPERATION} == "update" ]]; then |
| 110 | if [[ -z ${GROUP_ID} ]]; then |
| 111 | echo "The maven group id must be provided when operation is create|update" |
| 112 | usage |
| 113 | exit 4 |
| 114 | fi |
| 115 | |
| 116 | if [[ -z ${ARTIFACT_ID} ]]; then |
| 117 | echo "The maven artifact id must be provided when operation is create|update" |
| 118 | usage |
| 119 | exit 5 |
| 120 | fi |
| 121 | |
| 122 | if [[ -z ${VERSION} ]]; then |
| 123 | echo "The maven version must be provided when operation is create|update" |
| 124 | usage |
| 125 | exit 6 |
| 126 | fi |
| 127 | fi |
| 128 | |
| 129 | UPDATE_BODY=$(< <(cat <<EOF |
| 130 | { |
| 131 | "requestID": "${REQUEST_ID}", |
| 132 | "entity": "controller", |
| 133 | "controllers": [{ |
| 134 | "name": "${CONTROLLER_NAME}", |
| 135 | "drools": { |
| 136 | "groupId": "${GROUP_ID}", |
| 137 | "artifactId": "${ARTIFACT_ID}", |
| 138 | "version": "${VERSION}" |
| 139 | }, |
| 140 | "operation": "${OPERATION}" |
| 141 | }] |
| 142 | } |
| 143 | |
| 144 | EOF |
| 145 | )) |
| 146 | |
| 147 | LOCK_BODY=$(< <(cat <<EOF |
| 148 | { |
| 149 | "requestID": "${REQUEST_ID}", |
| 150 | "entity": "controller", |
| 151 | "controllers": [{ |
| 152 | "name": "${CONTROLLER_NAME}", |
| 153 | "operation": "${OPERATION}" |
| 154 | }] |
| 155 | } |
| 156 | |
| 157 | EOF |
| 158 | )) |
| 159 | |
| 160 | unset http_proxy |
| 161 | |
| 162 | if [[ ${OPERATION} == "lock" ]] || [[ ${OPERATION} == "unlock" ]]; then |
| 163 | if [[ -n ${API_KEY} ]]; then |
| 164 | DATE=$(date) |
| 165 | DATE_HASH=$(echo -n "${DATE}" | openssl sha1 -hmac "${API_SECRET}" -binary | openssl base64) |
| 166 | curl --silent -X POST \ |
| 167 | --header "Accept:" \ |
| 168 | --header "X-CambriaDate: ${DATE}" \ |
| 169 | --header "X-CambriaAuth: ${API_KEY}:${DATE_HASH}" \ |
| 170 | --header "Content-Type: application/json" \ |
| 171 | --data "${LOCK_BODY}" \ |
| 172 | http://${BUS_HOST}:${BUS_PORT}/events/${TOPIC} |
| 173 | else |
| 174 | curl --silent -X POST \ |
| 175 | --header "Accept:" \ |
| 176 | --header "Content-Type: application/json" \ |
| 177 | --data "${LOCK_BODY}" \ |
| 178 | http://${BUS_HOST}:${BUS_PORT}/events/${TOPIC} |
| 179 | fi |
| 180 | fi |
| 181 | |
| 182 | if [[ ${OPERATION} == "create" ]] || [[ ${OPERATION} == "update" ]]; then |
| 183 | if [[ -n ${API_KEY} ]]; then |
| 184 | DATE=$(date) |
| 185 | DATE_HASH=$(echo -n "${DATE}" | openssl sha1 -hmac "${API_SECRET}" -binary | openssl base64) |
| 186 | curl --silent -X POST \ |
| 187 | --header "Accept:" \ |
| 188 | --header "X-CambriaDate: ${DATE}" \ |
| 189 | --header "X-CambriaAuth: ${API_KEY}:${DATE_HASH}" \ |
| 190 | --header "Content-Type: application/json" \ |
| 191 | --data "${UPDATE_BODY}" \ |
| 192 | http://${BUS_HOST}:${BUS_PORT}/events/${TOPIC} |
| 193 | else |
| 194 | curl --silent -X POST \ |
| 195 | --header "Accept:" \ |
| 196 | --header "Content-Type: application/json" \ |
| 197 | --data "${UPDATE_BODY}" \ |
| 198 | http://${BUS_HOST}:${BUS_PORT}/events/${TOPIC} |
| 199 | fi |
| 200 | fi |