|
| 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