Skip to content

Commit d9f4b97

Browse files
committed
Add support to smart-1 cloud
1 parent 18c57bc commit d9f4b97

File tree

3 files changed

+56
-7
lines changed

3 files changed

+56
-7
lines changed

mgmt_api_lib/src/main/java/com/checkpoint/mgmt_api/client/ApiClient.java

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,27 @@ public ApiClient(ApiClientArgs args) {
120120
* @throws ApiClientException if error occurs while preforming an API call
121121
*/
122122
public ApiLoginResponse login(String serverIpAddress, String payload) throws ApiClientException {
123+
return login(serverIpAddress, payload, null);
124+
}
123125

126+
/**
127+
* This function uses the login command to login into the management server.
128+
*
129+
* @param serverIpAddress The IP address or name of the Check Point Management Server.
130+
* @param payload String representing a JSON object containing the login command's arguments.
131+
* @param cloudMgmtId The Smart-1 Cloud management UID.
132+
*
133+
* @return {@link ApiResponse} object
134+
*
135+
* @throws ApiClientException if error occurs while preforming an API call
136+
*/
137+
public ApiLoginResponse login(String serverIpAddress, String payload, String cloudMgmtId) throws ApiClientException {
124138
if(serverIpAddress == null || serverIpAddress.isEmpty()){
125139
throw new ApiClientException("Error: server IP address is invalid");
126140
}
127141

128142
int port = portResolver.getPort(false);
129-
ApiLoginResponse loginResponse = new ApiLoginResponse(serverIpAddress , OK_RESPONSE_CODE, port, new JSONObject());
143+
ApiLoginResponse loginResponse = new ApiLoginResponse(serverIpAddress , OK_RESPONSE_CODE, port, new JSONObject(), cloudMgmtId);
130144
return (ApiLoginResponse) apiCall(loginResponse,"login", payload);
131145
}
132146

@@ -135,18 +149,32 @@ public ApiLoginResponse login(String serverIpAddress, String payload) throws Api
135149
*
136150
* @param serverIpAddress The IP address or name of the Check Point Management Server.
137151
* @param payload JSON object containing the login command's arguments.
152+
* @param cloudMgmtId The Smart-1 Cloud management UID.
138153
*
139154
* @return {@link ApiResponse} object
140155
*
141156
* @throws ApiClientException if error occurs while preforming an API call
142157
*/
143-
public ApiLoginResponse login(String serverIpAddress, JSONObject payload) throws ApiClientException {
144-
158+
public ApiLoginResponse login(String serverIpAddress, JSONObject payload, String cloudMgmtId) throws ApiClientException {
145159
if(payload == null){
146160
throw new ApiClientException("Error: payload is invalid");
147161
}
148162
String pay = payload.toString();
149-
return login(serverIpAddress,pay);
163+
return login(serverIpAddress, pay, cloudMgmtId);
164+
}
165+
166+
/**
167+
* This function uses login command to login into the management server.
168+
*
169+
* @param serverIpAddress The IP address or name of the Check Point Management Server.
170+
* @param payload JSON object containing the login command's arguments.
171+
*
172+
* @return {@link ApiResponse} object
173+
*
174+
* @throws ApiClientException if error occurs while preforming an API call
175+
*/
176+
public ApiLoginResponse login(String serverIpAddress, JSONObject payload) throws ApiClientException {
177+
return login(serverIpAddress,payload,null);
150178
}
151179

152180
/**
@@ -242,7 +270,12 @@ public ApiResponse apiCall(ApiLoginResponse loginResponse, String command, Strin
242270
try {
243271
try {
244272
// 1) Establish Connection
245-
url = new URL(URL_PROTOCOL, loginResponse.getServerIP(), loginResponse.getPort(), CONTEXT + command);
273+
String urlSuffix = "";
274+
if(loginResponse.getCloudMgmtId() != null && !loginResponse.getCloudMgmtId().isEmpty()) {
275+
urlSuffix = "/" + loginResponse.getCloudMgmtId() + "/";
276+
}
277+
urlSuffix += CONTEXT + command;
278+
url = new URL(URL_PROTOCOL, loginResponse.getServerIP(), loginResponse.getPort(), urlSuffix);
246279
connection = establishConnection(loginResponse, command, url);
247280
}
248281
catch (Exception e) {
@@ -269,7 +302,7 @@ public ApiResponse apiCall(ApiLoginResponse loginResponse, String command, Strin
269302
StringBuilder response = readResponse(input);
270303
if (LOGIN_CMD.equals(command)) {
271304
res = new ApiLoginResponse(loginResponse.getServerIP(), connection.getResponseCode(),
272-
loginResponse.getPort(), (JSONObject)UtilClass.convertToJson(response.toString()));
305+
loginResponse.getPort(), (JSONObject)UtilClass.convertToJson(response.toString()), loginResponse.getCloudMgmtId());
273306

274307
// 4) When the command is 'login', hiding the password so that it would not appear in the debug file.
275308
data = changePasswordInData(data);

mgmt_api_lib/src/main/java/com/checkpoint/mgmt_api/client/ApiLoginResponse.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public final class ApiLoginResponse extends ApiResponse{
3030

3131
//Management server name or IP-address
3232
final private String serverIpAddress;
33+
final private String cloudMgmtId;
3334

3435
//Port of connection
3536
final private int port;
@@ -38,10 +39,15 @@ public final class ApiLoginResponse extends ApiResponse{
3839
final private String apiVersion;
3940

4041
public ApiLoginResponse(String serverIP, int statusCode, int port, JSONObject responseBody) {
42+
this(serverIP, statusCode, port, responseBody, null);
43+
}
44+
45+
public ApiLoginResponse(String serverIP, int statusCode, int port, JSONObject responseBody, String cloudMgmtId) {
4146

4247
super(statusCode,responseBody);
4348

4449
this.serverIpAddress = serverIP;
50+
this.cloudMgmtId = cloudMgmtId;
4551
this.port = port;
4652

4753
if (getPayload().containsKey("sid")) {
@@ -92,4 +98,11 @@ public int getPort()
9298
* @return The version
9399
*/
94100
public String getApiVersion(){return apiVersion;}
101+
102+
/**
103+
* Gets the Smart-1 Cloud management UID
104+
*
105+
* @return The Smart-1 Cloud management UID
106+
*/
107+
public String getCloudMgmtId() { return cloudMgmtId; }
95108
}

samples/clone_host/src/main/java/com/checkpoint/mgmt_api/examples/CloneHostExample.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@ public static void main(String[] args) {
3535

3636
//Management server IP address
3737
String server = "127.0.0.1";
38+
String cloudMgmtId = "aa3ce335-801a-4fc4-ba78-56cd20300d50";
3839

3940
//Login credentials
4041
String username = "username";
4142
String password = "password";
43+
String apiKey = "api-key";
4244

4345
//The name of the original Host object to be cloned
4446
String origHost = "originalHost";
@@ -69,9 +71,10 @@ public static void main(String[] args) {
6971
JSONObject loginPayload = new JSONObject();
7072
loginPayload.put("user", username);
7173
loginPayload.put("password",password);
74+
loginPayload.put("api-key", apiKey);
7275

7376
try {
74-
loginResponse = client.login(server,loginPayload);
77+
loginResponse = client.login(server, loginPayload, cloudMgmtId);
7578
} catch (ApiClientException e) {
7679
System.out.println(e.getMessage());
7780
System.exit(1);

0 commit comments

Comments
 (0)