|
| 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