Skip to content

Commit

Permalink
Merge pull request #105 from malinthaprasan/master
Browse files Browse the repository at this point in the history
Make project name a default parameter and few other improvements
  • Loading branch information
malinthaprasan authored Jun 29, 2018
2 parents 2a8c6ac + 2b92144 commit b20c7ca
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 72 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ Following is the structure of the label generated when running micro-gw setup co
```bash
micro-gw-<label>
├── bin (The binary scripts of the micro-gateway distribution)
│ └── micro-gw.sh
│ └── gateway
├── conf (micro gateway distribution configuration)
│ └── micro-gw.conf
├── exec (generated balx ballerina executable for the APIs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import java.io.IOException;
import java.io.PrintStream;
import java.util.List;

/**
* This class represents the "build" command and it holds arguments and flags specified by the user.
Expand All @@ -40,7 +41,10 @@ public class BuildCmd implements GatewayLauncherCmd {
@Parameter(names = "--java.debug", hidden = true)
private String javaDebugPort;

@Parameter(names = { "-n", "--project" }, hidden = true, required = true)
@SuppressWarnings("unused")
@Parameter(hidden = true, required = true)
private List<String> mainArgs;

private String projectName;

@Parameter(names = { "--help", "-h", "?" }, hidden = true, description = "for more information")
Expand All @@ -54,6 +58,7 @@ public void execute() {
}

try {
projectName = GatewayCmdUtils.getProjectName(mainArgs);
String projectRoot = GatewayCmdUtils.getUserDir();
GatewayCmdUtils.createLabelGWDistribution(projectRoot, projectName);
outStream.println("Build success");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@
public class SetupCmd implements GatewayLauncherCmd {
private static final Logger logger = LoggerFactory.getLogger(SetupCmd.class);
private static PrintStream outStream = System.err;

@SuppressWarnings("unused")
@Parameter(hidden = true, required = true)
private List<String> mainArgs;

@SuppressWarnings("unused")
@Parameter(names = "--java.debug", hidden = true)
private String javaDebugPort;
Expand All @@ -78,6 +83,7 @@ public class SetupCmd implements GatewayLauncherCmd {
@Parameter(names = { "-p", "--password" }, hidden = true)
private String password;

@SuppressWarnings("unused")
@Parameter(names = { "-l", "--label" }, hidden = true)
private String label;

Expand All @@ -90,18 +96,18 @@ public class SetupCmd implements GatewayLauncherCmd {
@Parameter(names = { "-w", "--truststore-pass" }, hidden = true)
private String trustStorePassword;

@Parameter(names = { "-n", "--project" }, hidden = true, required = true)
private String projectName;

@Parameter(names = { "-c", "--config" }, hidden = true)
private String configPath;

@SuppressWarnings("unused")
@Parameter(names = { "-a", "--api-name" }, hidden = true)
private String apiName;

@SuppressWarnings("unused")
@Parameter(names = { "-v", "--version" }, hidden = true)
private String version;

@SuppressWarnings("unused")
@Parameter(names = { "-f", "--force" }, hidden = true, arity = 0)
private boolean isForcefully;

Expand All @@ -115,6 +121,7 @@ public void execute() {
String clientID;
String workspace = GatewayCmdUtils.getUserDir();

String projectName = GatewayCmdUtils.getProjectName(mainArgs);
validateAPIGetRequestParams(label, apiName, version);

if (StringUtils.isEmpty(configPath)) {
Expand Down Expand Up @@ -308,11 +315,15 @@ public void execute() {
GatewayCmdUtils.saveConfig(newConfig, configPath);
}

if (!changesDetected) {
outStream.println(
"No changes received from the server since the previous setup."
+ " If you already have a built distribution, it can be reused.");
}
outStream.println("Setting up project " + projectName + " successful.");

//There should not be any logic after this system exit
if (!changesDetected) {
outStream.println(
"No changes received from the server. If you already have a built distribution, it can be reused.");
Runtime.getRuntime().exit(GatewayCliConstants.EXIT_CODE_NOT_MODIFIED);
}
}
Expand All @@ -334,8 +345,8 @@ private void validateAPIGetRequestParams(String label, String apiName, String ve
throw GatewayCmdUtils.createUsageException(
"Either label (-l <label>) or API name (-a <api-name>) with version (-v <version>) "
+ "should be provided."
+ "\n\nEx:\tmicro-gw setup -l accounts -n accounts-project"
+ "\n\tmicro-gw setup -a Pizzashack -v 1.0.0 -n pizzashack-project");
+ "\n\nEx:\tmicro-gw setup accounts-project -l accounts"
+ "\n\tmicro-gw setup pizzashack-project -a Pizzashack -v 1.0.0");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class GatewayCliConstants {
public static final String GW_DIST_FILTERS = "filters";
public static final String GW_DIST_RUNTIME = "runtime";
public static final String GW_DIST_EXEC = "exec";
public static final String GW_DIST_SH = "micro-gw.sh";
public static final String GW_DIST_SH = "gateway";
public static final String GW_DIST_SH_PATH = "distribution" + File.separator + GW_DIST_BIN + File.separator + GW_DIST_SH;
public static final String GW_DIST_EXTENSION_FILTER = "extension_filter.bal";
public static final String GW_DIST_CONF_FILE = "micro-gw.conf";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.slf4j.LoggerFactory;
import org.wso2.apimgt.gateway.cli.constants.TokenManagementConstants;
import org.wso2.apimgt.gateway.cli.exception.CLIInternalException;
import org.wso2.apimgt.gateway.cli.exception.CLIRuntimeException;
import org.wso2.apimgt.gateway.cli.oauth.builder.DCRRequestBuilder;
import org.wso2.apimgt.gateway.cli.oauth.builder.OAuthTokenRequestBuilder;
import org.wso2.apimgt.gateway.cli.utils.TokenManagementUtil;
Expand All @@ -32,6 +33,7 @@
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

public class OAuthServiceImpl implements OAuthService {
private static final Logger logger = LoggerFactory.getLogger(OAuthServiceImpl.class);
Expand Down Expand Up @@ -69,7 +71,11 @@ public String generateAccessToken(String tokenEndpoint, String username, char[]
throw new CLIInternalException("Error occurred while getting token. Status code: " + responseCode);
}
} catch (IOException e) {
throw new CLIInternalException("Error occurred while communicate with token endpoint " + tokenEndpoint, e);
String serverUrl = getServerUrl(tokenEndpoint);
throw new CLIRuntimeException(
"Error occurred while trying to connect with server. Is the server running at " + serverUrl + "?",
"Error occurred while trying to connect with token endpoint: " + tokenEndpoint, 1,
e);
} finally {
if (urlConn != null) {
urlConn.disconnect();
Expand Down Expand Up @@ -101,27 +107,41 @@ public String[] generateClientIdAndSecret(String dcrEndpoint, String username, c
urlConn.setRequestProperty(TokenManagementConstants.AUTHORIZATION,
TokenManagementConstants.BASIC + " " + clientEncoded);
urlConn.getOutputStream().write(requestBody.getBytes(TokenManagementConstants.UTF_8));
logger.debug("DCR url: {}", dcrEndpoint);
logger.trace("Request body for DCR call: {}", requestBody);
int responseCode = urlConn.getResponseCode();
if (responseCode == 200) { //If the DCR call is success
String responseStr = TokenManagementUtil.getResponseString(urlConn.getInputStream());
logger.debug("Received response status code for DCR call: {}", responseCode);
logger.trace("Received response body for DCR call: {}", responseStr);
JsonNode rootNode = mapper.readTree(responseStr);
JsonNode clientIdNode = rootNode.path(TokenManagementConstants.CLIENT_ID);
JsonNode clientSecretNode = rootNode.path(TokenManagementConstants.CLIENT_SECRET);
String clientId = clientIdNode.asText();
String clientSecret = clientSecretNode.asText();
String[] clientInfo = { clientId, clientSecret };
logger.debug("Successfully received client id:{} from DCR endpoint", clientId);
return clientInfo;
} else { //If DCR call fails
logger.error("Error occurred while creating oAuth application. Status code: {} ", responseCode);
throw new CLIInternalException("Error occurred while creating oAuth application");
throw new CLIInternalException(
"Error occurred while creating oAuth application Status code: " + responseCode);
}
} catch (IOException e) {
logger.error("Error occurred while communicate with DCR endpoint {}", dcrEndpoint, e);
throw new CLIInternalException("Error occurred while communicate with DCR endpoint", e);
String serverUrl = getServerUrl(dcrEndpoint);
throw new CLIRuntimeException(
"Error occurred while trying to connect with server. Is the server running at " + serverUrl + "?",
"Error occurred while communicate with DCR endpoint: " + dcrEndpoint, 1,
e);
} finally {
if (urlConn != null) {
urlConn.disconnect();
}
}
}

private String getServerUrl(String dcrEndpoint) {
String[] serverUrlParts = dcrEndpoint.split("/", 4);
return String.join("/",
Arrays.copyOfRange(serverUrlParts, 0, serverUrlParts.length >= 3 ? 3 : serverUrlParts.length));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.wso2.apimgt.gateway.cli.utils;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.ballerinalang.config.cipher.AESCipherTool;
import org.ballerinalang.config.cipher.AESCipherToolException;
import org.slf4j.Logger;
Expand All @@ -26,6 +27,7 @@
import org.wso2.apimgt.gateway.cli.config.TOMLConfigParser;
import org.wso2.apimgt.gateway.cli.constants.GatewayCliConstants;
import org.wso2.apimgt.gateway.cli.exception.CLIInternalException;
import org.wso2.apimgt.gateway.cli.exception.CLIRuntimeException;
import org.wso2.apimgt.gateway.cli.exception.CliLauncherException;
import org.wso2.apimgt.gateway.cli.exception.ConfigParserException;
import org.wso2.apimgt.gateway.cli.model.config.Config;
Expand All @@ -43,6 +45,8 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Collections;
import java.util.List;

public class GatewayCmdUtils {

Expand Down Expand Up @@ -369,6 +373,21 @@ public static void storeResourceHashesFileContent(String content) throws IOExcep
writeContent(content, pathFile);
}

/**
* Validate the list of main args and returns the first element as the project name
*
* @param mainArgs List of main args provided to the command
* @return first element
*/
public static String getProjectName (List<String> mainArgs) {
if (mainArgs.size() != 1) {
throw new CLIRuntimeException("Only one argument accepted as the project name, "
+ "but provided: " + String.join(",", mainArgs));
} else {
return mainArgs.get(0);
}
}

/**
* Get resource hash holder file path
*
Expand Down Expand Up @@ -446,7 +465,7 @@ private static void copyTargetDistBalx(String projectRoot, String labelName) thr
if (balxSourceFile.exists()) {
FileUtils.copyFile(balxSourceFile, gatewayDistExecPathFile);
} else {
System.err.println(labelName + ".balx could not be found in " + labelTargetPath);
throw new CLIInternalException(labelName + ".balx could not be found in " + labelTargetPath);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,14 @@
NAME
micro-gw run - run command use to start services generated from the
from api definitions of given label. Using build command, it's generating the binary(balx)
sources of given label. Run command is used to start the services
micro-gw build - The build command generates a gateway distribution using the given project resources.

SYNOPSIS
micro-gw run [-l|--label] [args...]
micro-gw build <project>

DESCRIPTION
Run command executes services generated for the APIs specified for the given label

It's required run build command to compile a source and
provide the generated binary file (.balx file)

OPTIONS

--n <project-name>
--project <project-name>
Name of the project that should run the generated services from binary(balx) source

DEFAULT BEHAVIOR
Runs the services generated for given label
The build command generates a gateway distribution using the resources in the given project. The project should exist
in the current working directory and should be previously created using "micro-gw setup" command.

EXAMPLES
Run the services of given label
$ micro-gw build -l accounts
$ micro-gw build accounts-project

Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ NAME
micro-gw help setup - set up the environment and generate the ballerina sources from the APIs of given label

SYNOPSIS
micro-gw setup [-u|--username] [-p|--password] [-l|--label]
[-o|--overwrite] [--path] [args...]
micro-gw setup <project> <[-l|--label] | [-a|--api-name] & [-v|--version]> [-u|--username] [-p|--password]
[-s|--server-url] [-c|--config] [-t|--trust-store] [-w|--truststore-pass]

DESCRIPTION
Setup command is used to create ballerina sources from api definition of given label. Setup command take username
Expand All @@ -15,19 +15,21 @@ OPTIONS

-l <label-name>
--label <label-name>
<Optional>
Name of the label which compile ballerina sources should be generated

-u
--username
<Optional>
Username of the user
-p
--password
<Optional>
Password of the user

--path
Path to the project location

-s
--server-url
<Optional>
APIM base url assuming all the portals are running in same node. By default base-url is https://localhost:9443/

-t
Expand All @@ -36,28 +38,31 @@ OPTIONS
Path to the trustStore file
By default consider <CLI_HOME>/lib/platform/bre/security/ballerinaTruststore.p12

-s
-w
--truststore-pass
<Optional>
Password for the given trustStore
By default use default trustStore password

-n
--project
<Optional>
Project name
If not present consider label as project name

-c
--config
<Optional>
Path to the cli configuration file
Be default use the <CLI_HOME>/resources/conf/cli-config.toml


DEFAULT BEHAVIOR
Generate the ballerina sources from given label along with the policies configured in the deployment

-a
--api-name
<Optional>
Name of the API

-v
--version
<Optional>
Version of the API

EXAMPLES
Setup the project
$ micro-gw.sh setup -l accounts --path /home/services
Setup the project using a label
$ micro-gw setup accounts-project -l accounts

Setup the project using a single API
$ micro-gw setup pizzashack-project -a PizzaShackAPI -v 1.0.0
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public type ThrottleFilter object {
@Return { value: "FilterResult: Authorization result to indicate if the request can proceed or not" }
public function filterRequest(http:Listener listener, http:Request request, http:FilterContext context) returns
boolean {
log:printDebug("Processing request in ThrottleFilter");
//Throttle Tiers
string applicationLevelTier;
string subscriptionLevelTier;
Expand Down Expand Up @@ -108,6 +109,7 @@ public type ThrottleFilter object {
//Publish throttle event to internal policies
RequestStreamDTO throttleEvent = generateThrottleEvent(request, context, keyvalidationResult);
publishNonThrottleEvent(throttleEvent);
log:printDebug("Request is not throttled");
return true;
}

Expand Down
Loading

0 comments on commit b20c7ca

Please sign in to comment.