| /** | |
| * | |
| * Licensed to the Apache Software Foundation (ASF) under one | |
| * or more contributor license agreements. See the NOTICE file | |
| * distributed with this work for additional information | |
| * regarding copyright ownership. The ASF licenses this file | |
| * to you under the Apache License, Version 2.0 (the | |
| * "License"); you may not use this file except in compliance | |
| * with the License. You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, | |
| * software distributed under the License is distributed on an | |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
| * KIND, either express or implied. See the License for the | |
| * specific language governing permissions and limitations | |
| * under the License. | |
| */ | |
| // Package controller serves the Slurm cluster, partition and cluster-config routes. | |
| package controller | |
| import ( | |
| "net/http" | |
| "github.com/apache/airavata/internal/httpx" | |
| dto "github.com/apache/airavata/api/compute/dto" | |
| "github.com/apache/airavata/api/compute/service" | |
| ) | |
| // SlurmClusterController serves /api/v1/slurm-clusters. | |
| type SlurmClusterController struct{ svc *service.SlurmClusterService } | |
| // NewSlurmClusterController returns a handler delegating to svc. | |
| func NewSlurmClusterController(svc *service.SlurmClusterService) *SlurmClusterController { | |
| return &SlurmClusterController{svc: svc} | |
| } | |
| // Register mounts the cluster routes. | |
| func (h *SlurmClusterController) Register(mux *http.ServeMux) { | |
| mux.HandleFunc("GET /api/v1/slurm-clusters", h.list) | |
| mux.HandleFunc("POST /api/v1/slurm-clusters", h.create) | |
| mux.HandleFunc("GET /api/v1/slurm-clusters/{slurmClusterId}", h.get) | |
| mux.HandleFunc("PUT /api/v1/slurm-clusters/{slurmClusterId}", h.update) | |
| mux.HandleFunc("DELETE /api/v1/slurm-clusters/{slurmClusterId}", h.delete) | |
| } | |
| func (h *SlurmClusterController) list(w http.ResponseWriter, r *http.Request) { | |
| clusters, err := h.svc.List(r.Context()) | |
| if err != nil { | |
| httpx.WriteError(w, r, err) | |
| return | |
| } | |
| httpx.WriteJSON(w, http.StatusOK, clusters) | |
| } | |
| func (h *SlurmClusterController) get(w http.ResponseWriter, r *http.Request) { | |
| cluster, err := h.svc.Get(r.Context(), r.PathValue("slurmClusterId")) | |
| if err != nil { | |
| httpx.WriteError(w, r, err) | |
| return | |
| } | |
| httpx.WriteJSON(w, http.StatusOK, cluster) | |
| } | |
| func (h *SlurmClusterController) create(w http.ResponseWriter, r *http.Request) { | |
| var req dto.SlurmClusterRequest | |
| if err := httpx.Bind(r, &req); err != nil { | |
| httpx.WriteError(w, r, err) | |
| return | |
| } | |
| cluster, err := h.svc.Create(r.Context(), &req) | |
| if err != nil { | |
| httpx.WriteError(w, r, err) | |
| return | |
| } | |
| httpx.WriteJSON(w, http.StatusCreated, cluster) | |
| } | |
| func (h *SlurmClusterController) update(w http.ResponseWriter, r *http.Request) { | |
| var req dto.SlurmClusterRequest | |
| if err := httpx.Bind(r, &req); err != nil { | |
| httpx.WriteError(w, r, err) | |
| return | |
| } | |
| cluster, err := h.svc.Update(r.Context(), r.PathValue("slurmClusterId"), &req) | |
| if err != nil { | |
| httpx.WriteError(w, r, err) | |
| return | |
| } | |
| httpx.WriteJSON(w, http.StatusOK, cluster) | |
| } | |
| func (h *SlurmClusterController) delete(w http.ResponseWriter, r *http.Request) { | |
| if err := h.svc.Delete(r.Context(), r.PathValue("slurmClusterId")); err != nil { | |
| httpx.WriteError(w, r, err) | |
| return | |
| } | |
| httpx.WriteJSON(w, http.StatusNoContent, nil) | |
| } | |
| // ClusterPartitionController serves /api/v1/slurm-clusters/{slurmClusterId}/partitions. | |
| type ClusterPartitionController struct { | |
| svc *service.ClusterPartitionService | |
| } | |
| // NewClusterPartitionController returns a handler delegating to svc. | |
| func NewClusterPartitionController(svc *service.ClusterPartitionService) *ClusterPartitionController { | |
| return &ClusterPartitionController{svc: svc} | |
| } | |
| // Register mounts the partition routes as a sub-resource of a cluster. | |
| func (h *ClusterPartitionController) Register(mux *http.ServeMux) { | |
| mux.HandleFunc("GET /api/v1/slurm-clusters/{slurmClusterId}/partitions", h.list) | |
| mux.HandleFunc("POST /api/v1/slurm-clusters/{slurmClusterId}/partitions", h.create) | |
| mux.HandleFunc("GET /api/v1/slurm-clusters/{slurmClusterId}/partitions/{partitionId}", h.get) | |
| mux.HandleFunc("PUT /api/v1/slurm-clusters/{slurmClusterId}/partitions/{partitionId}", h.update) | |
| mux.HandleFunc("DELETE /api/v1/slurm-clusters/{slurmClusterId}/partitions/{partitionId}", h.delete) | |
| } | |
| func (h *ClusterPartitionController) list(w http.ResponseWriter, r *http.Request) { | |
| partitions, err := h.svc.List(r.Context(), r.PathValue("slurmClusterId")) | |
| if err != nil { | |
| httpx.WriteError(w, r, err) | |
| return | |
| } | |
| httpx.WriteJSON(w, http.StatusOK, partitions) | |
| } | |
| func (h *ClusterPartitionController) get(w http.ResponseWriter, r *http.Request) { | |
| partition, err := h.svc.Get(r.Context(), r.PathValue("slurmClusterId"), r.PathValue("partitionId")) | |
| if err != nil { | |
| httpx.WriteError(w, r, err) | |
| return | |
| } | |
| httpx.WriteJSON(w, http.StatusOK, partition) | |
| } | |
| func (h *ClusterPartitionController) create(w http.ResponseWriter, r *http.Request) { | |
| var req dto.ClusterPartitionRequest | |
| if err := httpx.Bind(r, &req); err != nil { | |
| httpx.WriteError(w, r, err) | |
| return | |
| } | |
| partition, err := h.svc.Create(r.Context(), r.PathValue("slurmClusterId"), &req) | |
| if err != nil { | |
| httpx.WriteError(w, r, err) | |
| return | |
| } | |
| httpx.WriteJSON(w, http.StatusCreated, partition) | |
| } | |
| func (h *ClusterPartitionController) update(w http.ResponseWriter, r *http.Request) { | |
| var req dto.ClusterPartitionRequest | |
| if err := httpx.Bind(r, &req); err != nil { | |
| httpx.WriteError(w, r, err) | |
| return | |
| } | |
| partition, err := h.svc.Update(r.Context(), r.PathValue("slurmClusterId"), r.PathValue("partitionId"), &req) | |
| if err != nil { | |
| httpx.WriteError(w, r, err) | |
| return | |
| } | |
| httpx.WriteJSON(w, http.StatusOK, partition) | |
| } | |
| func (h *ClusterPartitionController) delete(w http.ResponseWriter, r *http.Request) { | |
| err := h.svc.Delete(r.Context(), r.PathValue("slurmClusterId"), r.PathValue("partitionId")) | |
| if err != nil { | |
| httpx.WriteError(w, r, err) | |
| return | |
| } | |
| httpx.WriteJSON(w, http.StatusNoContent, nil) | |
| } | |
| // DiscoveryController serves the routes that read a cluster's partitions and a login's | |
| // allocations off the cluster itself. | |
| type DiscoveryController struct { | |
| svc *service.ClusterDiscoveryService | |
| } | |
| // NewDiscoveryController returns a handler delegating to svc. | |
| func NewDiscoveryController(svc *service.ClusterDiscoveryService) *DiscoveryController { | |
| return &DiscoveryController{svc: svc} | |
| } | |
| // Register mounts the discovery routes. Partition discovery sits beside the cluster | |
| // routes rather than under one cluster, because it runs before the cluster is | |
| // registered; allocation discovery sits under the config whose login it reads. | |
| func (h *DiscoveryController) Register(mux *http.ServeMux) { | |
| mux.HandleFunc("POST /api/v1/slurm-clusters/discover-partitions", h.discoverPartitions) | |
| mux.HandleFunc("POST /api/v1/slurm-cluster-configs/{slurmClusterConfigId}/discover-allocations", h.discoverAllocations) | |
| } | |
| func (h *DiscoveryController) discoverAllocations(w http.ResponseWriter, r *http.Request) { | |
| found, err := h.svc.DiscoverAllocations(r.Context(), r.PathValue("slurmClusterConfigId")) | |
| if err != nil { | |
| httpx.WriteError(w, r, err) | |
| return | |
| } | |
| httpx.WriteJSON(w, http.StatusOK, found) | |
| } | |
| func (h *DiscoveryController) discoverPartitions(w http.ResponseWriter, r *http.Request) { | |
| var req dto.PartitionDiscoveryRequest | |
| if err := httpx.Bind(r, &req); err != nil { | |
| httpx.WriteError(w, r, err) | |
| return | |
| } | |
| found, err := h.svc.DiscoverPartitions(r.Context(), &req) | |
| if err != nil { | |
| httpx.WriteError(w, r, err) | |
| return | |
| } | |
| httpx.WriteJSON(w, http.StatusOK, found) | |
| } |