|
| 1 | +/* |
| 2 | + * Copyright (c) 2017 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 5 | + * in compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License |
| 10 | + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 11 | + * or implied. See the License for the specific language governing permissions and limitations under |
| 12 | + * the License. |
| 13 | + */ |
| 14 | + |
| 15 | +package com.example; |
| 16 | + |
| 17 | +import org.kohsuke.args4j.Argument; |
| 18 | +import org.kohsuke.args4j.Option; |
| 19 | +import org.kohsuke.args4j.spi.SubCommand; |
| 20 | +import org.kohsuke.args4j.spi.SubCommandHandler; |
| 21 | +import org.kohsuke.args4j.spi.SubCommands; |
| 22 | + |
| 23 | +import java.io.IOException; |
| 24 | + |
| 25 | +/** |
| 26 | + * Defines the different sub-commands and their parameters, for command-line invocation. |
| 27 | + */ |
| 28 | +class SnippetCommands { |
| 29 | + /** |
| 30 | + * An interface for a command-line sub-command. |
| 31 | + */ |
| 32 | + interface Command { |
| 33 | + public void run() throws IOException; |
| 34 | + } |
| 35 | + |
| 36 | + // Most of the commands take some subset of the same arguments, so specify groups of arguments |
| 37 | + // as classes for greater code reuse. |
| 38 | + static class ProjectIdArgs { |
| 39 | + @Option(name = "--project-id", aliases = "-p", required = true, usage = "Your GCP project ID") |
| 40 | + String projectId; |
| 41 | + } |
| 42 | + |
| 43 | + static class KeyRingArgs extends ProjectIdArgs { |
| 44 | + @Argument(metaVar = "ringId", required = true, index = 0, usage = "The ring id") |
| 45 | + String ringId; |
| 46 | + } |
| 47 | + |
| 48 | + static class KeyArgs extends KeyRingArgs { |
| 49 | + @Argument(metaVar = "keyId", required = true, index = 1, usage = "The key id") |
| 50 | + String keyId; |
| 51 | + } |
| 52 | + |
| 53 | + static class KeyVersionArgs extends KeyArgs { |
| 54 | + @Argument(metaVar = "version", required = true, index = 2, usage = "The key version") |
| 55 | + String version; |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | + public static class CreateKeyRingCommand extends KeyRingArgs implements Command { |
| 60 | + public void run() throws IOException { |
| 61 | + Snippets.createKeyRing(projectId, ringId); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + public static class CreateKeyCommand extends KeyArgs implements Command { |
| 66 | + public void run() throws IOException { |
| 67 | + Snippets.createCryptoKey(projectId, ringId, keyId); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public static class ListVersionsCommand extends KeyArgs implements Command { |
| 72 | + public void run() throws IOException { |
| 73 | + Snippets.listCryptoKeyVersions(projectId, ringId, keyId); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + public static class DisableKeyCommand extends KeyVersionArgs implements Command { |
| 78 | + public void run() throws IOException { |
| 79 | + Snippets.disableCryptoKeyVersion(projectId, ringId, keyId, version); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + public static class DestroyCommand extends KeyVersionArgs implements Command { |
| 84 | + public void run() throws IOException { |
| 85 | + Snippets.destroyCryptoKeyVersion(projectId, ringId, keyId, version); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + public static class GetKeyRingPolicyCommand extends KeyRingArgs implements Command { |
| 90 | + public void run() throws IOException { |
| 91 | + Snippets.getKeyRingPolicy(projectId, ringId); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + public static class GetKeyPolicyCommand extends KeyArgs implements Command { |
| 96 | + public void run() throws IOException { |
| 97 | + Snippets.getCryptoKeyPolicy(projectId, ringId, keyId); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + public static class AddMemberToKeyRingPolicy extends KeyRingArgs implements Command { |
| 102 | + @Argument(metaVar = "member", required = true, index = 2, |
| 103 | + usage = "The member to add.\n" |
| 104 | + + "See https://g.co/cloud/kms/docs/reference/rest/v1beta1/Policy#binding " |
| 105 | + + "for valid values.") |
| 106 | + String member; |
| 107 | + @Argument(metaVar = "role", required = true, index = 3, |
| 108 | + usage = "The role for the member.\n" |
| 109 | + + "See https://g.co/cloud/iam/docs/understanding-roles for valid values.") |
| 110 | + String role; |
| 111 | + |
| 112 | + public void run() throws IOException { |
| 113 | + Snippets.addToKeyRingPolicy(projectId, ringId, member, role); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + public static class AddMemberToKeyPolicy extends KeyArgs implements Command { |
| 118 | + @Argument(metaVar = "member", required = true, index = 3, |
| 119 | + usage = "The member to add.\n" |
| 120 | + + "See https://g.co/cloud/kms/docs/reference/rest/v1beta1/Policy#binding " |
| 121 | + + "for valid values.") |
| 122 | + String member; |
| 123 | + @Argument(metaVar = "role", required = true, index = 4, |
| 124 | + usage = "The role for the member.\n" |
| 125 | + + "See https://g.co/cloud/iam/docs/understanding-roles for valid values.") |
| 126 | + String role; |
| 127 | + |
| 128 | + public void run() throws IOException { |
| 129 | + Snippets.addToCryptokeyPolicy(projectId, ringId, keyId, member, role); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + public static class RemoveMemberFromKeyRingPolicy extends KeyRingArgs implements Command { |
| 134 | + @Argument(metaVar = "member", required = true, index = 2, |
| 135 | + usage = "The member to add.\n" |
| 136 | + + "See https://g.co/cloud/kms/docs/reference/rest/v1beta1/Policy#binding " |
| 137 | + + "for valid values.") |
| 138 | + String member; |
| 139 | + @Argument(metaVar = "role", required = true, index = 3, |
| 140 | + usage = "The role for the member.\n" |
| 141 | + + "See https://g.co/cloud/iam/docs/understanding-roles for valid values.") |
| 142 | + String role; |
| 143 | + |
| 144 | + public void run() throws IOException { |
| 145 | + Snippets.removeFromKeyRingPolicy(projectId, ringId, member, role); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + public static class RemoveMemberFromKeyPolicy extends KeyArgs implements Command { |
| 150 | + @Argument(metaVar = "member", required = true, index = 3, |
| 151 | + usage = "The member to add.\n" |
| 152 | + + "See https://g.co/cloud/kms/docs/reference/rest/v1beta1/Policy#binding " |
| 153 | + + "for valid values.") |
| 154 | + String member; |
| 155 | + @Argument(metaVar = "role", required = true, index = 4, |
| 156 | + usage = "The role for the member.\n" |
| 157 | + + "See https://g.co/cloud/iam/docs/understanding-roles for valid values.") |
| 158 | + String role; |
| 159 | + |
| 160 | + public void run() throws IOException { |
| 161 | + Snippets.removeFromCryptokeyPolicy(projectId, ringId, keyId, member, role); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + @Argument(metaVar = "command", required = true, handler = SubCommandHandler.class, |
| 166 | + usage = "The subcommand to run") |
| 167 | + @SubCommands({ |
| 168 | + @SubCommand(name = "createKeyRing", impl = CreateKeyRingCommand.class), |
| 169 | + @SubCommand(name = "createKey", impl = CreateKeyCommand.class), |
| 170 | + @SubCommand(name = "listVersions", impl = ListVersionsCommand.class), |
| 171 | + @SubCommand(name = "disableKey", impl = DisableKeyCommand.class), |
| 172 | + @SubCommand(name = "destroyKey", impl = DestroyCommand.class), |
| 173 | + @SubCommand(name = "getKeyRingPolicy", impl = GetKeyRingPolicyCommand.class), |
| 174 | + @SubCommand(name = "getKeyPolicy", impl = GetKeyPolicyCommand.class), |
| 175 | + @SubCommand(name = "addMemberToKeyRing", impl = AddMemberToKeyRingPolicy.class), |
| 176 | + @SubCommand(name = "addMemberToKey", impl = AddMemberToKeyPolicy.class), |
| 177 | + @SubCommand(name = "removeMemberFromKeyRing", impl = RemoveMemberFromKeyRingPolicy.class), |
| 178 | + @SubCommand(name = "removeMemberFromKey", impl = RemoveMemberFromKeyPolicy.class) |
| 179 | + }) |
| 180 | + Command command; |
| 181 | +} |
0 commit comments