Skip to content

Commit

Permalink
#487 Fix HTTP endpoint usage in cloud2edge tour.
Browse files Browse the repository at this point in the history
Using new REGISTRY_BASE_URL and HTTP_ADAPTER_BASE_URL
environment variables that will contain either
the secure or insecure endpoint base URL depending
on the chart configuration.

Signed-off-by: Carsten Lohmann <[email protected]>
  • Loading branch information
calohmn committed Aug 27, 2023
1 parent 20cd131 commit f7e95f4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 40 deletions.
2 changes: 1 addition & 1 deletion homepage/_packages/cloud2edge/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ IP addresses. To install the Cloud2Edge package using load balancers, run the fo

{% clipboard %}
RELEASE=c2e
helm install -n $NS --wait --timeout 15m --set hono.useLoadBalancer=true --set ditto.nginx.service.type=LoadBalancer $RELEASE eclipse-iot/cloud2edge
helm install -n $NS --wait --timeout 20m --set hono.useLoadBalancer=true --set ditto.nginx.service.type=LoadBalancer $RELEASE eclipse-iot/cloud2edge
{% endclipboard %}
{% endvariant %}

Expand Down
47 changes: 27 additions & 20 deletions homepage/_packages/cloud2edge/scripts/setCloud2EdgeEnv.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash
#*******************************************************************************
# Copyright (c) 2020 Contributors to the Eclipse Foundation
# Copyright (c) 2020, 2023 Contributors to the Eclipse Foundation
#
# See the NOTICE file(s) distributed with this work for additional
# information regarding copyright ownership.
Expand All @@ -12,24 +12,33 @@
# SPDX-License-Identifier: EPL-2.0
#*******************************************************************************

RELEASE=$1
NS=${2:-default}
RELEASE=${1:-$RELEASE}
NS=${2:-$NS}

if [[ -z "$RELEASE" ]] || [[ -z "$NS" ]] ; then
echo "# Usage: $(basename "$0") [RELEASE] [NS]"
echo "# RELEASE is the release name used for the cloud2edge deployment"
echo "# NS is the namespace the cloud2edge chart was deployed in"
echo "# If arguments are omitted, the values of RELEASE and NS environment variables are used, respectively."
exit 1
fi

NODE_IP=$(kubectl get nodes -n $NS -o jsonpath='{.items[0].status.addresses[?(@.type=="InternalIP")].address}' 2> /dev/null)

function getPorts {
SERVICENAME=$1
PORT_NAMES=$2
ENV_VAR_PREFIX=$3
TYPE=$4
IP=$2
PORT_NAMES=$3
ENV_VAR_PREFIX=$4
TYPE=$5

for NAME in $PORT_NAMES
do
for NAME in $PORT_NAMES; do
PORT=$(kubectl get service $SERVICENAME -n $NS -o jsonpath='{.spec.ports[?(@.name=='"'$NAME'"')].'$TYPE'}' 2> /dev/null)
if [ $? -eq 0 -a "$PORT" != '' ]
then
if [ $? -eq 0 ] && [ "$PORT" != '' ]; then
NAME=${NAME/secure-mqtt/mqtts}
UPPERCASE_PORT_NAME=$(echo $NAME | tr [a-z\-] [A-Z_])
echo "export ${ENV_VAR_PREFIX}_PORT_${UPPERCASE_PORT_NAME}=\"$PORT\""
echo "export ${ENV_VAR_PREFIX}_BASE_URL=\"$NAME://$IP:$PORT\""
fi
done

Expand All @@ -41,19 +50,17 @@ function getService {
ENV_VAR_PREFIX=$3

SERVICE_TYPE=$(kubectl get service $SERVICENAME -n $NS -o jsonpath='{.spec.type}' 2> /dev/null)
if [ $? -eq 0 ]
then
if [ $? -eq 0 ]; then
case $SERVICE_TYPE in
NodePort)
echo "export ${ENV_VAR_PREFIX}_IP=\"$NODE_IP\""
getPorts $SERVICENAME "$PORT_NAMES" $ENV_VAR_PREFIX nodePort
getPorts $SERVICENAME "$NODE_IP" "$PORT_NAMES" $ENV_VAR_PREFIX nodePort
;;
LoadBalancer)
IP=$(kubectl get service $SERVICENAME -o jsonpath='{.status.loadBalancer.ingress[0].ip}' -n $NS 2> /dev/null)
if [ $? -eq 0 -a "$IP" != '' ]
then
if [ $? -eq 0 ] && [ "$IP" != '' ]; then
echo "export ${ENV_VAR_PREFIX}_IP=\"$IP\""
getPorts $SERVICENAME "$PORT_NAMES" $ENV_VAR_PREFIX port
getPorts $SERVICENAME "$IP" "$PORT_NAMES" $ENV_VAR_PREFIX port
fi
;;
esac
Expand All @@ -68,9 +75,9 @@ getService adapter-http "http https" HTTP_ADAPTER
getService adapter-mqtt "mqtt secure-mqtt" MQTT_ADAPTER
getService ditto-nginx "http" DITTO_API

echo
echo "# Run this command to populate environment variables"
echo "# with the NodePorts of Hono's and Ditto's API endpoints:"
echo "# eval \"\$(./setCloud2EdgeEnv.sh RELEASE_NAME [NAMESPACE])\""
echo "# with NAMESPACE being the Kubernetes name space that you installed Hono to"
echo "# if no name space is given, the default name space is used"

echo "#"
echo "# eval \"\$(./$(basename "$0") $*)\""
echo
41 changes: 22 additions & 19 deletions homepage/_packages/cloud2edge/tour.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ Download the [setCloud2EdgeEnv script](../scripts/setCloud2EdgeEnv.sh) and use i
to set environment variables for the Cloud2Edge package's service endpoints.

{% clipboard %}
curl https://www.eclipse.org/packages/packages/cloud2edge/scripts/setCloud2EdgeEnv.sh --output setCloud2EdgeEnv.sh
chmod u+x setCloud2EdgeEnv.sh

RELEASE=c2e
NS=cloud2edge
./setCloud2EdgeEnv.sh $RELEASE $NS
eval "$(./setCloud2EdgeEnv.sh $RELEASE $NS)
{% endclipboard %}
{% endvariant %}

Expand All @@ -63,27 +66,27 @@ The demo device's digital twin supports a temperature property which will be set
by means of the following command:

{% clipboard %}
curl -i -u [email protected]2e:demo-secret -H 'application/json' --data-binary '{
curl -i -k -u [email protected]2e:demo-secret -H 'application/json' --data-binary '{
"topic": "org.eclipse.packages.c2e/demo-device/things/twin/commands/modify",
"headers": {},
"path": "/features/temperature/properties/value",
"value": 45
}' http://${HTTP_ADAPTER_IP}:${HTTP_ADAPTER_PORT_HTTP}/telemetry
}' ${HTTP_ADAPTER_BASE_URL:?}/telemetry
{% endclipboard %}

## Retrieving the digital twin's current state

The updated state of the digital twin can then be retrieved using:

{% clipboard %}
curl -u ditto:ditto -w '\n' http://$DITTO_API_IP:$DITTO_API_PORT_HTTP/api/2/things/org.eclipse.packages.c2e:demo-device
curl -u ditto:ditto -w '\n' ${DITTO_API_BASE_URL:?}/api/2/things/org.eclipse.packages.c2e:demo-device
{% endclipboard %}

Alternatively you can also use the following command to subscribe to Ditto's
stream of thing update events:

{% clipboard %}
curl --http2 -u ditto:ditto -H 'Accept:text/event-stream' -N http://$DITTO_API_IP:$DITTO_API_PORT_HTTP/api/2/things
curl --http2 -u ditto:ditto -H 'Accept:text/event-stream' -N ${DITTO_API_BASE_URL:?}/api/2/things
{% endclipboard %}

## Sending a command to the device via its digital twin
Expand All @@ -93,7 +96,7 @@ Ditto digital twin may also be used to send a command down to the device connect
{% clipboard %}
curl -i -X POST -u ditto:ditto -H 'Content-Type: application/json' -w '\n' --data '{
"water-amount": "3liters"
}' http://$DITTO_API_IP:$DITTO_API_PORT_HTTP/api/2/things/org.eclipse.packages.c2e:demo-device/inbox/messages/start-watering?timeout=0
}' ${DITTO_API_BASE_URL:?}/api/2/things/org.eclipse.packages.c2e:demo-device/inbox/messages/start-watering?timeout=0
{% endclipboard %}

Specifying the `timeout=0` parameter indicates that the HTTP request will directly be accepted and Ditto does not wait
Expand All @@ -105,20 +108,20 @@ timeout to the amount of seconds to wait:
{% clipboard %}
curl -i -X POST -u ditto:ditto -H 'Content-Type: application/json' -w '\n' --data '{
"water-amount": "3liters"
}' http://$DITTO_API_IP:$DITTO_API_PORT_HTTP/api/2/things/org.eclipse.packages.c2e:demo-device/inbox/messages/start-watering?timeout=60
}' ${DITTO_API_BASE_URL:?}/api/2/things/org.eclipse.packages.c2e:demo-device/inbox/messages/start-watering?timeout=60
{% endclipboard %}

### Receiving a command at the device

The device may receive a command by specifying a `ttd` when e.g. sending telemetry via HTTP to Hono:

{% clipboard %}
curl -i -u [email protected]2e:demo-secret -H 'hono-ttd: 50' -H 'application/json' -w '\n' --data '{
curl -i -k -u [email protected]2e:demo-secret -H 'hono-ttd: 50' -H 'application/json' -w '\n' --data '{
"topic": "org.eclipse.packages.c2e/demo-device/things/twin/commands/modify",
"headers": {},
"path": "/features/temperature/properties/value",
"value": 45
}' http://${HTTP_ADAPTER_IP}:${HTTP_ADAPTER_PORT_HTTP}/telemetry
}' ${HTTP_ADAPTER_BASE_URL:?}/telemetry
{% endclipboard %}

An example response for the device containing the command sent via the Ditto twin (see previous step for sending the
Expand All @@ -145,7 +148,7 @@ The response has to be correlated twice:
with the `"correlation-id"` value from the received Ditto Protocol message's `"headers"` object.

{% clipboard %}
curl -i -X PUT -u [email protected]2e:demo-secret -H "content-type: application/json" --data-binary '{
curl -i -k -X PUT -u [email protected]2e:demo-secret -H "content-type: application/json" --data-binary '{
"topic": "org.eclipse.packages.c2e/demo-device/things/live/messages/start-watering",
"headers": {
"content-type": "application/json",
Expand All @@ -156,7 +159,7 @@ curl -i -X PUT -u [email protected]:demo-secret -H "content-t
"starting-watering": true
},
"status": 200
}' http://${HTTP_ADAPTER_IP}:${HTTP_ADAPTER_PORT_HTTP}/command/res/org.eclipse.packages.c2e/org.eclipse.packages.c2e:demo-device/insert-hono-cmd-req-id-here?hono-cmd-status=200
}' ${HTTP_ADAPTER_BASE_URL:?}/command/res/org.eclipse.packages.c2e/org.eclipse.packages.c2e:demo-device/insert-hono-cmd-req-id-here?hono-cmd-status=200
{% endclipboard %}

An example message response (omitting some additional HTTP headers) at the Ditto twin which waited for the command
Expand Down Expand Up @@ -184,7 +187,7 @@ e.g.: `org.acme:my-device`.
Create a new tenant named `my-tenant` in Hono:

{% clipboard %}
curl -i -X POST http://${REGISTRY_IP}:${REGISTRY_PORT_HTTP}/v1/tenants/my-tenant
curl -i -k -X POST ${REGISTRY_BASE_URL:?}/v1/tenants/my-tenant
{% endclipboard %}

This should return a result of `201 Created`.
Expand All @@ -204,7 +207,7 @@ This should return a result of `201 Created`.
Next we can register a new device, named `org.acme:my-device-1` for the tenant we just created:

{% clipboard %}
curl -i -X POST http://${REGISTRY_IP}:${REGISTRY_PORT_HTTP}/v1/devices/my-tenant/org.acme:my-device-1
curl -i -k -X POST ${REGISTRY_BASE_URL:?}/v1/devices/my-tenant/org.acme:my-device-1
{% endclipboard %}

This should return a result of `201 Created`.
Expand All @@ -227,14 +230,14 @@ a *gateway device*, but we want this device to be able to connect, so the next s
to assign a username/password combination:

{% clipboard %}
curl -i -X PUT -H "Content-Type: application/json" --data '[
curl -i -k -X PUT -H "Content-Type: application/json" --data '[
{
"type": "hashed-password",
"auth-id": "my-auth-id-1",
"secrets": [{
"pwd-plain": "my-password"
}]
}]' http://${REGISTRY_IP}:${REGISTRY_PORT_HTTP}/v1/credentials/my-tenant/org.acme:my-device-1
}]' ${REGISTRY_BASE_URL:?}/v1/credentials/my-tenant/org.acme:my-device-1
{% endclipboard %}

{% details Example of a successful result %}
Expand All @@ -261,7 +264,7 @@ we use distinct values to show the difference.

In order to create digital twins in Ditto for a newly added Hono tenant, a new connection has to be created.

The `NS` and `RELEASE` variables must still be set to the value you chose during the [installation](../installation/#install-the-package).
The `NS` and `RELEASE` variables must still be set to the values you chose during the [installation](../installation/#install-the-package).
The documented defaults are:
{% clipboard %}
NS=cloud2edge
Expand Down Expand Up @@ -388,7 +391,7 @@ curl -i -X POST -u devops:${DITTO_DEVOPS_PWD} -H 'Content-Type: application/json
]
}
}
}' http://${DITTO_API_IP}:${DITTO_API_PORT_HTTP}/devops/piggyback/connectivity
}' ${DITTO_API_BASE_URL:?}/devops/piggyback/connectivity
{% endclipboard %}


Expand Down Expand Up @@ -443,7 +446,7 @@ curl -i -X PUT -u ditto:ditto -H 'Content-Type: application/json' --data '{
}
}
}
}' http://${DITTO_API_IP}:${DITTO_API_PORT_HTTP}/api/2/policies/org.acme:my-policy
}' ${DITTO_API_BASE_URL:?}/api/2/policies/org.acme:my-policy
{% endclipboard %}

This should return a result of `201 Created` containing as response body of the created policy JSON.
Expand Down Expand Up @@ -476,7 +479,7 @@ curl -i -X PUT -u ditto:ditto -H 'Content-Type: application/json' --data '{
}
}
}
}' http://${DITTO_API_IP}:${DITTO_API_PORT_HTTP}/api/2/things/org.acme:my-device-1
}' ${DITTO_API_BASE_URL:?}/api/2/things/org.acme:my-device-1
{% endclipboard %}

This should return a result of `201 Created` containing as response body of the created thing JSON.
Expand Down

0 comments on commit f7e95f4

Please sign in to comment.