forked from hashicorp/nomad
/
namespace_endpoint.go
122 lines (102 loc) · 3.27 KB
/
namespace_endpoint.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package agent
import (
"net/http"
"strings"
"github.com/hernad/nomad/nomad/structs"
)
func (s *HTTPServer) NamespacesRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
if req.Method != "GET" {
return nil, CodedError(405, ErrInvalidMethod)
}
args := structs.NamespaceListRequest{}
if s.parse(resp, req, &args.Region, &args.QueryOptions) {
return nil, nil
}
var out structs.NamespaceListResponse
if err := s.agent.RPC("Namespace.ListNamespaces", &args, &out); err != nil {
return nil, err
}
setMeta(resp, &out.QueryMeta)
if out.Namespaces == nil {
out.Namespaces = make([]*structs.Namespace, 0)
}
return out.Namespaces, nil
}
func (s *HTTPServer) NamespaceSpecificRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
name := strings.TrimPrefix(req.URL.Path, "/v1/namespace/")
if len(name) == 0 {
return nil, CodedError(400, "Missing Namespace Name")
}
switch req.Method {
case "GET":
return s.namespaceQuery(resp, req, name)
case "PUT", "POST":
return s.namespaceUpdate(resp, req, name)
case "DELETE":
return s.namespaceDelete(resp, req, name)
default:
return nil, CodedError(405, ErrInvalidMethod)
}
}
func (s *HTTPServer) NamespaceCreateRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
if req.Method != "PUT" && req.Method != "POST" {
return nil, CodedError(405, ErrInvalidMethod)
}
return s.namespaceUpdate(resp, req, "")
}
func (s *HTTPServer) namespaceQuery(resp http.ResponseWriter, req *http.Request,
namespaceName string) (interface{}, error) {
args := structs.NamespaceSpecificRequest{
Name: namespaceName,
}
if s.parse(resp, req, &args.Region, &args.QueryOptions) {
return nil, nil
}
var out structs.SingleNamespaceResponse
if err := s.agent.RPC("Namespace.GetNamespace", &args, &out); err != nil {
return nil, err
}
setMeta(resp, &out.QueryMeta)
if out.Namespace == nil {
return nil, CodedError(404, "Namespace not found")
}
return out.Namespace, nil
}
func (s *HTTPServer) namespaceUpdate(resp http.ResponseWriter, req *http.Request,
namespaceName string) (interface{}, error) {
// Parse the namespace
var namespace structs.Namespace
if err := decodeBody(req, &namespace); err != nil {
return nil, CodedError(http.StatusBadRequest, err.Error())
}
// Ensure the namespace name matches
if namespaceName != "" && namespace.Name != namespaceName {
return nil, CodedError(400, "Namespace name does not match request path")
}
// Format the request
args := structs.NamespaceUpsertRequest{
Namespaces: []*structs.Namespace{&namespace},
}
s.parseWriteRequest(req, &args.WriteRequest)
var out structs.GenericResponse
if err := s.agent.RPC("Namespace.UpsertNamespaces", &args, &out); err != nil {
return nil, err
}
setIndex(resp, out.Index)
return nil, nil
}
func (s *HTTPServer) namespaceDelete(resp http.ResponseWriter, req *http.Request,
namespaceName string) (interface{}, error) {
args := structs.NamespaceDeleteRequest{
Namespaces: []string{namespaceName},
}
s.parseWriteRequest(req, &args.WriteRequest)
var out structs.GenericResponse
if err := s.agent.RPC("Namespace.DeleteNamespaces", &args, &out); err != nil {
return nil, err
}
setIndex(resp, out.Index)
return nil, nil
}