You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwhisk.apache.org by ms...@apache.org on 2021/07/28 12:57:03 UTC

[openwhisk-runtime-go] branch master updated: New example aws (#152)

This is an automated email from the ASF dual-hosted git repository.

msciabarra pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/openwhisk-runtime-go.git


The following commit(s) were added to refs/heads/master by this push:
     new 9cab25e  New example aws (#152)
9cab25e is described below

commit 9cab25ed8f115981ac06d85b7f9700cb59a37b0d
Author: Michele Sciabarra <30...@users.noreply.github.com>
AuthorDate: Wed Jul 28 14:56:35 2021 +0200

    New example aws (#152)
    
    * updated examples
    
    * using actionloop-base as image for the bash example
    
    Co-authored-by: Michele Sciabarra <gi...@sciabarra.com>
---
 examples/Makefile                           | 34 +++++++++++-------
 examples/{module-main => aws-main}/Makefile |  9 ++---
 examples/aws-main/go.mod                    |  5 +++
 examples/aws-main/go.sum                    | 17 +++++++++
 examples/aws-main/main.go                   | 55 +++++++++++++++++++++++++++++
 examples/module-main/Makefile               |  5 +--
 examples/package-main/Makefile              |  5 +--
 examples/single-hello/Makefile              |  5 +--
 examples/single-main/Makefile               |  5 +--
 examples/standalone/Makefile                |  5 +--
 10 files changed, 118 insertions(+), 27 deletions(-)

diff --git a/examples/Makefile b/examples/Makefile
index cde98ef..1cb85ba 100644
--- a/examples/Makefile
+++ b/examples/Makefile
@@ -17,40 +17,48 @@
 
 # prefix
 OW_USER?=openwhisk
+OW_VER?=v1.16:nightly
 
-test: .PHONY
+.PHONY: test
+test: all
 	cat */test.out | grep 'Hello' | sort >test.out
 	diff test.out test.expected
 
-all: .PHONY
+.PHONY: all
+all: bash standalone single-main single-hello package-main module-main
 
 .PHONY: bash
 bash:
-	cd $@ && OW_USER=$(OW_USER) make clean deploy test
+	cd $@ && OW_USER=$(OW_USER) OW_VER=$(OW_VER) make clean deploy test
 
 .PHONY: standalone
 standalone:
-	cd $@ && OW_USER=$(OW_USER) make clean devel test
-	cd $@ && OW_USER=$(OW_USER) make clean deploy test
+	cd $@ && OW_USER=$(OW_USER) OW_VER=$(OW_VER) make clean devel test
+	cd $@ && OW_USER=$(OW_USER) OW_VER=$(OW_VER) make clean deploy test
 
 .PHONY: single-main
 single-main:
-	cd $@ && OW_USER=$(OW_USER) make clean devel test
-	cd $@ && OW_USER=$(OW_USER) make clean deploy test
+	cd $@ && OW_USER=$(OW_USER) OW_VER=$(OW_VER) make clean devel test
+	cd $@ && OW_USER=$(OW_USER) OW_VER=$(OW_VER) make clean deploy test
 
 .PHONY: single-hello
 single-hello:
-	cd $@ && OW_USER=$(OW_USER) make clean devel test
-	cd $@ && OW_USER=$(OW_USER) make clean deploy test
+	cd $@ && OW_USER=$(OW_USER) OW_VER=$(OW_VER) make clean devel test
+	cd $@ && OW_USER=$(OW_USER) OW_VER=$(OW_VER) make clean deploy test
 
 .PHONY: package-main
 package-main:
-	cd $@ && OW_USER=$(OW_USER) make clean devel test
-	cd $@ && OW_USER=$(OW_USER) make clean deploy test
+	cd $@ && OW_USER=$(OW_USER) OW_VER=$(OW_VER) make clean devel test
+	cd $@ && OW_USER=$(OW_USER) OW_VER=$(OW_VER) make clean deploy test
 
 .PHONY: module-main
 module-main:
 	# this exceeds 3 seconds for initialization
-	#cd $@ && OW_USER=$(OW_USER) make clean devel test
-	cd $@ && OW_USER=$(OW_USER) make clean deploy test
+	#cd $@ && OW_USER=$(OW_USER) OW_VER=$(OW_VER) make clean devel test
+	cd $@ && OW_USER=$(OW_USER) OW_VER=$(OW_VER) make clean deploy test
 
+.PHONY: aws-main
+aws-main:
+	# this exceeds 3 seconds for initialization
+	#cd $@ && OW_USER=$(OW_USER) OW_VER=$(OW_VER) make clean devel test
+	cd $@ && OW_USER=$(OW_USER) OW_VER=$(OW_VER) make clean deploy test
diff --git a/examples/module-main/Makefile b/examples/aws-main/Makefile
similarity index 91%
copy from examples/module-main/Makefile
copy to examples/aws-main/Makefile
index 4ae74ba..5d8d99b 100644
--- a/examples/module-main/Makefile
+++ b/examples/aws-main/Makefile
@@ -16,13 +16,14 @@
 #
 
 OW_USER?=openwhisk
-OW_RUNTIME?=$(OW_USER)/action-golang-v1.15:nightly
-OW_COMPILER?=$(OW_USER)/action-golang-v1.15:nightly
+OW_VER?=v1.16:nightly
+OW_RUNTIME?=$(OW_USER)/action-golang-$(OW_VER)
+OW_COMPILER?=$(OW_USER)/action-golang-$(OW_VER)
 WSK?=wsk
 MAIN=main
 PACKAGE=test
 SRCS=main.go go.mod go.sum
-NAME=module-main
+NAME=aws-main
 BINZIP=$(MAIN)-bin.zip
 SRCZIP=$(MAIN)-src.zip
 
@@ -48,7 +49,7 @@ test: test.json
 	$(WSK) action invoke test/$(NAME) -P test.json -r | tee -a test.out
 
 test.json:
-	echo '{ "name": "Mike" }' >test.json
+	echo '{}' >test.json
 
 package.done:
 	$(WSK) package update $(PACKAGE)
diff --git a/examples/aws-main/go.mod b/examples/aws-main/go.mod
new file mode 100644
index 0000000..619045e
--- /dev/null
+++ b/examples/aws-main/go.mod
@@ -0,0 +1,5 @@
+module action
+
+go 1.16
+
+require github.com/aws/aws-sdk-go v1.40.8
diff --git a/examples/aws-main/go.sum b/examples/aws-main/go.sum
new file mode 100644
index 0000000..5085797
--- /dev/null
+++ b/examples/aws-main/go.sum
@@ -0,0 +1,17 @@
+github.com/aws/aws-sdk-go v1.40.8 h1:LBeBOKdgxaR1tknlENTBhcN8CjutpofbMJPtl/6Yug4=
+github.com/aws/aws-sdk-go v1.40.8/go.mod h1:585smgzpB/KqRA+K3y/NL/oYRqQvpNJYvLm+LY1U59Q=
+github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
+github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
+github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
+github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
+github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
+github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
+golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
+golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
+golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
+golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
diff --git a/examples/aws-main/main.go b/examples/aws-main/main.go
new file mode 100644
index 0000000..3bceae9
--- /dev/null
+++ b/examples/aws-main/main.go
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+package main
+
+import (
+	"github.com/aws/aws-sdk-go/aws"
+	"github.com/aws/aws-sdk-go/aws/credentials"
+	"github.com/aws/aws-sdk-go/aws/session"
+	"github.com/aws/aws-sdk-go/service/ec2"
+)
+
+// Main function for the action
+func Main(obj map[string]interface{}) map[string]interface{} {
+	msg := make(map[string]interface{})
+
+	id, ok1 := obj["id"].(string)
+	key, ok2 := obj["key"].(string)
+	region, ok3 := obj["region"].(string)
+	if ok1 && ok2 && ok3 {
+		sess := session.Must(session.NewSession(&aws.Config{
+			Region:      aws.String(region),
+			Credentials: credentials.NewStaticCredentials(id, key, ""),
+		}))
+		service := ec2.New(sess)
+		res, err := service.DescribeInstances(&ec2.DescribeInstancesInput{})
+		if err != nil {
+			msg["error"] = err.Error()
+		} else {
+			instances := []string{}
+			for _, resv := range res.Reservations {
+				for _, inst := range resv.Instances {
+					instances = append(instances, *inst.InstanceId)
+				}
+			}
+			msg["instances"] = instances
+		}
+	} else {
+		msg["help"] = "required id, key and region"
+	}
+	return msg
+}
diff --git a/examples/module-main/Makefile b/examples/module-main/Makefile
index 4ae74ba..05b0a57 100644
--- a/examples/module-main/Makefile
+++ b/examples/module-main/Makefile
@@ -16,8 +16,9 @@
 #
 
 OW_USER?=openwhisk
-OW_RUNTIME?=$(OW_USER)/action-golang-v1.15:nightly
-OW_COMPILER?=$(OW_USER)/action-golang-v1.15:nightly
+OW_VER?=v1.16:nightly
+OW_RUNTIME?=$(OW_USER)/action-golang-$(OW_VER)
+OW_COMPILER?=$(OW_USER)/action-golang-$(OW_VER)
 WSK?=wsk
 MAIN=main
 PACKAGE=test
diff --git a/examples/package-main/Makefile b/examples/package-main/Makefile
index 6050065..7c67be9 100644
--- a/examples/package-main/Makefile
+++ b/examples/package-main/Makefile
@@ -16,8 +16,9 @@
 #
 
 OW_USER?=openwhisk
-OW_RUNTIME?=$(OW_USER)/action-golang-v1.15:nightly
-OW_COMPILER?=$(OW_USER)/action-golang-v1.15:nightly
+OW_VER?=v1.16:nightly
+OW_RUNTIME?=$(OW_USER)/action-golang-$(OW_VER)
+OW_COMPILER?=$(OW_USER)/action-golang-$(OW_VER)
 WSK?=wsk
 MAIN=main
 PACKAGE=test
diff --git a/examples/single-hello/Makefile b/examples/single-hello/Makefile
index 71817d7..919b22d 100644
--- a/examples/single-hello/Makefile
+++ b/examples/single-hello/Makefile
@@ -16,8 +16,9 @@
 #
 
 OW_USER?=openwhisk
-OW_RUNTIME?=$(OW_USER)/action-golang-v1.15:nightly
-OW_COMPILER?=$(OW_USER)/action-golang-v1.15:nightly
+OW_VER?=v1.16:nightly
+OW_RUNTIME?=$(OW_USER)/action-golang-$(OW_VER)
+OW_COMPILER?=$(OW_USER)/action-golang-$(OW_VER)
 WSK?=wsk
 MAIN=hello
 PACKAGE=test
diff --git a/examples/single-main/Makefile b/examples/single-main/Makefile
index 9cdd3b5..c33c4c4 100644
--- a/examples/single-main/Makefile
+++ b/examples/single-main/Makefile
@@ -16,8 +16,9 @@
 #
 
 OW_USER?=openwhisk
-OW_RUNTIME?=$(OW_USER)/action-golang-v1.15:nightly
-OW_COMPILER?=$(OW_USER)/action-golang-v1.15:nightly
+OW_VER?=v1.16:nightly
+OW_RUNTIME?=$(OW_USER)/action-golang-$(OW_VER)
+OW_COMPILER?=$(OW_USER)/action-golang-$(OW_VER)
 WSK?=wsk
 MAIN=main
 PACKAGE=test
diff --git a/examples/standalone/Makefile b/examples/standalone/Makefile
index ca6bb99..247f4d9 100644
--- a/examples/standalone/Makefile
+++ b/examples/standalone/Makefile
@@ -16,8 +16,9 @@
 #
 
 OW_USER?=openwhisk
-OW_RUNTIME?=$(OW_USER)/action-golang-v1.15:nightly
-OW_COMPILER?=$(OW_USER)/action-golang-v1.15:nightly
+OW_VER?=v1.16:nightly
+OW_RUNTIME?=$(OW_USER)/action-golang-$(OW_VER)
+OW_COMPILER?=$(OW_USER)/action-golang-$(OW_VER)
 WSK?=wsk
 MAIN=main
 PACKAGE=test