Merge "Add INFO.yaml file"
[aaf/authz.git] / auth / auth-cmd / src / main / java / org / onap / aaf / auth / cmd / ns / Create.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.ns;
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.NsRequest;
36
37 /**
38  * @author Jonathan
39  *
40  */
41 public class Create extends Cmd {
42         private static final String COMMA = ",";
43
44         public Create(NS parent) {
45                 super(parent,"create", 
46                                 new Param("ns-name",true),
47                                 new Param("owner (id[,id]*)",true), 
48                                 new Param("admin (id[,id]*)",false));
49         }
50
51         @Override
52         public int _exec(int _idx, final String ... args) throws CadiException, APIException, LocatorException {
53                 int idx = _idx;
54
55                 final NsRequest nr = new NsRequest();
56                 
57                 nr.setName(args[idx++]);
58                 String[] responsible = args[idx++].split(COMMA);
59                 for(String s : responsible) {
60                         nr.getResponsible().add(fullID(s));
61                 }
62                 String[] admin;
63                 if(args.length>idx) {
64                         admin = args[idx++].split(COMMA);
65                 } else {
66                         admin = responsible;
67                 }
68                 for(String s : admin) {
69                         nr.getAdmin().add(fullID(s));
70                 }
71                 
72                 // Set Start/End commands
73                 setStartEnd(nr);
74                 
75                 return same(new Retryable<Integer>() {
76                         @Override
77                         public Integer code(Rcli<?> client) throws CadiException, APIException {
78                                 // Requestable
79                                 setQueryParamsOn(client);
80                                 Future<NsRequest> fp = client.create(
81                                                 "/authz/ns", 
82                                                 getDF(NsRequest.class),
83                                                 nr
84                                                 );
85                                 if(fp.get(AAFcli.timeout())) {
86                                         pw().println("Created Namespace");
87                                 } else {
88                                         if(fp.code()==202) {
89                                                 pw().println("Namespace Creation Accepted, but requires Approvals before actualizing");
90                                         } else {
91                                                 error(fp);
92                                         }
93                                 }
94                                 return fp.code();
95                         }
96                 });
97         }
98
99         @Override
100         public void detailedHelp(int _indent, StringBuilder sb) {
101                 int indent = _indent;
102                 detailLine(sb,indent,"Create a Namespace");
103                 indent+=2;
104                 detailLine(sb,indent,"name        - Namespaces are dot-delimited, ex com.att.myapp");
105                 detailLine(sb,indent+14,"and must be created with parent credentials.");
106                 detailLine(sb,indent+14,"Ex: to create com.att.myapp, you must be admin for com.att");
107                 detailLine(sb,indent+14,"or com");
108                 detailLine(sb,indent,"owner       - This is the person(s) who is responsible for the ");
109                 detailLine(sb,indent+14,"app. These person or persons receive Notifications and");
110                 detailLine(sb,indent+14,"approves Requests regarding this Namespace. Companies have");
111                 detailLine(sb,indent+14,"Policies as to who may take on this responsibility");
112                 detailLine(sb,indent,"admin       - These are the people who are allowed to make changes on");
113                 detailLine(sb,indent+14,"the Namespace, including creating Roles, Permissions");
114                 detailLine(sb,indent+14,"and Credentials");
115                 sb.append('\n');
116                 detailLine(sb,indent,"Namespaces can be created even though there are Roles/Permissions which");
117                 detailLine(sb,indent,"start with the requested sub-namespace.  They are reassigned to the");
118                 detailLine(sb,indent,"Child Namespace");
119                 indent-=2;
120                 api(sb,indent,HttpMethods.POST,"authz/ns",NsRequest.class,true);
121         }
122
123 }