Skip to content

Commit 04a6034

Browse files
committed
NetBackup module in go and its example
1 parent efd64f6 commit 04a6034

4 files changed

Lines changed: 208 additions & 0 deletions

File tree

snippets/go/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# NetBackup package for GO language
2+
3+
This package gives a wrapper to call NetBackup APIs.
4+
There are few raw functions that can be used generically to
5+
call NetBackup APIs.
6+
There are also some specific call that creates the URL, calls the API, parse the output and return back the data.
7+
8+
9+
## Building and executing sample program
10+
Pre-requisites:
11+
- NetBackup 8.1.1 or higher
12+
- Go version 1.10.2
13+
14+
Use the following commands to build and execute the sample program
15+
```sh
16+
$ cd netbackup
17+
$ # Build sample program
18+
$ go build example/sample.go
19+
$ # execute sample program
20+
$ ./sample
21+
```
22+
To use in your own Go program just import the package and use it as shown in the sample.go

snippets/go/example/sample.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"netbackup"
7+
"os"
8+
)
9+
10+
func main() {
11+
fmt.Printf("Welcome to sample Go program to call NetBackup APIs\n")
12+
scanner := bufio.NewScanner(os.Stdin)
13+
fmt.Printf("Enter the server to connect: ")
14+
scanner.Scan()
15+
server := scanner.Text()
16+
fmt.Printf("Checking connectivity with NetBackup Server[%s]\n", server)
17+
fmt.Printf("-----------------------\n")
18+
fmt.Println(netbackup.Ping(server))
19+
fmt.Printf("-----------------------\n")
20+
fmt.Printf("Enter username for server:")
21+
scanner.Scan()
22+
username := scanner.Text()
23+
fmt.Printf("Enter password for server:")
24+
scanner.Scan()
25+
password := scanner.Text()
26+
fmt.Printf("Calling NetBackup Login\n")
27+
fmt.Printf("-----------------------\n")
28+
jwt := netbackup.Login(username, password, server)
29+
fmt.Printf("-----------------------\n")
30+
// fmt.Printf(netbackup.MappingList(jwt , "V-067223A"))
31+
fmt.Printf("Mapping List using jwt\n")
32+
fmt.Printf("-----------------------\n")
33+
netbackup.MappingList(jwt, server)
34+
fmt.Printf("-----------------------\n")
35+
}

snippets/go/netbackup.go

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
// Package netbackup provides functions for calling netbackup APIs and
2+
// get there response back in structured formata.
3+
package netbackup
4+
5+
import (
6+
"bytes"
7+
"crypto/tls"
8+
"encoding/json"
9+
"fmt"
10+
"io/ioutil"
11+
"net/http"
12+
)
13+
14+
// NetBackup structure to directly call raw APIs
15+
// or some predefined APIs
16+
type NetBackup struct {
17+
jwt string
18+
baseURL string
19+
skipVerification bool
20+
}
21+
22+
// ----------------------------------------
23+
//
24+
// Raw functions to call APIs and their helper functions
25+
//
26+
// ----------------------------------------
27+
28+
// Get Raw function to call any GET APIs of NetBackup
29+
func Get(jwt string, url string) (jsonData map[string]interface{}, code int, err error) {
30+
31+
client := getHTTPClient()
32+
req, err := http.NewRequest("GET", url, nil)
33+
if jwt != "" {
34+
req.Header.Add("Authorization", jwt)
35+
}
36+
req.Header.Add("ContentType", "application/vnd.netbackup+json; version=1.0")
37+
38+
response, err := client.Do(req)
39+
if err == nil {
40+
code = response.StatusCode
41+
if code >= 200 && code <= 299 {
42+
data, _ := ioutil.ReadAll(response.Body)
43+
if err := json.Unmarshal(data, &jsonData); err != nil {
44+
panic(err)
45+
}
46+
}
47+
}
48+
defer response.Body.Close()
49+
return jsonData, response.StatusCode, err
50+
}
51+
52+
// Post Raw function to call any POST APIs of NetBackup
53+
func Post(jwt string, url string, reqValue []byte) (jsonData map[string]interface{}, code int, err error) {
54+
55+
client := getHTTPClient()
56+
req, err := http.NewRequest("POST", url, bytes.NewBuffer(reqValue))
57+
if jwt != "" {
58+
req.Header.Add("Authorization", jwt)
59+
}
60+
req.Header.Add("Content-Type", "application/vnd.netbackup+json; version=1.0")
61+
response, err := client.Do(req)
62+
63+
if err == nil {
64+
code = response.StatusCode
65+
if code >= 200 && code <= 299 {
66+
resData, _ := ioutil.ReadAll(response.Body)
67+
if err := json.Unmarshal(resData, &jsonData); err != nil {
68+
panic(err)
69+
}
70+
}
71+
}
72+
defer response.Body.Close()
73+
return jsonData, code, err
74+
}
75+
76+
// ----------------------------------------
77+
//
78+
// Some additional functions that you can directly call to
79+
// achieve some basic functionality.
80+
//
81+
// ----------------------------------------
82+
83+
// Ping function call the netbackup ping API. This can be used
84+
// to check the connectivity to Master server web services
85+
func Ping(server string) string {
86+
var data []byte
87+
url := GetBaseURLString(server) + "/ping"
88+
client := getHTTPClient()
89+
response, err := client.Get(url)
90+
if err != nil {
91+
fmt.Printf("Request Failed %s \n", err)
92+
} else {
93+
data, _ = ioutil.ReadAll(response.Body)
94+
return (string(data))
95+
}
96+
return (string(""))
97+
}
98+
99+
//Login function
100+
func Login(username string, password string, server string) string {
101+
url := GetBaseURLString(server) + "/login"
102+
reqData := map[string]string{"userName": username, "password": password}
103+
reqValue, _ := json.Marshal(reqData)
104+
105+
jsonData, code, err := Post("", url, reqValue)
106+
107+
if err != nil {
108+
fmt.Printf("Request Failed %s \n", err)
109+
} else {
110+
fmt.Println(code)
111+
if code != 201 {
112+
fmt.Printf("Wrong response %d \n", code)
113+
return ""
114+
}
115+
}
116+
fmt.Printf("Login succesful. got jwt[%s]", jsonData["token"].(string))
117+
return jsonData["token"].(string)
118+
}
119+
120+
// MappingList Returns list of mappings for the server in JSON format
121+
func MappingList(jwt string, server string) {
122+
url := GetBaseURLString(server) + "/config/hosts"
123+
124+
jsonData, code, err := Get(jwt, url)
125+
if err != nil {
126+
fmt.Printf("Request Failed %s \n", err)
127+
} else {
128+
if code != 200 {
129+
fmt.Printf("Request returned wrong code %d \n", code)
130+
return
131+
}
132+
}
133+
fmt.Println(jsonData)
134+
}
135+
136+
// GetBaseURLString This returns the base URL for netbackup
137+
func GetBaseURLString(server string) string {
138+
return "https://" + server + ":1556/netbackup"
139+
}
140+
141+
func getContentType() string {
142+
return "application/vnd.netbackup+json; version=1.0"
143+
}
144+
145+
func getHTTPClient() http.Client {
146+
tr := &http.Transport{
147+
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
148+
}
149+
client := &http.Client{Transport: tr}
150+
return *client
151+
}

snippets/go/sample

6.15 MB
Binary file not shown.

0 commit comments

Comments
 (0)