f5cb4499fbd64c0aca7b912722c444b85b28d386
[aaf/authz.git] / auth / auth-cmd / src / main / java / org / onap / aaf / auth / cmd / user / Delg.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  *
7  * Modifications Copyright (C) 2018 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.text.ParseException;
27 import java.util.Date;
28
29 import org.onap.aaf.auth.cmd.AAFcli;
30 import org.onap.aaf.auth.cmd.BaseCmd;
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 import org.onap.aaf.misc.env.util.Chrono;
40 import org.onap.aaf.misc.rosetta.env.RosettaDF;
41
42 import aaf.v2_0.DelgRequest;
43
44 public class Delg extends BaseCmd<User> {
45     static final String AUTHZ_DELG = "/authz/delegate";
46     private static final String[] options = {"add","upd","del"};
47
48     public Delg(User user){
49         super(user,"delegate",
50                 new Param(optionsToString(options),true),
51                 new Param("from",true),
52                 new Param("to REQ A&U",false),
53                 new Param("until (YYYY-MM-DD) REQ A", false)
54         );
55     }
56
57     @Override
58     public int _exec(final int index, final String ... args) throws CadiException, APIException, LocatorException {
59         return same(new Retryable<Integer>() {
60             @Override
61             public Integer code(Rcli<?> client) throws CadiException, APIException {
62                 int idx = index;
63                 DelgRequest dr = new DelgRequest();
64                 setStartEnd(dr);
65
66                 int option= whichOption(options, args[idx++]);
67                 String user = fullID(args[idx++]);
68                 dr.setUser(user);
69                 if (option<2) {
70                     String delegate = fullID(args[idx++]);
71                     dr.setDelegate(delegate);
72                     if (option<2 && args.length>idx) {
73                         Date date;
74                         try {
75                             date = Chrono.dateOnlyFmt.parse(args[idx++]);
76                         } catch (ParseException e) {
77                             throw new CadiException(e);
78                         }
79                         dr.setEnd(Chrono.timeStamp(date));
80                     }
81                 }
82
83                 Future<DelgRequest> fp;
84                 RosettaDF<DelgRequest> df = getDF(DelgRequest.class);
85                 String verb;
86                 setQueryParamsOn(client);
87
88                 switch(option) {
89                     case 0:
90                         fp = client.create(AUTHZ_DELG, df, dr);
91                         verb = "Added";
92                         break;
93                     case 1:
94                         fp = client.update(AUTHZ_DELG, df, dr);
95                         verb = "Updated";
96                         break;
97                     case 2:
98                         fp = client.delete(AUTHZ_DELG, df, dr);
99                         verb = "Deleted";
100                         break;
101                     default:
102                         throw new CadiException("Bad Argument");
103                 };
104
105                 if (fp.get(AAFcli.timeout())) {
106                     pw().append("Delegate ");
107                     pw().println(verb);
108                 } else {
109                     error(fp);
110                 }
111                 return fp.code();
112             }
113         });
114     }
115
116     @Override
117     public void detailedHelp(int indentParam, StringBuilder sb) {
118             int indent = indentParam;
119         detailLine(sb,indent,"Add, Update or Delete Delegate");
120         indent+=2;
121         detailLine(sb,indent,"A Delegate is a person who will temporarily cover the Approval and");
122         detailLine(sb,indent,"Ownership questions on behalf of the person Responsible.");
123         sb.append('\n');
124         detailLine(sb,indent,"fromID - the person who is the Responsible person of record");
125         detailLine(sb,indent,"toID   - the person who will be delegated (required for Add/Update)");
126         detailLine(sb,indent,"until  - the end date for this delegation");
127         indent-=2;
128         api(sb,indent,HttpMethods.POST,AUTHZ_DELG,DelgRequest.class,true);
129         api(sb,indent,HttpMethods.DELETE,AUTHZ_DELG,DelgRequest.class,false);
130         api(sb,indent,HttpMethods.PUT,AUTHZ_DELG,DelgRequest.class,false);
131     }
132
133 }