0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

IBM Cloud: Event NotificationsでAPIをsource/webhookをdestinationとした通知構成例

Last updated at Posted at 2023-04-24

1. はじめに

IBM Cloud Event Notificationでは、IBM Cloudのサービスをsourceとすることも可能だが、APIをsource(カスタムイベント)とすることも可能である。
https://cloud.ibm.com/docs/event-notifications?topic=event-notifications-en-source
今回は、APIをsourceとし、destinationをwebhookとした以下のような設定の手順を示す。

通知元 ->  (API) -> Event Notofication -> (webhook) -> 監視サーバー

2. 手順

2-1. Event Notificationを注文する

image.png

2-2. sourceを作成

  • API sourceを選択。image.png
  • Event Notification URLを記録しておく(API呼び出し時に利用)。image.png
  • source作成後、今回作成したsourceに対するsourceIDを記録しておく(API呼び出し時に利用)。image.png

2-3. topicを作成。

  • 先ほど作成したsourceを指定。image.png

2-4. destinationを作成

  • destination typeとしてWebhookを指定する。webhookの転送先URLを指定する。
    ここでは、Webhook.siteのインストール方法で作成した、http://<IP address>:8084/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxを指定する。
    image.png

2-5. subscriptionを作成。

  • 先ほど作成した、topicおよびdestinationを登録する。image.png

3. 通知元でのAPI呼び出し例

  • IAM Tokenの取得方法はここも参考。
  • API情報
    • Request Bodyとして、必須項目が結構ある。以下が必須項目
      • specversion
      • id
      • source
      • type
      • ibmensourceid
      • ibmendefaultshort
      • ibmendefaultlong
$ cat en.json
{
   "specversion": "3",
   "id": "1",
   "source": "1",
   "type": "1",
   "ibmensourceid": "d5620ecd-3854-4f21-b1fa-a5cb66b7b83b:api",
   "ibmendefaultshort": "1",
   "ibmendefaultlong": "3000"
}
Event Notificationの実行。URLは、source画面に記載あり。
$ iam_token=`ibmcloud iam oauth-tokens | awk '{printf $4}'`
$ curl -X POST -L -H "Authorization: Bearer ${iam_token}" -H 'Content-type: application/json' -d '@en.json' https://eu-de.event-notifications.cloud.ibm.com/event-notifications/v1/instances/<instance ID>/notifications

image.png

もしくは

[root@new-syasuda-tok1-vpc1 ~]# export IBMCLOUD_EN_ENDPOINT=https://eu-de.event-notifications.cloud.ibm.com/event-notifications
[root@new-syasuda-tok1-vpc1 ~]# ibmcloud event-notifications send-notifications --instance-id <instance ID> --body '{"subject": "This is test", "id": "1", "specversion": "3", "ibmensourceid":"d5620ecd-3854-4f21-b1fa-a5cb66b7b83b:api", "source": "1", "type": "1", "ibmendefaultshort":"1", "ibmendefaultlong":"3000"}'
...

notification_id   d6ae7e4b-c7da-455b-a649-75db96648371

以下はここで作成した、Webhook.site環境に送付した際の実行例。
image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?