Remove Tabs, per Jococo
[aaf/authz.git] / auth / auth-cmd / src / main / java / org / onap / aaf / auth / cmd / user / Role.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.user;
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  * 
40  * @author Jonathan
41  *
42  */
43 public class Role extends Cmd {
44     private static final String[] options = {"add", "del", "extend"};
45     public Role(User parent) {
46         super(parent, "role", new Param(optionsToString(options), true), new Param("user", true),
47                 new Param("role[,role]*", false));
48     }
49
50     @Override
51     public int _exec(final int index, final String ... args) throws CadiException, APIException, LocatorException {
52         return same(new Retryable<Integer>() {
53             @Override
54             public Integer code(Rcli<?> client) throws CadiException, APIException {
55                 int idx = index;
56                 String key = args[idx++];
57                 int option = whichOption(options, key);
58                 final String user = fullID(args[idx++]);
59
60                 UserRoleRequest urr = new UserRoleRequest();
61                 urr.setUser(user);
62                 // Set Start/End commands
63                 setStartEnd(urr);
64
65                 Future<?> fp = null;
66
67                 if (args.length < 5) {
68                     throw new CadiException(build(new StringBuilder("Too few args: "), null).toString());                        
69                 }
70                 String[] roles = args[idx++].split(",");
71                 for (String role : roles) {
72                     String verb = null,participle=null;
73                     urr.setRole(role);
74                     // You can request to be added or removed from role.
75                     setQueryParamsOn(client);
76                     switch(option) {
77                       case 0:
78                         fp = client.create("/authz/userRole", getDF(UserRoleRequest.class), urr);
79                         verb = "Added";
80                         participle = "] to User [" ;
81                         break;
82                       case 1:
83                         fp = client.delete("/authz/userRole/" + urr.getUser() + '/' + urr.getRole(), Void.class);
84                         verb = "Removed";
85                         participle = "] from User [" ;
86                         break;
87                       case 2:
88                         fp = client.update("/authz/userRole/extend/" + urr.getUser() + '/' + urr.getRole());
89                         verb = "Extended";
90                         participle = "] to User [" ;
91                         break;
92                       default:
93                         throw new CadiException("Invalid action [" + key + ']');
94                     }
95                     if (fp.get(AAFcli.timeout())) {
96                         pw().print(verb);
97                         pw().print(" Role [");
98                         pw().print(urr.getRole());
99                         pw().print(participle);
100                         pw().print(urr.getUser());
101                         pw().println(']');
102                     } else {
103                         switch(fp.code()) {
104                         case 202:
105                             pw().print("UserRole ");
106                             pw().print(option == 0 ? "Creation" : option==1?"Deletion":"Extension");
107                             pw().println(" Accepted, but requires Approvals before actualizing");
108                             break;
109                         case 404:
110                             if (option==3) {
111                                 pw().println("Failed with code 404: UserRole is not found, or you do not have permission to view");
112                                 break;
113                             }
114                         default:
115                             error(fp);
116                         }
117                     }
118                 }
119                 return fp == null ? 0 : fp.code();
120             }
121         });
122     }
123
124     @Override
125     public void detailedHelp(int indent, StringBuilder sb) {
126         detailLine(sb, indent, "Add or Delete a User to/from a Role OR extend Expiration");
127         detailLine(sb, indent + 2, "user    - ID of User");
128         detailLine(sb, indent + 2, "role(s) - Role or Roles to which to add the User");
129         sb.append('\n');
130         api(sb, indent, HttpMethods.POST, "authz/userRole", UserRoleRequest.class, true);
131         api(sb, indent, HttpMethods.DELETE, "authz/userRole/<user>/<role>", Void.class, false);
132         api(sb,indent,HttpMethods.PUT,"authz/userRole/extend/<user>/<role>",Void.class,false);
133
134     }
135
136 }