google-apiinvalidationgoogle-cloud-cdn

Invalidate Google Cloud CDN cache from the backend


After few days of research and reading documentation, I'm almost sure It's nearly impossible, but still I would like to ask:

The goal is to invalidate all cached content in GCloud CDN on demand (due to headers changes) on the 3rd party backend, which does not use that CDN. Using gsuite it can be achieved by using the following command:

gcloud compute url-maps invalidate-cdn-cache web --path '/*' --async

But the problem is that this command requires us to login to google account via browser with client's credentials, which makes it absolutely worthless.

The sad story is that it seems Google has pretty rich API for its other services, but for CDN there is no API :(

The idea is to accept user's credentials and invalidate that cache using them. Is it even possible?


Solution

  • So, I was wrong about impossibility. I found the respective REST API method (https://cloud.google.com/compute/docs/reference/rest/v1/urlMaps/invalidateCache):

    POST https://www.googleapis.com/compute/v1/projects/{project}/global/urlMaps/{resourceId}/invalidateCache
    

    And here is a sample Ruby code to work with it using official gem:

    require 'google/apis/compute_v1'
    
    service = Google::Apis::ComputeV1::ComputeService.new
    service.authorization =
      Google::Auth::ServiceAccountCredentials
        .make_creds(
          json_key_io: File.open('/path/to/credentials.json'),
          scope:       [Google::Apis::ComputeV1::AUTH_COMPUTE]
        )
    service.invalidate_url_map_cache(
       'some-gcloud-project-id',
       'some-url-map',
       nil,
       request_id: SecureRandom.uuid
    )