/
iscsi_create.go
39 lines (32 loc) · 1006 Bytes
/
iscsi_create.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package rest
import (
"encoding/json"
"fmt"
"net/http"
log "github.com/sirupsen/logrus"
"github.com/LINBIT/linstor-gateway/pkg/iscsi"
)
// ISCSICreate creates a highly-available iSCSI target via the REST-API
func (s *server) ISCSICreate() http.HandlerFunc {
return func(writer http.ResponseWriter, request *http.Request) {
var rsc iscsi.ResourceConfig
decoder := json.NewDecoder(request.Body)
err := decoder.Decode(&rsc)
if err != nil {
_, _ = Errorf(http.StatusBadRequest, writer, "failed to parse request body: %v", err)
return
}
result, err := s.iscsi.Create(request.Context(), &rsc)
if err != nil {
_, _ = Errorf(http.StatusBadRequest, writer, "failed to create iscsi resource: %v", err)
return
}
writer.Header().Add("Location", fmt.Sprintf("./iscsi/%s", result.IQN))
writer.WriteHeader(http.StatusCreated)
encoder := json.NewEncoder(writer)
err = encoder.Encode(result)
if err != nil {
log.WithError(err).Warn("failed to write response")
}
}
}