Created
March 11, 2020 20:58
-
-
Save drew-russell/3ae2079ce716a5f00729dbf376439ecf to your computer and use it in GitHub Desktop.
Example GoLang script that calls the Rubrik CDM GraphQL Service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
“bytes” | |
“crypto/tls” | |
“encoding/json” | |
“fmt” | |
“io/ioutil” | |
“net” | |
“net/http” | |
) | |
func main() { | |
nodeIP := “” | |
username := “” | |
password := “” | |
graphqlServiceEndpoint := fmt.Sprintf(“https://%s/api/internal/graphql”, nodeIP) | |
var request *http.Request | |
operationName := “ClusterDetails” | |
variables := map[string]string{“clusterID”: “me"} | |
query := ` | |
query ClusterDetails($clusterID: String!) { | |
cluster(id: $clusterID) { | |
version | |
} | |
}` | |
payload := map[string]interface{}{ | |
"operationName": operationName, | |
"variables": variables, | |
"query": query} | |
payloadJSON, _ := json.Marshal(payload) | |
request, _ = http.NewRequest("POST”, graphqlServiceEndpoint, bytes.NewBuffer(payloadJSON)) | |
request.SetBasicAuth(username, password) | |
request.Header.Set(“Content-Type”, “application/json”) | |
request.Header.Set(“Accept”, “application/json”) | |
apiRequest, _ := client.Do(request) | |
body, err := ioutil.ReadAll(apiRequest.Body) | |
apiResponse := []byte(body) | |
if err, ok := err.(net.Error); ok && err.Timeout() { | |
fmt.Errorf(“Unable to establish a connection to the Rubrik cluster”) | |
} else if err != nil { | |
fmt.Errorf(err.Error()) | |
} | |
var convertedAPIResponse interface{} | |
if err := json.Unmarshal(apiResponse, &convertedAPIResponse); err != nil { | |
if apiRequest.StatusCode == 204 { | |
convertedAPIResponse = map[string]interface{}{} | |
convertedAPIResponse.(map[string]interface{})[“statusCode”] = apiRequest.StatusCode | |
} else if apiRequest.StatusCode != 200 { | |
fmt.Errorf(apiRequest.Status) | |
} | |
} | |
fmt.Println(convertedAPIResponse) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment