Template

Qmonus SDKにおけるTemplateサービスは、テンプレートエンジンを各種プラグインから利用するためのサービスです。テンプレートエンジンはjinja2を使用しています。

Templateが保有する属性

  • tag: テンプレートをユニークに識別するためのタグ名を指定します。

  • template: テンプレートを記述します。jinja2フォーマットで記述する必要があります。

  • schema: テンプレートのレンダリングパラメータのバリデーションに利用するjsonスキーマを記述します。オプションです。

  • expire_seconds: Qmonus SDKでは、テンプレートにレンダリングした結果を一定期間キャッシュすることができ、そのキャッシュ保持期限を指定します。デフォルトは0秒です。Templateサービスは、テンプレートのレンダリング結果をデータベースに一定期間保存できます。例えば、Scenarioからレンダリングされた結果を、別のシステムが特定の期間内に取得しに来るようなシーケンスで使用されます。

  • metadata: テンプレートに任意のメタデータ辞書を設定できます。


レンダリングの方法

登録したテンプレートのレンダリングは、/templatesAPIを利用する、rendering組込み関数を利用する、あるいはFrontalのGUIを利用することによって実行できます。

  • サンプルテンプレート
tag: config
template: |-
  hostname {{ hostName }}
  !
  enable password {{ enablepass }}
  !
  username {{ username }} password 0 {{ password }}
  no aaa new-model
  !
  int vlan 1
  ip address {{ ipAddress }} {{ subnet }}
  !
  end
expire_seconds: 10


  • rendering組込み関数を使ったレンダリング方法

rendering組込み関数は、tagを引数として指定してレンダリングに必要なパラメータはキーワード引数で渡すことができます。

# Example of getting the result of template rendering by text
>>> r = await rendering("config", hostName="R1", enablepass="admin", username="aaa", password="bbb", ipAddress="10.10.10.101", subnet="255.255.255.0")↵
... print(r)↵
... ↵
hostname R1
!
enable password admin
!
username aaa password 0 bbb
no aaa new-model
!
int vlan 1
ip address 10.10.10.101 255.255.255.0
!
end↵
↵


Tip

複数のテンプレートを組み合わせる場合は、includeを使用できます。

親テンプレートの例

tag: main_template
template: |-
  name: {{ name }}
  % if age > 0:
      {% include "sub_template" %}
  % endif
expire_seconds: 3600


子テンプレートの例

tag: sub_template
template: 'age: {{ age }}'
expire_seconds: 3600


レンダリング

>>> r = await rendering("main_template", name="testUser", age=0)↵
... print(r)↵
... ↵
name: testUser↵
↵
>>> r = await rendering("main_template", name="testUser", age=10)↵
... print(r)↵
... ↵
name: testUser
    age: 10↵
↵


レンダリングAPI

テンプレートのレンダリング結果を取得するレベルのユースケースは、前述したrendering組込み関数で充分賄えます。レンダリングAPIは、レンダリング結果をキャッシュして別のルーチンでそれを取り出したり、外部へ転送するようなより高度なユースケースで利用することを想定しています。

PATCH /templates/{tag}
リクエスト本文の定義スキーマは以下の通りです。
type: object
properties:
  output_type:
    type: string
    enum:
    - response
    - file
    - db
  params:
    type: object
  output_tag:
    type:
    - 'null'
    - string
  rendering:
    type: string
    enum:
    - each time
    - in advance
  file_transfer_dests:
    type: array
    minItems: 1
    items:
      type: object
      properties:
        host:
          type: string
        port:
          type:
          - integer
          - string
        protocol:
          type: string
          enum:
          - scp
          - sftp
        username:
          type: string
        password:
          type: string
        pem_file:
          type: string
        directory:
          type: string
        line_feed:
          type: string
          enum:
          - CR
          - CR+LF
        charset:
          type: string
          enum:
          - utf-8
          - shift_jis
          - cp932
      required:
      - protocol
      - host
      - port
      - username
      - password
required:
- output_type
- params


  • output_type:
    レンダリング結果の出力方法を指定します。responseを指定すると、結果はHTTP応答本文に格納されます。fileが指定されている場合、結果はファイルに保存され、ファイルパスがHTTP応答本文に格納されます。

Warning

dbモードは、レンダリング結果を取得するためにAPIエンドポイントが取得できますがこのエンドポイントは、テンプレート登録時に指定された expire_secondsの間のみ使用できます。

  • params:
    レンダリングに使用するテンプレート変数を辞書型(dict)で指定します。
  • output_tag:
    fileまたはdbモードの場合にレンダリング結果を出力する際の識別タグを指定します。指定しない場合、自動的に割り当てられます。
  • rendering:
    dbモードでは、APIを介してレンダリング結果を取得できますが、APIリクエストごとにレンダリングするか、レンダリング結果を事前にキャッシュしておくかを指定できます。前者はeach time、後者はin advanceを選択します。
  • file_transfer_dests:
    レンダリング結果のファイルを外部に転送するオプションが指定できます。転送プロトコルはscpまたはsftpをサポートしています。fileモードにのみ有効なパラメータです。