Browse Source

remove data from the metadata service if the data is made non-discoverable

Mark deVilliers 2 years ago
parent
commit
53dc8405ea
2 changed files with 40 additions and 9 deletions
  1. 39 8
      node/api/entitlements.go
  2. 1 1
      node/server.go

+ 39 - 8
node/api/entitlements.go

@@ -6,6 +6,9 @@ import (
6 6
 
7 7
 	validator "gopkg.in/validator.v2"
8 8
 
9
+	metadataclient "gogs.dyne.org/DECODE/decode-prototype-da/client/metadata"
10
+	"gogs.dyne.org/DECODE/decode-prototype-da/utils"
11
+
9 12
 	restful "github.com/emicklei/go-restful"
10 13
 	restfulspec "github.com/emicklei/go-restful-openapi"
11 14
 	uuid "github.com/satori/go.uuid"
@@ -59,12 +62,16 @@ const (
59 62
 
60 63
 type entitlementResource struct {
61 64
 	// all data held in memory
62
-	store *EntitlementStore
65
+	store     *EntitlementStore
66
+	mClient   *metadataclient.MetadataApi
67
+	metaStore *MetadataStore
63 68
 }
64 69
 
65
-func NewEntitlementService(store *EntitlementStore) entitlementResource {
70
+func NewEntitlementService(store *EntitlementStore, metaStore *MetadataStore, mClient *metadataclient.MetadataApi) entitlementResource {
66 71
 	return entitlementResource{
67
-		store: store,
72
+		store:     store,
73
+		metaStore: metaStore,
74
+		mClient:   mClient,
68 75
 	}
69 76
 }
70 77
 
@@ -158,6 +165,7 @@ func (e entitlementResource) WebService() *restful.WebService {
158 165
 		Returns(http.StatusInternalServerError, "something went wrong", ErrorResponse{}))
159 166
 
160 167
 	// revoke an entitlement
168
+	// NOT useful at the moment but when we get users it will revoke a specific users entitlement
161 169
 	// TODO : review GET
162 170
 	ws.Route(ws.GET("/accepted/{entitlement-uid}/revoke").
163 171
 		To(e.revokeEntitlement).
@@ -298,18 +306,13 @@ func (e entitlementResource) amendAcceptedEntitlement(request *restful.Request,
298 306
 
299 307
 	uid := request.PathParameter("entitlement-uid")
300 308
 
301
-	log.Println(uid, req, e.store.Accepted)
302
-
303 309
 	// we only allow updating the access level at the moment
304 310
 	err := e.store.Accepted.Update(uid, func(e *Entitlement) error {
305 311
 		e.AccessLevel = req.AccessLevel
306 312
 		return nil
307 313
 	})
308 314
 
309
-	log.Println(err, e.store.Accepted)
310
-
311 315
 	if err != nil {
312
-
313 316
 		if err == ErrEntitlementNotFound {
314 317
 			response.WriteHeader(http.StatusNotFound)
315 318
 			return
@@ -318,6 +321,34 @@ func (e entitlementResource) amendAcceptedEntitlement(request *restful.Request,
318 321
 		return
319 322
 	}
320 323
 
324
+	// if the entitlement is now not discoverable we will need to
325
+	// remove the data from the metadata service
326
+	if !req.IsDiscoverable() {
327
+		go func() {
328
+
329
+			subject, err := utils.ParseSubject(req.Subject)
330
+
331
+			if err != nil {
332
+				log.Println("error parsing subject : ", subject)
333
+				return
334
+			}
335
+
336
+			metaData, found := e.metaStore.FindBySubject(subject)
337
+
338
+			if !found {
339
+				log.Println("error finding metadata for subject : ", subject)
340
+				return
341
+			}
342
+
343
+			_, err = e.mClient.RemoveFromCatalog(metaData.CatalogUID)
344
+
345
+			if err != nil {
346
+				log.Println("error removing from catalog : ", err.Error())
347
+				return
348
+			}
349
+		}()
350
+	}
351
+
321 352
 	response.WriteEntity(req)
322 353
 
323 354
 }

+ 1 - 1
node/server.go

@@ -53,7 +53,7 @@ func Serve(options Options) error {
53 53
 	deviceManager.Start()
54 54
 
55 55
 	// wire up the json apis
56
-	restful.DefaultContainer.Add(api.NewEntitlementService(entitlementStore).WebService())
56
+	restful.DefaultContainer.Add(api.NewEntitlementService(entitlementStore, metaStore, metadataClient).WebService())
57 57
 	restful.DefaultContainer.Add(api.NewDataService(entitlementStore, storageClient, metaStore).WebService())
58 58
 	restful.DefaultContainer.Add(api.NewDeviceService(ctx, entitlementStore, metaStore, deviceManager.Out()).WebService())
59 59