|
| 1 | +//This script can be run using NetBackup 8.2 and higher. |
| 2 | + |
| 3 | +package main |
| 4 | + |
| 5 | +import ( |
| 6 | + "bytes" |
| 7 | + "encoding/json" |
| 8 | + "flag" |
| 9 | + "fmt" |
| 10 | + "io/ioutil" |
| 11 | + "log" |
| 12 | + "net/http" |
| 13 | + "net/http/httputil" |
| 14 | + "os" |
| 15 | + "apihelper" |
| 16 | +) |
| 17 | + |
| 18 | +//################### |
| 19 | +// Global Variables |
| 20 | +//################### |
| 21 | +var ( |
| 22 | + nbmaster = flag.String("nbmaster", "", "NetBackup Master Server") |
| 23 | + username = flag.String("username", "", "User name to log into the NetBackup webservices") |
| 24 | + password = flag.String("password", "", "Password for the given user") |
| 25 | + domainName = flag.String("domainName", "", "Domain name of the given user") |
| 26 | + domainType = flag.String("domainType", "", "Domain type of the given user") |
| 27 | + accessHost = flag.String("accessHost", "dummy.access.host", "Access Host used for demonstration") |
| 28 | +) |
| 29 | + |
| 30 | +//################### |
| 31 | +// Global Constants |
| 32 | +//################### |
| 33 | +const ( |
| 34 | + baseUrl = "https://%s:1556/netbackup" |
| 35 | + accessHostsUri = "/config/%s/access-hosts" |
| 36 | + contentTypeV3 = "application/vnd.netbackup+json;version=3.0" |
| 37 | + authorizationHeader = "Authorization" |
| 38 | + contentTypeHeader = "Content-Type" |
| 39 | + acceptHeader = "Accept" |
| 40 | + |
| 41 | + usage = "\n\nUsage: go run ./manage_access_hosts.go [-nbmaster <masterServer>] -username <username> -password <password> [-domainName <domainName>] [-domainType <domainType>] [-accessHost <accessHost>]\n\n" |
| 42 | + workload = "vmware" |
| 43 | +) |
| 44 | + |
| 45 | +func main() { |
| 46 | + flag.Usage = func() { |
| 47 | + fmt.Println(usage) |
| 48 | + os.Exit(1) |
| 49 | + } |
| 50 | + |
| 51 | + // Read command line arguments |
| 52 | + flag.Parse() |
| 53 | + |
| 54 | + if len(*nbmaster) == 0 { |
| 55 | + log.Fatalf("Please specify the name of the NetBackup Master Server using the -nbmaster parameter.\n") |
| 56 | + } |
| 57 | + if len(*username) == 0 { |
| 58 | + log.Fatalf("Please specify the username using the -username parameter.\n") |
| 59 | + } |
| 60 | + if len(*password) == 0 { |
| 61 | + log.Fatalf("Please specify the password using the -password parameter.\n") |
| 62 | + } |
| 63 | + if len(*accessHost) == 0 { |
| 64 | + log.Println("-accessHost parameter not specified in the command line. Defaulting to 'dummy.access.host'.\n") |
| 65 | + } |
| 66 | + |
| 67 | + httpClient := apihelper.GetHTTPClient() |
| 68 | + jwt := apihelper.Login(*nbmaster, httpClient, *username, *password, *domainName, *domainType) |
| 69 | + |
| 70 | + // Prints all the access hosts configured on the master server |
| 71 | + fmt.Println("Use-case 1\n==========\nReading all the configured access hosts.") |
| 72 | + getAllAccessHosts(httpClient, jwt) |
| 73 | + fmt.Println("\n\n") |
| 74 | + |
| 75 | + // Add the provided access host to the list of access hosts |
| 76 | + fmt.Println("Use-case 2\n==========\nAdding a new access host.") |
| 77 | + addAccessHost(httpClient, jwt) |
| 78 | + fmt.Println("\n\n") |
| 79 | + |
| 80 | + // Get access hosts of type 'CLIENT' |
| 81 | + fmt.Println("Use-case 3\n==========\nReading 'CLIENT' type access hosts.") |
| 82 | + getAccessHostsOfSpecificType("CLIENT", httpClient, jwt) |
| 83 | + fmt.Println("\n\n") |
| 84 | + |
| 85 | + // Delete the recently added access host |
| 86 | + fmt.Println("Use-case 4\n==========\nDeleting the dummy access host.") |
| 87 | + deleteAccessHost(httpClient, jwt) |
| 88 | + fmt.Println("\n\n") |
| 89 | + |
| 90 | + // Prints all the access hosts configured on the master server |
| 91 | + fmt.Println("Use-case 5\n==========\nReading all the configured access hosts.") |
| 92 | + getAllAccessHosts(httpClient, jwt) |
| 93 | + fmt.Println("\n\n") |
| 94 | +} |
| 95 | + |
| 96 | +func getAllAccessHosts(httpClient *http.Client, jwt string) { |
| 97 | + fmt.Println("Workload: " + workload +"\t\tMaster Server: " + *nbmaster) |
| 98 | + apiResponse := getAccessHosts(httpClient, jwt, "") |
| 99 | + printAccessHostsResponse(apiResponse) |
| 100 | +} |
| 101 | + |
| 102 | +func getAccessHostsOfSpecificType(hostType string, httpClient *http.Client, jwt string) { |
| 103 | + fmt.Println("Workload: " + workload +"\tHost Type: " + hostType + "\tMaster Server: " + *nbmaster) |
| 104 | + filter := "hostType eq '" + hostType + "'" |
| 105 | + apiResponse := getAccessHosts(httpClient, jwt, filter) |
| 106 | + printAccessHostsResponse(apiResponse) |
| 107 | +} |
| 108 | + |
| 109 | +func getAccessHosts(httpClient *http.Client, jwt string, filter string) []byte { |
| 110 | + apiUrl := fmt.Sprintf(baseUrl, *nbmaster) + fmt.Sprintf(accessHostsUri, workload) |
| 111 | + |
| 112 | + request, _ := http.NewRequest(http.MethodGet, apiUrl, nil) |
| 113 | + request.Header.Add(authorizationHeader, jwt) |
| 114 | + request.Header.Add(acceptHeader, contentTypeV3) |
| 115 | + if filter != "" { |
| 116 | + query := request.URL.Query() |
| 117 | + query.Add("filter", filter) |
| 118 | + request.URL.RawQuery = query.Encode() |
| 119 | + } |
| 120 | + |
| 121 | + response, err := httpClient.Do(request) |
| 122 | + var emptyByte []byte |
| 123 | + |
| 124 | + if err != nil { |
| 125 | + fmt.Println("The HTTP request failed with error: %s\n", err) |
| 126 | + panic("Unable to read access hosts for the master server: " + *nbmaster) |
| 127 | + } else { |
| 128 | + if response.StatusCode != http.StatusOK { |
| 129 | + printErrorResponse(response) |
| 130 | + } else { |
| 131 | + resp, _ := ioutil.ReadAll(response.Body) |
| 132 | + return resp |
| 133 | + } |
| 134 | + } |
| 135 | + return emptyByte |
| 136 | +} |
| 137 | + |
| 138 | +func addAccessHost(httpClient *http.Client, jwt string) { |
| 139 | + fmt.Println("Adding access host: " + *accessHost) |
| 140 | + apiUrl := fmt.Sprintf(baseUrl, *nbmaster) + fmt.Sprintf(accessHostsUri, workload) |
| 141 | + |
| 142 | + accessHostRequest := map[string]interface{}{ |
| 143 | + "data": map[string]interface{}{ |
| 144 | + "type": "accessHostRequest", |
| 145 | + "id": workload, |
| 146 | + "attributes": map[string]interface{}{ |
| 147 | + "hostname": *accessHost, |
| 148 | + "validate": false}}} |
| 149 | + |
| 150 | + accessHostRequestBody, _ := json.Marshal(accessHostRequest) |
| 151 | + |
| 152 | + request, _ := http.NewRequest(http.MethodPost, apiUrl, bytes.NewBuffer(accessHostRequestBody)) |
| 153 | + request.Header.Add(authorizationHeader, jwt) |
| 154 | + request.Header.Add(contentTypeHeader, contentTypeV3) |
| 155 | + |
| 156 | + response, err := httpClient.Do(request) |
| 157 | + |
| 158 | + if err != nil { |
| 159 | + fmt.Printf("The HTTP request failed with error: %s\n", err) |
| 160 | + panic("Unable to add access host for workload '" + workload + "' on master '" + *nbmaster +"'\n") |
| 161 | + } else { |
| 162 | + if response.StatusCode != 204 { |
| 163 | + printErrorResponse(response) |
| 164 | + } else { |
| 165 | + fmt.Printf("Access Host '%s' added successfully.\n", *accessHost); |
| 166 | + } |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +func deleteAccessHost(httpClient *http.Client, jwt string) { |
| 171 | + fmt.Println("Deleting access host: " + *accessHost) |
| 172 | + apiUrl := fmt.Sprintf(baseUrl, *nbmaster) + fmt.Sprintf(accessHostsUri, workload) + "/" + *accessHost |
| 173 | + |
| 174 | + request, _ := http.NewRequest(http.MethodDelete, apiUrl, nil) |
| 175 | + request.Header.Add(authorizationHeader, jwt) |
| 176 | + request.Header.Add(acceptHeader, contentTypeV3) |
| 177 | + |
| 178 | + response, err := httpClient.Do(request) |
| 179 | + |
| 180 | + if err != nil { |
| 181 | + fmt.Println("The HTTP request failed with error: %s\n", err) |
| 182 | + panic("Unable to delete access host for the master server: " + *nbmaster) |
| 183 | + } else { |
| 184 | + if response.StatusCode != http.StatusNoContent { |
| 185 | + printErrorResponse(response) |
| 186 | + } else { |
| 187 | + fmt.Printf("Access Host '%s' deleted successfully.", *accessHost) |
| 188 | + } |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +func printErrorResponse(response *http.Response) { |
| 193 | + responseBody, _ := ioutil.ReadAll(response.Body) |
| 194 | + var obj interface{} |
| 195 | + json.Unmarshal(responseBody, &obj) |
| 196 | + |
| 197 | + if obj != nil { |
| 198 | + error := obj.(map[string]interface{}) |
| 199 | + errorCode := error["errorCode"].(float64) |
| 200 | + errorMessage := error["errorMessage"].(string) |
| 201 | + fmt.Printf("Error code:%.0f\nError message:%s\n", errorCode, errorMessage) |
| 202 | + } else { |
| 203 | + responseDetails, _ := httputil.DumpResponse(response, true); |
| 204 | + fmt.Printf(string(responseDetails)) |
| 205 | + } |
| 206 | + |
| 207 | + panic("Request failed"); |
| 208 | +} |
| 209 | + |
| 210 | +func printAccessHostsResponse(apiResponse []byte) { |
| 211 | + var obj interface{} |
| 212 | + json.Unmarshal([]byte(apiResponse), &obj) |
| 213 | + data := obj.(map[string]interface{}) |
| 214 | + var accessHosts []interface{} = data["data"].([]interface{}) |
| 215 | + fmt.Println(" Host Type Workload Hostname") |
| 216 | + fmt.Println("===============.===============.=========================") |
| 217 | + for _, host := range accessHosts { |
| 218 | + hostName := (host.(map[string]interface{}))["id"] |
| 219 | + attributes := ((host.(map[string]interface{}))["attributes"]).(map[string]interface{}) |
| 220 | + respWorkload := attributes["workloadType"] |
| 221 | + respHostType := attributes["hostType"] |
| 222 | + fmt.Printf("%15s %15s %25s\n", respHostType, respWorkload, hostName) |
| 223 | + } |
| 224 | +} |
0 commit comments