1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright © 2021 Orange.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 *******************************************************************************/
25 package org.onap.dmaap.mr.tools;
27 import com.att.nsa.apiClient.credentials.ApiCredential;
28 import com.att.nsa.apiClient.http.HttpException;
29 import com.att.nsa.apiClient.http.HttpObjectNotFoundException;
30 import com.att.nsa.cmdtool.Command;
31 import com.att.nsa.cmdtool.CommandNotReadyException;
32 import java.io.IOException;
33 import java.io.PrintStream;
34 import org.onap.dmaap.mr.client.MRClient.MRApiException;
35 import org.onap.dmaap.mr.client.MRClientFactory;
36 import org.onap.dmaap.mr.client.MRIdentityManager;
37 import org.onap.dmaap.mr.client.MRIdentityManager.ApiKey;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 public class ApiKeyCommand implements Command<MRCommandContext> {
42 final Logger logger = LoggerFactory.getLogger(ApiKeyCommand.class);
45 public String[] getMatches() {
47 "key (create|update) (\\S*) (\\S*)",
54 public void checkReady(MRCommandContext context) throws CommandNotReadyException {
55 if (!context.checkClusterReady()) {
56 throw new CommandNotReadyException("Use 'cluster' to specify a cluster to use.");
61 public void execute(String[] parts, MRCommandContext context, PrintStream out) throws CommandNotReadyException {
62 final MRIdentityManager tm = MRClientFactory.createIdentityManager(context.getCluster(), context.getApiKey(), context.getApiPwd());
63 context.applyTracer(tm);
68 final ApiKey key = tm.getApiKey(parts[1]);
70 out.println("email: " + key.getEmail());
71 out.println("description: " + key.getDescription());
73 out.println("No key returned");
77 final ApiCredential ac = tm.createApiKey(parts[1], parts[2]);
79 out.println(" key: " + ac.getApiKey());
80 out.println("secret: " + ac.getApiSecret());
82 out.println("No credential returned?");
86 tm.updateCurrentApiKey(parts[1], parts[2]);
87 out.println("Updated");
90 tm.deleteCurrentApiKey();
91 out.println("Updated");
94 throw new CommandNotReadyException("The command " + parts[0] + " is not available");
96 } catch (HttpObjectNotFoundException e) {
97 out.println("Object not found: " + e.getMessage());
98 logger.error("HttpObjectNotFoundException: ", e);
99 } catch (HttpException e) {
100 out.println("HTTP exception: " + e.getMessage());
101 logger.error("HttpException: ", e);
102 } catch (MRApiException e) {
103 out.println("API exception: " + e.getMessage());
104 logger.error("MRApiException: ", e);
105 } catch (IOException e) {
106 out.println("IO exception: " + e.getMessage());
107 logger.error("IOException: ", e);
114 public void displayHelp(PrintStream out) {
115 out.println("key create <email> <description>");
116 out.println("key update <email> <description>");
117 out.println("key list <key>");
118 out.println("key revoke");