Skip to content

Commit c4ed14f

Browse files
committed
use hostname and path flags in all of the CLI
App Engine is deprecating app IDs in config and we want to get away from this model anyway. This change adds --hostname and --path to get-discovery-doc and get-client-lib to fully specify where an API is hosted. This involves the following code changes: * refactor of ServiceContext to allow specifying a hostname instead of just an app id * Renamed the appHostName property to appHostname in ServiceContext * debug flag in get-discovery-doc is now a no-op. It really already was, but now it has been removed from the code except for the interface itself so we don't break anyone. It stopped outputting intermediate files because there no longer are any. * small cleanups (removed extra space, unused CLI options, test formatting adjustments)
1 parent d766aa6 commit c4ed14f

File tree

10 files changed

+106
-99
lines changed

10 files changed

+106
-99
lines changed

endpoints-framework-tools/src/main/java/com/google/api/server/spi/tools/EndpointsToolAction.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -326,17 +326,15 @@ protected Option makeHostnameOption() {
326326
OPTION_HOSTNAME_SHORT,
327327
OPTION_HOSTNAME_LONG,
328328
"HOSTNAME",
329-
"Sets the hostname for the generated OpenAPI document. Default is the app's default "
330-
+ "hostname.");
329+
"Sets the hostname for the generated document. Default is the app's default hostname.");
331330
}
332331

333332
protected Option makeBasePathOption() {
334333
return EndpointsOption.makeVisibleNonFlagOption(
335334
OPTION_BASE_PATH_SHORT,
336335
OPTION_BASE_PATH_LONG,
337336
"BASE_PATH",
338-
"Sets the base path for the generated OpenAPI document. Default is " + DEFAULT_BASE_PATH
339-
+ ".");
337+
"Sets the base path for the generated document. Default is " + DEFAULT_BASE_PATH + ".");
340338
}
341339

342340
/**

endpoints-framework-tools/src/main/java/com/google/api/server/spi/tools/GetClientLibAction.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,18 @@ public class GetClientLibAction extends EndpointsToolAction {
3333
public static final String NAME = "get-client-lib";
3434

3535
private Option classPathOption = makeClassPathOption();
36-
3736
private Option languageOption = makeLanguageOption();
38-
3937
private Option outputOption = makeOutputOption();
40-
4138
private Option warOption = makeWarOption();
42-
4339
private Option buildSystemOption = makeBuildSystemOption();
44-
4540
private Option debugOption = makeDebugOption();
41+
private Option hostnameOption = makeHostnameOption();
42+
private Option basePathOption = makeBasePathOption();
4643

4744
public GetClientLibAction() {
4845
super(NAME);
4946
setOptions(Arrays.asList(classPathOption, languageOption, outputOption, warOption,
50-
buildSystemOption, debugOption));
47+
buildSystemOption, debugOption, hostnameOption, basePathOption));
5148
setShortDescription("Generates a client library");
5249
setExampleString("<Endpoints tool> get-client-lib --language=java --build_system=maven "
5350
+ "com.google.devrel.samples.ttt.spi.BoardV1 com.google.devrel.samples.ttt.spi.ScoresV1");
@@ -62,8 +59,10 @@ public boolean execute() throws ClassNotFoundException, IOException, ApiConfigEx
6259
return false;
6360
}
6461
getClientLib(computeClassPath(warPath, getClassPath(classPathOption)),
65-
getLanguage(languageOption), getOutputPath(outputOption), warPath, serviceClassNames,
66-
getBuildSystem(buildSystemOption), getDebug(debugOption));
62+
getLanguage(languageOption), getOutputPath(outputOption), serviceClassNames,
63+
getBuildSystem(buildSystemOption), getHostname(hostnameOption, warPath),
64+
getBasePath(basePathOption), getDebug(debugOption)
65+
);
6766
return true;
6867
}
6968

@@ -73,16 +72,17 @@ public boolean execute() throws ClassNotFoundException, IOException, ApiConfigEx
7372
* @param classPath Class path to load service classes and their dependencies
7473
* @param language Language of the client library. Only "java" is supported right now
7574
* @param outputDirPath Directory to write output files into
76-
* @param warPath Directory or file containing a WAR layout
7775
* @param serviceClassNames Array of service class names of the API
7876
* @param buildSystem The build system to use for the client library
77+
* @param hostname The hostname to use
78+
* @param basePath The base path to use
7979
* @param debug Whether or not to output intermediate output files
8080
*/
8181
public Object getClientLib(URL[] classPath, String language, String outputDirPath,
82-
String warPath, List<String> serviceClassNames, String buildSystem, boolean debug)
83-
throws ClassNotFoundException, IOException, ApiConfigException {
82+
List<String> serviceClassNames, String buildSystem, String hostname, String basePath,
83+
boolean debug) throws ClassNotFoundException, IOException, ApiConfigException {
8484
Map<String, String> discoveryDocs = new GetDiscoveryDocAction().getDiscoveryDoc(
85-
classPath, outputDirPath, warPath, serviceClassNames, debug, false /* outputToDisk */);
85+
classPath, outputDirPath, serviceClassNames, basePath, hostname, debug /* outputToDisk */);
8686
for (Map.Entry<String, String> entry : discoveryDocs.entrySet()) {
8787
new GenClientLibAction().genClientLib(language, outputDirPath, entry.getValue(), buildSystem);
8888
}

endpoints-framework-tools/src/main/java/com/google/api/server/spi/tools/GetDiscoveryDocAction.java

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,18 @@ public class GetDiscoveryDocAction extends EndpointsToolAction {
5858
public static final String NAME = "get-discovery-doc";
5959

6060
private Option classPathOption = makeClassPathOption();
61-
6261
private Option outputOption = makeOutputOption();
63-
6462
private Option warOption = makeWarOption();
65-
6663
private Option debugOption = makeDebugOption();
64+
private Option hostnameOption = makeHostnameOption();
65+
private Option basePathOption = makeBasePathOption();
6766

6867
public GetDiscoveryDocAction() {
6968
super(NAME);
70-
setOptions(Arrays.asList(classPathOption, outputOption, warOption, debugOption));
69+
setOptions(Arrays.asList(classPathOption, outputOption, warOption, debugOption, hostnameOption,
70+
basePathOption));
7171
setShortDescription("Generates discovery documents");
72-
setExampleString("<Endpoints tool> get-discovery-doc --format=rpc "
72+
setExampleString("<Endpoints tool> get-discovery-doc "
7373
+ "com.google.devrel.samples.ttt.spi.BoardV1 com.google.devrel.samples.ttt.spi.ScoresV1");
7474
setHelpDisplayNeeded(true);
7575
}
@@ -82,7 +82,8 @@ public boolean execute() throws ClassNotFoundException, IOException, ApiConfigEx
8282
return false;
8383
}
8484
getDiscoveryDoc(computeClassPath(warPath, getClassPath(classPathOption)),
85-
getOutputPath(outputOption), warPath, serviceClassNames, getDebug(debugOption));
85+
getOutputPath(outputOption), serviceClassNames, getHostname(hostnameOption, warPath),
86+
getBasePath(basePathOption), true);
8687
return true;
8788
}
8889

@@ -91,28 +92,13 @@ public boolean execute() throws ClassNotFoundException, IOException, ApiConfigEx
9192
* configuration, generating Discovery doc and generating client library into one.
9293
* @param classPath Class path to load service classes and their dependencies
9394
* @param outputDirPath Directory to write output files into
94-
* @param warPath Directory or file containing a WAR layout
95-
* @param serviceClassNames Array of service class names of the API
96-
* @param debug Whether or not to output intermediate output files
97-
*/
98-
public Map<String, String> getDiscoveryDoc(URL[] classPath, String outputDirPath,
99-
String warPath, List<String> serviceClassNames, boolean debug)
100-
throws ClassNotFoundException, IOException, ApiConfigException {
101-
return getDiscoveryDoc(classPath, outputDirPath, warPath, serviceClassNames, debug, true);
102-
}
103-
104-
/**
105-
* Generates a Java client library for an API. Combines the steps of generating API
106-
* configuration, generating Discovery doc and generating client library into one.
107-
* @param classPath Class path to load service classes and their dependencies
108-
* @param outputDirPath Directory to write output files into
109-
* @param warPath Directory or file containing a WAR layout
11095
* @param serviceClassNames Array of service class names of the API
111-
* @param debug Whether or not to output intermediate output files
96+
* @param hostname The hostname to use
97+
* @param basePath The base path to use
11298
* @param outputToDisk Whether or not to output discovery docs to disk
11399
*/
114100
public Map<String, String> getDiscoveryDoc(URL[] classPath, String outputDirPath,
115-
String warPath, List<String> serviceClassNames, boolean debug, boolean outputToDisk)
101+
List<String> serviceClassNames, String hostname, String basePath, boolean outputToDisk)
116102
throws ClassNotFoundException, IOException, ApiConfigException {
117103
File outputDir = new File(outputDirPath);
118104
if (!outputDir.isDirectory()) {
@@ -138,13 +124,13 @@ public ApiKey apply(ApiConfig input) {
138124
}
139125
ApiConfigLoader configLoader = new ApiConfigLoader(
140126
configFactory, typeLoader, new ApiConfigAnnotationReader(typeLoader.getAnnotationTypes()));
141-
ServiceContext serviceContext = ServiceContext.create(
142-
AppEngineUtil.getApplicationId(warPath), ServiceContext.DEFAULT_API_NAME);
127+
ServiceContext serviceContext = ServiceContext.createFromHostname(
128+
hostname, ServiceContext.DEFAULT_API_NAME);
143129
for (Class<?> serviceClass : loadClasses(classLoader, serviceClassNames)) {
144130
apiConfigs.add(configLoader.loadConfiguration(serviceContext, serviceClass));
145131
}
146132
DiscoveryGenerator.Result result = discoveryGenerator.writeDiscovery(
147-
apiConfigs, new DiscoveryContext().setHostname(serviceContext.getAppHostName()),
133+
apiConfigs, new DiscoveryContext().setHostname(hostname).setBasePath(basePath),
148134
schemaRepository);
149135
ObjectWriter writer =
150136
ObjectMapperUtil.createStandardObjectMapper().writer(new EndpointsPrettyPrinter());

endpoints-framework-tools/src/test/java/com/google/api/server/spi/tools/GetClientLibActionTest.java

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@ public class GetClientLibActionTest extends EndpointsToolTest {
4242
private URL[] classPath;
4343
private String language;
4444
private String outputDirPath;
45-
private String warPath;
4645
private List<String> serviceClassNames;
4746
private String buildSystem;
4847
private boolean debugOutput;
48+
private String hostname;
49+
private String basePath;
4950
private GetClientLibAction testAction;
5051

5152
@Override
@@ -61,19 +62,21 @@ public void setUp() throws Exception {
6162
classPath = null;
6263
language = null;
6364
outputDirPath = null;
64-
warPath = null;
6565
serviceClassNames = null;
66+
hostname = null;
67+
basePath = null;
6668
testAction = new GetClientLibAction() {
6769

6870
@Override
6971
public Object getClientLib(
70-
URL[] c, String l, String o, String w, List<String> s, String b, boolean d) {
72+
URL[] c, String l, String o, List<String> s, String bs, String h, String bp, boolean d) {
7173
classPath = c;
7274
language = l;
7375
outputDirPath = o;
74-
warPath = w;
7576
serviceClassNames = s;
76-
buildSystem = b;
77+
buildSystem = bs;
78+
hostname = h;
79+
basePath = bp;
7780
debugOutput = d;
7881
return null;
7982
}
@@ -82,8 +85,9 @@ public Object getClientLib(
8285

8386
@Test
8487
public void testMissingOption() throws Exception {
85-
tool.execute(new String[]{
86-
GetClientLibAction.NAME, option(EndpointsToolAction.OPTION_CLASS_PATH_SHORT), "classPath",
88+
tool.execute(new String[]{GetClientLibAction.NAME,
89+
option(EndpointsToolAction.OPTION_CLASS_PATH_SHORT), "classPath",
90+
option(EndpointsToolAction.OPTION_HOSTNAME_SHORT), "foo.com",
8791
"MyService"});
8892
assertFalse(usagePrinted);
8993
assertThat(Lists.newArrayList(classPath))
@@ -94,7 +98,6 @@ GetClientLibAction.NAME, option(EndpointsToolAction.OPTION_CLASS_PATH_SHORT), "c
9498
.toURL());
9599
assertEquals(EndpointsToolAction.DEFAULT_LANGUAGE, language);
96100
assertEquals(EndpointsToolAction.DEFAULT_OUTPUT_PATH, outputDirPath);
97-
assertEquals(EndpointsToolAction.DEFAULT_WAR_PATH, warPath);
98101
assertStringsEqual(Arrays.asList("MyService"), serviceClassNames);
99102
}
100103

@@ -108,10 +111,11 @@ GetClientLibAction.NAME, option(EndpointsToolAction.OPTION_CLASS_PATH_SHORT),
108111

109112
@Test
110113
public void testGetClientLib() throws Exception {
111-
tool.execute(
112-
new String[]{GetClientLibAction.NAME, option(EndpointsToolAction.OPTION_CLASS_PATH_SHORT),
113-
"classPath", option(EndpointsToolAction.OPTION_LANGUAGE_SHORT), "java",
114+
tool.execute(new String[]{GetClientLibAction.NAME,
115+
option(EndpointsToolAction.OPTION_CLASS_PATH_SHORT), "classPath",
116+
option(EndpointsToolAction.OPTION_LANGUAGE_SHORT), "java",
114117
option(EndpointsToolAction.OPTION_OUTPUT_DIR_SHORT), "outputDir",
118+
option(EndpointsToolAction.OPTION_HOSTNAME_SHORT), "foo.com",
115119
"MyService", "MyService2"});
116120
assertFalse(usagePrinted);
117121
assertThat(Lists.newArrayList(classPath))
@@ -122,19 +126,20 @@ public void testGetClientLib() throws Exception {
122126
.toURL());
123127
assertEquals("java", language);
124128
assertEquals("outputDir", outputDirPath);
125-
assertEquals(EndpointsToolAction.DEFAULT_WAR_PATH, warPath);
126129
assertStringsEqual(Arrays.asList("MyService", "MyService2"), serviceClassNames);
127130
assertEquals(null, buildSystem);
128131
assertFalse(debugOutput);
132+
assertThat(basePath).isEqualTo("/_ah/api");
129133
}
130134

131135
@Test
132136
public void testGetClientLibWithBuildSystem() throws Exception {
133-
tool.execute(
134-
new String[]{GetClientLibAction.NAME, option(EndpointsToolAction.OPTION_CLASS_PATH_SHORT),
135-
"classPath", option(EndpointsToolAction.OPTION_LANGUAGE_SHORT), "java",
137+
tool.execute(new String[]{GetClientLibAction.NAME,
138+
option(EndpointsToolAction.OPTION_CLASS_PATH_SHORT), "classPath",
139+
option(EndpointsToolAction.OPTION_LANGUAGE_SHORT), "java",
136140
option(EndpointsToolAction.OPTION_OUTPUT_DIR_SHORT), "outputDir",
137141
option(EndpointsToolAction.OPTION_BUILD_SYSTEM_SHORT), "maven",
142+
option(EndpointsToolAction.OPTION_HOSTNAME_SHORT), "foo.com",
138143
"MyService", "MyService2"});
139144
assertFalse(usagePrinted);
140145
assertThat(Lists.newArrayList(classPath))
@@ -145,19 +150,21 @@ public void testGetClientLibWithBuildSystem() throws Exception {
145150
.toURL());
146151
assertEquals("java", language);
147152
assertEquals("outputDir", outputDirPath);
148-
assertEquals(EndpointsToolAction.DEFAULT_WAR_PATH, warPath);
149153
assertStringsEqual(Arrays.asList("MyService", "MyService2"), serviceClassNames);
150154
assertEquals("maven", buildSystem);
151155
assertFalse(debugOutput);
152156
}
153157

154158
@Test
155159
public void testGetClientLibWithDebugOutput() throws Exception {
156-
tool.execute(
157-
new String[]{GetClientLibAction.NAME, option(EndpointsToolAction.OPTION_CLASS_PATH_SHORT),
158-
"classPath", option(EndpointsToolAction.OPTION_LANGUAGE_SHORT), "java",
160+
tool.execute(new String[]{GetClientLibAction.NAME,
161+
option(EndpointsToolAction.OPTION_CLASS_PATH_SHORT), "classPath",
162+
option(EndpointsToolAction.OPTION_LANGUAGE_SHORT), "java",
159163
option(EndpointsToolAction.OPTION_OUTPUT_DIR_SHORT), "outputDir",
160-
option(EndpointsToolAction.OPTION_DEBUG, false), "MyService", "MyService2"});
164+
option(EndpointsToolAction.OPTION_DEBUG, false),
165+
option(EndpointsToolAction.OPTION_HOSTNAME_SHORT), "foo.com",
166+
option(EndpointsToolAction.OPTION_BASE_PATH_SHORT), "/api",
167+
"MyService", "MyService2"});
161168
assertFalse(usagePrinted);
162169
assertThat(Lists.newArrayList(classPath))
163170
.containsExactly(new File("classPath").toURI().toURL(),
@@ -167,9 +174,10 @@ public void testGetClientLibWithDebugOutput() throws Exception {
167174
.toURL());
168175
assertEquals("java", language);
169176
assertEquals("outputDir", outputDirPath);
170-
assertEquals(EndpointsToolAction.DEFAULT_WAR_PATH, warPath);
171177
assertStringsEqual(Arrays.asList("MyService", "MyService2"), serviceClassNames);
172178
assertEquals(null, buildSystem);
173179
assertTrue(debugOutput);
180+
assertThat(hostname).isEqualTo("foo.com");
181+
assertThat(basePath).isEqualTo("/api");
174182
}
175183
}

endpoints-framework-tools/src/test/java/com/google/api/server/spi/tools/GetDiscoveryDocActionTest.java

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ public class GetDiscoveryDocActionTest extends EndpointsToolTest {
4141

4242
private URL[] classPath;
4343
private String outputDirPath;
44-
private String warPath;
4544
private List<String> serviceClassNames;
4645
private GetDiscoveryDocAction testAction;
47-
private boolean debugOutput;
46+
private String hostname;
47+
private String basePath;
48+
private boolean outputToDisk;
4849

4950
@Override
5051
protected void addTestAction(Map<String, EndpointsToolAction> commands) {
@@ -58,28 +59,31 @@ public void setUp() throws Exception {
5859
usagePrinted = false;
5960
classPath = null;
6061
outputDirPath = null;
61-
warPath = null;
6262
serviceClassNames = null;
63+
hostname = null;
64+
basePath = null;
6365
testAction = new GetDiscoveryDocAction() {
6466

6567
@Override
6668
public Map<String, String> getDiscoveryDoc(
67-
URL[] c, String o, String w, List<String> s, boolean d) {
69+
URL[] c, String o, List<String> s, String h, String b, boolean d) {
6870
classPath = c;
6971
outputDirPath = o;
70-
warPath = w;
7172
serviceClassNames = s;
72-
debugOutput = d;
73+
hostname = h;
74+
basePath = b;
75+
outputToDisk = d;
7376
return null;
7477
}
7578
};
7679
}
7780

7881
@Test
7982
public void testMissingOption() throws Exception {
80-
tool.execute(new String[]{
81-
GetDiscoveryDocAction.NAME, option(EndpointsToolAction.OPTION_CLASS_PATH_SHORT),
82-
"classPath", "MyService"});
83+
tool.execute(new String[]{GetDiscoveryDocAction.NAME,
84+
option(EndpointsToolAction.OPTION_CLASS_PATH_SHORT), "classPath",
85+
option(EndpointsToolAction.OPTION_HOSTNAME_SHORT), "foo.com",
86+
"MyService"});
8387
assertFalse(usagePrinted);
8488
assertThat(Lists.newArrayList(classPath))
8589
.containsExactly(new File("classPath").toURI().toURL(),
@@ -88,15 +92,15 @@ GetDiscoveryDocAction.NAME, option(EndpointsToolAction.OPTION_CLASS_PATH_SHORT),
8892
.toURI()
8993
.toURL());
9094
assertEquals(EndpointsToolAction.DEFAULT_OUTPUT_PATH, outputDirPath);
91-
assertEquals(EndpointsToolAction.DEFAULT_WAR_PATH, warPath);
9295
assertStringsEqual(Arrays.asList("MyService"), serviceClassNames);
9396
}
9497

9598
@Test
9699
public void testMissingArgument() throws Exception {
97-
tool.execute(new String[]{
98-
GetDiscoveryDocAction.NAME, option(EndpointsToolAction.OPTION_CLASS_PATH_SHORT),
99-
"classPath", option(EndpointsToolAction.OPTION_OUTPUT_DIR_SHORT), "outputDir"});
100+
tool.execute(new String[]{GetDiscoveryDocAction.NAME,
101+
option(EndpointsToolAction.OPTION_CLASS_PATH_SHORT), "classPath",
102+
option(EndpointsToolAction.OPTION_OUTPUT_DIR_SHORT), "outputDir",
103+
option(EndpointsToolAction.OPTION_HOSTNAME_SHORT), "foo.com"});
100104
assertTrue(usagePrinted);
101105
}
102106

@@ -105,6 +109,8 @@ public void testGetDiscoveryDoc() throws Exception {
105109
tool.execute(new String[]{GetDiscoveryDocAction.NAME,
106110
option(EndpointsToolAction.OPTION_CLASS_PATH_SHORT), "classPath",
107111
option(EndpointsToolAction.OPTION_OUTPUT_DIR_SHORT), "outputDir",
112+
option(EndpointsToolAction.OPTION_HOSTNAME_SHORT), "foo.com",
113+
option(EndpointsToolAction.OPTION_BASE_PATH_SHORT), "/api",
108114
"MyService", "MyService2"});
109115
assertFalse(usagePrinted);
110116
assertThat(Lists.newArrayList(classPath))
@@ -114,8 +120,9 @@ public void testGetDiscoveryDoc() throws Exception {
114120
.toURI()
115121
.toURL());
116122
assertEquals("outputDir", outputDirPath);
117-
assertEquals(EndpointsToolAction.DEFAULT_WAR_PATH, warPath);
118123
assertStringsEqual(Arrays.asList("MyService", "MyService2"), serviceClassNames);
119-
assertFalse(debugOutput);
124+
assertTrue(outputToDisk);
125+
assertThat(hostname).isEqualTo("foo.com");
126+
assertThat(basePath).isEqualTo("/api");
120127
}
121128
}

0 commit comments

Comments
 (0)