OpenAPI2Apigee (Renamed from swagger2api) - 0.2.0 Version - Generating Apigee Policies from swagger spec - Apigee-127 (a127 / swagger-node) Extensions Support

Hello Everyone,

Initial version of OpenAPI2Apigee supported conversion of OpenAPI Spec 2.0 into Apigee API Proxies & Flows. It also helps conversion of Apigee-127 or Swagger Node generated swagger spec to Apigee API proxies but what was missing initial version is support for Apigee Policies.

We are excited to announce Swagger2Api version 0.2.0 which now supports Apigee-127 Extensions. It converts a-127 extensions to Apigee Polices. In this article we will show you how to upgrade swagger2api tool and what kind of Apigee-127 extensions are supported.

Upgrade :

Upgrading npm modules are very simple and same applies for swagger2api. Please find the command line below to upgrade swagger2api to generate Apigee API Proxies -

$sudo npm update -g openapi2apigee 

That's it , all it takes is a single line of command execution in your terminal to upgrade the swagger2api to latest version. If it's successful you will see output similar to below in your terminal.

1067-screen-shot-2015-09-03-at-21127-pm.png

OpenAPI2Apigee Apigee-127 Volos Extensions :

Apigee-127 allows you to build and manage APIs using Volos. Apigee-127 also enables you to add and configure certain API behaviour without writing any code, by using 'policies'. Apigee-127 (a-127) supports following policies to do API Management locally.

  • Quota
  • Spike arrest
  • OAuth 2.0
  • API key
  • Basic auth
  • Cache
  • Analytics

How about converting a127 policies to Apigee Edge Policies ? Swagger2Api 0.20 does exactly the same. It helps you generate Apigee Edge Policies and Attach them to different paths or conditional flows. In this article let's checkout what kind of policies are currently supported and how to generate them automatically from Swagger 2.0 spec and a-127 swagger extensions.

OpenAPI2Apigee 0.2.0 Version Supported Apigee-127 Extensions :

At present , Swagger2Api 0.2.0 version supports following Apigee-127 policies,

  • Quota
  • Spike Arrest
  • Cache - Partial Support

Let's implement them and see how they work,

OpenApi2Apigee - Quota - Policy :

Please find the yaml file which demonstrates Quota policy capabilities ,

swagger: "2.0"
info:
  version: 1.0.0
  title: Swagger Petstore
  license:
    name: MIT
host: petstore.swagger.io
basePath: /v1
schemes:
  - http
consumes:
  - application/json
produces:
  - application/json
x-a127-services:
  quotaAnil:
    provider: volos-quota-memory
    options:
      timeUnit: day
      interval: 2
      allow: 2500
  spikearrest:
    provider: "volos-spikearrest-memory"
    options:
      timeUnit: "minute"
      bufferSize: 10
      allow: 10
  cache:
      provider: volos-cache-memory
      options:
        name: hello-cache
        ttl: 60000
paths:
  /pets:
    get:
      x-a127-apply:
        quotaAnil:
          pipe: request
          endPoint: proxy
        spikearrest:
          pipe: request
          endPoint: proxy
      summary: List all pets
      operationId: listPets
      tags:
        - pets
      parameters:
        - name: limit
          in: query
          description: How many items to return at one time (max 100)
          required: false
          type: integer
          format: int32
      responses:
        200:
          description: An paged array of pets
          headers:
            x-next:
              type: string
              description: A link to the next page of responses
          schema:
            $ref: Pets
        default:
          description: unexpected error
          schema:
            $ref: Error
    post:
      summary: Create a pet
      operationId: createPets
      tags:
        - pets
      responses:
        201:
          description: Null response
        default:
          description: unexpected error
          schema:
            $ref: Error
  /pets/{petId}:
    get:
      summary: Info for a specific pet
      operationId: showPetById
      tags:
        - pets
      parameters:
        - name: petId
          in: path
          required: true
          description: The id of the pet to retrieve
          type: string
      responses:
        200:
          description: Expected response to a valid request
          schema:
            $ref: Pets
        default:
          description: unexpected error
          schema:
            $ref: Error
definitions:
  Pet:
    required:
      - id
      - name
    properties:
      id:
        type: integer
        format: int64
      name:
        type: string
      tag:
        type: string
  Pets:
    type: array
    items:
      $ref: Pet
  Error:
    required:
      - code
      - message
    properties:
      code:
        type: integer
        format: int32
      message:
        type: string

If you observe above YAML file it defines two a-127 extensions,

  • x-a127-services : which helps you define the policy
  • x-a127-apply : which helps you attach the policy to specific end point.

What's new in Swagger2Api apart from A-127 spec ? In x-a127-apply extension swagger2api expects two additional options.

1. pipe :

  • To which part of flow it's attached, request or response ?
  • In Swagger2API, Quota is always attached to request for now.

2. endPoint :

  • To which end point it's attached , proxy or target ?
  • In Swagger2API, Quota is always attached to proxy for now.

Lets go ahead and use above swagger spec to generate an Api Proxy called petQuota , let's see what happens.

$ openapi2apigee generateApi petQuota -s api/swagger/swagger.yaml -d /Users/Anil/Desktop/ -D

1070-screen-shot-2015-09-03-at-25035-pm.png

Voila, It creates a petQuota API proxy with Quota Policy and attached the policy to /pets GET conditional flow. Swagger Spec , a-127 swagger extension to Apigee Policy on edge. Cool isn't it ?

1069-screen-shot-2015-09-03-at-24915-pm.png

Note : Quota Policy Swagger2Api supports only on paths for now. Doesn't support attaching to same on preflows / postFlows.

Swagger2Api - Spike - Policy : See sample a-127 extension below for same.

x-a127-services:
  spikearrest:
    provider: "volos-spikearrest-memory"
    options:
      timeUnit: "minute"
      bufferSize: 10
      allow: 10
paths:
  /pets:
    get:
      x-a127-apply:
        spikearrest:
          pipe: request
          endPoint: proxy

Note : Spike Arrest Policy Swagger2Api supports only on paths for now. Doesn't support attaching to same on preflows / postFlows.

Swagger2Api - Cache - Policy : See sample a-127 extension below for same.

cache:
    provider: volos-cache-memory
    options:
      name: hello-cache
      ttl: 60000

This version of Swagger2API has limited support for cache. It generates policies but doesn't attach to any of the flows. You need to manually attach same from the Edge UI to Proxy Pre-flow & Target Post-flow to make it work.

We are planning to support more policies soon. Please feel free to post your suggestions, issues, contributions in github repo. You can also post your feedback in comments section below.

Cheers,

Anil Sagar

Comments
merin172315
New Member

Hi Anil,

I am creating a API Proxy bundle from the yaml using openapi2apigee npm module. Is there a way to automatically add some existing shared flows into the proxy bundle before deploying the bundle and reduce the effort to add them manually.

Some shared flows needs to be included in all the proxies both in the request and response flows developed in the project. Suppose I have two shared flows namely InitialChecks and LoggingChecks to be included in the Proxy Preflow and Target PostFlow respectively. Now when I develop the proxybundle from openapi2apigee node.js module which creates proxy bundle from swagger file. Is there a way to by default add these shared flows in all the proxy bundles to be created? This will save time in adding all the basic shared flow without actually adding them from the UI or making manual changes in the proxy bundle.

Regards,

Merin

Not applicable

Hello Anil,

I've been trying to generate a new API Proxy project: RunKit + npm. How can I do this with public RunKit documents? I'm also checking the CDN for npm and GitHub to write my assignment - so I need to convert openapi yaml file to Apigee API Proxy Bundle. My task is to convert the file and generate a new project. I can use only licensed tools and apps.

Version history
Last update:
‎09-03-2015 02:33 AM
Updated by: