e44185ec65311f27daee269c926c23c49a459911
[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  * 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.role;
25
26 import org.onap.aaf.auth.cmd.AAFcli;
27 import org.onap.aaf.auth.cmd.Cmd;
28 import org.onap.aaf.auth.cmd.Param;
29 import org.onap.aaf.auth.rserv.HttpMethods;
30 import org.onap.aaf.cadi.CadiException;
31 import org.onap.aaf.cadi.LocatorException;
32 import org.onap.aaf.cadi.client.Future;
33 import org.onap.aaf.cadi.client.Rcli;
34 import org.onap.aaf.cadi.client.Retryable;
35 import org.onap.aaf.misc.env.APIException;
36
37 import aaf.v2_0.RoleRequest;
38
39 /**
40  * 
41  * @author Jonathan
42  *
43  */
44 public class CreateDelete extends Cmd {
45     private static final String ROLE_PATH = "/authz/role";
46     private static final String[] options = {"create","delete"};
47     public CreateDelete(Role parent) {
48         super(parent,null, 
49                 new Param(optionsToString(options),true),
50                 new Param("name",true)); 
51     }
52
53     @Override
54     public int _exec(final int index, final String ... args) throws CadiException, APIException, LocatorException {
55         return same(new Retryable<Integer>() {
56             @Override
57             public Integer code(Rcli<?> client) throws CadiException, APIException {
58                 int idx = index;
59                 String action = args[idx++];
60                 int option = whichOption(options, action);
61         
62                 RoleRequest rr = new RoleRequest();
63                 rr.setName(args[idx++]);
64         
65                 // Set Start/End commands
66                 setStartEnd(rr);
67                 
68                 Future<RoleRequest> fp = null;
69                 String verb = null;
70                 int rv;
71                 switch(option) {
72                     case 0:
73                         fp = client.create(
74                             ROLE_PATH,
75                             getDF(RoleRequest.class),
76                             rr
77                             );
78                         verb = "Create";
79                         break;
80                     case 1:
81                         // Send "Force" if set
82                         setQueryParamsOn(client);
83                         fp = client.delete(
84                                 ROLE_PATH, // +args[idx++], 
85                                 getDF(RoleRequest.class),
86                                 rr
87                                 );
88                         verb = "Delete";
89                         break;
90                     default: // note, if not an option, whichOption throws Exception
91                         break;
92                         
93                 }
94                 boolean rolesSupplied = (args.length>idx);
95                 if (fp == null) {// This useless code brought to you by Sonar.
96                     throw new CadiException("No call made.");  
97                 }
98                 if (fp.get(AAFcli.timeout())) {
99                     rv=fp.code();
100                     pw().print(verb);
101                     pw().println("d Role");
102                     if (rolesSupplied) {
103                         for (;args.length>idx;++idx ) {
104                             try {
105                                 if (201!=(rv=((Role)parent)._exec(0,new String[] {"user","add",rr.getName(),args[idx]}))) {
106                                     rv = 206 /*HttpStatus.PARTIAL_CONTENT_206*/;
107                                 }
108                             } catch (LocatorException e) {
109                                 throw new CadiException(e);
110                             }
111                         }
112                     }
113                 } else {
114                     if ((rv=fp.code())==202) {
115                         pw().print("Role ");
116                         pw().print(verb);
117                         pw().println(" Accepted, but requires Approvals before actualizing");
118                     } else {
119                         error(fp);
120                     }
121                 }
122                 return rv;
123             }
124         });
125     }
126
127     @Override
128     public void detailedHelp(int indent, StringBuilder sb) {
129         detailLine(sb,indent,"Create OR Delete a Role");
130         detailLine(sb,indent+2,"name - Name of Role to create");
131         api(sb,indent,HttpMethods.POST,"authz/role",RoleRequest.class,true);
132         api(sb,indent,HttpMethods.DELETE,"authz/role",RoleRequest.class,false);
133     }
134
135 }