Browse Source

add method to update an existing entitlement

Mark deVilliers 2 years ago
parent
commit
2b327b424a
3 changed files with 68 additions and 4 deletions
  1. 1 1
      node/api/devices.go
  2. 12 2
      node/api/entitlement_store.go
  3. 55 1
      node/api/entitlements.go

+ 1 - 1
node/api/devices.go

@@ -54,7 +54,7 @@ func (e deviceResource) WebService() *restful.WebService {
54 54
 	ws.Route(ws.POST("/").To(e.newDevice).
55 55
 		Doc("add a new device").
56 56
 		Metadata(restfulspec.KeyOpenAPITags, tags).
57
-		Reads(DeviceRequest{Description: "a demonstration sine wave generator.", Type: "sine"}).
57
+		Reads(DeviceRequest{}).
58 58
 		Writes(DeviceResponse{}).
59 59
 		Returns(http.StatusOK, "OK", DeviceResponse{}).
60 60
 		Returns(http.StatusBadRequest, "error validating request", ErrorResponse{}).

+ 12 - 2
node/api/entitlement_store.go

@@ -50,7 +50,15 @@ func (e entitlementMap) Update(uid string, f entitlementUpdater) error {
50 50
 		return ErrEntitlementNotFound
51 51
 	}
52 52
 
53
-	return f(&ent)
53
+	err := f(&ent)
54
+
55
+	if err != nil {
56
+		return err
57
+	}
58
+
59
+	e.store[uid] = ent
60
+
61
+	return nil
54 62
 }
55 63
 
56 64
 func (e entitlementMap) Get(uid string) (Entitlement, bool) {
@@ -126,7 +134,9 @@ func (e entitlementMap) AppendOrReplaceOnSubject(ent Entitlement) {
126 134
 	}
127 135
 
128 136
 	if found {
129
-		delete(e.store, existing.UID)
137
+		ent.UID = existing.UID
138
+		e.store[existing.UID] = ent
139
+		return
130 140
 	}
131 141
 
132 142
 	e.store[ent.UID] = ent

+ 55 - 1
node/api/entitlements.go

@@ -1,6 +1,7 @@
1 1
 package api
2 2
 
3 3
 import (
4
+	"log"
4 5
 	"net/http"
5 6
 
6 7
 	validator "gopkg.in/validator.v2"
@@ -144,9 +145,23 @@ func (e entitlementResource) WebService() *restful.WebService {
144 145
 		Returns(200, "OK", []Entitlement{}).
145 146
 		Returns(404, "Not Found", nil))
146 147
 
148
+	// update an existing accepted entitlement
149
+	// TODO : when adding in authN/R ensure only the creator can modify
150
+	ws.Route(ws.POST("/accepted/{entitlement-uid}").
151
+		To(e.amendAcceptedEntitlement).
152
+		Param(entitlementUIDParameter).
153
+		Doc("amend an accepted entitlement.").
154
+		Metadata(restfulspec.KeyOpenAPITags, tags).
155
+		Reads(Entitlement{}).
156
+		Returns(http.StatusOK, "OK", Entitlement{}).
157
+		Returns(http.StatusNotFound, "Not found", nil).
158
+		Returns(http.StatusBadRequest, "error validating request", ErrorResponse{}).
159
+		Returns(http.StatusInternalServerError, "something went wrong", ErrorResponse{}))
160
+
147 161
 	// revoke an entitlement
148 162
 	// TODO : review GET
149
-	ws.Route(ws.GET("/accepted/{entitlement-uid}/revoke").To(e.revokeEntitlement).
163
+	ws.Route(ws.GET("/accepted/{entitlement-uid}/revoke").
164
+		To(e.revokeEntitlement).
150 165
 		Doc("revoke an entitlement").
151 166
 		Param(entitlementUIDParameter).
152 167
 		Metadata(restfulspec.KeyOpenAPITags, tags).
@@ -269,6 +284,45 @@ func (e entitlementResource) findEntitlements(request *restful.Request, response
269 284
 
270 285
 }
271 286
 
287
+func (e entitlementResource) amendAcceptedEntitlement(request *restful.Request, response *restful.Response) {
288
+
289
+	req := Entitlement{}
290
+	if err := request.ReadEntity(&req); err != nil {
291
+		response.WriteHeaderAndEntity(http.StatusBadRequest, ErrorResponse{Error: err.Error()})
292
+		return
293
+	}
294
+
295
+	if errs := validator.Validate(req); errs != nil {
296
+		response.WriteHeaderAndEntity(http.StatusBadRequest, ErrorResponse{Error: errs.Error()})
297
+		return
298
+	}
299
+
300
+	uid := request.PathParameter("entitlement-uid")
301
+
302
+	log.Println(uid, req, e.store.Accepted)
303
+
304
+	// we only allow updating the access level at the moment
305
+	err := e.store.Accepted.Update(uid, func(e *Entitlement) error {
306
+		e.AccessLevel = req.AccessLevel
307
+		return nil
308
+	})
309
+
310
+	log.Println(err, e.store.Accepted)
311
+
312
+	if err != nil {
313
+
314
+		if err == ErrEntitlementNotFound {
315
+			response.WriteHeader(http.StatusNotFound)
316
+			return
317
+		}
318
+		response.WriteHeaderAndEntity(http.StatusInternalServerError, ErrorResponse{Error: err.Error()})
319
+		return
320
+	}
321
+
322
+	response.WriteEntity(req)
323
+
324
+}
325
+
272 326
 func (e entitlementResource) revokeEntitlement(request *restful.Request, response *restful.Response) {
273 327
 
274 328
 	uid := request.PathParameter("entitlement-uid")