Friday, March 29, 2024
No menu items!
HomeCloud ComputingUtilizing Cloud Support API to Programmatically Update Support Cases

Utilizing Cloud Support API to Programmatically Update Support Cases

Have you ever thought of leveraging Google Cloud’s Support API to programmatically manage your support cases? Or what if you wanted to create some scripts that can be plugged into your existing support case management system? In this blog post, I’ll walk you through how we can utilize Google Cloud’s Support API in order to solve a very particular use case a company is facing. 

NOTE: Be very careful when making requests to the Cloud Support API as this affects live cases that could notify live support agents when updated. If you are just testing the API, be sure to create cases with the testCase flag set to true.

Problem: The company wants to copy the operations team’s email alias to each support case that is open. One way to do this is to manually go through each support case in the GCP console to add the email address. But this can get very tedious, especially if they have many active support cases!

(You can manually add an email address in the console under “Case sharing” but this can be inefficient if you have a lot of support cases)

Solution: To solve this problem, we can write a simple Java program that will update all open cases with the email address that they want to include. This blog is going to walk you through the steps of the solution but feel free to refer to the final code here.

Before you begin:

We need to set up the following before getting started:

Verify that you have a GCP account with at least Standard Support

Enable “Google Cloud Support API” in your Cloud Console’s API & Services page 

Create a service account with the following roles:

“Organization Viewer” role or any role that grants the “resourcemanager.organizations.get” permission

“Tech Support Editor” role

Download the service account’s private key in a JSON format and set your environment variable “GOOGLE_APPLICATION_CREDENTIALS” to point to its path

Let’s start coding!

Step 1: Initialize a Java project

You’ll first need to initialize a Java project. I highly recommend using build automation tools like maven in order to do so. You may follow the quickstart guide here to initialize your Java project.

Step 2: Add dependencies for Cloud Support API

There are three main dependencies that are needed for our Java program to connect with the Cloud Support API – Google API Client Library for Java, Cloud Support API Client Library for Java (v2 beta), and Google Auth Library. Since we are using maven, it’s as simple as adding these three dependencies into your .pom file for these to be included in your Java project.

code_block[StructValue([(u’code’, u'<dependency>rn <groupId>com.google.api-client</groupId>rn <artifactId>google-api-client</artifactId>rn <version>1.33.2</version>rn </dependency>rn rn <dependency>rn <groupId>com.google.apis</groupId>rn <artifactId>google-api-services-cloudsupport</artifactId>rn <version>v2beta-rev20211103-1.32.1</version>rn </dependency>rn rn <dependency>rn <groupId>com.google.auth</groupId>rn <artifactId>google-auth-library-oauth2-http</artifactId>rn <version>1.4.0</version>rn </dependency>’), (u’language’, u”), (u’caption’, <wagtail.wagtailcore.rich_text.RichText object at 0x3e565f2757d0>)])]

Step 3: Add the boilerplate code to your project

You may use the boilerplate code that I’ve written below to simplify your code.

code_block[StructValue([(u’code’, u’import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;rnimport com.google.api.client.http.HttpRequestInitializer;rnimport com.google.api.client.http.HttpTransport;rnimport com.google.api.client.json.JsonFactory;rnimport com.google.api.client.json.gson.GsonFactory;rnimport com.google.api.services.cloudsupport.v2beta.CloudSupport;rnimport com.google.api.services.cloudsupport.v2beta.model.CloudSupportCase;rnimport com.google.api.services.cloudsupport.v2beta.model.ListCasesResponse;rnimport com.google.auth.http.HttpCredentialsAdapter;rnimport com.google.auth.oauth2.GoogleCredentials;rnimport java.io.IOException;rnimport java.security.GeneralSecurityException;rnimport java.util.ArrayList;rnimport java.util.Collections;rnimport java.util.List;rnimport javax.swing.JOptionPane;rn rn rn// starter code for cloud support API to initialize itrnpublic class App {rn rn // Shared constantsrn static final String CLOUD_SUPPORT_SCOPE = “https://www.googleapis.com/auth/cloudsupport”;rn rn static String projectResource = “projects/”;rn rn public static void main(String[] args) {rn rn try {rn CloudSupport cloudSupport = getCloudSupportService();rn // with the CloudSupport object, you may call other methods of the Support APIrn // for example, cloudSupport.cases().get(“name of case”).execute();rn rn System.out.println(“CloudSupport API is ready to use: ” + cloudSupport);rn rn projectResource = projectResource + JOptionPane.showInputDialog(“Please enter your project number: “);rn rn rn } catch (IOException e) {rn System.out.println(“IOException caught! \n” + e);rn rn } catch (GeneralSecurityException e) {rn System.out.println(“GeneralSecurityException caught! \n” + e);rn }rn }rn rn // helper method will return a CloudSupport object which is required for thern // main API service to be used.rn public static CloudSupport getCloudSupportService() throws IOException, GeneralSecurityException {rn rn JsonFactory jsonFactory = GsonFactory.getDefaultInstance();rn HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();rn rn // this will only work if you have already set the environment variablern // GOOGLE_APPLICATION_CREDENTIALS to point to the path with your service accountrn // keyrn GoogleCredentials credentials = GoogleCredentials.getApplicationDefault()rn .createScoped(Collections.singletonList(CLOUD_SUPPORT_SCOPE));rn HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(credentials);rn rn return new CloudSupport.Builder(httpTransport, jsonFactory, requestInitializer).build();rn }rn rn}’), (u’language’, u”), (u’caption’, <wagtail.wagtailcore.rich_text.RichText object at 0x3e567517ca90>)])]

This boilerplate code has one main helper function – getCloudSupportService(). This is used to authenticate your app to the Cloud Support API and create a CloudSupport object, which is the starting point for all other Cloud Support API calls.

Step 4: Write methods to update cc field of active cases

You may include the helper methods below to your app to implement the ability to update the cc field of active cases. This post assumes that you have at least one open support case.

code_block[StructValue([(u’code’, u’public static void updateAllEmailsWith(String email) throws IOException, GeneralSecurityException {rn rn List<CloudSupportCase> listCases = listAllCases(projectResource);rn rn for (CloudSupportCase csc : listCases) {rn System.out.println(“I’m going to update email address for case: ” + csc.getName() + “\n”);rn rn updateCaseWithNewEmail(csc, email);rn }rn }rn rn // list all open casesrn public static List<CloudSupportCase> listAllCases(String parentResource)rn throws IOException, GeneralSecurityException {rn rn CloudSupport supportService = getCloudSupportService();rn rn ListCasesResponse listCasesResponse = supportService.cases().list(parentResource).setFilter(“state=OPEN”)rn .execute();rn rn List<CloudSupportCase> listCases = listCasesResponse.getCases();rn rn System.out.println(rn “Printing all ” + listCases.size() + ” open cases of parent resource: ” + parentResource);rn rn for (CloudSupportCase csc : listCases) {rn System.out.println(csc + “\n\n”);rn }rn rn return listCases;rn }rn rn // this helper method is used in updateAllEmailsWith()rn private static void updateCaseWithNewEmail(CloudSupportCase caseToBeUpdated, String email)rn throws IOException, GeneralSecurityException {rn List<String> currentAddresses = caseToBeUpdated.getSubscriberEmailAddresses();rn CloudSupport supportService = getCloudSupportService();rn if (caseToBeUpdated.getState().toLowerCase().equals(“closed”)) {rn System.out.println(“Case is closed, we cannot update its cc field.”);rn return;rn }rn if (currentAddresses != null && !currentAddresses.contains(email)) {rn currentAddresses.add(email);rn CloudSupportCase updatedCase = caseToBeUpdated.setSubscriberEmailAddresses(currentAddresses);rn //Ensure that severity is not set. Only priority should be used, or elsern //this will throw an error.rn updatedCase.setSeverity(null);rn rn supportService.cases().patch(caseToBeUpdated.getName(), updatedCase).execute();rn System.out.println(“I updated the email for: \n” + updatedCase + “\n”);rn rn // must handle case when they don’t have any email addressesrn } else if (currentAddresses == null) {rn currentAddresses = new ArrayList<String>();rn currentAddresses.add(email);rn CloudSupportCase updatedCase = caseToBeUpdated.setSubscriberEmailAddresses(currentAddresses);rn supportService.cases().patch(caseToBeUpdated.getName(), updatedCase).execute();rn System.out.println(“I updated the email for: ” + updatedCase + “\n”);rn rn } else {rn System.out.println(“Email is already there! \n\n”);rn }rn }’), (u’language’, u”), (u’caption’, <wagtail.wagtailcore.rich_text.RichText object at 0x3e5674d5c6d0>)])]

The code snippet above has three methods: 

updateAllEmailsWith() listAllCases() updateCaseWithNewEmail()

Note that updateAllEmailsWith() has to first call listAllCases() in order to get all open support cases. Afterwards, it will call updateCaseWithNewEmail() for each open support case in order to update it to include the new email address. Also note that when calling listAllCases() you need to pass in the parent resource, which is specified in the variable projectResource. This variable is updated as the app takes in your project number as an input. 

Step 5: Call updateAllEmailsWith() frommain()

Now that we have set up our app to include all the methods we need, what remains is simply calling the method updateAllEmailsWith() with the email address that you’d like to add. It will use all the helper methods that we just wrote in order to get all open support cases and update each one with the email address that we want.

We’re done coding! Easy, right? Leveraging Cloud Support API will definitely make managing your support cases much easier. It can help automate some actions that would be very tedious to do!

Next steps: This example is just one of many different use cases that we can have for the Cloud Support API. We can also use the API to create attachments, add comments, or even search cases. Each organization may require different functionalities from Google Cloud’s Support, and these can be implemented with the use of Cloud Support API. For more information, check out the following resources bellow:

Cloud Support API documentation 

Sample Code for Cloud Support API

Google Cloud Support General Documentation

Technical Support Services Guidelines

Cloud BlogRead More

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments