Skip to content

Commit d7b54c4

Browse files
committed
operation CRUD
1 parent 96284ab commit d7b54c4

File tree

3 files changed

+218
-0
lines changed

3 files changed

+218
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// This sample is HAND-WRITTEN
18+
19+
package com.google.cloud.examples.automl.v1beta1;
20+
21+
import com.google.cloud.automl.v1beta1.AutoMlClient;
22+
import com.google.longrunning.OperationsClient;
23+
import org.apache.commons.cli.CommandLine;
24+
import org.apache.commons.cli.DefaultParser;
25+
import org.apache.commons.cli.Option;
26+
import org.apache.commons.cli.Options;
27+
28+
public class AutomlCancelOperation {
29+
// [START automl_cancel_operation]
30+
31+
/**
32+
* Cancel Long-Running Operation
33+
*
34+
* @param operationId Required. The ID of the operation.
35+
* @param project Required. Your Google Cloud Project ID.
36+
*/
37+
public static void sampleCancelOperation(String project, String operationId) {
38+
try (AutoMlClient client = AutoMlClient.create()) {
39+
40+
OperationsClient operationsClient = client.getOperationsClient();
41+
42+
// project = '[Google Cloud Project ID]'
43+
// operation_id = '[Operation ID]'
44+
String name = String.format("projects/%s/locations/us-central1/operations/%s",
45+
project, operationId);
46+
operationsClient.cancelOperation(name);
47+
48+
System.out.printf("Cancelled operation: %s", name);
49+
} catch (Exception exception) {
50+
System.err.println("Failed to create the client due to: " + exception);
51+
}
52+
}
53+
// [END automl_cancel_operation]
54+
55+
public static void main(String[] args) throws Exception {
56+
Options options = new Options();
57+
options.addOption(Option.builder("").required(true).hasArg(true)
58+
.longOpt("operation_id").build());
59+
options.addOption(Option.builder("").required(true).hasArg(true)
60+
.longOpt("project").build());
61+
62+
CommandLine cl = (new DefaultParser()).parse(options, args);
63+
64+
String modelId = cl.getOptionValue("operation_id", "[Operation ID]");
65+
String project = cl.getOptionValue("project", "[Google Cloud Project ID]");
66+
sampleCancelOperation(project, modelId);
67+
68+
}
69+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// This sample is HAND-WRITTEN
18+
19+
package com.google.cloud.examples.automl.v1beta1;
20+
21+
import com.google.cloud.automl.v1beta1.AutoMlClient;
22+
import com.google.longrunning.Operation;
23+
import com.google.longrunning.OperationsClient;
24+
import org.apache.commons.cli.CommandLine;
25+
import org.apache.commons.cli.DefaultParser;
26+
import org.apache.commons.cli.Option;
27+
import org.apache.commons.cli.Options;
28+
29+
public class AutomlGetOperation {
30+
// [START automl_get_operation]
31+
32+
/**
33+
* Get Long-Running Operation Status
34+
*
35+
* @param operationId Required. The ID of the operation.
36+
* @param project Required. Your Google Cloud Project ID.
37+
*/
38+
public static void sampleGetOperation(String project, String operationId) {
39+
try (AutoMlClient client = AutoMlClient.create()) {
40+
41+
OperationsClient operationsClient = client.getOperationsClient();
42+
43+
// project = '[Google Cloud Project ID]'
44+
// operation_id = '[Operation ID]'
45+
String name = String.format("projects/%s/locations/us-central1/operations/%s",
46+
project, operationId);
47+
48+
Operation operation = operationsClient.getOperation(name);
49+
50+
// Print Operation status and info
51+
System.out.printf("Operation Name: %s", (operation.getName()));
52+
System.out.printf("Done: %s", (operation.getDone()));
53+
System.out.printf("Response: %s",(operation.getResponse()));
54+
System.out.printf("Metadata: %s", (operation.getMetadata()));
55+
56+
} catch (Exception exception) {
57+
System.err.println("Failed to create the client due to: " + exception);
58+
}
59+
}
60+
// [END automl_get_operation]
61+
62+
public static void main(String[] args) throws Exception {
63+
Options options = new Options();
64+
options.addOption(Option.builder("").required(true).hasArg(true).longOpt("operation_id").build());
65+
options.addOption(Option.builder("").required(true).hasArg(true).longOpt("project").build());
66+
67+
CommandLine cl = (new DefaultParser()).parse(options, args);
68+
69+
String modelId = cl.getOptionValue("operation_id", "[Operation ID]");
70+
String project = cl.getOptionValue("project", "[Google Cloud Project ID]");
71+
sampleGetOperation(project, modelId);
72+
73+
}
74+
}
75+
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// This sample is HAND-WRITTEN
18+
19+
package com.google.cloud.examples.automl.v1beta1;
20+
21+
import com.google.cloud.automl.v1beta1.AutoMlClient;
22+
import com.google.cloud.automl.v1beta1.LocationName;
23+
import com.google.longrunning.Operation;
24+
import com.google.longrunning.OperationsClient;
25+
import org.apache.commons.cli.CommandLine;
26+
import org.apache.commons.cli.DefaultParser;
27+
import org.apache.commons.cli.Option;
28+
import org.apache.commons.cli.Options;
29+
30+
public class AutomlListOperations {
31+
// [START automl_list_operations]
32+
33+
/**
34+
* List Long-Running Operations
35+
*
36+
* @param filter Filter operations.
37+
* @param project Required. Your Google Cloud Project ID.
38+
*/
39+
public static void sampleListOperations(String project, String filter) {
40+
try (AutoMlClient client = AutoMlClient.create()) {
41+
42+
OperationsClient operationsClient = client.getOperationsClient();
43+
44+
// project = '[Google Cloud Project ID]'
45+
LocationName parent = LocationName.of(project, "us-central1");
46+
47+
// Iterate over all results
48+
for (Operation responseItem :
49+
operationsClient.listOperations(parent.toString(), filter).iterateAll()) {
50+
System.out.printf("Operation Name: %s", (responseItem.getName()));
51+
System.out.printf("Done: %s", (responseItem.getDone()));
52+
System.out.printf("Response: %s",(responseItem.getResponse()));
53+
System.out.printf("Metadata: %s", (responseItem.getMetadata()));
54+
}
55+
56+
} catch (Exception exception) {
57+
System.err.println("Failed to create the client due to: " + exception);
58+
}
59+
}
60+
// [END automl_list_operations]
61+
62+
public static void main(String[] args) throws Exception {
63+
Options options = new Options();
64+
options.addOption(Option.builder("").required(false).hasArg(true).longOpt("filter").build());
65+
options.addOption(Option.builder("").required(true).hasArg(true).longOpt("project").build());
66+
67+
CommandLine cl = (new DefaultParser()).parse(options, args);
68+
69+
String modelId = cl.getOptionValue("filter", "");
70+
String project = cl.getOptionValue("project", "[Google Cloud Project ID]");
71+
sampleListOperations(project, modelId);
72+
73+
}
74+
}

0 commit comments

Comments
 (0)