vishal
vishal

Reputation: 104

How to get GCP Quota limits for virtual machine using token based access

I want to get the GCP VM limits using token based access. Below is my python code.

from googleapiclient import discovery
from oauth2client import AccessTokenCredentials
import httplib2

token = 'xyz'
credentials = AccessTokenCredentails(access_token=token, user_agent='MyAgent')
http = credentials.authorize(httplib2.Http())
service = discovery.build('serviceusage', 'v1', http=http)
#...
# code to get the GCP Quota for VM.

I have to get the quota before creating the VMs before running terraform to create resources to make sure the number of instances are available.

Upvotes: 2

Views: 914

Answers (1)

Wojtek_B
Wojtek_B

Reputation: 4443

To get a value about the actual resource's usage you can use a Cloud Monitoring API and the consumer_quota resource. It's not going to be a percentage but actual value.

For example:

You want to know how many VM's there's in us-central1

There's a metric for that: serviceruntime.googleapis.com/quota/allocation/usage. It provides with actual usage.

Then you can create a MQL query to filter everything you need:

fetch consumer_quota
| metric 'serviceruntime.googleapis.com/quota/allocation/usage'
| filter
    (resource.service == 'compute.googleapis.com')
    && (metric.quota_metric == 'compute.googleapis.com/instances')
| group_by 1d, [value_usage_aggregate: aggregate(value.usage)]
| every 1d

Now - when you run this through API with curl:

curl -d @query.json -H "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" -X POST \
https://monitoring.googleapis.com/v3/projects/${PROJECT_ID}/timeSeries:query

you will get a response:

{
  "timeSeriesDescriptor": {
    "labelDescriptors": [
      {
        "key": "resource.project_id"
      },
      {
        "key": "resource.service"
      },
      {
        "key": "resource.location"
      },
      {
        "key": "metric.quota_metric"
      }
    ],
    "pointDescriptors": [
      {
        "key": "value_usage_aggregate",
        "valueType": "INT64",
        "metricKind": "GAUGE",
        "unit": "1"
      }
    ]
  },
  "timeSeriesData": [
    {
      "labelValues": [
        {
          "stringValue": "my-test-project"
        },
        {
          "stringValue": "compute.googleapis.com"
        },
        {
          "stringValue": "us-central1"
        },
        {
          "stringValue": "compute.googleapis.com/instances"
        }
      ],
      "pointData": [
        {
          "values": [
            {
              "int64Value": "11"
            }
          ],
          "timeInterval": {
            "startTime": "2022-03-30T08:38:44.064174Z",
            "endTime": "2022-03-30T08:38:44.064174Z"
          }
        }
      ]
    }
  ]
}

Line that interests you is "int64Value": "11" that contains number 11 which in my case is the current number or VM's (it's a sum of both running and stoped).

Now "all" you need to do is to incorporate that into your python script.

Upvotes: 3

Related Questions