JS7® JobScheduler Web API 利用サンプル

# 概要

JS7 JobSchedulerの、Web APIを使った処理のサンプルです。
初期設定、定型動作チェック、外部システムとの連携処理時に利用すると思われる処理の例をとりあげます。

# 前提条件

- JS7 jobschedulerのセットアップや基本操作を習熟していること。
まだの方はこちらから  [JS7® JobScheduler 関連記事の目次](https://qiita.com/saitamanokusa/items/645021798f5d17a79baf)
- curlおよびjqコマンドを使用した例を取り上げます。利用環境に応じて読み替えてください。
- 本記事はJobScheduler Ver. 2.5.2 で確認した内容です。
今後内容が変わる可能性があります。

# 認証

## Login/Logoutなど

認証でaccessTokenを取得。
以降の処理では、ヘッダーにこのTokenを指定する必要がある。

```shell
# Basic認証
js7_api_url="http://js7-docker.saitamanokusa.net:4446/joc/api"
js7_user="root"
js7_password="root"
js7_basicauth=$(echo -n "$js7_user:$js7_password" | base64)
res=$(curl -k -s -X POST \
    -H "Authorization: Basic $js7_basicauth" -H "Content-Type: application/json" \
    $js7_api_url/authentication/login)

# 認証Token取得
js7_access_token=$(echo "$res" |jq -r .accessToken)

# logout
res=$(curl -k -s -X POST \
   -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
   $js7_api_url/authentication/logout)

```

## session 確認

```shell
# sessin 取得 ※単純に json="{}" でも可
json=$(cat << EOS
{
  "accountName": "$js7_user"
}
EOS
)
res=$(curl -k -s -X POST \
   -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
   -d "$json" $js7_api_url/iam/sessions)
echo "$res" | jq .

# session 更新
curl -k -s -X POST -H "X-Access-Token: $js7_access_token" $js7_api_url/touch | jq .
```

## Permission確認

```shell
# 現在の割り当てられているRole、Permissionをチェック
res=$(curl -k -s -X POST \
    -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
    "$js7_api_url/authentication/joc_cockpit_permissions")
echo "$res" | jq .
```

# Controller/Agent

## controller管理

```shell
# controller 接続テスト
json=$(cat << EOS
{
  "url": "http://js7-controller-primary:4444"
}
EOS
)
res=$(curl -k -s -X POST \
   -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
   -d "$json" $js7_api_url/controller/test)


# controller 登録
json=$(cat << EOS
{
  "controllers": [
    {
      "url": "http://js7-controller-primary:4444",
      "role": "STANDALONE",
      "title": "STANDALONE CONTROLLER"
    }
  ]
}
EOS
)
res=$(curl -k -s -X POST \
   -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
   -d "$json" $js7_api_url/controller/register)

# controllrId 取得
res=$(curl -k -s -X POST -H "X-Access-Token: $js7_access_token" $js7_api_url/controller/ids)
js7_controller_id=$(echo "$res" |jq -r .controllerIds[])

# controller 削除
json=$(cat << EOS
{
  "controllerId": "$js7_controller_id"
}
EOS
)
res=$(curl -k -s -X POST \
   -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
   -d "$json" $js7_api_url/controller/unregister)
```

## agent管理

```shell
# agent 登録
json=$(cat << EOS
{
  "controllerId": "$js7_controller_id",
  "agents": [
    {
      "agentId": "js7-agent-primary",
      "agentName": "js7-agent-primary",
      "agentNameAliases": ["primaryAgent"],
      "url": "http://js7-agent-primary:4445"
    }
  ]
}
EOS
)
res=$(curl -k -s -X POST \
   -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
   -d "$json" $js7_api_url/agents/inventory/store)


# agent 情報取得
json=$(cat << EOS
{
  "controllerId": "$js7_controller_id"
}
EOS
)
res=$(curl -k -s -X POST \
   -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
   -d "$json" $js7_api_url/agents)
echo "$res" | jq '.agents'


# agent 削除
json=$(cat << EOS
{
  "controllerId": "$js7_controller_id",
  "agentId": "js7-agent-primary"
}
EOS
)
res=$(curl -k -s -X POST \
   -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
   -d "$json" $js7_api_url/agent/delete)


# agent deploy
json=$(cat << EOS
{
  "controllerId": "$js7_controller_id",
  "agentIds": ["js7-agent-primary"]
}
EOS
)
res=$(curl -k -s -X POST \
   -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
   -d "$json" $js7_api_url/agents/inventory/deploy)


# agent reset
json=$(cat << EOS
{
  "controllerId": "$js7_controller_id",
  "agentId": "js7-agent-primary",
  "force": false
}
EOS
)
res=$(curl -k -s -X POST \
   -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
   -d "$json" $js7_api_url/agent/reset)
```

# Identity管理

## IAM global settings

セッションタイムアウト値の設定など
公式ドキュメントに掲載されていない方法であるため注意！

```shell
# 初期設定時は、手動で直接値を定義
item_json=$(cat << EOS
{
  "initialPassword": "initial",
  "sessionTimeout": 7200,
  "minPasswordLength": 4
}
EOS
)
# 設定を更新
item_json=$(echo "$item_json" | jq -c . |jq -R)
json=$(cat << EOS
{
  "id": 0,
  "account": ".",
  "configurationType": "IAM",
  "objectType": "GENERAL",
  "configurationItem": $item_json
}
EOS
)
res=$(curl -k -s -X POST \
  -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
  -d "$json" $js7_api_url/configuration/save)

# 次回以降は、既存の設定を取得し、それを編集する手段のほうがよさそう
# 設定を取得
json="{}"
res=$(curl -k -s -X POST \
  -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
  -d "$json" $js7_api_url/configurations)
configuration_json=$(echo "$res" | jq -r '.configurations[] | select (.objectType=="GENERAL" and .configurationType=="IAM")')
item_json=$(echo "$configuration_json" | jq '.configurationItem'| jq -r)
# 設定値を修正
item_json=$(echo "$item_json" | jq  '.sessionTimeout = 14400')
# 設定を更新
item_json=$(echo "$item_json" | jq -c . |jq -R)
json=$(echo "$configuration_json" | jq ".configurationItem = $item_json")
res=$(curl -k -s -X POST \
  -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
  -d "$json" $js7_api_url/configuration/save)
```

## IAM Identity Service

Identity Service や role, account の追加、編集を行う場合

```shell
# Identity Service を追加
json=$(cat << EOS
{
  "identityServiceName": "JOC",
  "identityServiceType": "JOC",
  "ordering": 2,
  "singleFactorPwd": true
}
EOS
)
res=$(curl -k -s -X POST \
   -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
   -d "$json" $js7_api_url/iam/identityservice/store)

# Identity Service 一覧
json="{}"
res=$(curl -k -s -X POST \
  -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
  -d "$json" $js7_api_url/iam/identityservices)
echo "$res" | jq '.identityServiceItems'

## role 追加
json=$(cat << EOS
{
  "identityServiceName": "JOC",
  "roleName": "all"
}
EOS
)
res=$(curl -k -s -X POST \
   -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
   -d "$json" $js7_api_url/iam/role/store)

json=$(cat << EOS
{
  "identityServiceName": "JOC",
  "roleName": "api_user"
}
EOS
)
res=$(curl -k -s -X POST \
   -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
   -d "$json" $js7_api_url/iam/role/store)

# role 一覧
json=$(cat << EOS
{
  "identityServiceName": "JOC"
}
EOS
)
res=$(curl -k -s -X POST \
   -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
   -d "$json" $js7_api_url/iam/roles)
echo "$res" | jq -r .roles[].roleName

# roleにpermission設定
json=$(cat << EOS
{
  "controllerId": "",
  "identityServiceName": "JOC",
  "roleName": "all",
  "permissions": [{"permissionPath": "sos:products"}]
}
EOS
)
res=$(curl -k -s -X POST \
   -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
   -d "$json" $js7_api_url/iam/permissions/store)

json=$(cat << EOS
{
  "controllerId": "",
  "identityServiceName": "JOC",
  "roleName": "api_user",
  "permissions": [
    {"permissionPath": "sos:products:joc:auditlog:view"},
    {"permissionPath": "sos:products:joc:calendars:view"},
    {"permissionPath": "sos:products:joc:dailyplan:view"},
    {"permissionPath": "sos:products:joc:documentations:view"},
    {"permissionPath": "sos:products:joc:filetransfer:view"},
    {"permissionPath": "sos:products:controller:view"},
    {"permissionPath": "sos:products:controller:agents:view"},
    {"permissionPath": "sos:products:controller:deployment:view"},
    {"permissionPath": "sos:products:controller:locks:view"},
    {"permissionPath": "sos:products:controller:workflows:view"},
    {"permissionPath": "sos:products:controller:orders:view"}
  ]
}
EOS
)
res=$(curl -k -s -X POST \
   -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
   -d "$json" $js7_api_url/iam/permissions/store)

# account 追加
json=$(cat << EOS
{
  "identityServiceName": "JOC",
  "accountName": "api_user-001",
  "forcePasswordChange": false,
  "password": "api_user-001",
  "roles": ["api_user"]
}
EOS
)
res=$(curl -k -s -X POST \
   -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
   -d "$json" $js7_api_url/iam/account/store)

```

## IAM Identity LDAP認証追加

事前に保存済みのLDAP認証設定、Role設定を取り込み、認証設定を追加
他のIdentityTypeでも同じ要領で追加できます

```shell
servicename="LDAP"
servicetype="LDAP"

# サービス設定が存在しない場合は追加
json="{}"
identityservices=$(curl -k -s -X POST \
    -H "X-Access-Token: $js7accesstoken" -H "Content-Type: application/json" \
    -d "$json" $js7apiurl/iam/identityservices)

identity_service=$(echo "$identityservices" | jq ".identityServiceItems[] | select (.identityServiceName==\"$servicename\")")
if [ ! "$identity_service" ]; then

    ordernum=$(echo "$identityservices" | jq -r '.identityServiceItems' | jq length)
    ordernum=$(($ordernum + 1))
    json=$(
        cat <<EOS
{
  "identityServiceName": "$servicename",
  "identityServiceType": "$servicetype",
  "ordering": $ordernum,
  "singleFactorPwd": true
}
EOS
    )
    res=$(curl -k -s -X POST \
        -H "X-Access-Token: $js7accesstoken" -H "Content-Type: application/json" \
        -d "$json" $js7apiurl/iam/identityservice/store)
fi

sleep 1

# あらかじめExport済みのLDAP設定を取り込み
ldap_json=$(cat ./Identity_ldaps.saitamanokusa.net.json)
item_json=$(echo "{}" | jq ".ldap|= .+$ldap_json")
item_json=$(echo "$item_json" | jq -c . | jq -R)
json=$(
    cat <<EOS
{
  "id": 0,
  "objectType": "$servicetype",
  "configurationType": "IAM",
  "name": "$servicename",
  "configurationItem": $item_json,
  "auditLog": {}
}
EOS
)

res=$(curl -k -s -X POST \
    -H "X-Access-Token: $js7accesstoken" -H "Content-Type: application/json" \
    -d "$json" $js7apiurl/configuration/save)
sleep 1

# あらかじめExport済みのrole設定を追加
identity_roles=$(cat ./Identity_roles.json)
role_names=$(echo "$identity_roles" | jq '.|keys' | jq -r .[])
for role_name in $role_names; do
    # role 作成
    json="{\"identityServiceName\": \"$servicename\", \"roleName\": \"$role_name\"}"
    res=$(curl -k -s -X POST \
        -H "X-Access-Token: $js7accesstoken" -H "Content-Type: application/json" \
        -d "$json" $js7apiurl/iam/role/store)
    sleep 1

    # permission追加
    permissoins=$(echo "$identity_roles" | jq -r ".${role_name}.controllers"[0].permissions)
    json=$(
        cat <<EOS
{
  "controllerId": "",
  "identityServiceName": "$servicename",
  "roleName": "$role_name",
  "permissions": $permissoins
}
EOS
    )
    res=$(curl -k -s -X POST \
        -H "X-Access-Token: $js7accesstoken" -H "Content-Type: application/json" \
        -d "$json" $js7apiurl/iam/permissions/store)
    sleep 1
done

```

# 初期設定

## settingsの編集

全設定値をまるごと指定する必要がある。
そのため、既存の設定値(json)を取得し、必要な修正をしたjson値を使って更新を行う。

```shell
# 設定を取得
res=$(curl -k -s -X POST -H "X-Access-Token: $js7_access_token" $js7_api_url/settings)
item_json=$(echo "$res" | jq -r '.configuration.configurationItem')
# 設定値を修正
item_json=$(echo "$item_json" | jq '.cleanup.time_zone.value = "Asia/Tokyo"')
item_json=$(echo "$item_json" | jq '.dailyplan.time_zone.value = "Asia/Tokyo"')
item_json=$(echo "$item_json" | jq '.joc.joc_reverse_proxy_url.value = "http://js7-docker.saitamanokusa.net:4446"')
# 設定を更新
item_json=$(echo "$item_json" | jq -c . |jq -R)
json=$(cat << EOS
{
  "configurationItem": $item_json
}
EOS
)
res=$(curl -k -s -X POST \
   -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
   -d "$json" $js7_api_url/settings/store)
```

## profileの編集

全設定値をまるごと指定する必要がある。
そのため、既存の設定値(json)を取得し、必要な修正をしたjson値を使って更新を行う。

※ログインしないまま処理を行うと、Profile設定が崩れるため注意が必要

```shell
# 設定を取得 ※一度ログインしなければProfileは作られないため注意
json=$(cat << EOS
{
  "controllerId": "$js7_controller_id",
  "accountName": "$js7_user"
}
EOS
)
res=$(curl -k -s -X POST \
   -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
   -d "$json" $js7_api_url/profile/prefs)
item_json=$(echo "$res" | jq -r '.profileItem')

# 設定値を修正
item_json=$(echo "$item_json" | jq '.zone = "Asia/Tokyo"')
item_json=$(echo "$item_json" | jq '.locale = "en"')
item_json=$(echo "$item_json" | jq '.dateFormat = "YYYY-MM-DD HH:mm:ss"')
# 設定を更新
item_json=$(echo "$item_json" | jq -c . |jq -R)
json=$(cat << EOS
{
  "controllerId": "$js7_controller_id",
  "accountName": "$js7_user",
  "profileItem": $item_json
}
EOS
)
res=$(curl -k -s -X POST \
   -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
   -d "$json" $js7_api_url/profile/prefs/store)
echo "$res" | jq
```

# Inventory

## Inventory 登録

Inventory - Importの処理
あらかじめ保存済みの`zip`, `tar.gz`ファイルをインポート

```shell
res=$(curl -k -s -X POST \
   -H "X-Access-Token: $js7_access_token" -H "Content-Type: multipart/form-data" \
   -F file=@Defaults.zip \
   -F format=ZIP \
   -F overwrite=true \
   -F targetFolder="/" \
   $js7_api_url/inventory/import)

res=$(curl -k -s -X POST \
   -H "X-Access-Token: $js7_access_token" -H "Content-Type: multipart/form-data" \
   -F file=@Examples.tar.gz \
   -F format=TAR_GZ \
   -F overwrite=true \
   -F targetFolder="/" \
   $js7_api_url/inventory/import)
```

## Inventory 個別に追加／削除

オブジェクトの追加、読取、削除の例
※Workflow以外のオブジェクトも同様の操作で対応可

```shell
# あらかじめ保存済みのjsonファイルからWorkflow追加／更新
item_json=$(cat ./workflow/test_workflow.json)
item_json=$(echo "$item_json" | jq -c)
json=$(
  cat <<EOS
{
  "path": "/Practice/test_workflow",
  "objectType": "WORKFLOW",
  "configuration": $item_json
  }
}
EOS
)
res=$(curl -k -s -X POST \
  -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
  -d "$json" $js7_api_url/inventory/store)

# 指定したWorkflow設定をjsonで保存
json=$(
  cat <<EOS
{
  "path": "/Practice/test_workflow",
  "objectType": "WORKFLOW"
  }
}
EOS
)
res=$(curl -k -s -X POST \
  -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
  -d "$json" $js7_api_url/inventory/read/configuration)
echo "$res" | jq '.configuration' > backup_test_workflow.json

# 指定したWorkflowオブジェクトを削除
json=$(
  cat <<EOS
{
  "objects":[
    {
      "path": "/Practice/test_workflow",
      "objectType": "WORKFLOW"
    }
  ]
  }
}
EOS
)
res=$(curl -k -s -X POST \
  -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
  -d "$json" $js7_api_url/inventory/remove)

```

## JobResource編集

利用環境に応じて設定の一部を編集する例
(設定済みデータをインポートし、最小限の修正のみで済ませたほうがよい)

```shell
# 設定を取得
json=$(cat << EOS
{
  "path": "/Defaults/Default",
  "objectType": "JOBRESOURCE"
}
EOS
)
res=$(curl -k -s -X POST \
   -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
   -d "$json" $js7_api_url/inventory/read/configuration)
item_json=$(echo "$res" | jq .configuration)

# 設定値を修正
item_json=$(echo "$item_json" |jq '.arguments.js7JocUrl = "\"http://js7-docker.saitamanokusa.net:4446\""')
item_json=$(echo "$item_json" |jq '.arguments.js7AgentHostname= "env('\''HOSTNAME'\'', env('\''COMPUTERNAME'\'',\"\"))"')
item_json=$(echo "$item_json" |jq '.arguments.js7ScheduledDate = "scheduledOrEmpty(format='\''yyyy-MM-dd HH:mm:ssZ'\'', timezone='\''Asia/Tokyo'\'')"')
item_json=$(echo "$item_json" |jq '.arguments.js7ScheduledYear = "scheduledOrEmpty(format='\''yyyy'\'', timezone='\''Asia/Tokyo'\'')"')
item_json=$(echo "$item_json" |jq '.arguments.js7ScheduledMonth = "scheduledOrEmpty(format='\''MM'\'', timezone='\''Asia/Tokyo'\'')"')
item_json=$(echo "$item_json" |jq '.arguments.js7ScheduledDay = "scheduledOrEmpty(format='\''dd'\'', timezone='\''Asia/Tokyo'\'')"')
item_json=$(echo "$item_json" |jq '.arguments.js7ScheduledHour = "scheduledOrEmpty(format='\''HH'\'', timezone='\''Asia/Tokyo'\'')"')
item_json=$(echo "$item_json" |jq '.arguments.js7ScheduledMinute = "scheduledOrEmpty(format='\''mm'\'', timezone='\''Asia/Tokyo'\'')"')
item_json=$(echo "$item_json" |jq '.arguments.js7ScheduledSecond = "scheduledOrEmpty(format='\''ss'\'', timezone='\''Asia/Tokyo'\'')"')
item_json=$(echo "$item_json" |jq '.arguments.js7JobStartDate = "now(format='\''yyyy-MM-dd HH:mm:ssZ'\'', timezone='\''Asia/Tokyo'\'')"')
item_json=$(echo "$item_json" |jq '.arguments.js7JobStartYear = "now(format='\''yyyy'\'', timezone='\''Asia/Tokyo'\'')"')
item_json=$(echo "$item_json" |jq '.arguments.js7JobStartMonth = "now(format='\''MM'\'', timezone='\''Asia/Tokyo'\'')"')
item_json=$(echo "$item_json" |jq '.arguments.js7JobStartDay = "now(format='\''dd'\'', timezone='\''Asia/Tokyo'\'')"')
item_json=$(echo "$item_json" |jq '.arguments.js7JobStartHour = "now(format='\''HH'\'', timezone='\''Asia/Tokyo'\'')"')
item_json=$(echo "$item_json" |jq '.arguments.js7JobStartMinute = "now(format='\''mm'\'', timezone='\''Asia/Tokyo'\'')"')
item_json=$(echo "$item_json" |jq '.arguments.js7JobStartSecond = "now(format='\''ss'\'', timezone='\''Asia/Tokyo'\'')"')

# 設定を更新
json=$(cat << EOS
{
  "path": "/Defaults/Default",
  "objectType": "JOBRESOURCE",
  "configuration": $item_json
}
EOS
)
res=$(curl -k -s -X POST \
   -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
   -d "$json" $js7_api_url/inventory/store)
```

## Inventory Deploy

Workflow,JobResourceなどをcontrollerにデプロイし、実行可能な状態にする。

```shell
json=$(cat << EOS
{
  "controllerIds": [
    "$js7_controller_id"
  ],
  "store": {
    "draftConfigurations": [
      {
        "configuration": {
          "path": "/Defaults",
          "objectType": "FOLDER",
          "recursive": true
        }
      },
      {
        "configuration": {
          "path": "/Examples.Unix",
          "objectType": "FOLDER",
          "recursive": true
        }
      }
    ],
    "deployConfigurations": [
      {
        "configuration": {
          "path": "/Defaults",
          "objectType": "FOLDER",
          "recursive": true
        }
      },
      {
        "configuration": {
          "path": "/Examples.Unix",
          "objectType": "FOLDER",
          "recursive": true
        }
      }
    ]
  }
}
EOS
)
res=$(curl -k -s -X POST \
   -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
   -d "$json" $js7_api_url/inventory/deployment/deploy)
```

## Inventory 保存

Inventory - Exportの処理

```shell
json=$(cat << EOS
{
  "exportFile" : {
    "filename" : "export_test.zip",
    "format" : "ZIP"
  },
  "shallowCopy": {
    "objectTypes": [
      "WORKFLOW",
      "FILEORDERSOURCE",
      "JOBRESOURCE",
      "NOTICEBOARD",
      "LOCK",
      "INCLUDESCRIPT",
      "SCHEDULE",
      "WORKINGDAYSCALENDAR",
      "NONWORKINGDAYSCALENDAR",
      "JOBTEMPLATE"
    ],
    "folders": ["/"],
    "recursive": true,
    "onlyValidObjects": false,
    "withoutDrafts": false,
    "withoutDeployed": false,
    "withoutReleased": false
  }
}
EOS
)
curl -k -s -X POST \
  -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
  -o ./export_test.zip \
  -d "$json" $js7_api_url/inventory/export/folder
```

# Notification

あらかじめ保存済みの通知設定XMLファイルから設定を取り込み、リリース

```shell
# あらかじめ保存したNotificaton設定を読み込む  ※ ver 2.5.3 にて対応可
notification_xml=$(cat "./Notification_slackonly.xml")
json=$(echo "$notification_xml" | jq -R -s '{"configuration": . }')
res=$(curl -k -s -X POST \
  -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
  -d "$json" "$js7_api_url/notification/store")
echo "$res" | jq

# 読み込んだNotificaton設定でRelease  ※ ver 2.5.3 にて対応可
json="{}"
res=$(curl -k -s -X POST \
  -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
  -d "$json" "$js7_api_url/notification/release")
echo "$res" | jq

# Notificaton設定読み込み、Releaseを一度にする  ※ ver 2.5.2 にて対応可
notification_xml=$(cat "./Notification_slackonly.xml")
json=$(echo "$notification_xml" | jq -R -s '{"configuration": . }')
res=$(curl -k -s -X POST \
  -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
  -d "$json" "$js7_api_url/notification/release")
echo "$res" | jq

# 既存のnotification設定をファイルに保存
json="{}"
res=$(curl -k -s -X POST \
  -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
  -d "$json" $js7_api_url/notification)
echo "$res" | jq -r .configuration > Notification_backup.xml

# release済みのNotification設定を削除
json='{"release": true}'
res=$(curl -k -s -X POST \
  -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
  -d "$json" $js7_api_url/notification/delete)
echo "$res" | jq
```

# Order

## Workflow実行

```shell
# Workflow pathを指定して実行
json=$(cat << EOS
{
  "controllerId": "$js7_controller_id",
  "orders": [
    {
      "workflowPath": "/Examples.Unix/01_HelloWorld/jduHelloWorld"
    }
  ]
}
EOS
)
res=$(curl -k -s -X POST \
   -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
   -d "$json" $js7_api_url/orders/add)

# ordername指定、60秒後、変数指定で実行
json=$(cat << EOS
{
  "controllerId": "$js7_controller_id",
  "orders": [
    {
      "orderName": "api_order",
      "workflowPath": "/Examples.Unix/04_ErrorHandling/jduFail",
      "scheduledFor": "now+60",
      "arguments": {
        "return_value_job1": "0"
      }
    }
  ]
}
EOS
)
res=$(curl -k -s -X POST \
   -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
   -d "$json" $js7_api_url/orders/add)

# order 状態確認  ※完了すると消える
js7_order_id=$(echo "$res" | jq -r '.orderIds[]')
json=$(cat << EOS
{
  "controllerId": "$js7_controller_id",
  "orderIds": ["$js7_order_id"]
}
EOS
)
res=$(curl -k -s -X POST \
   -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
   -d "$json" $js7_api_url/orders)


# order 結果確認  ※スケジュール状態では表示されない
js7_order_id=$(echo "$res" | jq -r '.orderIds[]')
json=$(cat << EOS
{
  "controllerId": "$js7_controller_id",
  "orderId": "$js7_order_id"
}
EOS
)
res=$(curl -k -s -X POST \
   -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
   -d "$json" $js7_api_url/orders/history)

echo "$res" | jq -r .

# order キャンセル
json=$(cat << EOS
{
  "controllerId": "$js7_controller_id",
  "path": "/Examples.Unix/04_ErrorHandling/jduFail",
  "orderIds": ["$js7_order_id"]
}
EOS
)
res=$(curl -k -s -X POST \
   -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
   -d "$json" $js7_api_url/orders/cancel)
```

## Notification確認

通知の状況を確認（事前にNotification設定が追加されていること）

```shell
json=$(
  cat <<EOS
{
  "controllerId": "$js7_controller_id"
}
EOS
)
res=$(curl -k -s -X POST \
  -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
  -d "$json" $js7_api_url/monitoring/notifications)
echo "$res" | jq
```

# Calendar

```shell
# カレンダー設定から対象日付をチェック
json="{\"path\": \"/Practice/work_workingday\"}"
res=$(curl -k -s -X POST \
  -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
  -d "$json" $js7_api_url/calendar/dates)
echo "$res" | jq '.'
```

# DailyPlan

## DailyPlan作成

```shell
# 指定日のDailyPlan作成  ※withSubmit true または未指定だと、submittedとなる
json=$(
  cat <<EOS
{
  "controllerId": "${js7_controller_id}",
  "dailyPlanDate": "2023-04-08",
  "withSubmit": false
}
EOS
)
res=$(curl -k -s -X POST \
  -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
  -d "$json" $js7_api_url/daily_plan/orders/generate)
```

## Submit

```shell
# Plan状態のOrderをSubmitにする
json="{}"
res=$(curl -k -s -X POST \
  -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
  -d "$json" $js7_api_url/daily_plan/orders/submit)

# 日付範囲指定  ※ dailyPlanDateToはfromと合わせて指定する必要あり
json=$(
  cat <<EOS
{
  "dailyPlanDateFrom": "2023-04-08",
  "dailyPlanDateTo": "2023-04-10"
}
EOS
)
res=$(curl -k -s -X POST \
  -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
  -d "$json" $js7_api_url/daily_plan/orders/submit)

```

## Cancel

```shell
# DailyPlanでSubmit状態のOrderをCancel（Planに戻す）
json="{}"
res=$(curl -k -s -X POST \
  -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
  -d "$json" $js7_api_url/daily_plan/orders/cancel)

# 日付範囲指定  ※ dailyPlanDateToはfromと合わせて指定する必要あり
json=$(
  cat <<EOS
{
  "dailyPlanDateFrom": "2023-04-08",
  "dailyPlanDateTo": "2023-04-10"
}
EOS
)
res=$(curl -k -s -X POST \
  -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
  -d "$json" $js7_api_url/daily_plan/orders/cancel)

```

## DailyPlan削除

```shell
# 指定日のDailyPlan削除  ※Submit状態のOrderがある場合は削除できないため注意
json=$(
  cat <<EOS
{
  "controllerId": "${js7_controller_id}",
  "filter": {
    "dateFor": "2023-04-10"
  }
}
EOS
)
res=$(curl -k -s -X POST \
  -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
  -d "$json" $js7_api_url/daily_plan/submissions/delete)

# 日付範囲を指定して削除 ※dateFrom指定時は、Toも必要
json=$(
  cat <<EOS
{
  "controllerId": "${js7_controller_id}",
  "filter": {
    "dateFrom": "2023-04-08",
    "dateTo": "2023-04-14"
  }
}
EOS
)
res=$(curl -k -s -X POST \
  -H "X-Access-Token: $js7_access_token" -H "Content-Type: application/json" \
  -d "$json" $js7_api_url/daily_plan/submissions/delete)

```

# 最後に

JS7は基本的にGUIでの操作イメージが強いですが、定型処理にはAPIを使っての自動化を進めることで、より利便性が向上します。
JS7を使った環境構築、動作テスト、システム間連携など、自動化を検討する際の参考にしていただければ幸いです。

# 参考

[JS7 - REST Web Service API](https://kb.sos-berlin.com/display/JS7/JS7+-+REST+Web+Service+API)
[JOC API](https://www.sos-berlin.com/JOC/latest/raml-doc/JOC-API/index.html)
[JS7 - How To - API - Shell](https://kb.sos-berlin.com/display/JS7/JS7+-+How+To+-+API+-+Shell)
サンプルがいくつか掲載されている

その他`audit.log`にて、必要操作の確認が可能