Merge "CORD-3028 Example Tosca to create a SimpleExampleServiceInstance"
diff --git a/xos/examples/README.md b/xos/examples/README.md
new file mode 100644
index 0000000..de64192
--- /dev/null
+++ b/xos/examples/README.md
@@ -0,0 +1,21 @@
+The following describes a demo that brings up a `SimpleExampleServiceInstance`. The purpose of this ServiceInstance is to host a single web inside of a Kubernetes container. Creating the `SimpleExampleServiceInstance` will cause a model policy to be invoked, which will create the necessary Kubernetes resources to launch the web server and configure it to host the desired page.
+
+1. Set your username and password
+
+    USERNAME=admin@opencord.org
+    PASSWORD=letmein
+
+2. Run the TOSCA recipe
+
+    TOSCA_URL=$(minikube service xos-tosca --url)
+    curl -H "xos-username: $ACCOUNT" -H "xos-password: $PASSWORD" -X POST --data-binary @$RECIPE $TOSCA_URL/run    
+
+3. Wait a few seconds for the Kubernetes instances to be created.
+
+4. View the status
+
+    CHAMELEON_URL=$(minikube service xos-chameleon --url)
+    python ./show-instances.py $CHAMELEON_URL $USERNAME $PASSWORD
+
+5. View the web page
+Enter one of the other Kubernetes containers, any container such as one of the synchronizer containers will do, and perform a curl on the IP address obtained in step 4.
diff --git a/xos/examples/SimpleExampleServiceInstance.yaml b/xos/examples/SimpleExampleServiceInstance.yaml
new file mode 100644
index 0000000..d44d98f
--- /dev/null
+++ b/xos/examples/SimpleExampleServiceInstance.yaml
@@ -0,0 +1,41 @@
+---
+# Copyright 2017-present Open Networking Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+tosca_definitions_version: tosca_simple_yaml_1_0
+
+description: Make a SimpleExampleServiceInstance
+
+imports:
+  - custom_types/simpleexampleservice.yaml
+  - custom_types/simpleexampleserviceinstance.yaml
+
+topology_template:
+  node_templates:
+    service#simpleexampleservice:
+      type: tosca.nodes.SimpleExampleService
+      properties:
+        name: simpleexampleservice
+        must-exist: true
+
+    simpleexampleserviceinstance:
+      type: tosca.nodes.SimpleExampleServiceInstance
+      properties:
+        name: "My Simple Example Service Instance"
+        tenant_message: "world"
+        tenant_secret: "p@ssw0rd"
+      requirements:
+        - owner:
+            node: service#simpleexampleservice
+            relationship: tosca.relationships.BelongsToOne
diff --git a/xos/examples/show-instances.py b/xos/examples/show-instances.py
new file mode 100755
index 0000000..65755b4
--- /dev/null
+++ b/xos/examples/show-instances.py
@@ -0,0 +1,66 @@
+#!/usr/bin/env python
+
+# Copyright 2017-present Open Networking Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# show-instances.py
+# Show the SimpleExampleServiceInstances and their ip addresses in a human-readable table.
+# Syntax: show-instances.py <base_url> <username> <password>
+#
+# Example: show-instances.py http://192.168.42.253:30006 admin@opencord.org letmein
+
+import sys
+import time
+import requests
+from requests.auth import HTTPBasicAuth
+
+DELAY=1
+
+def main():
+    if len(sys.argv)<4:
+        print "Syntax: xos-instances.py <base_url> <username> <password>"
+        sys.exit(-1)
+
+    base_url = sys.argv[1]
+    username = sys.argv[2]
+    password = sys.argv[3]
+
+    auth = HTTPBasicAuth(username, password)
+
+    r = requests.get(base_url + "/xosapi/v1/simpleexampleservice/simpleexampleserviceinstances", auth=auth)
+
+    if r.status_code != 200:
+        print "Received error response", r.status_code
+        print r.text
+        sys.exit(-1)
+
+
+    print "%-4s %-40s %-4s %-4s" % ("id", "Name", "Comp", "IP")
+    for item in r.json()["items"]:
+        name = item.get("name")
+        compute_id = item["compute_instance_id"]
+        if compute_id:
+            r_compute = requests.get(base_url + "/xosapi/v1/kubernetes/kubernetesserviceinstances/%s" % compute_id, auth=auth)
+            if r_compute.status_code != 200:
+                print "Received error response when fetching compute instance", r_compute.status_code
+                print r_compute.text
+                sys.exit(-1)
+            pod_ip = r_compute.json().get("pod_ip", "")
+        else:
+            pod_ip = ""
+        print "%4s %-40s %4s %s" % (item["id"], name, compute_id, pod_ip)
+
+
+if __name__=="__main__":
+    main()