Post Init Service Starter
[aaf/authz.git] / auth / auth-cmd / src / main / java / org / onap / aaf / auth / cmd / role / User.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===========================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END====================================================
19  *
20  */
21
22 package org.onap.aaf.auth.cmd.role;
23
24 import org.onap.aaf.auth.cmd.AAFcli;
25 import org.onap.aaf.auth.cmd.Cmd;
26 import org.onap.aaf.auth.cmd.Param;
27 import org.onap.aaf.auth.rserv.HttpMethods;
28 import org.onap.aaf.cadi.CadiException;
29 import org.onap.aaf.cadi.LocatorException;
30 import org.onap.aaf.cadi.client.Future;
31 import org.onap.aaf.cadi.client.Rcli;
32 import org.onap.aaf.cadi.client.Retryable;
33 import org.onap.aaf.misc.env.APIException;
34
35 import aaf.v2_0.UserRoleRequest;
36
37 /**
38  * p
39  * @author Jonathan
40  *
41  */
42 public class User extends Cmd {
43     private final static String[] options = {"add","del","extend"};
44     public User(Role parent) {
45         super(parent,"user", 
46                 new Param(optionsToString(options),true),
47                 new Param("role",true),
48                 new Param("id[,id]*",false)); 
49     }
50
51     @Override
52     public int _exec(final int index, final String ... args) throws CadiException, APIException, LocatorException {
53         return same(new Retryable<Integer>() {
54             @Override
55             public Integer code(Rcli<?> client) throws CadiException, APIException {
56                 int idx = index;
57                 String action = args[idx++];
58                 int option = whichOption(options, action);
59                 UserRoleRequest urr = new UserRoleRequest();
60                 urr.setRole(args[idx++]);
61                 // Set Start/End commands
62                 setStartEnd(urr);
63                 
64                 Future<?> fp = null;
65                 
66                 String[] ids = args[idx++].split(",");
67                 String verb=null,participle=null;
68                 // You can request to be added or removed from role.
69                 setQueryParamsOn(client);
70
71                 for (String id: ids) {
72                     id=fullID(id);
73                     urr.setUser(id);
74                     switch(option) {
75                         case 0:
76                             fp = client.create(
77                                     "/authz/userRole", 
78                                     getDF(UserRoleRequest.class), 
79                                     urr);
80                             verb = "Added";
81                             participle = "] to Role [" ;
82                             break;
83                         case 1:
84                             fp = client.delete(
85                                     "/authz/userRole/"+urr.getUser()+'/'+urr.getRole(), 
86                                     Void.class);
87                             verb = "Removed";
88                             participle = "] from Role [" ;
89                             break;
90                         case 2:
91                             fp = client.update("/authz/userRole/extend/" + urr.getUser() + '/' + urr.getRole());
92                             verb = "Extended";
93                             participle = "] in Role [" ;
94                             break;
95
96                         default: // actually, should never get here...
97                             throw new CadiException("Invalid action [" + action + ']');
98                     }
99                     if (fp.get(AAFcli.timeout())) {
100                         pw().print(verb);
101                         pw().print(" User [");
102                         pw().print(urr.getUser());
103                         pw().print(participle);
104                         pw().print(urr.getRole());
105                         pw().println(']');
106                     } else {
107                         switch(fp.code()) {
108                             case 202:
109                                 pw().print("User Role ");
110                                 pw().print(action);
111                                 pw().println(" is Accepted, but requires Approvals before actualizing");
112                                 break;
113                             case 404:
114                                 if (option==3) {
115                                     pw().println("Failed with code 404: UserRole is not found, or you do not have permission to view");
116                                     break;
117                                 }
118                             default:
119                                 error(fp);
120                         }
121                     }
122                 }
123                 return fp==null?0:fp.code();
124             }
125         });
126     }
127     
128     @Override
129     public void detailedHelp(int indent, StringBuilder sb) {
130         detailLine(sb,indent,"Add OR Delete a User to/from a Role OR extend Expiration");
131         detailLine(sb,indent+2,"role  - Name of Role to create");
132         detailLine(sb,indent+2,"id(s) - ID or IDs to add to the Role");
133         sb.append('\n');
134         api(sb,indent,HttpMethods.POST,"authz/userRole",UserRoleRequest.class,true);
135         api(sb,indent,HttpMethods.DELETE,"authz/userRole/<user>/<role>",Void.class,false);
136         api(sb,indent,HttpMethods.PUT,"authz/userRole/extend/<user>/<role>",Void.class,false);
137     }
138
139 }