Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/get nb jobs #44

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added snippets/go/src/.DS_Store
Binary file not shown.
17 changes: 17 additions & 0 deletions snippets/go/src/nb_jobs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use markdown to format the readme. For example, # designates headings and indentation or backticks designate code.


go build get-nb-jobs.go




go run ./get-nb-jobs.go -nbmaster rsvlmvc01vm231.rmnus.sen.symantec.com -username <USERNAME> -password <PASSWORD>
230 changes: 230 additions & 0 deletions snippets/go/src/nb_jobs/get-nb-jobs.go
Original file line number Diff line number Diff line change
@@ -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 <masterServer> -userName <username> -password <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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not just for this test. It's pretty standard to disable certificate validation in all NetBackup communication because the certificate is self-signed and thus unverifiable anyway. I think you can just remove the comment because it's not really indicating anything special about this code.

}

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");
}