Skip to content

Commit 6f21b7a

Browse files
committed
very basic post
1 parent 2c97c3c commit 6f21b7a

File tree

3 files changed

+91
-2
lines changed

3 files changed

+91
-2
lines changed

cns/restserver/api.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,8 +1348,7 @@ func (service *HTTPRestService) ibDevicesHandler(w http.ResponseWriter, r *http.
13481348

13491349
switch r.Method {
13501350
case http.MethodPost:
1351-
// TODO: Implement POST
1352-
http.Error(w, "POST method not yet implemented", http.StatusNotImplemented)
1351+
service.assignIBDevicesToPod(w, r)
13531352
case http.MethodGet:
13541353
// TODO: Implement GET
13551354
http.Error(w, "GET method not yet implemented", http.StatusNotImplemented)

cns/restserver/api_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"encoding/xml"
1111
"fmt"
1212
"io"
13+
"net"
1314
"net/http"
1415
"net/http/httptest"
1516
"net/url"
@@ -1916,6 +1917,38 @@ func TestGetVMUniqueIDFailed(t *testing.T) {
19161917
assert.Equal(t, types.UnexpectedError, vmIDResp.Response.ReturnCode)
19171918
}
19181919

1920+
func TestIBDevices(t *testing.T) {
1921+
var (
1922+
err error
1923+
req *http.Request
1924+
ibDevicesReq cns.AssignIBDevicesToPodRequest
1925+
body bytes.Buffer
1926+
)
1927+
1928+
ibDevicesReq.PodName = "testpod"
1929+
ibDevicesReq.PodNamespace = "testpodnamespace"
1930+
ibDevicesReq.IBMACAddresses = []net.HardwareAddr{
1931+
{0x00, 0x1A, 0x2B, 0x3C, 0x4D, 0x5E},
1932+
{0xDE, 0xAD, 0xBE, 0xEF, 0x00, 0x01},
1933+
}
1934+
1935+
json.NewEncoder(&body).Encode(ibDevicesReq)
1936+
req, err = http.NewRequest(http.MethodPost, cns.IBDevicesPath, &body)
1937+
if err != nil {
1938+
t.Fatal(err)
1939+
}
1940+
1941+
w := httptest.NewRecorder()
1942+
mux.ServeHTTP(w, req)
1943+
var ibDevicesResponse cns.AssignIBDevicesToPodResponse
1944+
1945+
if err = decodeResponse(w, &ibDevicesResponse); err != nil {
1946+
t.Errorf("AssignIBDevicesToPod failed with response %+v and error %v", ibDevicesResponse, err)
1947+
}
1948+
1949+
fmt.Printf("Raw response: %+v", w.Body)
1950+
}
1951+
19191952
// IGNORE TEST AS IT IS FAILING. TODO:- Fix it https://msazure.visualstudio.com/One/_workitems/edit/7720083
19201953
// // Tests CreateNetwork functionality.
19211954

cns/restserver/ibdevices.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package restserver
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
7+
"github.com/Azure/azure-container-networking/cns"
8+
"github.com/Azure/azure-container-networking/cns/logger"
9+
"github.com/Azure/azure-container-networking/cns/types"
10+
"github.com/Azure/azure-container-networking/common"
11+
)
12+
13+
// assignIBDevicesToPod handles POST requests to assign IB devices to a pod
14+
func (service *HTTPRestService) assignIBDevicesToPod(w http.ResponseWriter, r *http.Request) {
15+
opName := "assignIBDevicesToPod"
16+
var req cns.AssignIBDevicesToPodRequest
17+
var response cns.AssignIBDevicesToPodResponse
18+
19+
// Decode the request
20+
err := common.Decode(w, r, &req)
21+
logger.Request(service.Name, &req, err)
22+
if err != nil {
23+
return
24+
}
25+
26+
// Validate the request
27+
if err := validateAssignIBDevicesRequest(req); err != nil {
28+
response.Message = fmt.Sprintf("Invalid request: %v", err)
29+
w.WriteHeader(http.StatusBadRequest)
30+
err = common.Encode(w, &response)
31+
logger.Response(opName, response, types.InvalidRequest, err)
32+
return
33+
}
34+
35+
// TODO: Check that the pod exists
36+
// TODO: Check that the IB devices are "unprogrammed" (i.e. available)
37+
// TODO: Create MTPNC with IB devices in spec (and update cache)
38+
39+
// Report back a successful assignment
40+
response.Message = fmt.Sprintf("Successfully assigned %d IB devices to pod %s/%s",
41+
len(req.IBMACAddresses), req.PodNamespace, req.PodName)
42+
43+
w.WriteHeader(http.StatusOK)
44+
err = common.Encode(w, &response)
45+
logger.Response(opName, response, types.Success, err)
46+
}
47+
48+
func validateAssignIBDevicesRequest(req cns.AssignIBDevicesToPodRequest) error {
49+
if req.PodName == "" || req.PodNamespace == "" {
50+
return fmt.Errorf("pod name and namespace are required")
51+
}
52+
if len(req.IBMACAddresses) == 0 {
53+
return fmt.Errorf("at least one IB MAC address is required")
54+
}
55+
// TODO Make sure that the given MAC is valid too
56+
return nil
57+
}

0 commit comments

Comments
 (0)