-
Notifications
You must be signed in to change notification settings - Fork 9
/
realms.go
143 lines (126 loc) · 4.28 KB
/
realms.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright 2019 Google LLC
//
// Licensed 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 dam
import (
"fmt"
"net/http"
"google.golang.org/grpc/status" /* copybara-comment */
"github.com/golang/protobuf/proto" /* copybara-comment */
"github.com/pborman/uuid" /* copybara-comment */
"github.com/GoogleCloudPlatform/healthcare-federated-access-services/lib/auth" /* copybara-comment: auth */
"github.com/GoogleCloudPlatform/healthcare-federated-access-services/lib/ga4gh" /* copybara-comment: ga4gh */
"github.com/GoogleCloudPlatform/healthcare-federated-access-services/lib/handlerfactory" /* copybara-comment: handlerfactory */
"github.com/GoogleCloudPlatform/healthcare-federated-access-services/lib/httputils" /* copybara-comment: httputils */
"github.com/GoogleCloudPlatform/healthcare-federated-access-services/lib/storage" /* copybara-comment: storage */
pb "github.com/GoogleCloudPlatform/healthcare-federated-access-services/proto/dam/v1" /* copybara-comment: go_proto */
ppb "github.com/GoogleCloudPlatform/healthcare-federated-access-services/proto/process/v1" /* copybara-comment: go_proto */
)
func (s *Service) realmFactory() *handlerfactory.Options {
return &handlerfactory.Options{
TypeName: "realm",
NameField: "realm",
PathPrefix: realmPath,
HasNamedIdentifiers: true,
Service: func() handlerfactory.Service {
return newRealmHandler(s)
},
}
}
type realmHandler struct {
s *Service
input *pb.RealmRequest
item *pb.Realm
cfg *pb.DamConfig
id *ga4gh.Identity
tx storage.Tx
}
func newRealmHandler(s *Service) *realmHandler {
return &realmHandler{
s: s,
input: &pb.RealmRequest{},
}
}
func (h *realmHandler) Setup(r *http.Request, tx storage.Tx) (int, error) {
cfg, id, status, err := h.s.handlerSetup(tx, r, noScope, h.input)
h.cfg = cfg
h.id = id
h.tx = tx
return status, err
}
func (h *realmHandler) LookupItem(r *http.Request, name string, vars map[string]string) bool {
// Accept any name that passes the name check.
h.item = &pb.Realm{}
return true
}
func (h *realmHandler) NormalizeInput(r *http.Request, name string, vars map[string]string) error {
if err := httputils.DecodeProtoReq(h.input, r); err != nil {
return err
}
if h.input.Item == nil {
h.input.Item = &pb.Realm{}
}
return nil
}
func (h *realmHandler) Get(r *http.Request, name string) (proto.Message, error) {
if h.item != nil {
return h.item, nil
}
return nil, nil
}
func (h *realmHandler) Post(r *http.Request, name string) (proto.Message, error) {
// Accept, but do nothing.
return nil, nil
}
func (h *realmHandler) Put(r *http.Request, name string) (proto.Message, error) {
// Accept, but do nothing.
return nil, nil
}
func (h *realmHandler) Patch(r *http.Request, name string) (proto.Message, error) {
// Accept, but do nothing.
return nil, nil
}
func (h *realmHandler) Remove(r *http.Request, name string) (proto.Message, error) {
if name == storage.DefaultRealm {
return nil, fmt.Errorf("cannot remove the master realm")
}
a, err := auth.FromContext(r.Context())
if err != nil {
return nil, err
}
id := uuid.New()
work, err := h.s.lro.AddRealmRemoval(id, name, a.ID, h.tx)
if err != nil {
return nil, err
}
cfg, err := h.s.loadConfig(h.tx, storage.DefaultRealm)
if err != nil {
return nil, err
}
if cfg.Options.GcpServiceAccountProject != h.cfg.Options.GcpServiceAccountProject {
return nil, h.s.unregisterProject(h.cfg.Options.GcpServiceAccountProject, h.tx)
}
return &ppb.WorkResponse{
Id: id,
State: "queued",
Details: work,
Uri: h.s.lroURI(id),
}, nil
}
func (h *realmHandler) CheckIntegrity(*http.Request) *status.Status {
return nil
}
func (h *realmHandler) Save(r *http.Request, tx storage.Tx, name string, vars map[string]string, desc, typeName string) error {
// Accept, but do nothing.
return nil
}