From a3d9f9cd811c3bec415af6587704b5461eff9834 Mon Sep 17 00:00:00 2001 From: "pooja.gandhi" Date: Tue, 15 Oct 2019 22:54:24 -0700 Subject: [PATCH 1/2] GET all NetBackup Jobs and Jobs whose jobType is Backuo --- snippets/go/src/.DS_Store | Bin 0 -> 6148 bytes snippets/go/src/nb_jobs/README.md | 13 ++ snippets/go/src/nb_jobs/get-nb-jobs.go | 230 +++++++++++++++++++++++++ 3 files changed, 243 insertions(+) create mode 100644 snippets/go/src/.DS_Store create mode 100644 snippets/go/src/nb_jobs/README.md create mode 100644 snippets/go/src/nb_jobs/get-nb-jobs.go diff --git a/snippets/go/src/.DS_Store b/snippets/go/src/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..96a3cd2004ff1018e85ee7b3f06952bd46d102a1 GIT binary patch literal 6148 zcmeHKO-jQ+6n>*c18%x>IalZjj3q9_We%XEg^DDZYN_BZ_wf`SLhu5fL%;VU7!%Ny zNPQ1xzBli0CSU3N5K)+KmouU%5oKtMft3+q^Qddjf)9W}Y9y*?b6%%KRgV<`Ss!D_VMX-Sf@$L zA4ILMznks8yld;!t}p|B6BlQ|8E^)i0cT*qfSnK7(ZJ&dXTTZw?+oz%5YZTO!%k7Q z4s`Ma08U^QfzFp^&WVP(VW$WWBxov7Q-eJ*f~LbBEiO0g6g8cKJwAfX{PB1RGCS%= z38#=NdhZN41APYebvWho{|tYb(MNti#YfJ7Gq7U}gh^2rb9|JYt%J|wvo@k#p|NmW o#|i{`?-GCk?<2?JRDWb0ak*iq$W_=b=|H~-2qE4%1HZt)JKM%W`Tzg` literal 0 HcmV?d00001 diff --git a/snippets/go/src/nb_jobs/README.md b/snippets/go/src/nb_jobs/README.md new file mode 100644 index 0000000..fa88c42 --- /dev/null +++ b/snippets/go/src/nb_jobs/README.md @@ -0,0 +1,13 @@ +Call Netbackup APIs + +Calls the API and returns the data + +Call Get Admin/NetBackup/Jobs API and returns all Netbackup Admin Jobs + +Also, returns all the NetBackup jobs of the jobType 'Backup' + + +#######Use following command to execute the program + + go build get-nb-jobs.go + go run ./get-nb-jobs.go -nbmaster rsvlmvc01vm231.rmnus.sen.symantec.com -username -password diff --git a/snippets/go/src/nb_jobs/get-nb-jobs.go b/snippets/go/src/nb_jobs/get-nb-jobs.go new file mode 100644 index 0000000..00a8ea1 --- /dev/null +++ b/snippets/go/src/nb_jobs/get-nb-jobs.go @@ -0,0 +1,230 @@ +//This script can be run using NetBackup 8.2 and higher. +//It gets all NetBackup services available on the given host. + +package main + +import ( + "bytes" + "flag" + "fmt" + "log" + "os" + "crypto/tls" + "encoding/json" + "io/ioutil" + "net/http" + "net/http/httputil" +) + +const( + + port = "1556" + policiesUri = "config/policies/" + contentTypeV2 = "application/vnd.netbackup+json;version=2.0" + contentTypeV3 = "application/vnd.netbackup+json;version=3.0" + testPolicyName = "vmware_test_policy" + testClientName = "MEDIA_SERVER" + testScheduleName = "vmware_test_schedule" +) + + +//################### +// Global Variables +//################### +var ( + nbmaster = flag.String("nbmaster", "", "NetBackup Master Server") + username = flag.String("username", "", "User name to log into the NetBackup webservices") + password = flag.String("password", "", "Password for the given user") + domainType = flag.String("domainType", "", "Domain type of the given user") +) + +const usage = "\n\nUsage: go run ./get-nb-jobs.go -nbmaster -userName -password \n\n" + +func main() { + // Print usage + flag.Usage = func() { + fmt.Fprintf(os.Stderr, usage) + os.Exit(1) + } + + // Read command line arguments + flag.Parse() + + if len(*nbmaster) == 0 { + log.Fatalf("Please specify the name of the NetBackup Master Server using the -nbmaster parameter.\n") + } + if len(*username) == 0 { + log.Fatalf("Please specify the username using the -username parameter.\n") + } + if len(*password) == 0 { + log.Fatalf("Please specify the password using the -password parameter.\n") + } + + httpClient := GetHTTPClient() + + jwt := Login(*nbmaster, httpClient, *username, *password) + + //gives all the netbackup jobs + fmt.Printf("====================================================\n") + fmt.Printf("Gives list of all Netbackup jobs\n") + GetNetbackupJobs(httpClient, jwt, *nbmaster) + fmt.Printf("====================================================\n") + + //to get all the netbackup jobs whose jobType is 'Backup' + fmt.Printf("====================================================\n") + fmt.Printf("Gives list of all Netbackup jobs of jobType 'Backup'\n") + GetBackupJobs(httpClient, jwt, *nbmaster) + fmt.Printf("====================================================\n") +} + + +//############################################################## +// Setup the HTTP client to make NetBackup Policy API requests +//############################################################## +func GetHTTPClient() *http.Client { + tlsConfig := &tls.Config { + InsecureSkipVerify: true, //for this test, ignore ssl certificate + } + + tr := &http.Transport{TLSClientConfig: tlsConfig} + client := &http.Client{Transport: tr} + + return client +} + + +//##################################### +// Login to the NetBackup webservices +//##################################### +func Login(nbmaster string, httpClient *http.Client, username string, password string) string { + fmt.Printf("\nLog into the NetBackup webservices...\n") + fmt.Printf("\nLog into the NetBackup webservices...\n") + + loginDetails := map[string]string{"userName": username, "password": password} + loginRequest, _ := json.Marshal(loginDetails) + + uri := "https://" + nbmaster + ":" + port + "/netbackup/login" + + request, _ := http.NewRequest(http.MethodPost, uri, bytes.NewBuffer(loginRequest)) + request.Header.Add("Content-Type", contentTypeV2); + + response, err := httpClient.Do(request) + token := "" + if err != nil { + fmt.Printf("The HTTP request failed with error: %s\n", err) + panic("Unable to login to the NetBackup webservices.") + } else { + if response.StatusCode == 201 { + data, _ := ioutil.ReadAll(response.Body) + var obj interface{} + json.Unmarshal(data, &obj) + loginResponse := obj.(map[string]interface{}) + token = loginResponse["token"].(string) + } else { + printErrorResponse(response) + } + } + + return token +} + +//##################################################### +// Get NETBACKUP JOBS +//##################################################### +func GetNetbackupJobs(httpClient *http.Client, jwt string, nbmaster string) { + fmt.Printf("Get the netbackup jobs\n") + fmt.Printf("================================================\n") + url := "https://" + nbmaster + ":" + port + "/netbackup" + "/admin/jobs" + + request, _ := http.NewRequest(http.MethodGet, url, nil) + query := request.URL.Query() + request.URL.RawQuery = query.Encode() + + request.Header.Add("Authorization", jwt); + request.Header.Add("Accept", contentTypeV3); + + response, err := httpClient.Do(request) + + var jsonDataresponse map[string]interface{} + + if err != nil { + fmt.Printf("The HTTP request failed with error: %s\n", err) + panic("Unable to get the netbackup jobs") + } else { + if response.StatusCode == 200 { + data, _ := ioutil.ReadAll(response.Body) + json.Unmarshal(data, &jsonDataresponse) + jobs, err := json.MarshalIndent(jsonDataresponse, "", " ") + fmt.Printf("\n\nAdmin jobs are: %s\n", jobs) + + if err != nil { + fmt.Println("error:", err) + } + + + } else { + printErrorResponse(response) + } + } +} + +//##################################################### +// Get NETBACKUP JOBS of jobtype Backup +//##################################################### +func GetBackupJobs(httpClient *http.Client, jwt string, nbmaster string) { + fmt.Printf("\n===================Get the netbackup jobs of jobType Backup=================") + + url := "https://" + nbmaster + ":" + port + "/netbackup" + "/admin/jobs" + + request, _ := http.NewRequest(http.MethodGet, url, nil) + query := request.URL.Query() + query.Add("filter","jobType eq 'BACKUP'") + request.URL.RawQuery = query.Encode() + + request.Header.Add("Authorization", jwt); + request.Header.Add("Accept", contentTypeV3); + + response, err := httpClient.Do(request) + + var jsonDataresponse map[string]interface{} + + if err != nil { + fmt.Printf("The HTTP request failed with error: %s\n", err) + panic("Unable to get the netbackup jobs") + } else { + if response.StatusCode == 200 { + data, _ := ioutil.ReadAll(response.Body) + json.Unmarshal(data, &jsonDataresponse) + backupJobs, err := json.MarshalIndent(jsonDataresponse, "", " ") + + fmt.Printf("\n\nBackup Jobs are: %s\n", backupJobs) + + if err != nil { + fmt.Println("error:", err) + } + + } else { + printErrorResponse(response) + } + } + +} + + +func printErrorResponse(response *http.Response) { + responseBody, _ := ioutil.ReadAll(response.Body) + var obj interface{} + json.Unmarshal(responseBody, &obj) + + if obj != nil { + error := obj.(map[string]interface{}) + errorCode := error["errorCode"].(float64) + errorMessage := error["errorMessage"].(string) + fmt.Printf("Error code:%.0f\nError message:%s\n", errorCode, errorMessage) + } else { + responseDetails, _ := httputil.DumpResponse(response, true); + fmt.Printf(string(responseDetails)) + } + + panic("Request failed"); +} From c339317a7603a41e1be74791d53e013526191b53 Mon Sep 17 00:00:00 2001 From: "pooja.gandhi" Date: Tue, 15 Oct 2019 22:59:00 -0700 Subject: [PATCH 2/2] README modified --- snippets/go/src/nb_jobs/README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/snippets/go/src/nb_jobs/README.md b/snippets/go/src/nb_jobs/README.md index fa88c42..21ef74f 100644 --- a/snippets/go/src/nb_jobs/README.md +++ b/snippets/go/src/nb_jobs/README.md @@ -10,4 +10,8 @@ Also, returns all the NetBackup jobs of the jobType 'Backup' #######Use following command to execute the program go build get-nb-jobs.go + + + + go run ./get-nb-jobs.go -nbmaster rsvlmvc01vm231.rmnus.sen.symantec.com -username -password