forked from CheckPointSW/cp-mgmt-api-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto_publish.go
99 lines (79 loc) · 2.71 KB
/
auto_publish.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package Examples
import (
api "../APIFiles"
"fmt"
"os"
"strconv"
"time"
)
func AutoPublish() {
var apiServer string
var username string
var password string
fmt.Printf("Enter server IP address or hostname: ")
fmt.Scanln(&apiServer)
fmt.Printf("Enter username: ")
fmt.Scanln(&username)
fmt.Printf("Enter password: ")
fmt.Scanln(&password)
args := api.APIClientArgs(api.DefaultPort, "", "", apiServer, "", -1, "", false, false, "deb.txt", api.WebContext, api.TimeOut, api.SleepTime, "", "", api.AutoPublishBatchSize)
client := api.APIClient(args)
if ok, _ := client.CheckFingerprint(); !ok {
fmt.Println("Could not get the server's fingerprint - Check connectivity with the server")
os.Exit(1)
}
loginRes, err := client.ApiLogin(username, password,false, "", false, nil)
if err != nil {
fmt.Println("Login error")
os.Exit(1)
}
if !loginRes.Success {
fmt.Println("Login failed: " + loginRes.ErrorMsg)
os.Exit(1)
}
numOfThreads := 10
numOfObjectsToCreate := 20
threadNamePrefix := "auto-publish-thread"
fmt.Println("Start auto publish program. Number of threads " + strconv.Itoa(numOfThreads))
for i := 0; i < numOfThreads; i++ {
go run(threadNamePrefix + strconv.Itoa(i), numOfObjectsToCreate, client)
}
time.Sleep(time.Minute * 3)
client.ApiCallSimple("publish", map[string]interface{}{})
fmt.Println("Finished to create all objects")
deleteObjects(client, threadNamePrefix)
client.ApiCallSimple("logout", map[string]interface{}{})
fmt.Println("Auto publish example finished successfully")
}
func run(threadName string, numOfObjectsToCreate int, apiClient interface{}){
fmt.Println("Start thread -> " + threadName)
client := apiClient.(*api.ApiClient)
for i := 0; i < numOfObjectsToCreate; i++ {
groupName := threadName + "-group" + strconv.Itoa(i)
res, err := client.ApiCallSimple("add-group", map[string]interface{}{"name":groupName})
if err != nil {
fmt.Println("Error: " + err.Error())
}
if !res.Success {
fmt.Println("Failed to add group: " + res.ErrorMsg)
}
}
fmt.Println(threadName + " finished")
}
func deleteObjects(client *api.ApiClient, objPrefix string) {
res, _ := client.ApiQuery("show-groups", "standard", "", true, map[string]interface{}{"filter": objPrefix})
if res.Success {
client.ResetTotalCallsCounter()
objects := res.GetData()["objects"].([]map[string]interface{})
fmt.Println("Delete " + strconv.Itoa(len(objects)) + " objects...")
if len(objects) > 0 {
for i := 0 ; i < len(objects) ; i++ {
client.ApiCallSimple("delete-group", map[string]interface{}{"uid":objects[i]["uid"]})
}
client.ApiCallSimple("publish", map[string]interface{}{})
fmt.Println("Groups deleted")
}else{
fmt.Println("Not found groups to delete")
}
}
}