github.com/GoogleCloudPlatform/testgrid@v0.0.174/cluster/create-topic.sh (about) 1 #!/usr/bin/env bash 2 # Copyright 2021 The TestGrid Authors. 3 # 4 # Licensed under the Apache License, Version 2.0 (the "License"); 5 # you may not use this file except in compliance with the License. 6 # You may obtain a copy of the License at 7 # 8 # http://www.apache.org/licenses/LICENSE-2.0 9 # 10 # Unless required by applicable law or agreed to in writing, software 11 # distributed under the License is distributed on an "AS IS" BASIS, 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 # See the License for the specific language governing permissions and 14 # limitations under the License. 15 16 17 set -o nounset 18 set -o errexit 19 20 # See gsutil notification --help 21 22 args=() 23 topic= 24 prefixes=() 25 26 while getopts "e:t:p:b:" flag; do 27 case "$flag" in 28 e) args+=(-e "$OPTARG");; 29 t) topic=$OPTARG;; 30 p) prefixes+=("$OPTARG");; 31 esac 32 done 33 34 shift $((OPTIND -1)) 35 36 if [[ -z "$topic" || $# == 0 ]]; then 37 echo "Usage: $(basename "$0") [-e EVENT [-e ...]] [-p PREFIX] <-t TOPIC> <BUCKET ...>" >&2 38 echo >&2 39 echo " -e EVENT: OBJECT_FINALIZE|OBJECT_METADATA_UPDATE|OBJECT_DELETE|OBJECT_ARCHIVE (repeatable)" >&2 40 echo " -p PREFIX: only publish messages for objects that start with this name, such as logs/" >&2 41 echo " -t TOPIC: foo or projects/proj/topics/foo" >&2 42 echo >&2 43 echo " More info: gsutil notification --help" >&2 44 exit 1 45 fi 46 47 log() { 48 ( 49 set -o xtrace 50 "$@" 51 ) 52 } 53 54 for bucket in "$@"; do 55 existing=( $(gsutil notification list "$bucket" 2>/dev/null | grep -B 1 "$topic" | grep notificationConfigs || true) ) 56 for prefix in "${prefixes[@]}"; do 57 log gsutil notification create -f json -t "$topic" -p "$prefix" "${args[@]}" "$bucket" 58 done 59 if [[ ${#existing[@]} -gt 0 ]]; then 60 log gsutil notification delete "${existing[@]}" 61 fi 62 done