Skip to content

Commit 9f7320b

Browse files
author
Chris Wiechmann
authored
Merge pull request #47 from Axway-API-Management-Plus/org-management
Added a number of features with this branch
2 parents 055a6d2 + c0b5197 commit 9f7320b

87 files changed

Lines changed: 4087 additions & 145 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

distribution/assembly/dist-release.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<assembly>
22
<id>release</id>
33
<formats>
4+
<format>dir</format>
45
<format>tar.gz</format>
56
<format>zip</format>
6-
<format>dir</format>
77
</formats>
88
<includeBaseDirectory>true</includeBaseDirectory>
99
<includeSiteDirectory>false</includeSiteDirectory>
@@ -17,6 +17,8 @@
1717
<include>com.github.axway-api-management-plus.apim-cli:apimcli-api-export</include>
1818
<include>com.github.axway-api-management-plus.apim-cli:apimcli-app-import</include>
1919
<include>com.github.axway-api-management-plus.apim-cli:apimcli-app-export</include>
20+
<include>com.github.axway-api-management-plus.apim-cli:apimcli-organizations</include>
21+
<include>com.github.axway-api-management-plus.apim-cli:apimcli-users</include>
2022
<include>com.github.axway-api-management-plus.apim-cli:apimcli-tool</include>
2123
</includes>
2224
<binaries>

distribution/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@
3232
<groupId>com.github.axway-api-management-plus.apim-cli</groupId>
3333
<artifactId>apimcli-app-export</artifactId>
3434
</dependency>
35+
<dependency>
36+
<groupId>com.github.axway-api-management-plus.apim-cli</groupId>
37+
<artifactId>apimcli-organizations</artifactId>
38+
</dependency>
39+
<dependency>
40+
<groupId>com.github.axway-api-management-plus.apim-cli</groupId>
41+
<artifactId>apimcli-users</artifactId>
42+
</dependency>
3543
</dependencies>
3644

3745
<build>

modules/api-export/pom.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
<dependency>
2727
<groupId>com.github.axway-api-management-plus.apim-cli</groupId>
2828
<artifactId>apimcli-api-import</artifactId>
29-
<version>${project.version}</version>
30-
<scope>test</scope>
3129
</dependency>
3230
<dependency>
3331
<groupId>com.github.axway-api-management-plus.apim-cli</groupId>

modules/api-export/src/main/java/com/axway/apim/APIExportApp.java

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
import com.axway.apim.api.API;
1111
import com.axway.apim.api.export.impl.APIResultHandler;
1212
import com.axway.apim.api.export.impl.APIResultHandler.APIListImpl;
13+
import com.axway.apim.api.export.lib.APIChangeParams;
1314
import com.axway.apim.api.export.lib.APIDeleteCLIOptions;
1415
import com.axway.apim.api.export.lib.APIExportGetCLIOptions;
1516
import com.axway.apim.api.export.lib.APIExportParams;
1617
import com.axway.apim.api.export.lib.APIUnpublishCLIOptions;
18+
import com.axway.apim.api.export.lib.ChangeAPICLIOptions;
1719
import com.axway.apim.cli.APIMCLIServiceProvider;
1820
import com.axway.apim.cli.CLIServiceMethod;
1921
import com.axway.apim.lib.errorHandling.AppException;
@@ -33,7 +35,7 @@ public class APIExportApp implements APIMCLIServiceProvider {
3335
private static ErrorState errorState = ErrorState.getInstance();
3436

3537
public static void main(String args[]) {
36-
int rc = export(args);
38+
int rc = change(args);
3739
System.exit(rc);
3840
}
3941

@@ -43,13 +45,13 @@ public static int export(String args[]) {
4345
APIExportParams params = new APIExportParams(new APIExportGetCLIOptions(args));
4446
switch(params.getOutputFormat()) {
4547
case console:
46-
return runExport(params, APIListImpl.CONSOLE_EXPORTER);
48+
return execute(params, APIListImpl.CONSOLE_EXPORTER);
4749
case json:
48-
return runExport(params, APIListImpl.JSON_EXPORTER);
50+
return execute(params, APIListImpl.JSON_EXPORTER);
4951
case csv:
50-
return runExport(params, APIListImpl.CSV_EXPORTER);
52+
return execute(params, APIListImpl.CSV_EXPORTER);
5153
default:
52-
return runExport(params, APIListImpl.CONSOLE_EXPORTER);
54+
return execute(params, APIListImpl.CONSOLE_EXPORTER);
5355
}
5456
} catch (AppException e) {
5557

@@ -71,7 +73,7 @@ public static int export(String args[]) {
7173
public static int delete(String args[]) {
7274
try {
7375
APIExportParams params = new APIExportParams(new APIDeleteCLIOptions(args));
74-
return runExport(params, APIListImpl.API_DELETE_HANDLER);
76+
return execute(params, APIListImpl.API_DELETE_HANDLER);
7577
} catch (Exception e) {
7678
LOG.error(e.getMessage(), e);
7779
return ErrorCode.UNXPECTED_ERROR.getCode();
@@ -82,14 +84,36 @@ public static int delete(String args[]) {
8284
public static int unpublish(String args[]) {
8385
try {
8486
APIExportParams params = new APIExportParams(new APIUnpublishCLIOptions(args));
85-
return runExport(params, APIListImpl.API_UNPUBLISH_HANDLER);
87+
return execute(params, APIListImpl.API_UNPUBLISH_HANDLER);
8688
} catch (Exception e) {
8789
LOG.error(e.getMessage(), e);
8890
return ErrorCode.UNXPECTED_ERROR.getCode();
8991
}
9092
}
9193

92-
private static int runExport(APIExportParams params, APIListImpl resultHandlerImpl) {
94+
@CLIServiceMethod(name = "publish", description = "Publish the selected APIs")
95+
public static int publish(String args[]) {
96+
try {
97+
APIExportParams params = new APIExportParams(new APIUnpublishCLIOptions(args));
98+
return execute(params, APIListImpl.API_PUBLISH_HANDLER);
99+
} catch (Exception e) {
100+
LOG.error(e.getMessage(), e);
101+
return ErrorCode.UNXPECTED_ERROR.getCode();
102+
}
103+
}
104+
105+
@CLIServiceMethod(name = "change", description = "Changes the selected APIs according to given parameters")
106+
public static int change(String args[]) {
107+
try {
108+
APIChangeParams params = new APIChangeParams(new ChangeAPICLIOptions(args));
109+
return execute(params, APIListImpl.API_CHANGE_HANDLER);
110+
} catch (Exception e) {
111+
LOG.error(e.getMessage(), e);
112+
return ErrorCode.UNXPECTED_ERROR.getCode();
113+
}
114+
}
115+
116+
private static int execute(APIExportParams params, APIListImpl resultHandlerImpl) {
93117
try {
94118
// We need to clean some Singleton-Instances, as tests are running in the same JVM
95119
APIManagerAdapter.deleteInstance();
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package com.axway.apim.api.export.impl;
2+
3+
import java.util.ArrayList;
4+
import java.util.Iterator;
5+
import java.util.List;
6+
7+
import com.axway.apim.adapter.APIManagerAdapter;
8+
import com.axway.apim.adapter.APIManagerAdapter.CUSTOM_PROP_TYPE;
9+
import com.axway.apim.adapter.apis.APIFilter;
10+
import com.axway.apim.adapter.apis.APIFilter.Builder.APIType;
11+
import com.axway.apim.api.API;
12+
import com.axway.apim.api.export.lib.APIChangeParams;
13+
import com.axway.apim.api.export.lib.APIExportParams;
14+
import com.axway.apim.api.model.ServiceProfile;
15+
import com.axway.apim.api.state.APIChangeState;
16+
import com.axway.apim.apiimport.APIImportManager;
17+
import com.axway.apim.lib.errorHandling.AppException;
18+
import com.axway.apim.lib.errorHandling.ErrorCode;
19+
import com.axway.apim.lib.utils.Utils;
20+
21+
public class APIChangeHandler extends APIResultHandler {
22+
23+
APIChangeParams changeParams;
24+
25+
public APIChangeHandler(APIExportParams params) {
26+
super(params);
27+
this.changeParams = (APIChangeParams)params;
28+
}
29+
30+
@Override
31+
public void execute(List<API> apis) throws AppException {
32+
APIManagerAdapter adapter = APIManagerAdapter.getInstance();
33+
List<APIChangeState> apisToChange = new ArrayList<APIChangeState>();
34+
LOG.info(apis.size() + " selected to change.");
35+
for(API api : apis) {
36+
try {
37+
if(changeParams.getNewBackend()!=null) {
38+
api = changeBackendBasePath(api, changeParams.getNewBackend(), changeParams.getOldBackend());
39+
}
40+
// Reload the actual API again, to get a clone
41+
API actualAPI = adapter.apiAdapter.getAPI(new APIFilter.Builder(APIType.ACTUAL_API).hasId(api.getId()).build(), false);
42+
APIChangeState changeState = new APIChangeState(actualAPI, api);
43+
if(!changeState.hasAnyChanges()) {
44+
LOG.warn("No changes for API: '"+api.getName()+"'");
45+
continue;
46+
}
47+
if(changeState.isBreaking() && !params.isForce()) {
48+
LOG.error("Changing API: '"+api.getName()+"' is a potentially breaking change which can't be applied without enforcing it! Try option: -force");
49+
continue;
50+
}
51+
LOG.info("Planned changes for API: '" + api.getName() + "': " + changeState.getAllChanges());
52+
apisToChange.add(changeState);
53+
} catch(Exception e) {
54+
LOG.error("Error preparing required changes for API: " + api.getName(), e);
55+
}
56+
}
57+
if(apisToChange.size()==0) {
58+
System.out.println("No changes required for the selected APIs.");
59+
return;
60+
}
61+
System.out.println("Okay, going to change: " + apisToChange.size() + " API(s)");
62+
if(Utils.askYesNo("Do you wish to proceed? (Y/N)")) {
63+
APIImportManager importManager = new APIImportManager();
64+
for(APIChangeState changeState : apisToChange) {
65+
LOG.info("Apply changes for API: '" + changeState.getDesiredAPI().getName() +"'");
66+
try {
67+
importManager.applyChanges(changeState);
68+
} catch(Exception e) {
69+
LOG.error("Error applying changes for API: " + changeState.getDesiredAPI().getName(), e);
70+
}
71+
}
72+
System.out.println("Done!");
73+
}
74+
}
75+
76+
@Override
77+
public APIFilter getFilter() {
78+
// We need to load the complete API, as this becomes the desired API
79+
// hence, all App, Orgs, Quotas, etc. must be taken over the new API
80+
return getBaseAPIFilterBuilder()
81+
.includeCustomProperties(APIManagerAdapter.getAllConfiguredCustomProperties(CUSTOM_PROP_TYPE.api))
82+
.includeOriginalAPIDefinition(true)
83+
.includeClientApplications(true)
84+
.includeClientAppQuota(true)
85+
.includeClientOrganizations(true)
86+
.includeQuotas(true)
87+
.build();
88+
}
89+
90+
private API changeBackendBasePath(API api, String newBackendBasepath, String oldBackendBasepath) throws AppException {
91+
if(oldBackendBasepath!=null) {
92+
String actualBackend = getActualBackendBasepath(api);
93+
if(!oldBackendBasepath.equals(actualBackend)) {
94+
LOG.warn("Backend of API: " + api.getName() + " wont be changed as it has a different backend configured. Current: '" + actualBackend + "' New: '"+oldBackendBasepath+"'");
95+
return api;
96+
}
97+
}
98+
Iterator<ServiceProfile> it = api.getServiceProfiles().values().iterator();
99+
while(it.hasNext()) {
100+
ServiceProfile profile = it.next();
101+
profile.setBasePath(newBackendBasepath);
102+
}
103+
return api;
104+
}
105+
106+
private String getActualBackendBasepath(API api) throws AppException {
107+
Iterator<ServiceProfile> it = api.getServiceProfiles().values().iterator();
108+
String lastBasepath = null;
109+
while(it.hasNext()) {
110+
ServiceProfile profile = it.next();
111+
if(lastBasepath!=null && !lastBasepath.equals(profile.getBasePath())) {
112+
throw new AppException("API has multiple backends configured. Please export - change - import the API to change it.", ErrorCode.UNSUPPORTED_FEATURE);
113+
}
114+
lastBasepath = profile.getBasePath();
115+
}
116+
return lastBasepath;
117+
}
118+
119+
}

modules/api-export/src/main/java/com/axway/apim/api/export/impl/APIResultHandler.java

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import java.util.Iterator;
88
import java.util.List;
99
import java.util.Map;
10-
import java.util.Scanner;
1110

1211
import org.slf4j.Logger;
1312
import org.slf4j.LoggerFactory;
@@ -17,16 +16,15 @@
1716
import com.axway.apim.adapter.apis.APIFilter;
1817
import com.axway.apim.adapter.apis.APIFilter.Builder;
1918
import com.axway.apim.adapter.apis.APIFilter.Builder.APIType;
20-
import com.axway.apim.adapter.apis.APIManagerPoliciesAdapter.PolicyType;
2119
import com.axway.apim.adapter.apis.APIFilter.METHOD_TRANSLATION;
2220
import com.axway.apim.adapter.apis.APIFilter.POLICY_TRANSLATION;
21+
import com.axway.apim.adapter.apis.APIManagerPoliciesAdapter.PolicyType;
2322
import com.axway.apim.api.API;
2423
import com.axway.apim.api.export.ExportAPI;
2524
import com.axway.apim.api.export.lib.APIExportParams;
2625
import com.axway.apim.api.model.InboundProfile;
2726
import com.axway.apim.api.model.Organization;
2827
import com.axway.apim.api.model.OutboundProfile;
29-
import com.axway.apim.api.model.Policy;
3028
import com.axway.apim.api.model.SecurityDevice;
3129
import com.axway.apim.api.model.SecurityProfile;
3230
import com.axway.apim.lib.errorHandling.AppException;
@@ -45,7 +43,9 @@ public enum APIListImpl {
4543
CONSOLE_EXPORTER(ConsoleAPIExporter.class),
4644
CSV_EXPORTER(CSVAPIExporter.class),
4745
API_DELETE_HANDLER(DeleteAPIHandler.class),
48-
API_UNPUBLISH_HANDLER(UnpublishAPIHandler.class);
46+
API_PUBLISH_HANDLER(PublishAPIHandler.class),
47+
API_UNPUBLISH_HANDLER(UnpublishAPIHandler.class),
48+
API_CHANGE_HANDLER(APIChangeHandler.class);
4949

5050
private final Class<APIResultHandler> implClass;
5151

@@ -99,25 +99,6 @@ protected Builder getBaseAPIFilterBuilder() {
9999
.failOnError(false);
100100
return builder;
101101
}
102-
103-
protected static boolean askYesNo(String question) {
104-
return askYesNo(question, "[Y]", "[N]");
105-
}
106-
107-
protected static boolean askYesNo(String question, String positive, String negative) {
108-
Scanner input = new Scanner(System.in);
109-
// Convert everything to upper case for simplicity...
110-
positive = positive.toUpperCase();
111-
negative = negative.toUpperCase();
112-
String answer;
113-
do {
114-
System.out.print(question+ " ");
115-
answer = input.next().trim().toUpperCase();
116-
} while (!answer.matches(positive) && !answer.matches(negative));
117-
input.close();
118-
// Assess if we match a positive response
119-
return answer.matches(positive);
120-
}
121102

122103
protected static String getBackendPath(API api) {
123104
ExportAPI exportAPI = new ExportAPI(api);

modules/api-export/src/main/java/com/axway/apim/api/export/impl/DeleteAPIHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.axway.apim.api.export.lib.APIExportParams;
1010
import com.axway.apim.lib.CommandParameters;
1111
import com.axway.apim.lib.errorHandling.AppException;
12+
import com.axway.apim.lib.utils.Utils;
1213

1314
public class DeleteAPIHandler extends APIResultHandler {
1415

@@ -23,7 +24,7 @@ public void execute(List<API> apis) throws AppException {
2324
if(CommandParameters.getInstance().isForce()) {
2425
System.out.println("Force flag given to delete: "+apis.size()+" API(s)");
2526
} else {
26-
if(askYesNo("Do you wish to proceed? (Y/N)")) {
27+
if(Utils.askYesNo("Do you wish to proceed? (Y/N)")) {
2728
} else {
2829
System.out.println("Canceled.");
2930
return;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.axway.apim.api.export.impl;
2+
3+
import java.util.List;
4+
5+
import com.axway.apim.adapter.APIStatusManager;
6+
import com.axway.apim.adapter.apis.APIFilter;
7+
import com.axway.apim.api.API;
8+
import com.axway.apim.api.export.lib.APIExportParams;
9+
import com.axway.apim.lib.errorHandling.AppException;
10+
11+
public class PublishAPIHandler extends APIResultHandler {
12+
13+
public PublishAPIHandler(APIExportParams params) {
14+
super(params);
15+
}
16+
17+
@Override
18+
public void execute(List<API> apis) throws AppException {
19+
APIStatusManager statusManager = new APIStatusManager();
20+
System.out.println("Going to publish: " + apis.size() + " API(s)");
21+
for(API api : apis) {
22+
try {
23+
statusManager.update(api, API.STATE_PUBLISHED, true);
24+
} catch(Exception e) {
25+
LOG.error("Error publishing API: " + api.getName());
26+
}
27+
}
28+
System.out.println("Done!");
29+
}
30+
31+
@Override
32+
public APIFilter getFilter() {
33+
return getBaseAPIFilterBuilder().build();
34+
}
35+
36+
}

modules/api-export/src/main/java/com/axway/apim/api/export/impl/UnpublishAPIHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.axway.apim.api.API;
88
import com.axway.apim.api.export.lib.APIExportParams;
99
import com.axway.apim.lib.errorHandling.AppException;
10+
import com.axway.apim.lib.utils.Utils;
1011

1112
public class UnpublishAPIHandler extends APIResultHandler {
1213

@@ -18,7 +19,7 @@ public UnpublishAPIHandler(APIExportParams params) {
1819
public void execute(List<API> apis) throws AppException {
1920
APIStatusManager statusManager = new APIStatusManager();
2021
System.out.println(apis.size() + " selected to unpublish.");
21-
if(askYesNo("Do you wish to proceed? (Y/N)")) {
22+
if(Utils.askYesNo("Do you wish to proceed? (Y/N)")) {
2223
System.out.println("Okay, going to unpublish: " + apis.size() + " API(s)");
2324
for(API api : apis) {
2425
try {

0 commit comments

Comments
 (0)