0

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.

Oliver
  • 6,152
  • 2
  • 42
  • 75

2 Answers2

1

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:

  • path as string
  • host as string

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

Nur
  • 596
  • 2
  • 7
  • Thanks for such a descriptive answer. I did what you said, now I am getting PKIX SSL Exception. Does this mean the request for cache invalidation is working but now this is another issue. If this is the case I'll accept your answer and raise another question for the next problem. But can you confirm if this is true ? – Oliver Apr 24 '20 at 13:39
  • To confirm if r cache invalidation is working or not, could you share the exact error you are getting? This might be causing because you are attempting to access applications that are encrypted with SSL,for example HTTPS, which will throws an exception and the connection will be refused. – Nur Apr 24 '20 at 16:32
  • You could check few things: Confirm that you have imported the public certificate of the target instance into the truststore according to the Connecting to SSL Services instructions. Also confirm that any certificates have been imported into the correct truststore; you may have multiple JRE/JDKs. You could also verify that the target server is configured to serve SSL correctly. This can be done with the SSL Server Test tool. If all else fails, your truststore might be out of date. Upgrade Java to the latest version supported by your application. – Nur Apr 24 '20 at 16:36
  • I did as you said I was able to set path to flush cache using requestBody. about the SSL issue Not sure what happened but it just worked after a few days. May be someone from DevOps team installed an SSL certificate. Thanks a lot – Oliver Apr 27 '20 at 13:29
0

I would suggest to checking developers.google.com Compute.UrlMaps.InvalidateCache class, use Method summary description I believe it would be helpful for you to understand this class and how to incorporate it in your code. It contains method details and parameter description for example

Constructor Detail InvalidateCache

protected InvalidateCache(java.lang.String project,
                          java.lang.String urlMap,
                          CacheInvalidationRule content)

Initiates a cache invalidation operation, invalidating the specified path, scoped to the specified UrlMap. Create a request for the method "urlMaps.invalidateCache". This request holds the parameters needed by the the compute server. After setting any optional parameters, call the AbstractGoogleClientRequest.execute() method to invoke the remote operation.

InvalidateCache#initialize(com.google.api.client.googleapis.services.AbstractGoogleC lientRequest)

must be called to initialize this instance immediately after invoking the constructor.

Parameters:

  • project - Project ID for this request.
  • urlMap - Name of the UrlMap scoping this request.
  • content - the CacheInvalidationRule

Method detail for example for setAlt set

public Compute.UrlMaps.InvalidateCache set(java.lang.String parameterName,
                                           java.lang.Object value)
userX
  • 93
  • 5