2018-09-05 14:54:38 -04:00
|
|
|
package hns
|
2018-03-19 18:57:30 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
2018-09-05 14:54:38 -04:00
|
|
|
"github.com/Microsoft/hcsshim/internal/hcserror"
|
|
|
|
"github.com/Microsoft/hcsshim/internal/interop"
|
2018-03-19 18:57:30 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2020-01-06 12:15:00 -05:00
|
|
|
func hnsCallRawResponse(method, path, request string) (*hnsResponse, error) {
|
2018-03-19 18:57:30 -04:00
|
|
|
var responseBuffer *uint16
|
|
|
|
logrus.Debugf("[%s]=>[%s] Request : %s", method, path, request)
|
|
|
|
|
|
|
|
err := _hnsCall(method, path, request, &responseBuffer)
|
|
|
|
if err != nil {
|
2020-01-06 12:15:00 -05:00
|
|
|
return nil, hcserror.New(err, "hnsCall ", "")
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|
2018-09-05 14:54:38 -04:00
|
|
|
response := interop.ConvertAndFreeCoTaskMemString(responseBuffer)
|
2018-03-19 18:57:30 -04:00
|
|
|
|
|
|
|
hnsresponse := &hnsResponse{}
|
|
|
|
if err = json.Unmarshal([]byte(response), &hnsresponse); err != nil {
|
2020-01-06 12:15:00 -05:00
|
|
|
return nil, err
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|
2020-01-06 12:15:00 -05:00
|
|
|
return hnsresponse, nil
|
|
|
|
}
|
2018-03-19 18:57:30 -04:00
|
|
|
|
2020-01-06 12:15:00 -05:00
|
|
|
func hnsCall(method, path, request string, returnResponse interface{}) error {
|
|
|
|
hnsresponse, err := hnsCallRawResponse(method, path, request)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed during hnsCallRawResponse: %v", err)
|
|
|
|
}
|
2018-03-19 18:57:30 -04:00
|
|
|
if !hnsresponse.Success {
|
2020-01-06 12:15:00 -05:00
|
|
|
return fmt.Errorf("hns failed with error : %s", hnsresponse.Error)
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(hnsresponse.Output) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
logrus.Debugf("Network Response : %s", hnsresponse.Output)
|
|
|
|
err = json.Unmarshal(hnsresponse.Output, returnResponse)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|