Skip to content

Commit e96e9d2

Browse files
author
Doug Davis
committed
cleanup
Signed-off-by: Doug Davis <[email protected]>
1 parent 7ed368a commit e96e9d2

File tree

2 files changed

+44
-43
lines changed

2 files changed

+44
-43
lines changed

cos-event/README.md

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
# Cloud Object Storage
22

3-
This sample shows how to create an IBM Cloud Object Storage (COS) Event Source and hook it up
4-
to an Application that will receive its events. An event is sent for each successful change to
5-
an object in the COS bucket.
3+
This sample shows how to create an IBM Cloud Object Storage (COS) Event Source
4+
and hook it up to an Application that will receive its events. An event is
5+
sent for each successful change to an object in the COS bucket.
66

7-
The `run` script will create the app and an event subscription connecting the app to the specified
8-
COS bucket. The script will then upload a file to the bucket, resulting in the generation of an event.
9-
On receipt of the event, the app will log the event to stdout and increment a set of internal
10-
counters. The script queries the app on the `/stats` endpoint and displays these updated counters.
7+
The `run` script will create the app and an event subscription connecting the
8+
app to the specified COS bucket. The script will then upload a file to the
9+
bucket, resulting in the generation of an event. On receipt of the event, the
10+
app will log the event to stdout and increment a set of internal counters.
11+
The script queries the app on the `/stats` endpoint and displays these
12+
updated counters.
1113

12-
If you already have a COS instance that you want to use, you can set the `COS_ID`
13-
environment variable to the CRN of that instance. You can find existing instances
14-
by running
14+
If you already have a COS instance that you want to use, you can set the
15+
`COS_ID` environment variable to the CRN of that instance. You can find
16+
existing instances by running
1517
```
1618
ibmcloud resource service-instances --service-name cloud-object-storage --long
1719
```
18-
If you want to use one of the listed instances, set `COS_ID` to the instance CRN/ID
19-
(the value in the first column of each row). If you leave this variable unset,
20-
the `run` script will attempt to create a `lite` (free of charge) COS instance
21-
for you, which it will then delete upon completion. It will not delete any existing
22-
instance you specify via the `COS_ID` variable.
20+
If you want to use one of the listed instances, set `COS_ID` to the instance
21+
CRN/ID (the value in the first column of each row). If you leave this
22+
variable unset, the `run` script will attempt to create a `lite` (free of
23+
charge) COS instance for you, which it will then delete upon completion. It
24+
will not delete any existing instance you specify via the `COS_ID` variable.

cos-event/run

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,39 @@
44
# REPOSITORY: name of the image registry/namespace to get the images
55
# COS_ID: If set, specifies the full CRN of a Cloud Object Storage instance to use
66

7-
function project_name() {
8-
ibmcloud ce project current | awk -F: '/Project Name/{ print $2 }'
9-
}
10-
11-
function project_id() {
12-
set +x
13-
ibmcloud ce project get --name $(project_name)| awk '/^ID/{ print $2 }'
14-
set -x
15-
}
7+
export REPOSITORY=${REPOSITORY:-ibmcom}
8+
export PROJECT_NAME=$(ibmcloud ce project current | \
9+
awk -F: '/Project Name/{ print $2 }')
10+
export PROJECT_ID=$(ibmcloud ce project get --name ${PROJECT_NAME} | \
11+
awk '/^ID/{ print $2 }')
1612

1713
# Clean up previous run
1814
function clean() {
1915
set +x
2016
DCOS=$1
2117
POLICY=$2
22-
echo "Cleaning...be patient"
18+
echo "Cleaning..."
2319

2420
# Delete the event subscription
2521
ibmcloud ce sub cos delete -n cos-sub -f --wait=true > /dev/null 2>&1 || true
2622

2723
# Delete the IAM auth policy
2824
if [[ $POLICY != "" ]]; then
29-
ibmcloud iam authorization-policy-delete $POLICY --force > /dev/null 2>&1 || true
25+
ibmcloud iam authorization-policy-delete $POLICY --force \
26+
> /dev/null 2>&1 || true
3027
fi
3128

3229
# Delete the app
3330
ibmcloud ce app delete --name cos-app --force > /dev/null 2>&1 || true
3431

3532
# Delete the COS bucket
36-
ibmcloud cos bucket-delete --bucket $(project_id) --force > /dev/null 2>&1 || true
33+
ibmcloud cos bucket-delete --bucket ${PROJECT_ID} --force \
34+
> /dev/null 2>&1 || true
3735

3836
# Delete the COS instance
3937
if [[ $DCOS -gt 0 ]]; then
40-
ibmcloud resource service-instance-delete codeengine-cos -f -q > /dev/null 2>&1 || true
38+
ibmcloud resource service-instance-delete codeengine-cos -f -q \
39+
> /dev/null 2>&1 || true
4140
fi
4241

4342
rm -f out
@@ -47,45 +46,47 @@ clean 0
4746
[[ "$1" == "clean" ]] && exit 0
4847

4948
set -ex
50-
export REPOSITORY=${REPOSITORY:-ibmcom}
5149

52-
# Create a COS instance unless one has been specified for use, mark it for deletion
50+
# Create a COS instance unless one has been specified for use, mark it for
51+
# deletion
5352
DELETE_COS=0
5453
if [[ $COS_ID == "" ]]; then
55-
ibmcloud resource service-instance-create codeengine-cos cloud-object-storage lite global
56-
COS_ID=$(ibmcloud resource service-instance codeengine-cos | awk '/^ID/{ print $2 }')
54+
ibmcloud resource service-instance-create codeengine-cos \
55+
cloud-object-storage lite global
56+
COS_ID=$(ibmcloud resource service-instance codeengine-cos | \
57+
awk '/^ID/{ print $2 }')
5758
DELETE_COS=1
5859
fi
5960

6061
# Set the COS config to use this instance
6162
ibmcloud cos config crn --crn $COS_ID --force
6263
ibmcloud cos config auth --method IAM
6364

64-
# Create the IAM authorization policy to allow us to receive notifications from COS
65+
# Create the IAM authorization policy to allow us to receive notifications
66+
# from COS
6567
POLICY_ID=$(ibmcloud iam authorization-policy-create codeengine \
6668
cloud-object-storage "Notifications Manager" \
67-
--source-service-instance-name $(project_name) \
69+
--source-service-instance-name ${PROJECT_NAME} \
6870
--target-service-instance-id ${COS_ID} | awk '/^Authorization/{ print $3 }')
6971

7072
# Create our bucket
71-
BUCKET=$(project_id)
73+
export BUCKET=${PROJECT_ID}
7274
ibmcloud cos bucket-create --bucket $BUCKET --ibm-service-instance-id $COS_ID
7375

74-
# Create the app
75-
ibmcloud ce app create -n cos-app --image ${REPOSITORY}/cos-listen --min-scale=1 --max-scale=1
76+
# Create the app && save its URL for later
77+
ibmcloud ce app create -n cos-app --image ${REPOSITORY}/cos-listen \
78+
--min-scale=1 --max-scale=1
79+
URL=$(ibmcloud ce app get --output jsonpath='{.status.url}' --name cos-app)
7680

7781
# Setup the COS Event Source
7882
ibmcloud ce sub cos create -n cos-sub -d cos-app -b ${BUCKET}
7983

80-
8184
# Extract the instance name from `ibmcloud ce app get`
82-
INSTANCE=$(ibmcloud ce app get --name cos-app | awk '/cos.*Running/{ print $1 }')
85+
INSTANCE=$(ibmcloud ce app get --name cos-app| awk '/cos.*Running/{ print $1 }')
8386
echo Instance name: $INSTANCE
8487

85-
8688
# Now wait until we get the event - shouldn't take more than a minute
8789
while true ; do
88-
8990
# Upload a file to COS (this will generate an event)
9091
ibmcloud cos object-put --bucket ${BUCKET} --key "README.md" --body README.md
9192

@@ -103,8 +104,6 @@ cat out
103104
echo "=============="
104105
echo "Event stats:"
105106

106-
URL=$(ibmcloud ce app get --output jsonpath='{.status.url}' --name cos-app)
107-
108107
curl -Ls ${URL}/stats
109108

110109
# Clean up

0 commit comments

Comments
 (0)