Skip to content

Commit 5cc8a88

Browse files
Get VMware assets API script
Get VMware assets API go script
1 parent bf694a4 commit 5cc8a88

2 files changed

Lines changed: 159 additions & 0 deletions

File tree

recipes/go/assets/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
### NetBackup API Code Samples in go (golang)
2+
3+
This directory contains code samples for NetBackup Asset Service APIs.
4+
5+
#### Disclaimer
6+
7+
The scripts are provided only for reference and not meant for production use.
8+
9+
#### Pre-requisites:
10+
11+
- NetBackup 8.3 or higher
12+
- go1.10.2 or higher
13+
14+
#### Executing the script
15+
16+
- get_vmware_assets:
17+
`go run ./get_vmware_assets.go -nbserver <NetBackup server> -username <username> -password <password> [-domainName <domainName>] [-domainType <domainType>] [-assetsFilter <filter>]`
18+
19+
The script invokes the NetBackup VMware Asset Service API to get the VMware workload assets (filtered by the given filter if specified). It prints the details (delimited by tab) such as asset display name, instance Id, vCenter and the protection plan names that the asset is protected by.
20+
_Note: assetsFilter option can be used to filter the assets returned. It should be in OData format (refer to the NetBackup API documentation for more details). It is optional, if not specified the script will print all the VM assets. Redirect the script output to a file to avoid printing the details on terminal._
21+
22+
Examples: `go run ./get_vmware_assets.go -nbserver localhost -username user -password password -domainName domain -domainType NT > vm_assets.txt`
23+
`go run ./get_vmware_assets.go -nbserver localhost -username user -password password -assetsFilter "contains(commonAssetAttributes/displayName, 'backup')" > vm_assets.txt`
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
//This script can be run using NetBackup 8.3 and higher.
2+
//It gets the list of VMware assets in NetBackup (based on the given filter if specified, else returns all the VMware assets).
3+
4+
package main
5+
6+
import (
7+
"flag"
8+
"fmt"
9+
"log"
10+
"os"
11+
"strconv"
12+
"net/url"
13+
"net/http"
14+
"io/ioutil"
15+
"encoding/json"
16+
"utils"
17+
)
18+
19+
var (
20+
nbserver = flag.String("nbserver", "", "NetBackup Server")
21+
username = flag.String("username", "", "User name for NetBackup API login")
22+
password = flag.String("password", "", "Password for the given user")
23+
domainName = flag.String("domainName", "", "Domain name")
24+
domainType = flag.String("domainType", "", "Domain type")
25+
assetsFilter = flag.String("assetsFilter", "", "Filter string (odata format) to filter the assets")
26+
)
27+
28+
const usage = "\n\nUsage: go run ./get_vmware_assets.go -nbserver <NetBackup server> -username <username> -password <password> [-domainName <domainName>] [-domainType <domainType>] [-assetsFilter <filter>]\n\n"
29+
30+
func main() {
31+
// Print usage
32+
flag.Usage = func() {
33+
fmt.Fprintf(os.Stderr, usage)
34+
os.Exit(1)
35+
}
36+
37+
// Read command line arguments
38+
flag.Parse()
39+
40+
if len(*nbserver) == 0 {
41+
log.Fatalf("Please specify the name of the NetBackup Server using the -nbserver option.\n")
42+
}
43+
if len(*username) == 0 {
44+
log.Fatalf("Please specify the username using the -username option.\n")
45+
}
46+
if len(*password) == 0 {
47+
log.Fatalf("Please specify the password using the -password option.\n")
48+
}
49+
50+
httpClient := apihelper.GetHTTPClient()
51+
jwt := apihelper.Login(*nbserver, httpClient, *username, *password, *domainName, *domainType)
52+
53+
vmwareAssetsApiUrl := "https://" + *nbserver + "/netbackup/asset-service/workloads/vmware/assets"
54+
defaultSort := "commonAssetAttributes.displayName"
55+
assetTypeFilter := "(assetType eq 'vm')"
56+
57+
req, err := http.NewRequest("GET", vmwareAssetsApiUrl, nil)
58+
59+
if err != nil {
60+
fmt.Printf("Making new HTTP request failed with error: %s\n", err)
61+
panic("Script failed.")
62+
}
63+
64+
req.Header.Add("Authorization", jwt)
65+
pageLimit := 100
66+
offset := 0
67+
next := true
68+
params := url.Values{}
69+
70+
if assetsFilter != nil {
71+
filter := ""
72+
if *assetsFilter != "" {
73+
filter = *assetsFilter + " and " + assetTypeFilter
74+
} else {
75+
filter = assetTypeFilter
76+
}
77+
params.Add("filter", filter)
78+
}
79+
80+
params.Add("sort", defaultSort)
81+
params.Add("page[offset]", strconv.Itoa(offset))
82+
params.Add("page[limit]", strconv.Itoa(pageLimit))
83+
84+
fmt.Println("\nGetting VMware assets...")
85+
fmt.Println("Printing the following asset details: Display Name, VM InstanceId, vCenter, Protection Plan Names\n")
86+
87+
for next {
88+
req.URL.RawQuery = params.Encode()
89+
resp, err := httpClient.Do(req)
90+
91+
if err != nil {
92+
fmt.Printf("Get VMware Assets failed with error: %s\n", err)
93+
panic("Script failed.")
94+
} else {
95+
respJson, _ := ioutil.ReadAll(resp.Body)
96+
if resp.StatusCode == 200 {
97+
var respPayload interface{}
98+
json.Unmarshal(respJson, &respPayload)
99+
respData := respPayload.(map[string]interface{})
100+
assetsData := respData["data"].([]interface{})
101+
printAssetDetails(assetsData)
102+
next = respData["meta"].(map[string]interface{})["pagination"].
103+
(map[string]interface{})["hasNext"].(bool)
104+
} else {
105+
fmt.Println(string(respJson))
106+
next = false
107+
}
108+
}
109+
offset, _ = strconv.Atoi(params["page[offset]"][0])
110+
params["page[offset]"][0] = strconv.Itoa(offset + pageLimit)
111+
}
112+
113+
fmt.Println("\nScript completed.\n")
114+
}
115+
116+
func printAssetDetails(assets []interface{}) {
117+
for _, asset := range assets {
118+
assetAttrs := asset.(map[string]interface{})["attributes"].(map[string]interface{})
119+
assetCommonAttrs := assetAttrs["commonAssetAttributes"].(map[string]interface{})
120+
displayName := assetCommonAttrs["displayName"]
121+
instanceId := assetAttrs["instanceUuid"]
122+
vCenter := assetAttrs["vCenter"]
123+
124+
var protectionPlans []string
125+
if activeProtections, protected := assetCommonAttrs["activeProtection"]; protected {
126+
protectionDetailsList := activeProtections.(map[string]interface{})["protectionDetailsList"].([]interface{})
127+
128+
for _, protectionDetails := range protectionDetailsList {
129+
protectionPlans = append(protectionPlans, protectionDetails.
130+
(map[string]interface{})["protectionPlanName"].(string))
131+
}
132+
}
133+
fmt.Printf("%s\t%s\t%s\t%v\n", displayName, instanceId, vCenter, protectionPlans)
134+
}
135+
136+
}

0 commit comments

Comments
 (0)