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