Skip to content

Commit b268ce0

Browse files
authored
Remove deprecated usage in the examples (#17)
1 parent afb5b18 commit b268ce0

File tree

8 files changed

+80
-83
lines changed

8 files changed

+80
-83
lines changed

APIFiles/APIClient.go

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@ import (
3131
)
3232

3333
const (
34-
InProgress string = "in progress"
35-
DefaultPort int = 443
36-
Limit int = 50
37-
Filename string = "fingerprints.json"
38-
TimeOut time.Duration = time.Second * 10
39-
SleepTime time.Duration = time.Second * 2
40-
GaiaContext string = "gaia_api"
41-
WebContext string = "web_api"
42-
DefaultProxyPort = -1
43-
DefaultProxyHost = ""
44-
AutoPublishBatchSize int = 100
34+
InProgress string = "in progress"
35+
DefaultPort int = 443
36+
Limit int = 50
37+
Filename string = "fingerprints.json"
38+
TimeOut time.Duration = time.Second * 10
39+
SleepTime time.Duration = time.Second * 2
40+
GaiaContext string = "gaia_api"
41+
WebContext string = "web_api"
42+
DefaultProxyPort = -1
43+
DefaultProxyHost = ""
44+
AutoPublishBatchSize int = 100
4545
)
4646

4747
// Check Point API Client (Management/GAIA)
@@ -66,15 +66,15 @@ type ApiClient struct {
6666
userAgent string
6767
cloudMgmtId string
6868
autoPublishBatchSize int
69-
activeCallsLock sync.Mutex
70-
autoPublishLock sync.Mutex
71-
totalCallsLock sync.Mutex
69+
activeCallsLock sync.Mutex
70+
autoPublishLock sync.Mutex
71+
totalCallsLock sync.Mutex
7272
duringPublish bool
73-
activeCallsCtr int
74-
totalCallsCtr int
73+
activeCallsCtr int
74+
totalCallsCtr int
7575
}
7676

77-
// Api Client constructor
77+
// ApiClient constructor
7878
// Input ApiClientArgs
7979
// Returns new client instance
8080
func APIClient(apiCA ApiClientArgs) *ApiClient {
@@ -212,7 +212,7 @@ func (c *ApiClient) DisableAutoPublish() {
212212
c.totalCallsCtr = 0
213213
}
214214

215-
// Deprecated: Do not use.
215+
// Deprecated: Do not use. Use ApiLogin instead
216216
func (c *ApiClient) Login(username string, password string, continueLastSession bool, domain string, readOnly bool, payload string) (APIResponse, error) {
217217
credentials := map[string]interface{}{
218218
"user": username,
@@ -221,7 +221,7 @@ func (c *ApiClient) Login(username string, password string, continueLastSession
221221
return c.commonLoginLogic(credentials, continueLastSession, domain, readOnly, make(map[string]interface{}))
222222
}
223223

224-
// Deprecated: Do not use.
224+
// Deprecated: Do not use. Use ApiLoginWithApiKey instead
225225
func (c *ApiClient) LoginWithApiKey(apiKey string, continueLastSession bool, domain string, readOnly bool, payload string) (APIResponse, error) {
226226
credentials := map[string]interface{}{
227227
"api-key": apiKey,
@@ -230,7 +230,7 @@ func (c *ApiClient) LoginWithApiKey(apiKey string, continueLastSession bool, dom
230230
}
231231

232232
/*
233-
Performs a 'login' API call to management server
233+
Performs login API call to the management server using username and password
234234
235235
username: Check Point admin name
236236
password: Check Point admin password
@@ -251,7 +251,7 @@ func (c *ApiClient) ApiLogin(username string, password string, continueLastSessi
251251
}
252252

253253
/*
254-
performs a 'login' API call to the management server
254+
Performs login API call to the management server using api key
255255
256256
api_key: Check Point api-key
257257
continue_last_session: [optional] It is possible to continue the last Check Point session
@@ -411,9 +411,9 @@ func (c *ApiClient) apiCall(command string, payload map[string]interface{}, sid
411411
if !internal && c.autoPublishBatchSize > 0 {
412412
waitToRun := true
413413
for waitToRun {
414-
if c.totalCallsCtr + 1 <= c.autoPublishBatchSize && !c.duringPublish {
414+
if c.totalCallsCtr+1 <= c.autoPublishBatchSize && !c.duringPublish {
415415
c.totalCallsLock.Lock()
416-
if c.totalCallsCtr + 1 <= c.autoPublishBatchSize && !c.duringPublish {
416+
if c.totalCallsCtr+1 <= c.autoPublishBatchSize && !c.duringPublish {
417417
c.totalCallsCtr++
418418
waitToRun = false
419419
}
@@ -519,9 +519,9 @@ func (c *ApiClient) apiCall(command string, payload map[string]interface{}, sid
519519

520520
if !internal && c.autoPublishBatchSize > 0 {
521521
c.decreaseActiveCalls()
522-
if c.totalCallsCtr > 0 && c.totalCallsCtr % c.autoPublishBatchSize == 0 && !c.duringPublish {
522+
if c.totalCallsCtr > 0 && c.totalCallsCtr%c.autoPublishBatchSize == 0 && !c.duringPublish {
523523
c.autoPublishLock.Lock()
524-
if c.totalCallsCtr > 0 && c.totalCallsCtr % c.autoPublishBatchSize == 0 && !c.duringPublish {
524+
if c.totalCallsCtr > 0 && c.totalCallsCtr%c.autoPublishBatchSize == 0 && !c.duringPublish {
525525
c.duringPublish = true
526526
c.autoPublishLock.Unlock()
527527
for c.activeCallsCtr > 0 {
@@ -531,16 +531,16 @@ func (c *ApiClient) apiCall(command string, payload map[string]interface{}, sid
531531
}
532532
// Going to publish
533533
fmt.Println("Start auto publish...")
534-
publishRes, _ := c.apiCall("publish", map[string]interface{}{},c.GetSessionID(),true,c.IsProxyUsed(), true)
534+
publishRes, _ := c.apiCall("publish", map[string]interface{}{}, c.GetSessionID(), true, c.IsProxyUsed(), true)
535535

536536
if !publishRes.Success {
537537
fmt.Println("Auto publish failed. Message: " + publishRes.ErrorMsg)
538-
}else{
538+
} else {
539539
fmt.Println("Auto publish finished successfully")
540540
}
541541
c.totalCallsCtr = 0
542542
c.duringPublish = false
543-
}else{
543+
} else {
544544
c.autoPublishLock.Unlock()
545545
}
546546
}

APIFiles/APIClientArgs.go

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package api_go_sdk
22

33
import "time"
44

5-
// Api Client arguments to init a new instance
5+
// ApiClient arguments to init a new instance
66
type ApiClientArgs struct {
77
Port int
88
Fingerprint string
@@ -16,16 +16,15 @@ type ApiClientArgs struct {
1616
AcceptServerCertificate bool
1717
DebugFile string
1818
Context string
19-
Timeout time.Duration
20-
Sleep time.Duration
21-
UserAgent string
19+
Timeout time.Duration
20+
Sleep time.Duration
21+
UserAgent string
2222
CloudMgmtId string
23-
AutoPublishBatchSize int
23+
AutoPublishBatchSize int
2424
}
2525

2626
/*
27-
Init a new APIClientArgs
28-
Construct a new ApiClientArgs instance with the given parameters.
27+
Init new APIClientArgs
2928
3029
Port: the port that is being used
3130
Fingerprint: server's fingerprint
@@ -42,26 +41,25 @@ Timeout: HTTP Client timeout value
4241
Sleep: Interval size in seconds of the task update
4342
UserAgent: User agent will be use in api call request header
4443
CloudMgmtId: Smart-1 Cloud management UID
45-
AutoPublishBatchSize: Number of batch size for auto publish
44+
AutoPublishBatchSize: Publish will run automatically on every 'X' number of api calls, set -1 (or any negative number) to disable the feature
4645
*/
4746
func APIClientArgs(port int, fingerprint string, sid string, server string, proxyHost string, proxyPort int, apiVersion string, ignoreServerCertificate bool, acceptServerCertificate bool, debugFile string, context string, timeout time.Duration, sleep time.Duration, userAgent string, cloudMgmtId string, autoPublishBatchSize int) ApiClientArgs {
48-
4947
return ApiClientArgs{
50-
Port: port,
51-
Fingerprint: fingerprint,
52-
Sid: sid,
53-
Server: server,
54-
ProxyHost: proxyHost,
55-
ProxyPort: proxyPort,
56-
ApiVersion: apiVersion,
48+
Port: port,
49+
Fingerprint: fingerprint,
50+
Sid: sid,
51+
Server: server,
52+
ProxyHost: proxyHost,
53+
ProxyPort: proxyPort,
54+
ApiVersion: apiVersion,
5755
IgnoreServerCertificate: ignoreServerCertificate,
5856
AcceptServerCertificate: acceptServerCertificate,
59-
DebugFile: debugFile,
60-
Context: context,
61-
Timeout: timeout,
62-
Sleep: sleep,
63-
UserAgent: userAgent,
64-
CloudMgmtId: cloudMgmtId,
65-
AutoPublishBatchSize: autoPublishBatchSize,
57+
DebugFile: debugFile,
58+
Context: context,
59+
Timeout: timeout,
60+
Sleep: sleep,
61+
UserAgent: userAgent,
62+
CloudMgmtId: cloudMgmtId,
63+
AutoPublishBatchSize: autoPublishBatchSize,
6664
}
6765
}

Examples/add_access_rule.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,12 @@ func AddAccessRule() {
3838
os.Exit(1)
3939
}
4040

41-
loginRes, err := client.Login(username, password, false, "", false, "")
41+
loginRes, err := client.ApiLogin(username, password, false, "", false, nil)
4242
if err != nil {
4343
print("Login error.\n")
4444
os.Exit(1)
4545
}
4646

47-
48-
4947
if loginRes.Success == false {
5048
print("Login failed:\n" + loginRes.ErrorMsg)
5149
os.Exit(1)

Examples/add_host.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func AddHost() {
2929
os.Exit(1)
3030
}
3131

32-
loginRes, err := client.Login(username, password,false, "", false, "")
32+
loginRes, err := client.ApiLogin(username, password, false, "", false, nil)
3333
if err != nil {
3434
print("Login error.\n")
3535
os.Exit(1)

Examples/auto_publish.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@ import (
88
"time"
99
)
1010

11+
// This program demonstrates auto publish feature
12+
// Publish will run automatically on every 'X' number of api calls, set 'autoPublishBatchSize' to -1 (or any negative number) to disable the feature
13+
// In this program, 10 threads runs in parallel, each thread create 20 group objects, auto publish runs on every 100 created objects
1114
func AutoPublish() {
15+
numOfThreads := 10 // Total number of threads
16+
numOfObjectsToCreate := 20 // Total number of objects to create by each thread
17+
autoPublishBatchSize := api.AutoPublishBatchSize // Call publish on every 100 api calls
18+
threadNamePrefix := "auto-publish-thread"
1219
var apiServer string
1320
var username string
1421
var password string
@@ -22,7 +29,7 @@ func AutoPublish() {
2229
fmt.Printf("Enter password: ")
2330
fmt.Scanln(&password)
2431

25-
args := api.APIClientArgs(api.DefaultPort, "", "", apiServer, "", -1, "", false, false, "deb.txt", api.WebContext, api.TimeOut, api.SleepTime, "", "", api.AutoPublishBatchSize)
32+
args := api.APIClientArgs(api.DefaultPort, "", "", apiServer, "", -1, "", false, false, "deb.txt", api.WebContext, api.TimeOut, api.SleepTime, "", "", autoPublishBatchSize)
2633

2734
client := api.APIClient(args)
2835

@@ -31,7 +38,7 @@ func AutoPublish() {
3138
os.Exit(1)
3239
}
3340

34-
loginRes, err := client.ApiLogin(username, password,false, "", false, nil)
41+
loginRes, err := client.ApiLogin(username, password, false, "", false, nil)
3542
if err != nil {
3643
fmt.Println("Login error")
3744
os.Exit(1)
@@ -42,34 +49,30 @@ func AutoPublish() {
4249
os.Exit(1)
4350
}
4451

45-
numOfThreads := 10
46-
numOfObjectsToCreate := 20
47-
threadNamePrefix := "auto-publish-thread"
48-
4952
fmt.Println("Start auto publish program. Number of threads " + strconv.Itoa(numOfThreads))
5053
for i := 0; i < numOfThreads; i++ {
51-
go run(threadNamePrefix + strconv.Itoa(i), numOfObjectsToCreate, client)
54+
go run(threadNamePrefix+strconv.Itoa(i), numOfObjectsToCreate, client)
5255
}
5356

5457
time.Sleep(time.Minute * 3)
5558

56-
client.ApiCallSimple("publish", map[string]interface{}{})
59+
_, _ = client.ApiCallSimple("publish", map[string]interface{}{}) // publish leftovers if exists
5760

5861
fmt.Println("Finished to create all objects")
5962

6063
deleteObjects(client, threadNamePrefix)
6164

62-
client.ApiCallSimple("logout", map[string]interface{}{})
65+
_, _ = client.ApiCallSimple("logout", map[string]interface{}{})
6366

6467
fmt.Println("Auto publish example finished successfully")
6568
}
6669

67-
func run(threadName string, numOfObjectsToCreate int, apiClient interface{}){
70+
func run(threadName string, numOfObjectsToCreate int, apiClient interface{}) {
6871
fmt.Println("Start thread -> " + threadName)
6972
client := apiClient.(*api.ApiClient)
7073
for i := 0; i < numOfObjectsToCreate; i++ {
7174
groupName := threadName + "-group" + strconv.Itoa(i)
72-
res, err := client.ApiCallSimple("add-group", map[string]interface{}{"name":groupName})
75+
res, err := client.ApiCallSimple("add-group", map[string]interface{}{"name": groupName})
7376
if err != nil {
7477
fmt.Println("Error: " + err.Error())
7578
}
@@ -87,12 +90,12 @@ func deleteObjects(client *api.ApiClient, objPrefix string) {
8790
objects := res.GetData()["objects"].([]map[string]interface{})
8891
fmt.Println("Delete " + strconv.Itoa(len(objects)) + " objects...")
8992
if len(objects) > 0 {
90-
for i := 0 ; i < len(objects) ; i++ {
91-
client.ApiCallSimple("delete-group", map[string]interface{}{"uid":objects[i]["uid"]})
93+
for i := 0; i < len(objects); i++ {
94+
client.ApiCallSimple("delete-group", map[string]interface{}{"uid": objects[i]["uid"]})
9295
}
93-
client.ApiCallSimple("publish", map[string]interface{}{})
96+
client.ApiCallSimple("publish", map[string]interface{}{}) // publish leftovers if exists
9497
fmt.Println("Groups deleted")
95-
}else{
98+
} else {
9699
fmt.Println("Not found groups to delete")
97100
}
98101
}

Examples/discard_sessions.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func DiscardSessions() {
3030
os.Exit(1)
3131
}
3232

33-
loginRes, err := client.Login(username, password, false, "", false, "")
33+
loginRes, err := client.ApiLogin(username, password, false, "", false, nil)
3434
if err != nil {
3535
print("Login error.\n")
3636
os.Exit(1)
@@ -77,9 +77,9 @@ func DiscardSessions() {
7777
discardRes, _ = client.ApiCall("discard", map[string]interface{}{"uid": sessionObj.(map[string]interface{})["uid"]}, "", false, false)
7878

7979
if discardRes.Success {
80-
fmt.Println("Session " + sessionObj.(map[string]interface{})["uid"].(string)+ " discarded successfully")
80+
fmt.Println("Session " + sessionObj.(map[string]interface{})["uid"].(string) + " discarded successfully")
8181
} else {
82-
fmt.Println("Session " + sessionObj.(map[string]interface{})["uid"].(string)+ " failed to discard")
82+
fmt.Println("Session " + sessionObj.(map[string]interface{})["uid"].(string) + " failed to discard")
8383
}
8484
}
8585

Examples/find_dup_ip.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func DupIp() {
2929
os.Exit(1)
3030
}
3131

32-
loginRes, err := client.Login(username, pass, false, "", false, "")
32+
loginRes, err := client.ApiLogin(username, pass, false, "", false, nil)
3333
if err != nil {
3434
print("Login error.\n")
3535
os.Exit(1)
@@ -53,7 +53,7 @@ func DupIp() {
5353
//dupIpSlice - a collection of the duplicate IP addresses in all the host objects.
5454
var dupIpSlice []string
5555

56-
for _, host := range showHostsRes.GetData(){
56+
for _, host := range showHostsRes.GetData() {
5757
ipaddr := host.(map[string]interface{})["ipv4-address"].(string)
5858
if ipaddr == "" {
5959
print(host.(map[string]interface{})["name"].(string) + " has no IPv4 address. Skipping...")
@@ -65,7 +65,7 @@ func DupIp() {
6565
if _, ok := objDictionary[ipaddr]; ok {
6666
ipExists := false
6767
for _, ip := range dupIpSlice {
68-
if ip == ipaddr{
68+
if ip == ipaddr {
6969
ipExists = true
7070
break
7171
}
@@ -91,12 +91,12 @@ func DupIp() {
9191
}
9292

9393
//for every duplicate ip - print hosts with that ip:
94-
for _, dupIp := range dupIpSlice {
94+
for _, dupIp := range dupIpSlice {
9595

9696
fmt.Println("\nIP Address: " + dupIp + "")
9797
fmt.Println("-------------------------------")
9898

99-
for _, hostData := range objDictionary[dupIp]{
99+
for _, hostData := range objDictionary[dupIp] {
100100

101101
fmt.Println("host name: " + hostData["name"] + " host uid: " + hostData["uid"])
102102
//fmt.Println(hostData[1])
@@ -105,5 +105,4 @@ func DupIp() {
105105

106106
}
107107

108-
109108
}

0 commit comments

Comments
 (0)