Bookmark this page

Guided Exercise: Importing OpenAPI Definitions

In this exercise, you will import the OpenApi definition from an existing API and explore the ActiveDocs generated by Red Hat 3scale API Management.

Outcomes

You should be able to:

  • Import the OpenApi definition of an API into 3scale API Management.

  • Use ActiveDocs in the Admin Portal to explore the API.

  • Update the Documentation template in Admin Portal to show all the ActiveDocs in the Developer Portal.

As the student user on the workstation machine, use the lab command to prepare your system for this exercise.

This command deploys a pet shelter API to the Red Hat OpenShift Container Platform (RHOCP) cluster.

[student@workstation ~]$ lab start portal-openapi

Procedure 5.3. Instructions

  1. Add the backend URL to the OpenApi definition. This is the pet shelter API internal URL accessible from within the RHOCP cluster.

    1. Examine the ~/DO240/labs/portal-openapi/openapi-definition.yaml file, and search for the CHANGE_ME URL value in the servers section.

    2. Replace the CHANGE_ME URL value with the cluster API URL, http://petshelter-api.portal-openapi.svc.cluster.local

  2. Import the openapi-definition.yaml OpenApi definition.

    1. Execute the 3scale_wrapper.sh script to import the OpenApi definition. Name the product resulting from the import with portal_openapi by providing the --target_system_name=portal_openapi argument.

      The 3scale_wrapper.sh script mounts the last parameter as a Podman volume file to run 3scale Toolbox CLI with Podman.

      [student@workstation ~]$ \
        ~/DO240/labs/portal-openapi/3scale_wrapper.sh import openapi \
        --target_system_name=portal_openapi \
        --destination=3scale-tenant \
        DO240/labs/portal-openapi/openapi-definition.yaml
      {
        "code": "E_3SCALE",
        "message": "User key must be provided by --default-credentials-userkey optional param",
        "class": "ThreeScaleToolbox::Error"
      }
      Created service id: 3, name: Pet Shelter
      Service proxy updated
      destroying all mapping rules
      Created PUT /pet$ endpoint
      Created POST /pet$ endpoint
      Created GET /pet/findByStatus$ endpoint
      Created GET /pet/{petId}$ endpoint
      Created DELETE /pet/{petId}$ endpoint

      You can ignore the error message.

      Note

      You can add the --default-credentials-userkey=anonymous_api_key option to allow for unauthenticated endpoints. This creates an Anonymous Access policy with the anonymous_api_key value.

    2. Use the 3scale Toolbox CLI proxy-config command to get the portal_openapi product's proxy configuration, and verify that the previous step creates a backend with the correct URL.

      [student@workstation ~]$ 3scale proxy-config show \
        3scale-tenant portal_openapi sandbox -o json \
        | jq -r '.content.proxy.api_backend'
      http://petshelter-api.portal-openapi.svc.cluster.local:80
  3. Create an application plan and an application.

    1. Create an application plan called basic_plan by using the 3scale Toolbox CLI from the command line.

      [student@workstation ~]$ 3scale application-plan create \
        3scale-tenant portal_openapi basic_plan
      Created application plan id: 13. Default: false; Disabled: false
    2. Create an application on the default john account called shelter-app, and assign it the basic_plan application plan.

      [student@workstation ~]$ 3scale application create \
        3scale-tenant john portal_openapi basic_plan shelter-app
      Created application id: 7
  4. Configure the portal_openapi product to allow access from the Admin Portal and the Developer Portal.

    1. Navigate to IntegrationPolicies from the portal_openapi product page. Then update the policy chain by adding the CORS Request Handling policy and placing it before the 3scale APIcast policy in the policy chain.

      Configure the CORS policy by setting:

      • allow_origin: (3scale-admin|3scale).apps.ocp4.example.com

      The syntax between parentheses allows our API to be called from:

      • The Admin Portal domain: 3scale-admin.apps.ocp4.example.com

      • The Developer Portal domain: 3scale.apps.ocp4.example.com

      Then click Update Policy and then Update Policy Chain.

    2. Promote the portal_openapi configuration first to staging and then to production.

  5. Verify that you can reach the petshelter-api RHOCP service by using the ActiveDocs in the Admin Portal.

    1. Click ActiveDocs from the side menu in the portal_openapi product. Then click portal_openapi to go to the interactive documentation.

    2. Click GET /pet/findByStatus to open the endpoint documentation.

      To test the call click Try it out, then:

      • Select the available value for the status parameter.

      • Click the Fisrt userkey from latest 5 applications drop-down menu and select the shelter-app to automatically fill the user_key parameter.

      • Click Execute to send the request.

      The response shows in the 200 code under the Server response section.

      {
        "0": {
          "photoUrls": [
            "photo_1_url",
            "photo_2_url"
          ],
          "name": "spotty",
          "id": 0,
          "category": {
            "name": "dogs",
            "id": 6
          },
      ...output omitted...
  6. Update the Developer Portal Documentation page to show all the ActiveDocs in the tenant.

    1. Open the ~/DO240/labs/portal-openapi/dev-portal-docs.liquid file to copy its content.

    2. Navigate to the Audience page. Then click Developer Portal > Content and click Documentation to edit the documentation page.

    3. Scroll to the Draft section and replace the default template by pasting the contents from the dev-portal-docs.liquid file. Then click Save and Publish.

  7. Use the 3scale Toolbox CLI to retrieve the user_key for the shelter-app application.

    [student@workstation ~]$ 3scale application list \
      --service=portal_openapi 3scale-tenant -o json \
      | jq -r '.[] | select(.name == "shelter-app")|.user_key'
    7acb0243fdc7a30882f39dbf95261ab7
  8. Navigate to https://3scale.apps.ocp4.example.com/docs to test the ActiveDocs from the Developer Portal.

    Scroll to the portal_openapi documentation. Then click GET /pet/findByStatus to open the endpoint documentation.

    To test the call click Try it out, then:

    • Select the available value for the status parameter.

    • Paste the user_key from the previous step to fill the user_key parameter.

    • Click Execute to send the request.

    The response shows in the 200 code under the Server response section.

Finish

On the workstation machine, use the lab command to complete this exercise. This is important to ensure that resources from previous exercises do not impact upcoming exercises.

[student@workstation ~]$ lab finish portal-openapi

This concludes the guided exercise.

Revision: do240-2.11-40390f6