javagoogle-cloud-platformgoogle-compute-api

How to invalidate Google CDN cache using REST API Java?


I have been researching for a working example for this but haven't found any.

I referred following links Stackoverflow Link and Google Official Docs

From these documentations I did understand that I need to implement this

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.compute.Compute;
import com.google.api.services.compute.model.CacheInvalidationRule;
import com.google.api.services.compute.model.Operation;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Arrays;

public class ComputeExample {
  public static void main(String args[]) throws IOException, GeneralSecurityException {
    // Project ID for this request.
    String project = "my-project"; // TODO: Update placeholder value.

    // Name of the UrlMap scoping this request.
    String urlMap = "my-url-map"; // TODO: Update placeholder value.

    // TODO: Assign values to desired fields of `requestBody`:
    CacheInvalidationRule requestBody = new CacheInvalidationRule();

    Compute computeService = createComputeService();
    Compute.UrlMaps.InvalidateCache request =
        computeService.urlMaps().invalidateCache(project, urlMap, requestBody);

    Operation response = request.execute();

    // TODO: Change code below to process the `response` object:
    System.out.println(response);
  }

  public static Compute createComputeService() throws IOException, GeneralSecurityException {
    HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();

    GoogleCredential credential = GoogleCredential.getApplicationDefault();
    if (credential.createScopedRequired()) {
      credential =
          credential.createScoped(Arrays.asList("https://www.googleapis.com/auth/cloud-platform"));
    }

    return new Compute.Builder(httpTransport, jsonFactory, credential)
        .setApplicationName("Google-ComputeSample/0.1")
        .build();
  }
}

BUT if you see this example it only has placeholder values in its place.

IF I WANTED TO FLUSH CACHE OF A PAGE CALLED https://mywebsite.com/homepage.html WHERE WOULD I ENTER THIS INFORMATION IN THE ABOVE CODE?

Do I added it here

 credential.createScoped(Arrays.asList("https://mywebsite.com/homepage.html"));

OR Should I add it in UrlMaps? This is very confusing.


Solution

  • It should go in the request body. The request body contains data with the following structure:

    JSON representation
    {
      "path": string,
      "host": string
    }
    

    These Fields takes followings:

    If set, this invalidation rule will only apply to requests with a Host header matching host.

    You might need to create requestbody object

    CacheInvalidationRule requestBody = new CacheInvalidationRule();
    

    this should creates cacheinvalidationrule object and assigns to requestBody

    Additionally, you might also need to something like this

    requestBody.setHostand requestBody.setPath = ""
    

    This two properties takes string as an argument

    requestBody.setHost=" mywebsite.com"
    

    and

    requestBody.setPath = "/homepage.html"
    

    Hope it helps, good luck