hnsfuncs.go 977 B

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