|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "time" |
| 10 | + |
| 11 | + "k8s.io/klog" |
| 12 | +) |
| 13 | + |
| 14 | +const ( |
| 15 | + ClusterIP = "http://34.8.181.238" // Replace with your actual cluster IP |
| 16 | + ClusterID = "af7863cd-3097-434e-9325-c1011a2c9366" // Replace with your actual cluster ID |
| 17 | + ClusterSecret = "HZn4ktklT8ux2xab41hH" // Replace with your actual cluster secret |
| 18 | +) |
| 19 | + |
| 20 | +func CallSBCLI(method, path string, args interface{}) (interface{}, error) { |
| 21 | + |
| 22 | + data := []byte(`{}`) |
| 23 | + var err error |
| 24 | + |
| 25 | + if args != nil { |
| 26 | + data, err = json.Marshal(args) |
| 27 | + if err != nil { |
| 28 | + return nil, fmt.Errorf("%s: %w", method, err) |
| 29 | + } |
| 30 | + } else { |
| 31 | + data = nil |
| 32 | + } |
| 33 | + |
| 34 | + requestURL := fmt.Sprintf("%s/%s", ClusterIP, path) |
| 35 | + klog.Infof("Calling Simplyblock API: Method: %s: RequestURL: %s: Body: %s\n", method, requestURL, string(data)) |
| 36 | + |
| 37 | + req, err := http.NewRequest("GET", requestURL, bytes.NewReader(data)) |
| 38 | + if err != nil { |
| 39 | + return nil, fmt.Errorf("%s: %w", method, err) |
| 40 | + } |
| 41 | + |
| 42 | + // authHeader := fmt.Sprintf("%s %s", ClusterID, ClusterSecret) |
| 43 | + |
| 44 | + // req.Header.Add("Authorization", authHeader) |
| 45 | + req.Header.Set("Content-Type", "application/json") |
| 46 | + |
| 47 | + for key, values := range req.Header { |
| 48 | + for _, value := range values { |
| 49 | + fmt.Printf("Header: %s: %s\n", key, value) |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + httpClient := &http.Client{Timeout: 10 * time.Second} |
| 54 | + //print request object |
| 55 | + fmt.Printf("Request: %s %s, Headers: %v, Body: %s", method, requestURL, req.Header, string(data)) |
| 56 | + // resp, err := httpClient.Get(requestURL) |
| 57 | + // if err != nil { |
| 58 | + // return nil, fmt.Errorf("%s: %w", method, err) |
| 59 | + // } |
| 60 | + |
| 61 | + resp, err := httpClient.Do(req) |
| 62 | + if err != nil { |
| 63 | + return nil, fmt.Errorf("%s: %w", method, err) |
| 64 | + } |
| 65 | + |
| 66 | + defer resp.Body.Close() |
| 67 | + if resp.StatusCode >= http.StatusInternalServerError { |
| 68 | + return nil, fmt.Errorf("%s: HTTP error code: %d", method, resp.StatusCode) |
| 69 | + } |
| 70 | + |
| 71 | + var response struct { |
| 72 | + Result any `json:"result"` |
| 73 | + Results any `json:"results"` |
| 74 | + Error string `json:"error"` |
| 75 | + } |
| 76 | + |
| 77 | + // copy resp.Body object |
| 78 | + copiedBody, err := io.ReadAll(resp.Body) |
| 79 | + if err != nil { |
| 80 | + return nil, fmt.Errorf("%s: HTTP error code: %d Error: %w", method, resp.StatusCode, err) |
| 81 | + } |
| 82 | + fmt.Println("copiedBody:", string(copiedBody), "for request:", requestURL) |
| 83 | + resp.Body = io.NopCloser(bytes.NewBuffer(copiedBody)) |
| 84 | + |
| 85 | + err = json.NewDecoder(resp.Body).Decode(&response) |
| 86 | + if err != nil { |
| 87 | + return nil, fmt.Errorf("%s: HTTP error code: %d Error: %w", method, resp.StatusCode, err) |
| 88 | + } |
| 89 | + |
| 90 | + if resp.StatusCode >= http.StatusBadRequest { |
| 91 | + return nil, fmt.Errorf("%s: HTTP error code: %d Error: %s", method, resp.StatusCode, response.Error) |
| 92 | + } |
| 93 | + |
| 94 | + if response.Result != nil { |
| 95 | + return response.Result, nil |
| 96 | + } |
| 97 | + return response.Results, nil |
| 98 | +} |
| 99 | + |
| 100 | +func main() { |
| 101 | + resp, err := CallSBCLI("GET", "lvol/"+"ce6cb6a1-0c63-43e9-a095-66db8f2f4997/", nil) |
| 102 | + if err != nil { |
| 103 | + klog.Errorf("Error calling SBCLI: %v", err) |
| 104 | + } else { |
| 105 | + klog.Info("Successfully called SBCLI") |
| 106 | + } |
| 107 | + fmt.Printf("Response: %v\n", resp) |
| 108 | +} |
0 commit comments