Browse Source

node doesn't need to track the CatalogUID as reusing Subject

Mark deVilliers 2 years ago
parent
commit
49845d775b
4 changed files with 17 additions and 45 deletions
  1. 7 28
      node/api/entitlements.go
  2. 0 1
      node/api/metadata_store.go
  3. 9 15
      node/device_manager.go
  4. 1 1
      node/server.go

+ 7 - 28
node/api/entitlements.go

@@ -7,7 +7,6 @@ import (
7 7
 	validator "gopkg.in/validator.v2"
8 8
 
9 9
 	metadataclient "gogs.dyne.org/DECODE/decode-prototype-da/client/metadata"
10
-	"gogs.dyne.org/DECODE/decode-prototype-da/utils"
11 10
 
12 11
 	restful "github.com/emicklei/go-restful"
13 12
 	restfulspec "github.com/emicklei/go-restful-openapi"
@@ -62,16 +61,14 @@ const (
62 61
 
63 62
 type entitlementResource struct {
64 63
 	// all data held in memory
65
-	store     *EntitlementStore
66
-	mClient   *metadataclient.MetadataApi
67
-	metaStore *MetadataStore
64
+	store   *EntitlementStore
65
+	mClient *metadataclient.MetadataApi
68 66
 }
69 67
 
70
-func NewEntitlementService(store *EntitlementStore, metaStore *MetadataStore, mClient *metadataclient.MetadataApi) entitlementResource {
68
+func NewEntitlementService(store *EntitlementStore, mClient *metadataclient.MetadataApi) entitlementResource {
71 69
 	return entitlementResource{
72
-		store:     store,
73
-		metaStore: metaStore,
74
-		mClient:   mClient,
70
+		store:   store,
71
+		mClient: mClient,
75 72
 	}
76 73
 }
77 74
 
@@ -325,28 +322,10 @@ func (e entitlementResource) amendAcceptedEntitlement(request *restful.Request,
325 322
 	// remove the data from the metadata service
326 323
 	if !req.IsDiscoverable() {
327 324
 
328
-		subject, err := utils.ParseSubject(req.Subject)
325
+		_, err = e.mClient.RemoveFromCatalog(req.Subject)
329 326
 
330 327
 		if err != nil {
331
-			log.Println("error parsing subject : ", subject)
332
-			response.WriteHeaderAndEntity(http.StatusInternalServerError, ErrorResponse{Error: err.Error()})
333
-
334
-			return
335
-		}
336
-
337
-		metaData, found := e.metaStore.FindBySubject(subject)
338
-
339
-		if !found {
340
-			log.Println("error finding metadata for subject : ", subject)
341
-			response.WriteHeaderAndEntity(http.StatusInternalServerError, ErrorResponse{Error: "error finding metadata"})
342
-
343
-			return
344
-		}
345
-
346
-		_, err = e.mClient.RemoveFromCatalog(metaData.CatalogUID)
347
-
348
-		if err != nil {
349
-			log.Println("error removing from catalog : ", err.Error(), subject.String(), metaData)
328
+			log.Println("error removing from catalog : ", err.Error(), req.Subject)
350 329
 			response.WriteHeaderAndEntity(http.StatusInternalServerError, ErrorResponse{Error: err.Error()})
351 330
 
352 331
 			return

+ 0 - 1
node/api/metadata_store.go

@@ -12,7 +12,6 @@ type Metadata struct {
12 12
 	Subject     string `json:"subject" description:"description of the data"`
13 13
 	Name        string `json:"name" description:"name of the data"`
14 14
 	Path        string `json:"path" description:"path to the key of the data"`
15
-	CatalogUID  string `json:"-"`
16 15
 }
17 16
 
18 17
 func NewMetadataStore() *MetadataStore {

+ 9 - 15
node/device_manager.go

@@ -65,9 +65,6 @@ func (d *device_manager) loop() {
65 65
 				subject := utils.BuildSubjectKey(message.SensorUID, k)
66 66
 				log.Println(subject)
67 67
 
68
-				// set the catalogUID to a known value
69
-				catalogUID := "UNKNOWN"
70
-
71 68
 				// find entitlement for subject
72 69
 				ent, found := d.entitlementStore.Accepted.FindForSubject(subject)
73 70
 
@@ -77,7 +74,7 @@ func (d *device_manager) loop() {
77 74
 					if ent.IsDiscoverable() {
78 75
 
79 76
 						var err error
80
-						catalogUID, err = d.sendDataToMetadataService(message.Schema, subject.String(), k, v)
77
+						err = d.sendDataToMetadataService(message.Schema, subject.String(), k, v)
81 78
 
82 79
 						if err != nil {
83 80
 							log.Println(err.Error())
@@ -107,7 +104,6 @@ func (d *device_manager) loop() {
107 104
 					// make sure we create the correct entitlement and metadata
108 105
 					m.Subject = currentSubject
109 106
 					m.Path = k
110
-					m.CatalogUID = catalogUID
111 107
 					d.metaStore.Add(m)
112 108
 
113 109
 					ent.Subject = currentSubject
@@ -116,8 +112,6 @@ func (d *device_manager) loop() {
116 112
 
117 113
 				} else {
118 114
 					// else we have seen this before
119
-					// ensure we capture the catalogUID
120
-					m.CatalogUID = catalogUID
121 115
 					d.metaStore.Add(m)
122 116
 				}
123 117
 
@@ -144,7 +138,7 @@ func (d *device_manager) sendDataToStorageService(subject string, value interfac
144 138
 	return nil
145 139
 }
146 140
 
147
-func (d *device_manager) sendDataToMetadataService(schema map[string]interface{}, subject, key string, value interface{}) (string, error) {
141
+func (d *device_manager) sendDataToMetadataService(schema map[string]interface{}, subject, key string, value interface{}) error {
148 142
 
149 143
 	// we first need to use the schema for the data to 'expand' out and fully qualify the metadata
150 144
 	// to do this we use the JSON-LD expand function that helpfully drops any unqualified metadata and values
@@ -164,23 +158,23 @@ func (d *device_manager) sendDataToMetadataService(schema map[string]interface{}
164 158
 	expanded, err := proc.Expand(s, options)
165 159
 
166 160
 	if err != nil {
167
-		return "", err
161
+		return err
168 162
 	}
169 163
 
170 164
 	// create our metadata request
171 165
 	req := metadataclient.ApiCatalogRequest{
172
-		Sample: fmt.Sprintf("%v", value), // TODO : respect the confidentiality
173
-		Key:    subject,
174
-		Tags:   harvestTagData("", expanded),
166
+		Sample:  fmt.Sprintf("%v", value), // TODO : respect the confidentiality
167
+		Subject: subject,
168
+		Tags:    harvestTagData("", expanded),
175 169
 	}
176 170
 
177
-	response, _, err := d.mClient.CatalogItem(d.locationToken, req)
171
+	_, _, err = d.mClient.CatalogItem(d.locationToken, req)
178 172
 
179 173
 	if err != nil {
180
-		return "", fmt.Errorf("error updating metadata : %s", err.Error())
174
+		return fmt.Errorf("error updating metadata : %s", err.Error())
181 175
 	}
182 176
 
183
-	return response.Uid, nil
177
+	return nil
184 178
 }
185 179
 
186 180
 func harvestTagData(parent string, v []interface{}) []string {

+ 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, metaStore, metadataClient).WebService())
56
+	restful.DefaultContainer.Add(api.NewEntitlementService(entitlementStore, 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