open whisk quick start guide

Post on 13-Apr-2017

208 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

IBM Bluemix OpenWhisk

Joseph Chang| IBM Cloud Architect | changhs@tw.ibm.com

• Openwhisk Programing Model• Let’s do it

– Install Command Line– Trigger action from Cloudant– Trigger action from Http Request– Define Trigger-Event for Node-Red

2

Openwhisk Programing Model

3

From:

Openwhisk Programming model

• Services define the events they emit as triggers, and developers associate the actions to handle the events via rules

• The developer only needs to care about implementing the desired application logic - the system handles the rest

T AR

Programming model

Trigger: „A class of events that can happen“T

Programming model

Actions: „An event-handler, i.e. code that runs in response to an event“A

Programming model

Actions: Multi-runtime support, e.g. JavaScriptA

function main(msg) {return { message: 'Hello, ' + msg.name + ' from ' +

msg.place };};

Programming model

Actions: Can be chained to create sequences to increase flexibility and foster reuse

A

AA := A1 + A2 + A3

AB := A2 + A1 + A3

AC := A3 + A1 + A2

Programming model

Rules: „An association of a trigger and an action“R

R := T A

Want to try it out?

• You want to try OpenWhisk on your own?– Want to try out our IBM Bluemix OpenWhisk offering?

• Sign-up today at: https://new-console.ng.bluemix.net/openwhisk/– Want to try out our open-source OpenWhisk offering?

• Visit: https://developer.ibm.com/openwhisk/

Whisk 命令列操作練習

11

Install Openwhisk command line tool

12

開啟 https://new-console.ng.bluemix.net/openwhisk

點選Configure CLI

Openwhisk command line tool

13

Mac 使用者 依指示完成命令列設定 , Win 使用者請看次頁

Openwhisk command line tool

14

Windows 使用者請下載安裝 Python 開啟 , 安裝完畢即可使

用 pip

https://www.python.org/downloads/windows/

pip install --upgrade https://new-console.ng.bluemix.net/openwhisk/cli/download

然後執行前頁的1,2,3,4 步驟 , 注意步驟 1 在 windows 是不需要 sudo 的

當 Cloudant DB 有異動時 , 觸發 Whisk Action

15

Exercise – 當 Cloudant DB 有異動時 , 觸發 whisk Action

}Event Providers

OpenWhisk

Cloudant

Git

Weather

Data event occurs, e.g.-Commit on a Git Repository-CRUD operation on Cloudant-….

Trigger execution of associated OpenWhisk action

…JS Swift Docker …

Update 一筆Document

顯示那一筆資料被異動 2 1

Write your whisk application

17

點選 Edit

Write your whisk application

18

1. 閱讀sample code

2. Click “Create An

Action”

Write your whisk application

19

填寫 Action 名稱

使用 template

Write your whisk application

20

範例程式產生

我們要修改output, 讓它顯示被修改的欄位

Get Cloudant Setting Info

21

1. 點選此處開啟選單2. 用新分頁開啟資料分析 , 我們之後會在 whisk 與 cloudant 交替使用

Get Cloudant Setting Info

22

2. 撰擇一個 Cloudant

NoSQL DB

1. 若尚未建立請建一個新的 Cloudant DB

Get Cloudant Setting Info

23

點選服認證

Get Cloudant Setting Info

24

1. 記下 username, password, host 等會兒要用到

2. 若此頁顯無認識證 , 請新增 一組

3. 回管理頁面

Create DB

25

點選LAUNCH

Create DB

26

點選 Create Database

為 DB 取個名字

記住 Database 名稱 , 等會兒要用到

Insert New Doc

27

點選 + , 再選 +New Doc

Insert New Doc

28

修改一下文件內容 , 加一些 欄位值可參考如下內容 ,完成後按

“ Create Document”

Insert New Doc

29

系統自動 新 增 _rev 欄位 回到OpenWhisk 頁面

Enable Docs

Modify the Action

30

1. 修改程式碼 , 讓被異動 的 doc id , 版本 顯示出來

function main(params) {return { "message": "The doc is changed " + params._id+":"+params._rev };

}

2. 按 Run it Now

Modify the Action

31

點選 Run With These

Values

Modify the Action

32

點選 Close

看到綠色邊及此訊息表示測試成功

Create a Trigger

33

點選Automate This

Action Create a Rule

Create A Trigger

34

點選CLOUDANT

changes

Create a Trigger

35

點選 NEW TRIGGER

Create a Trigger

36

完成後點選Save

Configuration

為 Trigger 取名字

Create a Rule

37點選 Next

檢查Trigger 是否新增 成功

檢查Trigger 是否新增 成功

Create a Rule

38

點選 This Looks Good

系統將建立此 Rule

Create a Rule

39

可修改Rule 名稱

點選 Save Rule

Create a Rule

40

點選Enable Rule

Rule 已產生 , 但是處於 disable 狀態

Create a Rule

41

1. 確認 Rule 是在 Enable 狀態

2. 點選Dashboard

View Dashboard

42

Do some changes in Cloudant DB

43

點選編輯

Do some changes in Cloudant DB

44

修改一下Note, 或其它欄位

點選 Save Changes, 回到 Whisk DashBoard

View Dashboard Again

45

點選勾勾可檢視完整 內容

由 Http Request 觸發 Whisk Action

46

OpenWhisk: How does it work?

OpenWhisk

JS Swift Docker …

Incoming HTTP request, e.g.HTTP GET mynewcoolapp.com/customers

1 2 Invoke associated OpenWhisk action „getCustomers“

Browser

Mobile App

Web App

Variety of languages

開啟 Openwhisk 編輯器

48

1. 選擇 Hello World With

Params 範例程式2. 點選 View

REST Endpoint

查看 Action 的 REST Endpoint

49

端點及範例在此

點選 Show Full

Command

可以直接將內容複製到程式中使用

50

Test Run

51

由 Node-RED 觸發 Event

52

從 Node-RED Trigger Openwhisk Event

53

節點說明

54

皆為預設值 , 無需調整

節點說明

55

由 payload 回傳參數 , 需為 JSON Object 型式

Define trigger with wsk

56

請先在 Command line 建立所需的 Triggerwsk trigger create <you trigger name>

Namespace Trigger name

Define trigger with wsk

57

在 OpenWhisk – Command CLI 說明可以找到 Auth Key

Auth key

節點說明

58

請將以上資訊填入

Test trigger

59

點選 timstamp 以觸發 Openwhisk trigger

View Result in Dashboard

60

收到由 Node-RED 觸發 的 Openwhisk Event

本單元結束

top related