6

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?

Jonas
  • 121,568
  • 97
  • 310
  • 388
nattfodd
  • 1,790
  • 1
  • 17
  • 35

2 Answers2

6

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
)
nattfodd
  • 1,790
  • 1
  • 17
  • 35
  • 1
    This pointed me in the right direction. I'm confused why they put a CDN Cache Invalidation in the 'Compute' library. Seems like having a CDN library would make logical sense; or even Storage!? Thanks! – Ian Newland Jun 03 '19 at 17:20
  • 1
    I'm getting `Caught error invalid: Invalid value for field 'resource.path': ''. path must begin with /`. Any idea how to solve this? – Ariel Cabib Sep 23 '19 at 10:46
  • @nattfodd Say I want to invalidate a path /homepage how and where do I specify this in this code ? – Oliver Apr 23 '20 at 07:33
2

Now you can call:

gcloud compute url-maps list
gcloud compute url-maps invalidate-cdn-cache prod-lb --path='/test/*'
gcloud compute url-maps invalidate-cdn-cache prod-lb --async --path='/test/*'
gcloud compute url-maps list-cdn-cache-invalidations --global prod-lb

It takes forever, --async & list-cdn-cache-invalidations are your friends...

https://cloud.google.com/cdn/docs/invalidating-cached-content

gavenkoa
  • 45,285
  • 19
  • 251
  • 303