AT&T 2.0.19 Code drop, stage 3
[aaf/authz.git] / auth / auth-cmd / src / main / java / org / onap / aaf / auth / cmd / perm / Rename.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.perm;
23
24
25 import org.onap.aaf.auth.cmd.AAFcli;
26 import org.onap.aaf.auth.cmd.Cmd;
27 import org.onap.aaf.auth.cmd.Param;
28 import org.onap.aaf.auth.rserv.HttpMethods;
29 import org.onap.aaf.cadi.CadiException;
30 import org.onap.aaf.cadi.LocatorException;
31 import org.onap.aaf.cadi.client.Future;
32 import org.onap.aaf.cadi.client.Rcli;
33 import org.onap.aaf.cadi.client.Retryable;
34 import org.onap.aaf.misc.env.APIException;
35
36 import aaf.v2_0.PermRequest;
37
38 public class Rename extends Cmd {
39         public Rename(Perm parent) {
40                 super(parent,"rename", 
41                                 new Param("type",true), 
42                                 new Param("instance",true),
43                                 new Param("action", true),
44                                 new Param("new type",true), 
45                                 new Param("new instance",true),
46                                 new Param("new action", true)
47                                 );
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 origType = args[idx++];
57                                 String origInstance = args[idx++];
58                                 String origAction = args[idx++];
59                                 
60                                 //Create new permission
61                                 PermRequest pr = new PermRequest();
62                                 pr.setType(args[idx++]);
63                                 pr.setInstance(args[idx++]);
64                                 pr.setAction(args[idx++]);
65                                 
66                                 // Set Start/End commands
67                                 setStartEnd(pr);
68                                 Future<PermRequest> fp = client.update(
69                                                 "/authz/perm/"+origType+"/"+origInstance+"/"+origAction,
70                                                 getDF(PermRequest.class),
71                                                 pr
72                                                 );
73                                 int rv;
74                                 if(fp.get(AAFcli.timeout())) {
75                                         rv = fp.code();
76                                         pw().println("Updated Permission");
77                                 } else {
78                                         rv = fp.code();
79                                         if(rv==202) {
80                                                 pw().println("Permission Update Accepted, but requires Approvals before actualizing");
81                                         } else {
82                                                 error(fp);
83                                         }
84                                 }
85                                 return rv;
86                         }
87                 });
88                 
89         }
90         
91         @Override
92         public void detailedHelp(int indent, StringBuilder sb) {
93                 detailLine(sb,indent,"Rename a Permission from:");
94                 detailLine(sb,indent+2,"<type> <instance> <action>");
95                 detailLine(sb,indent,"to:");
96                 detailLine(sb,indent+2,"<new type> <new instance> <new action>");
97                 sb.append('\n');
98                 detailLine(sb,indent,"Namespace must be the same in <type> and <new type>");
99                 detailLine(sb,indent+4,"see Create for definitions of type,instance and action");
100                 api(sb,indent,HttpMethods.PUT,"authz/perm/<type>/<instance>/<action>",PermRequest.class,true);
101         }
102 }