Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
dzlier-gcp authored Sep 13, 2018
2 parents a588ef6 + 633b9d6 commit 643315a
Show file tree
Hide file tree
Showing 17 changed files with 643 additions and 445 deletions.
18 changes: 4 additions & 14 deletions appengine-java8/tasks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ App Engine task attempts.
[Cloud Tasks API](https://console.cloud.google.com/launcher/details/google/cloudtasks.googleapis.com).
* Download and install the [Cloud SDK](https://cloud.google.com/sdk).
* Download and install [Maven](http://maven.apache.org/install.html).
* Set up [Google Application Credentials](https://cloud.google.com/docs/authentication/getting-started).

## Creating a queue

Expand All @@ -37,25 +38,18 @@ version unless configured to do otherwise.
[Using Maven and the App Engine Plugin](https://cloud.google.com/appengine/docs/flexible/java/using-maven)
& [Maven Plugin Goals and Parameters](https://cloud.google.com/appengine/docs/flexible/java/maven-reference)

### Running locally

```
mvn appengine:run
```
### Deploying

```
mvn appengine:deploy
```

## Running the Sample
## Run the Sample Using the Command Line

Set environment variables:

First, your project ID:

```
export GOOGLE_CLOUD_PROJECT=<YOUR_PROJECT_ID>
export GOOGLE_CLOUD_PROJECT=<YOUR_GOOGLE_CLOUD_PROJECT>
```

Then the queue ID, as specified at queue creation time. Queue IDs already
Expand Down Expand Up @@ -85,11 +79,7 @@ mvn exec:java -Dexec.mainClass="com.example.task.CreateTask" \

The App Engine app serves as a target for the push requests. It has an
endpoint `/tasks/create` that reads the payload (i.e., the request body) of the
HTTP POST request and logs it. The log output can be viewed with:

```
gcloud app logs read
```
HTTP POST request and logs it. The log output can be viewed with [Stackdriver Logging](https://console.cloud.google.com/logs/viewer?minLogLevel=0).

Create a task that will be scheduled for a time in the future using the
`--in-seconds` flag:
Expand Down
5 changes: 3 additions & 2 deletions appengine-java8/tasks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Copyright 2018 Google LLC
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>

<dependencies>
Expand All @@ -51,7 +52,7 @@ Copyright 2018 Google LLC
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-tasks</artifactId>
<version>0.54.0-beta</version>
<version>0.61.0-beta</version>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
Expand All @@ -69,7 +70,7 @@ Copyright 2018 Google LLC
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>1.3.1</version>
<version>1.3.2</version>
<configuration>
<deploy.promote>true</deploy.promote>
<deploy.stopPreviousVersion>true</deploy.stopPreviousVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

package com.example.task;

import com.google.cloud.tasks.v2beta2.AppEngineHttpRequest;
import com.google.cloud.tasks.v2beta2.CloudTasksClient;
import com.google.cloud.tasks.v2beta2.HttpMethod;
import com.google.cloud.tasks.v2beta2.QueueName;
import com.google.cloud.tasks.v2beta2.Task;
import com.google.cloud.tasks.v2beta3.AppEngineHttpRequest;
import com.google.cloud.tasks.v2beta3.CloudTasksClient;
import com.google.cloud.tasks.v2beta3.HttpMethod;
import com.google.cloud.tasks.v2beta3.QueueName;
import com.google.cloud.tasks.v2beta3.Task;
import com.google.common.base.Strings;
import com.google.protobuf.ByteString;
import com.google.protobuf.Timestamp;
Expand All @@ -38,7 +38,7 @@
import org.apache.commons.cli.ParseException;

public class CreateTask {
private static String GGOGLE_CLOUD_PROJECT_KEY = "GOOGLE_CLOUD_PROJECT";
private static String GOOGLE_CLOUD_PROJECT_KEY = "GOOGLE_CLOUD_PROJECT";

private static Option PROJECT_ID_OPTION = Option.builder("pid")
.longOpt("project-id")
Expand Down Expand Up @@ -109,7 +109,7 @@ public static void main(String... args) throws Exception {
if (params.hasOption("project-id")) {
projectId = params.getOptionValue("project-id");
} else {
projectId = System.getenv(GGOGLE_CLOUD_PROJECT_KEY);
projectId = System.getenv(GOOGLE_CLOUD_PROJECT_KEY);
}
if (Strings.isNullOrEmpty(projectId)) {
printUsage(options);
Expand All @@ -121,22 +121,37 @@ public static void main(String... args) throws Exception {
String payload = params.getOptionValue(PAYLOAD_OPTION.getOpt(), "default payload");

// [START cloud_tasks_appengine_create_task]
// Instantiates a client.
try (CloudTasksClient client = CloudTasksClient.create()) {

// Variables provided by the CLI.
// projectId = "my-project-id";
// queueName = "my-appengine-queue";
// location = "us-central1";
// payload = "hello";

// Construct the fully qualified queue name.
String queuePath = QueueName.of(projectId, location, queueName).toString();

// Construct the task body.
Task.Builder taskBuilder = Task
.newBuilder()
.setAppEngineHttpRequest(AppEngineHttpRequest.newBuilder()
.setPayload(ByteString.copyFrom(payload, Charset.defaultCharset()))
.setRelativeUrl("/tasks/create")
.setBody(ByteString.copyFrom(payload, Charset.defaultCharset()))
.setRelativeUri("/tasks/create")
.setHttpMethod(HttpMethod.POST)
.build());

if (params.hasOption(IN_SECONDS_OPTION.getOpt())) {
// Add the scheduled time to the request.
int seconds = Integer.parseInt(params.getOptionValue(IN_SECONDS_OPTION.getOpt()));
taskBuilder.setScheduleTime(Timestamp
.newBuilder()
.setSeconds(Instant.now(Clock.systemUTC()).plusSeconds(seconds).getEpochSecond()));
}
Task task = client.createTask(
QueueName.of(projectId, location, queueName).toString(), taskBuilder.build());

// Send create task request.
Task task = client.createTask(queuePath, taskBuilder.build());
System.out.println("Task created: " + task.getName());
}
// [END cloud_tasks_appengine_create_task]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,13 @@ public class TaskServlet extends HttpServlet {
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
log.info("Received task request: " + req.getServletPath());
if (req.getParameter("payload") != null) {
String payload = req.getParameter("payload");
log.info("Request payload: " + payload);
String output = String.format("Received task with payload %s", payload);
String body = req.getReader()
.lines()
.reduce("", (accumulator, actual) -> accumulator + actual);

if (!body.isEmpty()) {
log.info("Request payload: " + body);
String output = String.format("Received task with payload %s", body);
resp.getOutputStream().write(output.getBytes());
log.info("Sending response: " + output);
resp.setStatus(HttpServletResponse.SC_OK);
Expand Down
25 changes: 25 additions & 0 deletions iam/api-client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Cloud Identity & Access Management Samples

<a href="https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&page=editor&open_in_editor=iam/api-client/README.md">
<img alt="Open in Cloud Shell" src ="http://gstatic.com/cloudssh/images/open-btn.png"></a>

[Google Cloud Identity & Access Management](https://cloud.google.com/iam/) (IAM)
lets administrators authorize who can take action on specific resources.
These sample applications demonstrate how to interact with Cloud IAM using
the Google API Client Library for Java.

## Quickstart

Install [Maven](http://maven.apache.org/).

Build the project with:

```xml
mvn clean package
```

Run the Quickstart, which lists roles in a project:

```xml
mvn exec:java
```
78 changes: 78 additions & 0 deletions iam/api-client/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<!--
Copyright 2018 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.google.iam.snippets</groupId>
<artifactId>iam-snippets</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>iam-snippets</name>

<!--
The parent pom defines common style checks and testing strategies for our samples.
Removing or replacing it should not affect the execution of the samples in anyway.
-->
<parent>
<groupId>com.google.cloud.samples</groupId>
<artifactId>shared-configuration</artifactId>
<version>1.0.10</version>
</parent>

<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>

<dependencies>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-iam</artifactId>
<version>v1-rev247-1.23.0</version>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.4</version>
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<version>0.40</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<mainClass>com.google.iam.snippets.GrantableRoles</mainClass>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.iam.snippets;

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.iam.v1.Iam;
import com.google.api.services.iam.v1.IamScopes;
import com.google.api.services.iam.v1.model.QueryGrantableRolesRequest;
import com.google.api.services.iam.v1.model.QueryGrantableRolesResponse;
import com.google.api.services.iam.v1.model.Role;
import java.util.Collections;

public class GrantableRoles {

public static void main(String[] args) throws Exception {

GoogleCredential credential =
GoogleCredential.getApplicationDefault()
.createScoped(Collections.singleton(IamScopes.CLOUD_PLATFORM));

Iam service =
new Iam.Builder(
GoogleNetHttpTransport.newTrustedTransport(),
JacksonFactory.getDefaultInstance(),
credential)
.setApplicationName("grantable-roles")
.build();

String fullResourceName = args[0];

// [START iam_view_grantable_roles]
QueryGrantableRolesRequest request = new QueryGrantableRolesRequest();
request.setFullResourceName(fullResourceName);

QueryGrantableRolesResponse response = service.roles().queryGrantableRoles(request).execute();

for (Role role : response.getRoles()) {
System.out.println("Title: " + role.getTitle());
System.out.println("Name: " + role.getName());
System.out.println("Description: " + role.getDescription());
System.out.println();
}
// [START iam_view_grantable_roles]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// [START iam_quickstart]

package com.google.iam.snippets;

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.iam.v1.Iam;
import com.google.api.services.iam.v1.IamScopes;
import com.google.api.services.iam.v1.model.ListRolesResponse;
import com.google.api.services.iam.v1.model.Role;
import java.util.Collections;
import java.util.List;

public class Quickstart {

public static void main(String[] args) throws Exception {
// Get credentials
GoogleCredential credential =
GoogleCredential.getApplicationDefault()
.createScoped(Collections.singleton(IamScopes.CLOUD_PLATFORM));

// Create the Cloud IAM service object
Iam service =
new Iam.Builder(
GoogleNetHttpTransport.newTrustedTransport(),
JacksonFactory.getDefaultInstance(),
credential)
.setApplicationName("quickstart")
.build();

// Call the Cloud IAM Roles API
ListRolesResponse respose = service.roles().list().execute();
List<Role> roles = respose.getRoles();

// Process the response
for (Role role : roles) {
System.out.println("Title: " + role.getTitle());
System.out.println("Name: " + role.getName());
System.out.println("Description: " + role.getDescription());
System.out.println();
}
}
}
// [END iam_quickstart]
Loading

0 comments on commit 643315a

Please sign in to comment.