Skip to content

Commit fcc34c5

Browse files
authored
Add null check to task status + fix imports
1 parent b268ce0 commit fcc34c5

File tree

8 files changed

+39
-22
lines changed

8 files changed

+39
-22
lines changed

APIFiles/APIClient.go

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,6 @@ read_only: [optional] Login with Read Only permissions. This parameter is not co
240240
payload: [optional] More settings for the login command
241241
returns: APIResponse, error
242242
side-effects: updates the class's uid and server variables
243-
244243
*/
245244
func (c *ApiClient) ApiLogin(username string, password string, continueLastSession bool, domain string, readOnly bool, payload map[string]interface{}) (APIResponse, error) {
246245
credentials := map[string]interface{}{
@@ -309,15 +308,16 @@ command: the command is placed in the URL field
309308
payload: a JSON object (or a string representing a JSON object) with the command arguments
310309
sid: The Check Point session-id. when omitted use self.sid.
311310
waitForTask: determines the behavior when the API server responds with a "task-id".
311+
312312
by default, the function will periodically check the status of the task
313313
and will not return until the task is completed.
314314
when wait_for_task=False, it is up to the user to call the "show-task" API and check
315315
the status of the command.
316+
316317
useProxy: Determines if the user wants to use the proxy server and port provider.
317318
method: HTTP request method - POST by default
318319
return: APIResponse object
319320
side-effects: updates the class's uid and server variables
320-
321321
*/
322322
func (c *ApiClient) ApiCall(command string, payload map[string]interface{}, sid string, waitForTask bool, useProxy bool, method ...string) (APIResponse, error) {
323323
return c.apiCall(command, payload, sid, waitForTask, useProxy, false, method...)
@@ -549,7 +549,8 @@ func (c *ApiClient) apiCall(command string, payload map[string]interface{}, sid
549549
return res, nil
550550
}
551551

552-
/**
552+
/*
553+
*
553554
The APIs that return a list of objects are limited by the number of objects that they return.
554555
To get the full list of objects, there's a need to make repeated API calls each time using a different offset
555556
until all the objects are returned.
@@ -558,19 +559,25 @@ note: this function calls gen_api_query and iterates over the generator until it
558559
then returns.
559560
560561
command: name of API command. This command should be an API that returns an array of
562+
561563
objects (for example: show-hosts, show networks, ...)
564+
562565
details_level: query APIs always take a details-level argument.
566+
563567
possible values are "standard", "full", "uid"
568+
564569
container_key: name of the key that holds the objects in the JSON response (usually "objects").
565570
include_container_key: If set to False the 'data' field of the APIResponse object
571+
566572
will be a list of the wanted objects. Otherwise, the date field of the APIResponse will be a dictionary in the following
573+
567574
format: { container_key: [ List of the wanted objects], "total": size of the list}
568575
payload: a JSON object (or a string representing a JSON object) with the command arguments
569576
return: if include-container-key is False:
577+
570578
an APIResponse object whose .data member contains a list of the objects requested: [ , , , ...]
571579
if include-container-key is True:
572580
an APIResponse object whose .data member contains a dict: { container_key: [...], "total": n }
573-
574581
*/
575582
func (c *ApiClient) ApiQuery(command string, detailsLevel string, containerKey string, includeContainerKey bool, payload map[string]interface{}) (APIResponse, error) {
576583

@@ -617,7 +624,9 @@ This is in contrast to normal API calls that return only a limited number of obj
617624
This function can be used to show progress when requesting many objects (i.e. "Received x/y objects.")
618625
619626
command: name of API command. This command should be an API that returns an array of objects
627+
620628
(for example: show-hosts, show networks, ...)
629+
621630
details_level: query APIs always take a details-level argument. Possible values are "standard", "full", "uid"
622631
container_keys: the field in the .data dict that contains the objects
623632
payload: a JSON object (or a string representing a JSON object) with the command arguments
@@ -712,7 +721,8 @@ func (c *ApiClient) genApiQuery(command string, detailsLevel string, containerKe
712721
return serverResponse
713722
}
714723

715-
/**
724+
/*
725+
*
716726
When the server needs to perform an API call that may take a long time (e.g. run-script, install-policy,
717727
publish), the server responds with a 'task-id'.
718728
Using the show-task API it is possible to check on the status of this task until its completion.
@@ -759,10 +769,10 @@ func (c *ApiClient) waitForTask(taskId string) (APIResponse, error) {
759769
totalTasks := 0
760770
for _, task := range taskResult.GetData()["tasks"].([]interface{}) {
761771
totalTasks++
762-
if task.(map[string]interface{})["status"].(string) != InProgress {
772+
taskMap := task.(map[string]interface{})
773+
if taskMap["status"] != nil && taskMap["status"].(string) != InProgress {
763774
completedTasks++
764775
}
765-
766776
}
767777

768778
if completedTasks == totalTasks {
@@ -778,7 +788,8 @@ func (c *ApiClient) waitForTask(taskId string) (APIResponse, error) {
778788

779789
}
780790

781-
/**
791+
/*
792+
*
782793
The version of waitForTask function for the collection of tasks
783794
784795
task_objects: A list of task objects
@@ -823,7 +834,8 @@ func (c *ApiClient) waitForTasks(taskObjects []interface{}) APIResponse {
823834

824835
}
825836

826-
/**
837+
/*
838+
*
827839
This method checks if one of the tasks failed and if so, changes the response status to be False
828840
829841
task_result: api_response returned from "show-task" command
@@ -846,7 +858,8 @@ func checkTasksStatus(taskResult *APIResponse) {
846858
@===================@
847859
*/
848860

849-
/**
861+
/*
862+
*
850863
This function checks if the server's certificate is stored in the local fingerprints file.
851864
If the server's fingerprint is not found, an HTTPS connection is made to the server
852865
and the user is asked if he or she accepts the server's fingerprint.
@@ -928,7 +941,8 @@ func (c *ApiClient) loadFingerprintFromFile() (string, error) {
928941

929942
}
930943

931-
/**
944+
/*
945+
*
932946
This function takes the content of the file $FILENAME (which is a json file)
933947
and parses it's content to a map (from string to string)
934948
@@ -960,13 +974,16 @@ func (c *ApiClient) fpFileToMap() (map[string]string, error) {
960974

961975
}
962976

963-
/**
977+
/*
978+
*
964979
store a server's fingerprint into a local file.
965980
966981
server: the IP address/name of the Check Point management server.
967982
fingerprint: A SHA1 fingerprint of the server's certificate.
968983
filename: The file in which to store the certificates. The file will hold a JSON structure in which
984+
969985
the key is the server and the value is its fingerprint.
986+
970987
return: 'True' if everything went well. 'False' if there was some kind of error storing the fingerprint.
971988
*/
972989
func (c *ApiClient) saveFingerprintToFile(server string, fingerprint string) error {

Examples/add_access_rule.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package Examples
22

33
import (
4-
api "../APIFiles"
54
"fmt"
5+
api "github.com/cp-mgmt-api-go-sdk/APIFiles"
66
"os"
77
)
88

Examples/add_host.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package Examples
22

33
import (
4-
api "../APIFiles"
54
"fmt"
5+
api "github.com/cp-mgmt-api-go-sdk/APIFiles"
66
"os"
77
)
88

Examples/auto_publish.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package Examples
22

33
import (
4-
api "../APIFiles"
54
"fmt"
5+
api "github.com/cp-mgmt-api-go-sdk/APIFiles"
66
"os"
77
"strconv"
88
"time"

Examples/discard_sessions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package Examples
22

33
import (
4-
api "../APIFiles"
54
"fmt"
5+
api "github.com/cp-mgmt-api-go-sdk/APIFiles"
66
"os"
77
)
88

Examples/find_dup_ip.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package Examples
22

33
import (
4-
api "../APIFiles"
54
"fmt"
5+
api "github.com/cp-mgmt-api-go-sdk/APIFiles"
66
"os"
77
)
88

Examples/show_hosts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package Examples
22

33
import (
4-
api "../APIFiles"
54
"fmt"
5+
api "github.com/cp-mgmt-api-go-sdk/APIFiles"
66
"os"
77
)
88

main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
package main
22

33
import (
4-
"./Examples"
54
"fmt"
5+
"github.com/cp-mgmt-api-go-sdk/Examples"
66
"os"
77
)
88

9-
func main(){
9+
func main() {
1010
if len(os.Args) < 2 {
11-
fmt.Println("Operation not found. Optional operations are rule/discard/add_host/show_hosts/dup_ip/auto_publish")
11+
fmt.Println("cp-mgmt-api-go-sdk main : Operation not found. Optional operations are rule/discard/add_host/show_hosts/dup_ip/auto_publish")
1212
os.Exit(0)
1313
}
1414

1515
switch os.Args[1] {
1616
case "discard":
1717
Examples.DiscardSessions()
1818
case "rule":
19-
Examples.AddAccessRule ()
19+
Examples.AddAccessRule()
2020
case "add_host":
2121
Examples.AddHost()
2222
case "show_hosts":

0 commit comments

Comments
 (0)