mirror of
https://github.com/openfaas/faas.git
synced 2025-06-18 20:16:37 +00:00
Vendoring with Glide and delete function handler
This commit is contained in:
@ -76,6 +76,68 @@ func MakeFunctionReader(metricsOptions metrics.MetricOptions, c *client.Client)
|
||||
}
|
||||
}
|
||||
|
||||
func MakeDeleteFunctionHandler(metricsOptions metrics.MetricOptions, c *client.Client) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
req := requests.DeleteFunctionRequest{}
|
||||
defer r.Body.Close()
|
||||
reqData, _ := ioutil.ReadAll(r.Body)
|
||||
unmarshalErr := json.Unmarshal(reqData, &req)
|
||||
|
||||
if (len(req.FunctionName) == 0) || unmarshalErr != nil {
|
||||
log.Printf("Error parsing request to remove service: %s\n", unmarshalErr)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("Attempting to remove service %s\n", req.FunctionName)
|
||||
|
||||
serviceFilter := filters.NewArgs()
|
||||
options := types.ServiceListOptions{
|
||||
Filters: serviceFilter,
|
||||
}
|
||||
|
||||
services, err := c.ServiceList(context.Background(), options)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
// TODO: Filter only "faas" functions (via metadata?)
|
||||
var serviceIDs []string
|
||||
for _, service := range services {
|
||||
isFunction := len(service.Spec.TaskTemplate.ContainerSpec.Labels["function"]) > 0
|
||||
|
||||
if isFunction && req.FunctionName == service.Spec.Name {
|
||||
serviceIDs = append(serviceIDs, service.ID)
|
||||
}
|
||||
}
|
||||
|
||||
log.Println(len(serviceIDs))
|
||||
if len(serviceIDs) == 0 {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
w.Write([]byte(fmt.Sprintf("No such service found: %s.", req.FunctionName)))
|
||||
return
|
||||
}
|
||||
|
||||
var serviceRemoveErrors []error
|
||||
for _, serviceID := range serviceIDs {
|
||||
err := c.ServiceRemove(context.Background(), serviceID)
|
||||
if err != nil {
|
||||
serviceRemoveErrors = append(serviceRemoveErrors, err)
|
||||
}
|
||||
}
|
||||
|
||||
if len(serviceRemoveErrors) > 0 {
|
||||
log.Printf("Error(s) removing service: %s\n", req.FunctionName)
|
||||
log.Println(serviceRemoveErrors)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
} else {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// MakeNewFunctionHandler creates a new function (service) inside the swarm network.
|
||||
func MakeNewFunctionHandler(metricsOptions metrics.MetricOptions, c *client.Client) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
|
Reference in New Issue
Block a user