functionapigoogle-cloud-platformgoogle-deployment-manager

How to enable cloud scheduler API, pubsub API, bigquery API, bucket API, cloud build API and cloud function in GCP using deployment manager?


I have written a deployment manager template which will deploy cloud scheduler, pubsub, bigquery, bucket, cloud build, cloud function inside a new project. So when we are deploying for the first time, it is giving an error because by default in a new project, the API of those services are disabled. So my question is how to Enable cloud scheduler API, pubsub API, bigquery API, bucket API, cloud build API and cloud function in GCP using deployment manager?

Thanks in advance.


Solution

  • First, you need to know the URL of all the api endpoints that you want to enable; you can search for them in the Google APIs Explorer. Usually the URL has the form $api.googleapis.com; as an example, for PubSub API the URL is pubsub.googleapis.com.

    The Deployment Manager's template to enable the PubSub API would be something like this:

    Using Python:

    def GenerateConfig(context):
      """Generate configuration."""
    
      resources = []
    
      resources.append({
        'name': 'enable_pubsub',
        'type': 'deploymentmanager.v2.virtual.enableService',
        'properties': {
          'consumerId': 'project:' + context.properties['project'],
          'serviceName': 'pubsub.googleapis.com'
        }
            })
      return {'resources': resources}
    

    Using Jinja:

    resources:
    - name: enable_pubsub
      type: deploymentmanager.v2.virtual.enableService
      properties:
        consumerId: "project: {{ properties['project'] }}"
        serviceName: pubsub.googleapis.com
    

    And to use this template to enable a particular API, you could use gcloud command:

    gcloud deployment-manager deployments create $name --template $template_file --properties project:$project_id
    

    If you want to enable multiple APIs at the same time, you can use the extend method in python and create a list of dictionaries with all the APIs you need to enable, here is the code example to enable pubsub and bigquery APIs in one run:

    def GenerateConfig(context):
      """Generate configuration."""
    
      resources = []
    
      resources.extend([{
        'name': 'enable_pubsub',
        'type': 'deploymentmanager.v2.virtual.enableService',
        'properties': {
          'consumerId': 'project:' + context.properties['project'],
          'serviceName': 'pubsub.googleapis.com'
        }},
        {
        'name': 'enable_bigquery',
        'type': 'deploymentmanager.v2.virtual.enableService',
        'properties': {
          'consumerId': 'project:' + context.properties['project'],
          'serviceName': 'bigquery.googleapis.com'
        }}]
    )
      return {'resources': resources}
    

    Using Jinja:

    resources:
    - name: enable_pubsub
      type: deploymentmanager.v2.virtual.enableService
      properties:
        consumerId: "project: {{ properties['project'] }}"
        serviceName: pubsub.googleapis.com
    - name: enable_bigquery
      type: deploymentmanager.v2.virtual.enableService
      properties:
        consumerId: "project: {{ properties['project'] }}"
        serviceName: bigquery.googleapis.com