hnsfuncs.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. //go:build windows
  2. package hns
  3. import (
  4. "encoding/json"
  5. "fmt"
  6. "github.com/Microsoft/hcsshim/internal/hcserror"
  7. "github.com/Microsoft/hcsshim/internal/interop"
  8. "github.com/sirupsen/logrus"
  9. )
  10. func hnsCallRawResponse(method, path, request string) (*hnsResponse, error) {
  11. var responseBuffer *uint16
  12. logrus.Debugf("[%s]=>[%s] Request : %s", method, path, request)
  13. err := _hnsCall(method, path, request, &responseBuffer)
  14. if err != nil {
  15. return nil, hcserror.New(err, "hnsCall ", "")
  16. }
  17. response := interop.ConvertAndFreeCoTaskMemString(responseBuffer)
  18. hnsresponse := &hnsResponse{}
  19. if err = json.Unmarshal([]byte(response), &hnsresponse); err != nil {
  20. return nil, err
  21. }
  22. return hnsresponse, nil
  23. }
  24. func hnsCall(method, path, request string, returnResponse interface{}) error {
  25. hnsresponse, err := hnsCallRawResponse(method, path, request)
  26. if err != nil {
  27. return fmt.Errorf("failed during hnsCallRawResponse: %v", err)
  28. }
  29. if !hnsresponse.Success {
  30. return fmt.Errorf("hns failed with error : %s", hnsresponse.Error)
  31. }
  32. if len(hnsresponse.Output) == 0 {
  33. return nil
  34. }
  35. logrus.Debugf("Network Response : %s", hnsresponse.Output)
  36. err = json.Unmarshal(hnsresponse.Output, returnResponse)
  37. if err != nil {
  38. return err
  39. }
  40. return nil
  41. }