07a19d36c0cf9e7bb0fa71fcdca7d038de5c712e
[aaf/authz.git] / auth / auth-cmd / src / main / java / org / onap / aaf / auth / cmd / user / ListForCreds.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  *
7  * Modification Copyright (c) 2019 IBM
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  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END====================================================
21  *
22  */
23
24 package org.onap.aaf.auth.cmd.user;
25
26 import java.util.Collections;
27 import java.util.Comparator;
28
29 import org.onap.aaf.auth.cmd.AAFcli;
30 import org.onap.aaf.auth.cmd.Cmd;
31 import org.onap.aaf.auth.cmd.Param;
32 import org.onap.aaf.auth.rserv.HttpMethods;
33 import org.onap.aaf.cadi.CadiException;
34 import org.onap.aaf.cadi.LocatorException;
35 import org.onap.aaf.cadi.client.Future;
36 import org.onap.aaf.cadi.client.Rcli;
37 import org.onap.aaf.cadi.client.Retryable;
38 import org.onap.aaf.misc.env.APIException;
39
40 import aaf.v2_0.Users;
41 import aaf.v2_0.Users.User;
42
43 /**
44  * List for Creds
45  * @author Jonathan
46  *
47  */
48 public class ListForCreds extends Cmd {
49     private static final String[] options = {"ns","id"};
50
51     private static final String HEADER = "List creds by Namespace or ID ";
52     public ListForCreds(List parent) {
53         super(parent,"cred",
54                 new Param(optionsToString(options),true),
55                 new Param("value",true));
56     }
57
58     @Override
59     public int _exec(int idxParam, final String ... args) throws CadiException, APIException, LocatorException {
60             int idx = idxParam;
61         final int option = whichOption(options, args[idx++]);
62         final String which = options[option];
63         final String value = args[idx++];
64         return same(new Retryable<Integer>() {
65             @Override
66             public Integer code(Rcli<?> client) throws CadiException, APIException {
67                 Future<Users> fp = client.read(
68                         "/authn/creds/"+which+'/'+value,
69                         getDF(Users.class)
70                         );
71                 if (fp.get(AAFcli.timeout())) {
72                     if (aafcli.isTest())
73                         Collections.sort(fp.value.getUser(), new Comparator<User>() {
74                             @Override
75                             public int compare(User u1, User u2) {
76                                 return u1.getId().compareTo(u2.getId());
77                             }
78                         });
79                     ((org.onap.aaf.auth.cmd.user.List)parent).report(fp.value,option==1,HEADER+which,value);
80                     if (fp.code()==404) {
81                         return 200;
82                     }
83                 } else {
84                     error(fp);
85                 }
86                 return fp.code();
87             }
88         });
89     }
90
91     @Override
92     public void detailedHelp(int indentParam, StringBuilder sb) {
93             int indent = indentParam;
94         detailLine(sb,indent,HEADER);
95         indent+=2;
96         detailLine(sb,indent,"This report lists the users associated to either Namespaces or IDs.");
97         detailLine(sb,indent,"ns (literal) - which Namespace");
98         detailLine(sb,indent,"id (literal) - identity");
99         indent-=2;
100         api(sb,indent,HttpMethods.GET,"authn/creds/ns/<ns>",Users.class,true);
101         api(sb,indent,HttpMethods.GET,"authn/creds/id/<identity>",Users.class,true);
102     }
103
104 }