Skip to content
Prev Previous commit
Next Next commit
added go code
  • Loading branch information
suruchimalewar committed Apr 2, 2019
commit 54b78f68c1a53f60aa01500d47c442aaaacd8f5b
70 changes: 70 additions & 0 deletions recipes/go/AIRAPIs/AIRAPIs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//This script can be run using NetBackup 8.2 and higher.

package main

import (
"flag"
"fmt"
"log"
"os"
"storageHelper"
)

//###################
// 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")
domainName = flag.String("domainName", "", "Domain name of the given user")
domainType = flag.String("domainType", "", "Domain type of the given user")
)

const usage = "\n\nUsage: go run ./AIRAPIs.go -nbmaster <masterServer> -username <username> -password <password> [-domainName <domainName>] [-domainType <domainType>]\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 := storageHelper.GetHTTPClient()
jwt := storageHelper.Login(*nbmaster, httpClient, *username, *password, *domainName, *domainType)

status, stsName := storageHelper.CreateMSDPStorageServer(*nbmaster, httpClient, jwt)
if( status != 201){
panic("CreateMSDPStorageServer Failed. Exiting.\n")
}

candInx, candId := storageHelper.GetReplicationCandidates(*nbmaster, httpClient, jwt)
if ( candInx == 0 ) {
fmt.Println("Exiting")
os.Exit(0)
}

if ( storageHelper.AddReplicationTarget(*nbmaster, httpClient, jwt, stsName, candId) != 201 ) {
panic("AddReplicationTarget Failed. Exiting.\n")
}

tarInx, tarId := storageHelper.GetReplicationTargets(*nbmaster, httpClient, jwt, stsName)
if ( tarInx == 0 ) {
fmt.Println("Exiting")
os.Exit(0)
}
storageHelper.DeleteReplicationTargets(*nbmaster, httpClient, jwt, stsName, tarId)
}
64 changes: 64 additions & 0 deletions recipes/go/ApiUtil/ApiUtil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//This script consists of the helper functions to excute NetBackup APIs to assist in policy CRUD operations

// 1. Get the HTTP client to perform API requests
// 2. Login to the NetBackup webservices
// 3. Create a policy with default values for the policy attributes
// 4. Create a policy with specific values for the policy attributes, schedules, clients, and backup selection
// 5. Read a policy
// 6. List all policies
// 7. Add/Update a schedule
// 8. Delete a schedule
// 9. Add/Update a client
// 10. Delete a client
// 11. Add/Update backup selection
// 12. Delete a policy

package apiUtil

import (
"bufio"
"os"
"strings"
"io"
"fmt"
"bytes"
)

func TakeInput(displayStr string)(string) {

reader := bufio.NewReader(os.Stdin)
fmt.Print(displayStr)
output, _ := reader.ReadString('\n')
// convert CRLF to LF
output = strings.Replace(output, "\r\n", "", -1)
output = strings.Replace(output, "\n", "", -1)
return output
}

func AskForResponseDisplay(response io.ReadCloser) {
if strings.Compare(TakeInput("Show response? (Yes/No)"), "Yes") == 0 {
buf := new(bytes.Buffer)
buf.ReadFrom(response)
responseStr := buf.String()
responseStr = strings.Replace(responseStr, "}", "}\r\n", -1)
responseStr = strings.Replace(responseStr, ",", ",\r\n", -1)
responseStr = strings.Replace(responseStr, "]", "]\r\n", -1)

fmt.Print(responseStr)
} else {
fmt.Println("Response is not Yes!!")
}
}

func AskForGETResponseDisplay(response []byte) {
if strings.Compare(TakeInput("Show response? (Yes/No)"), "Yes") == 0 {
responseStr := string(response)
responseStr = strings.Replace(responseStr, "}", "}\r\n", -1)
responseStr = strings.Replace(responseStr, ",", ",\r\n", -1)
responseStr = strings.Replace(responseStr, "]", "]\r\n", -1)

fmt.Print(responseStr)
} else {
fmt.Println("Response is not Yes!!")
}
}
Loading