Azureの小ネタ (改)

~Azureネタを中心に、色々とその他の技術的なことなどを~

Azure REST API Specifications をパースしてモデル情報を見てみる

以前、手書き(UMLツール)で以下を作図したんですが、手書きで書き起こすのはちょっとシンドイです。

statemachine.hatenablog.com

この手の物には、機械可読可能なメタモデル的な定義があるわけでして、それが何かというと以下だったりします。

GitHub - Azure/azure-rest-api-specs: The source for REST API specifications for Microsoft Azure.

これはSwagger のJSONスキーマで定義されているので、パースすれば何らかのモデル情報を読み出せると思ったので試してみました。

当初の目論見では、

  1. JSONをパース
  2. パースした内容をEMF(Eclipseメタモデルフレームワーク)でモデリングするPGを書く。EcoreでもUMLモデルでもどちらでも。
  3. 生成したモデルをGUIエディタにD&DするとDiagramができあがり。

なんてことを夢想しましたが、まだ出来ていません。

パース

Swaggerのツールセットは以下にあります。Swagger謹製のフレームワークもいくつかあります。

Open Source Integrations – Swagger

とりあえず、JavaのSwagger Parserを使ってみます。

github.com

ちなみにAzure ARM APIで使っているAutoRestも列挙されています。

パースはとても簡単で、以下を実行するだけです。リンクを直接読んでくれます。リンク先は、Azure ARM API Computeのスキーマです。

String url = "https://raw.githubusercontent.com/Azure/azure-rest-api-specs/master/arm-compute/2015-06-15/swagger/compute.json";

SwaggerParser parser = new SwaggerParser();
Swagger swagger = parser.read(url);

swagger.getDefinitions() から定義されたモデル情報が取得できますので、適当にたどって表示させてみましょう。 例えば、VirtualMachinePropertiesというモデルのプロパティを表示させると、以下のようなプロパティ群を持つことが分かります。

** VirtualMachineProperties **
desc  : Describes the properties of a Virtual Machine.
  hardwareProfile      : ref -> #/definitions/HardwareProfile
  storageProfile       : ref -> #/definitions/StorageProfile
  osProfile            : ref -> #/definitions/OSProfile
  networkProfile       : ref -> #/definitions/NetworkProfile
  diagnosticsProfile   : ref -> #/definitions/DiagnosticsProfile
  availabilitySet      : ref -> #/definitions/SubResource
  provisioningState    : string              
  instanceView         : ref -> #/definitions/VirtualMachineInstanceView
  licenseType          : string        

なんか良い感じです。タブン、頂点のVirtualMachineモデルからたどって行けばなんとか行けそうです。

allOf

で、VirtualMachineモデルのパースがうまくいきません。スキーマとして allOfを持っているとこちらが望んだ形でパースされないことがわかりました。スキーマの該当部分は以下です。properitesをパースしてくれない感じでした。

    "VirtualMachine": {
      "properties": {
        "plan": {
          "$ref": "#/definitions/Plan",
          "description": "Gets or sets the purchase plan when deploying virtual machine from VM Marketplace images."
        },
        "properties": {
          "x-ms-client-flatten": true,
          "$ref": "#/definitions/VirtualMachineProperties"
        },
        "resources": {
          "readOnly": true,
          "type": "array",
          "items": {
            "$ref": "#/definitions/VirtualMachineExtension"
          },
          "description": "Gets the virtual machine child extension resources."
        }
      },
      "allOf": [
        {
          "$ref": "#/definitions/Resource"
        }
      ],
      "description": "Describes a Virtual Machine."
    },

ソースみたり原因さぐっていると、以下のIssueが見つかりました。どうも、そういうスキーマっぽい感じです。

Swagger is not parsing definitions with properties along with allOf · Issue #141 · swagger-api/swagger-parser · GitHub

AutoRestにも言及されていて、AutoRestのほうはヨロシクパースしてくれるんでしょうね、きっと。

というわけで、道半ばではありますが、備忘録変わりにBlogっておきます。Parserに手を入れて続けてみようかと。