forked from GoogleCloudPlatform/java-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnippetCommands.java
More file actions
205 lines (177 loc) · 7.85 KB
/
Copy pathSnippetCommands.java
File metadata and controls
205 lines (177 loc) · 7.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* Copyright (c) 2017 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.
*/
package com.example;
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.Option;
import org.kohsuke.args4j.spi.SubCommand;
import org.kohsuke.args4j.spi.SubCommandHandler;
import org.kohsuke.args4j.spi.SubCommands;
import java.io.IOException;
/**
* Defines the different sub-commands and their parameters, for command-line invocation.
*/
class SnippetCommands {
/**
* An interface for a command-line sub-command.
*/
interface Command {
public void run() throws IOException;
}
// Most of the commands take some subset of the same arguments, so specify groups of arguments
// as classes for greater code reuse.
static class ProjectIdArgs {
@Option(name = "--project-id", aliases = "-p", required = true, usage = "Your GCP project ID")
String projectId;
}
static class KeyRingArgs extends ProjectIdArgs {
@Argument(metaVar = "ringId", required = true, index = 0, usage = "The ring id")
String ringId;
}
static class KeyArgs extends KeyRingArgs {
@Argument(metaVar = "keyId", required = true, index = 1, usage = "The key id")
String keyId;
}
static class KeyVersionArgs extends KeyArgs {
@Argument(metaVar = "version", required = true, index = 2, usage = "The key version")
String version;
}
public static class CreateKeyRingCommand extends KeyRingArgs implements Command {
public void run() throws IOException {
Snippets.createKeyRing(projectId, ringId);
}
}
public static class CreateCryptoKeyCommand extends KeyArgs implements Command {
public void run() throws IOException {
Snippets.createCryptoKey(projectId, ringId, keyId);
}
}
public static class CreateCryptoKeyVersionCommand extends KeyArgs implements Command {
public void run() throws IOException {
Snippets.createCryptoKeyVersion(projectId, ringId, keyId);
}
}
public static class ListKeyRingsCommand extends ProjectIdArgs implements Command {
public void run() throws IOException {
Snippets.listKeyRings(projectId);
}
}
public static class ListCryptoKeysCommand extends KeyRingArgs implements Command {
public void run() throws IOException {
Snippets.listCryptoKeys(projectId, ringId);
}
}
public static class ListCryptoKeyVersionsCommand extends KeyArgs implements Command {
public void run() throws IOException {
Snippets.listCryptoKeyVersions(projectId, ringId, keyId);
}
}
public static class DisableCryptoKeyVersionCommand extends KeyVersionArgs implements Command {
public void run() throws IOException {
Snippets.disableCryptoKeyVersion(projectId, ringId, keyId, version);
}
}
public static class DestroyCryptoKeyVersionCommand extends KeyVersionArgs implements Command {
public void run() throws IOException {
Snippets.destroyCryptoKeyVersion(projectId, ringId, keyId, version);
}
}
public static class GetKeyRingPolicyCommand extends KeyRingArgs implements Command {
public void run() throws IOException {
Snippets.getKeyRingPolicy(projectId, ringId);
}
}
public static class GetCryptoKeyPolicyCommand extends KeyArgs implements Command {
public void run() throws IOException {
Snippets.getCryptoKeyPolicy(projectId, ringId, keyId);
}
}
public static class AddMemberToKeyRingPolicyCommand extends KeyRingArgs implements Command {
@Argument(metaVar = "member", required = true, index = 1,
usage = "The member to add.\n"
+ "See https://g.co/cloud/kms/docs/reference/rest/v1/Policy#binding "
+ "for valid values.")
String member;
@Argument(metaVar = "role", required = true, index = 2,
usage = "The role for the member.\n"
+ "See https://g.co/cloud/iam/docs/understanding-roles for valid values.")
String role;
public void run() throws IOException {
Snippets.addMemberToKeyRingPolicy(projectId, ringId, member, role);
}
}
public static class AddMemberToCryptoKeyPolicyCommand extends KeyArgs implements Command {
@Argument(metaVar = "member", required = true, index = 2,
usage = "The member to add.\n"
+ "See https://g.co/cloud/kms/docs/reference/rest/v1/Policy#binding "
+ "for valid values.")
String member;
@Argument(metaVar = "role", required = true, index = 3,
usage = "The role for the member.\n"
+ "See https://g.co/cloud/iam/docs/understanding-roles for valid values.")
String role;
public void run() throws IOException {
Snippets.addMemberToCryptoKeyPolicy(projectId, ringId, keyId, member, role);
}
}
public static class RemoveMemberFromKeyRingPolicyCommand extends KeyRingArgs implements Command {
@Argument(metaVar = "member", required = true, index = 1,
usage = "The member to add.\n"
+ "See https://g.co/cloud/kms/docs/reference/rest/v1/Policy#binding "
+ "for valid values.")
String member;
@Argument(metaVar = "role", required = true, index = 2,
usage = "The role for the member.\n"
+ "See https://g.co/cloud/iam/docs/understanding-roles for valid values.")
String role;
public void run() throws IOException {
Snippets.removeMemberFromKeyRingPolicy(projectId, ringId, member, role);
}
}
public static class RemoveMemberFromCryptoKeyPolicyCommand extends KeyArgs implements Command {
@Argument(metaVar = "member", required = true, index = 2,
usage = "The member to add.\n"
+ "See https://g.co/cloud/kms/docs/reference/rest/v1/Policy#binding "
+ "for valid values.")
String member;
@Argument(metaVar = "role", required = true, index = 3,
usage = "The role for the member.\n"
+ "See https://g.co/cloud/iam/docs/understanding-roles for valid values.")
String role;
public void run() throws IOException {
Snippets.removeMemberFromCryptoKeyPolicy(projectId, ringId, keyId, member, role);
}
}
@Argument(metaVar = "command", required = true, handler = SubCommandHandler.class,
usage = "The subcommand to run")
@SubCommands({
@SubCommand(name = "createKeyRing", impl = CreateKeyRingCommand.class),
@SubCommand(name = "createCryptoKey", impl = CreateCryptoKeyCommand.class),
@SubCommand(name = "createCryptoKeyVersion", impl = CreateCryptoKeyVersionCommand.class),
@SubCommand(name = "listKeyRings", impl = ListKeyRingsCommand.class),
@SubCommand(name = "listCryptoKeys", impl = ListCryptoKeysCommand.class),
@SubCommand(name = "listCryptoKeyVersions", impl = ListCryptoKeyVersionsCommand.class),
@SubCommand(name = "disableCryptoKeyVersion", impl = DisableCryptoKeyVersionCommand.class),
@SubCommand(name = "destroyCryptoKeyVersion", impl = DestroyCryptoKeyVersionCommand.class),
@SubCommand(name = "getKeyRingPolicy", impl = GetKeyRingPolicyCommand.class),
@SubCommand(name = "getCryptoKeyPolicy", impl = GetCryptoKeyPolicyCommand.class),
@SubCommand(name = "addMemberToKeyRingPolicy", impl = AddMemberToKeyRingPolicyCommand.class),
@SubCommand(name = "addMemberToCryptoKeyPolicy",
impl = AddMemberToCryptoKeyPolicyCommand.class),
@SubCommand(name = "removeMemberFromKeyRingPolicy",
impl = RemoveMemberFromKeyRingPolicyCommand.class),
@SubCommand(name = "removeMemberFromCryptoKeyPolicy",
impl = RemoveMemberFromCryptoKeyPolicyCommand.class)
})
Command command;
}