Adjust Agent for none K8s
[aaf/authz.git] / auth / auth-cmd / src / main / java / org / onap / aaf / auth / cmd / user / ID.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.user;
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.CredRequest;
36
37 public class ID extends Cmd {
38     private static final String CRED_PATH = "/authn/cred";
39     private static final String[] options = {"add","del"};
40     public ID(User parent) {
41         super(parent,"fqi",
42                 new Param(optionsToString(options),true),
43                 new Param("id",true)
44         );
45     }
46
47     @Override
48     public int _exec(int _idx, final String ... args) throws CadiException, APIException, LocatorException { 
49         int idx = _idx;
50         String key = args[idx++];
51         final int option = whichOption(options,key);
52
53         final CredRequest cr = new CredRequest();
54         cr.setId(args[idx++]);
55         cr.setType(10);
56         if (args.length>idx)
57             cr.setEntry(args[idx]);
58         
59         // Set Start/End commands
60         setStartEnd(cr);
61         Integer ret = same(new Retryable<Integer>() {
62             @Override
63             public Integer code(Rcli<?> client) throws CadiException, APIException {
64                 Future<CredRequest> fp=null;
65                 String verb =null;
66                 switch(option) {
67                     case 0:
68                         fp = client.create(
69                             CRED_PATH, 
70                             getDF(CredRequest.class), 
71                             cr
72                             );
73                         verb = "Added ID [";
74                         break;
75                     case 1:
76                         setQueryParamsOn(client);
77                         fp = client.delete(CRED_PATH,
78                             getDF(CredRequest.class),
79                             cr
80                             );
81                         verb = "Deleted ID [";
82                         break;
83                     default:
84                         break;
85                 }
86                 if (fp==null) {
87                     return null; // get by Sonar check.
88                 }
89                 if (fp.get(AAFcli.timeout())) {
90                     pw().print(verb);
91                     pw().print(cr.getId());
92                     pw().println(']');
93                 } else if (fp.code()==202) {
94                     pw().println("ID Action Accepted, but requires Approvals before actualizing");
95                 } else if (fp.code()==409 && option==0) {
96                     pw().println("FQI already exists");
97                 } else if (fp.code()==406 && option==1) {
98                     pw().println("FQI does not exist");
99                 } else {
100                     pw().println(Cred.ATTEMPT_FAILED_SPECIFICS_WITHELD);
101                 }
102                 return fp.code();
103             }
104         });
105         if (ret==null)ret = -1;
106         return ret;
107     }
108     
109     @Override
110     public void detailedHelp(int _indent, StringBuilder sb) {
111             int indent = _indent;
112         detailLine(sb,indent,"Add or Delete Fully Qualified Identity: An ID attached to the Namespace");
113         indent+=2;
114         detailLine(sb,indent,"fqi      - the ID to create/delete within AAF");
115         sb.append('\n');
116         detailLine(sb,indent,"This usage has NO Credential, and serves only to allow IDs to be attached");
117         detailLine(sb,indent,"to Roles before credentials such as Certificates are established.");
118         detailLine(sb,indent,"The Domain can be related to any Namespace you have access to *");
119         detailLine(sb,indent,"The Domain is in reverse order of Namespace, i.e. ");
120         detailLine(sb,indent+2,"NS of com.att.myapp can create user of XY1234@myapp.att.com");
121         indent-=2;
122         api(sb,indent,HttpMethods.POST,"authn/cred",CredRequest.class,true);
123         api(sb,indent,HttpMethods.DELETE,"authn/cred",CredRequest.class,false);
124         api(sb,indent,HttpMethods.PUT,"authn/cred",CredRequest.class,false);
125     }
126 }