GCP Goodies Part 3— Google Deployment Manager — Type Providers with Custom API

By Krzysztof Grajek

Krzysztof Grajek
Meena Kadri @ Flickr CC 2.0

I have showed you how custom Type Providers in Google Deployment Manager work using Kubernetes API as an example (link here). The Kubernetes API, among many others, is provided by Google and you don’t need to worry about it much, but what if you want to use your own API server for provisioning tasks outside the GCP resources? It happens that it’s not as difficult as it might look like at the first glance.

To make our setup quick and easy, we are going to use already prepared config files to deploy simple TODO API service to Google App Engine and then use that service with our deployment. The whole idea is to create a TODO when the deployment gets created and delete it when the deployment gets deleted.

To make your custom API work with Deployment Manager you need to adhere to Swagger 2.0 specification and make your API secure in some way.

The requirements for implementing external APIs, so they can work seamlessly with Google DM are listed here.

In short, it comes to these five points:

Deployment Manager expects a RESTful API. That is, the API exposes a REST-like interface with Create, Read, Update, and Delete (CRUD) methods.

All path and query parameters resolve successfully. All path and query parameters of the API must exist as part of the resource body or exist on all methods of the API, so that Deployment Manager can match the parameter when a user supplies it.

The API’s descriptor document endpoint must be accessible. Deployment Manager makes an HTTP request to get an API’s descriptor document so the document must be hosted somewhere accessible by Deployment Manager. The document must be publicly available or protected by basic authentication.

Related to the previous point, the API has either basic authentication, or, if the API is running on Google Kubernetes Engine or Google Cloud Endpoints, the API supports OAuth 2.0 authentication using an access token from the project’s service account. Read more about authentication.

If your API is complex, you might need to configure additional options to explain to Deployment Manager how to handle less obvious API behavior.

TODO Server

Our TODO API is a simple Flask-Restplus application, created in Python and deployed to GAE. All the sources are in Github repository provided by Google here.

You can read more about Flask-Restplus itself here.

The Flask-Restplus API is defined in the following way:

and then the resources are described using an annotation mechanism, e.g:

Open up the Cloud Shell Console and clone the repository if you haven’t already done so in the previous parts of this series.

Navigate to ~/deploymentmanager-samples/community/dm_type_providers/gae and execute the following:

pip install -t lib -r requirements.txt

gcloud app deploy app.yaml --version dmapi --no-promote

You will be asked, on the way, to select the zone id where the app should be deployed, choose any option which suits your location best.

Make a note of the link your app was deployed under, in my case it was:

https://dmapi-dot-softwaremill-playground.appspot.com

After navigating to that page, we can see the UI for our TODO application with a list of swagger endpoint definitions (once you click todo link):

JSON representation of the same API description can be found under /swagger.json path.

Deploy Type Provider

First, we need to make sure that our deployment is aware of the API we have just deployed and can read its definitions. For this, navigate to custom_type_provider.yaml and change the swagger_url value:

then create deployment by executing the command inside dm_type_provider directory:

gcloud deployment-manager deployments create todorest --config custom_type_provider.yaml

Verify if the deployment and the call to our custom API were executed:

$  curl  -X GET "https://dmapi-dot-softwaremill-playground.appspot.com/todos" -H 'X-API-KEY: foo'
{
"items": [
{
"id": 1135,
"task": "some task"
}
]
}

Now, let’s delete the deployment:

gcloud deployment-manager deployments delete todorest

Running the same curl again gives us an empty list:

curl  -X GET "https://dmapi-dot-softwaremill-playground.appspot.com/todos" -H 'X-API-KEY: foo'
{
"items": []
}

Remember to clean up any resources once you finish playing around with GCP, this time check if your deployment got deleted and disable/delete the GAE application we have deployed at the beginning of this tutorial.