aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/dmaap/mr/tools/ApiKeyCommand.java
blob: 6a2bf37d5fe9c5a0e415b7f4dec5c66eb3a904bb (plain)
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
/*******************************************************************************
 *  ============LICENSE_START=======================================================
 *  org.onap.dmaap
 *  ================================================================================
 *  Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 *  ================================================================================
 *  Modifications Copyright © 2021 Orange.
 *  ================================================================================
 *  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.
 *  ============LICENSE_END=========================================================
 *
 *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
 *
 *******************************************************************************/

package org.onap.dmaap.mr.tools;

import com.att.nsa.apiClient.credentials.ApiCredential;
import com.att.nsa.apiClient.http.HttpException;
import com.att.nsa.apiClient.http.HttpObjectNotFoundException;
import com.att.nsa.cmdtool.Command;
import com.att.nsa.cmdtool.CommandNotReadyException;
import java.io.IOException;
import java.io.PrintStream;
import org.onap.dmaap.mr.client.MRClient.MRApiException;
import org.onap.dmaap.mr.client.MRClientFactory;
import org.onap.dmaap.mr.client.MRIdentityManager;
import org.onap.dmaap.mr.client.MRIdentityManager.ApiKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ApiKeyCommand implements Command<MRCommandContext> {
    final Logger logger = LoggerFactory.getLogger(ApiKeyCommand.class);

    @Override
    public String[] getMatches() {
        return new String[] {
            "key (create|update) (\\S*) (\\S*)",
            "key (list) (\\S*)",
            "key (revoke)",
        };
    }

    @Override
    public void checkReady(MRCommandContext context) throws CommandNotReadyException {
        if (!context.checkClusterReady()) {
            throw new CommandNotReadyException("Use 'cluster' to specify a cluster to use.");
        }
    }

    @Override
    public void execute(String[] parts, MRCommandContext context, PrintStream out) throws CommandNotReadyException {
        final MRIdentityManager tm = MRClientFactory.createIdentityManager(context.getCluster(), context.getApiKey(), context.getApiPwd());
        context.applyTracer(tm);

        try {
            switch (parts[0]) {
                case "list":
                    final ApiKey key = tm.getApiKey(parts[1]);
                    if (key != null) {
                        out.println("email: " + key.getEmail());
                        out.println("description: " + key.getDescription());
                    } else {
                        out.println("No key returned");
                    }
                    break;
                case "create":
                    final ApiCredential ac = tm.createApiKey(parts[1], parts[2]);
                    if (ac != null) {
                        out.println("   key: " + ac.getApiKey());
                        out.println("secret: " + ac.getApiSecret());
                    } else {
                        out.println("No credential returned?");
                    }
                    break;
                case "update":
                    tm.updateCurrentApiKey(parts[1], parts[2]);
                    out.println("Updated");
                    break;
                case "revoke":
                    tm.deleteCurrentApiKey();
                    out.println("Updated");
                    break;
                default:
                    throw new CommandNotReadyException("The command " + parts[0] + " is not available");
            }
        } catch (HttpObjectNotFoundException e) {
            out.println("Object not found: " + e.getMessage());
            logger.error("HttpObjectNotFoundException: ", e);
        } catch (HttpException e) {
            out.println("HTTP exception: " + e.getMessage());
            logger.error("HttpException: ", e);
        } catch (MRApiException e) {
            out.println("API exception: " + e.getMessage());
            logger.error("MRApiException: ", e);
        } catch (IOException e) {
            out.println("IO exception: " + e.getMessage());
            logger.error("IOException: ", e);
        } finally {
            tm.close();
        }
    }

    @Override
    public void displayHelp(PrintStream out) {
        out.println("key create <email> <description>");
        out.println("key update <email> <description>");
        out.println("key list <key>");
        out.println("key revoke");
    }
}