Apigee API hub enables you to consolidate and organize information about all of the APIs in your company’s organization.
Prism is an open-source HTTP mock server for OpenAPI documents.
We have built an open source solution which combines the mocking capabilities of Prism and integrates with the OpenAPI specifications stored in Apigee API hub.This solution was influenced by the @joel_gauci 's post “Using Google Cloud Run to create mock targets for Apigee”
This service can mock any OpenAPI specifications stored in the hub.
You can run this service on GCE or Cloud Run.
Pre-requisites:
- GCP project
- gcloud command line utility
- Provisioned instance of Apigee API hub on the GCP project
- Latest version of registry tool https://github.com/apigee/registry/releases
Setting up the Registry Prism mock server using Google Compute Engine
-
Setup gcloud command to use to the Apigee API hub project
gcloud config set project apigee-apihub-project -
Setup required variables:
REGISTRY_PROJECT_IDENTIFIER=$(gcloud config list --format 'value(core.project)') GCP_ZONE=us-central1-a -
Create a service account with apigeeregistry.viewer role
gcloud iam service-accounts create registry-viewer \ --description="Registry Viewer" \ --display-name="Registry Viewer" gcloud projects add-iam-policy-binding $REGISTRY_PROJECT_IDENTIFIER \ --member="serviceAccount:registry-viewer@$REGISTRY_PROJECT_IDENTIFIER.iam.gserviceaccount.com" \ --role="roles/apigeeregistry.viewer" -
Create a compute instance with runs the Package registry-prism-mock-server · GitHub container
gcloud compute instances create-with-container registry-mock-server-instance \ --machine-type=e2-micro --tags=registry-mock-service,http-server \ --scopes=https://www.googleapis.com/auth/cloud-platform \ --restart-on-failure \ --zone=$GCP_ZONE \ --service-account=registry-viewer@$REGISTRY_PROJECT_IDENTIFIER.iam.gserviceaccount.com\ --container-image ghcr.io/apigee/registry-prism-mock-server:main -
Add firewall rules
gcloud compute firewall-rules create registry-mock-service-fw \ --action allow \ --target-tags registry-mock-service \ --source-ranges 0.0.0.0/0 \ --rules tcp:80 -
Public IP for the service
MOCK_SERVICE=$(gcloud compute instances describe registry-mock-server-instance --format='get(networkInterfaces[0].accessConfigs[0].natIP)' --zone=$GCP_ZONE)
Add the API information to API hub
To test this setup we will use the registry tool to upload the sample specifications [https://petstore.swagger.io/v2/swagger.json] and test the mock service.
-
Get an access token to use for registry tool
REGISTRY_TOKEN=$(gcloud auth print-access-token) REGISTRY_PROJECT_IDENTIFIER=$(gcloud config list --format 'value(core.project)') -
We will use the definition file (petstore-example.yaml) for uploading our documentation into the API Hub
apiVersion: apigeeregistry/v1 kind: API metadata: name: petstore-example labels: apihub-lifecycle: concept apihub-style: apihub-openapi apihub-target-users: public data: displayName: Petstore Example API description: Sample API to demonstrate mock service versions: - metadata: name: v1 annotations: apihub-end-of-life-type: apihub-unknown data: displayName: v1 state: concept specs: - metadata: name: petstore.json annotations: apihub-comment: Initial version data: filename: petstore.json mimeType: application/x.openapi+gzip sourceURI: https://petstore.swagger.io/v2/swagger.json artifacts: - kind: ReferenceList metadata: name: apihub-dependencies data: displayName: "" description: Defines a list of dependencies references: [] - kind: ReferenceList metadata: name: apihub-related data: displayName: "" description: Defines a list of related resources references: [] -
Save the petstore-example.yaml and run the following command:
registry apply -f petstore-example.yaml \ --parent=projects/$REGISTRY_PROJECT_IDENTIFIER/locations/global \ --registry.address="apigeeregistry.googleapis.com:443" \ --registry.token=$REGISTRY_TOKEN
Testing the mock service
-
Test using any of the curl commands:
-
Mock endpoint url to a particular revision of API spec
curl http://$MOCK_SERVICE/projects/$REGISTRY_PROJECT_IDENTIFIER/locations/global/apis/petstore-example/versions/v1/specs/petstore.json/pet/findByStatus?status=available \ -H 'accept: application/json' -
Mock endpoint with the spec information passed in the header
curl http://$MOCK_SERVICE/mock/pet/findByStatus?status=available \ -H 'accept: application/json' \ -H "apigee-registry-spec: projects/$REGISTRY_PROJECT_IDENTIFIER/locations/global/apis/petstore-example/versions/v1/specs/petstore.json"
-
-
You should see a response from the service
[ { "id": -9223372036854776000, "category": { "id": -9223372036854776000, "name": "string" }, "name": "doggie", "photoUrls": [ "string" ], "tags": [ { "id": -9223372036854776000, "name": "string" } ], "status": "available" } ]
(Optional) Add the Mock service information to API hub
To make it easy for others in your organization to discover the mock endpoint, create a mock deployment record in API hub.
registry rpc create-api-deployment \
--api_deployment_id="mock" \
--api_deployment.display_name="mock" \
--api_deployment.api_spec_revision="projects/$REGISTRY_PROJECT_IDENTIFIER/locations/global/apis/petstore-example/versions/v1/specs/petstore.json" \
--api_deployment.endpoint_uri="http://$MOCK_SERVICE/projects/$REGISTRY_PROJECT_IDENTIFIER/locations/global/apis/petstore-example/deployments/mock" \
--api_deployment.labels="apihub-gateway=apihub-unmanaged" \
--parent="projects/$REGISTRY_PROJECT_IDENTIFIER/locations/global/apis/petstore-example" \
--registry.address="apigeeregistry.googleapis.com:443" \
--registry.token=$REGISTRY_TOKEN
