0
votes

I am attempting to use functionality from V1 and V2 of the Cloud Resource Manager for GCP. V1 has functionality for Projects not found within the POM import for V2, and V2 has functionality for Folders not found within the POM import for V1. Importing both just causes a conflict. I can only either create a Project utilizing V1, or Create a Folder utilizing V2.

My use case requires me to utilize both functionalities, what is the proper way to go about project and folder creation for the GCP Java library?

com.google.api.services.cloudresourcemanager.model.Project -> Works in V1, not V2 import of CloudResourceManager

/*(crm is an instance of CloudResourceManager)*/ crm.projects().create(project);

com.google.api.services.cloudresourcemanager.model.Folder -> Works in V2, not V1 import of CloudResourceManager

/*(crm is an instance of CloudResourceManager)*/
crm.folders().create(folder);

Is there a different POM import I should be using for combined functionality?

https://developers.google.com/apis-explorer/#p/cloudresourcemanager/v1/ https://developers.google.com/apis-explorer/#p/cloudresourcemanager/v2/

I imagine there is a methodology I'm overlooking pertaining to how I should be creating both GCP folders and projects within java when utilizing their SDK. Below are the guilty POM imports (One commented out currently)

        <dependency>
            <groupId>com.google.apis</groupId>
            <artifactId>google-api-services-cloudresourcemanager</artifactId>
            <version>v2-rev20201111-1.30.10</version>
        </dependency>
        <!--V1 has different libraries then V2-->
        <!--https://search.maven.org/artifact/com.google.apis/google-api-services-cloudresourcemanager/v1-rev20201111-1.30.10/jar-->
<!--        <dependency>
            <groupId>com.google.apis</groupId>
            <artifactId>google-api-services-cloudresourcemanager</artifactId>
            <version>v1-rev20201111-1.30.10</version>
        </dependency>-->
1
You cannot combine the SDKs. Rewrite your question to show what features you need from the V2 libraries. Of course you can write your own code using the REST API to add missing features. - John Hanley
I have updated with two method calls that are problematic depending on version of SDK @JohnHanley - Mark Kibbe
I am confused by your edit. What functionality is missing in V2? Since you cannot mix the libraries, pick either V1 or V2 and create a question on that version. - John Hanley
It's the definition of bad API design. The V2 doesn't include the V1 REST API definition and thus, the automatically generated Client library are in conflict and not usable in the same time. I haven't client library solution, I can just recommend you to use directly the API, without the automatically generated client library. - guillaume blaquiere

1 Answers

0
votes

As explained in my comment, it's not possible. The client library are automatically generated (based on the discovery API). And because the API v2 don't reuse the v1 definition/spec for the project, and because using 2 dependencies of the same API isn't possible, you can't use both in the same project.

The solution is to use directly the API. Here an example to call the create project API

        GoogleCredentials credentials = GoogleCredentials.getApplicationDefault();
        HttpRequestFactory factory = new NetHttpTransport().createRequestFactory(new HttpCredentialsAdapter(credentials));

        GenericData createProjectData = new GenericData();
        createProjectData.set("name","gib-test-crmapi");
        createProjectData.set("project_id","gib-test-crmapi");
        JsonHttpContent createProjectContent = new JsonHttpContent(JacksonFactory.getDefaultInstance(), createProjectData);
        HttpRequest request = factory.buildPostRequest(new GenericUrl("https://cloudresourcemanager.googleapis.com/v1/projects"),createProjectContent);

        JsonObjectParser parser = new JsonObjectParser(JacksonFactory.getDefaultInstance());
        request.setParser(parser);
        request.setThrowExceptionOnExecuteError(false);

        HttpResponse httpResponse = request.execute();

        int statusCode = httpResponse.getStatusCode();
        if (statusCode != HttpStatusCodes.STATUS_CODE_OK) {
            throw new IOException(
                    String.format(
                            "Unexpected Error code %s trying to getIDToken: %s",
                            statusCode, httpResponse2.parseAsString()));
        }
        InputStream content = httpResponse.getContent();
        if (content == null) {
            throw new IOException("Empty content from generateIDToken server request.");
        }

        GenericJson responseData = httpResponse.parseAs(GenericJson.class);
        System.out.println(responseData.toPrettyString());

You simply need the Google auth client library; include in the client library.

For example, use the Resource Manager V2 for folder management (more complex than the project management) and create the project with my code sample. No new import required, the Resource Manager v2 already include the Google auth client library!