AT&T 2.0.19 Code drop, stage 3
[aaf/authz.git] / auth / auth-cmd / src / main / java / org / onap / aaf / auth / cmd / role / CreateDelete.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.RoleRequest;
36
37 /**
38  * 
39  * @author Jonathan
40  *
41  */
42 public class CreateDelete extends Cmd {
43         private static final String ROLE_PATH = "/authz/role";
44         private final static String[] options = {"create","delete"};
45         public CreateDelete(Role parent) {
46                 super(parent,null, 
47                                 new Param(optionsToString(options),true),
48                                 new Param("name",true)); 
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                 
60                                 RoleRequest rr = new RoleRequest();
61                                 rr.setName(args[idx++]);
62                 
63                                 // Set Start/End commands
64                                 setStartEnd(rr);
65                                 
66                                 Future<RoleRequest> fp = null;
67                                 String verb = null;
68                                 int rv;
69                                 switch(option) {
70                                         case 0:
71                                                 fp = client.create(
72                                                         ROLE_PATH,
73                                                         getDF(RoleRequest.class),
74                                                         rr
75                                                         );
76                                                 verb = "Create";
77                                                 break;
78                                         case 1:
79                                                 // Send "Force" if set
80                                                 setQueryParamsOn(client);
81                                                 fp = client.delete(
82                                                                 ROLE_PATH, // +args[idx++], 
83                                                                 getDF(RoleRequest.class),
84                                                                 rr
85                                                                 );
86                                                 verb = "Delete";
87                                                 break;
88                                         default: // note, if not an option, whichOption throws Exception
89                                                 break;
90                                                 
91                                 }
92                                 boolean rolesSupplied = (args.length>idx);
93                                 if(fp.get(AAFcli.timeout())) {
94                                         rv=fp.code();
95                                         pw().print(verb);
96                                         pw().println("d Role");
97                                         if(rolesSupplied) {
98                                                 for(;args.length>idx;++idx ) {
99                                                         try {
100                                                                 if(201!=(rv=((Role)parent)._exec(0,new String[] {"user","add",rr.getName(),args[idx]}))) {
101                                                                         rv = 206 /*HttpStatus.PARTIAL_CONTENT_206*/;
102                                                                 }
103                                                         } catch (LocatorException e) {
104                                                                 throw new CadiException(e);
105                                                         }
106                                                 }
107                                         }
108                                 } else {
109                                         if((rv=fp.code())==202) {
110                                                 pw().print("Role ");
111                                                 pw().print(verb);
112                                                 pw().println(" Accepted, but requires Approvals before actualizing");
113                                         } else {
114                                                 error(fp);
115                                         }
116                                 }
117                                 return rv;
118                         }
119                 });
120         }
121
122         @Override
123         public void detailedHelp(int indent, StringBuilder sb) {
124                 detailLine(sb,indent,"Create OR Delete a Role");
125                 detailLine(sb,indent+2,"name - Name of Role to create");
126                 api(sb,indent,HttpMethods.POST,"authz/role",RoleRequest.class,true);
127                 api(sb,indent,HttpMethods.DELETE,"authz/role",RoleRequest.class,false);
128         }
129
130 }