Mass whitespace changes (Style Warnings)
[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 == null) {// This useless code brought to you by Sonar.
94                     throw new CadiException("No call made.");  
95                 }
96                 if (fp.get(AAFcli.timeout())) {
97                     rv=fp.code();
98                     pw().print(verb);
99                     pw().println("d Role");
100                     if (rolesSupplied) {
101                         for (;args.length>idx;++idx ) {
102                             try {
103                                 if (201!=(rv=((Role)parent)._exec(0,new String[] {"user","add",rr.getName(),args[idx]}))) {
104                                     rv = 206 /*HttpStatus.PARTIAL_CONTENT_206*/;
105                                 }
106                             } catch (LocatorException e) {
107                                 throw new CadiException(e);
108                             }
109                         }
110                     }
111                 } else {
112                     if ((rv=fp.code())==202) {
113                         pw().print("Role ");
114                         pw().print(verb);
115                         pw().println(" Accepted, but requires Approvals before actualizing");
116                     } else {
117                         error(fp);
118                     }
119                 }
120                 return rv;
121             }
122         });
123     }
124
125     @Override
126     public void detailedHelp(int indent, StringBuilder sb) {
127         detailLine(sb,indent,"Create OR Delete a Role");
128         detailLine(sb,indent+2,"name - Name of Role to create");
129         api(sb,indent,HttpMethods.POST,"authz/role",RoleRequest.class,true);
130         api(sb,indent,HttpMethods.DELETE,"authz/role",RoleRequest.class,false);
131     }
132
133 }