Browse Source

capture and store the catalogUID

Mark deVilliers 2 years ago
parent
commit
b8ad044ba4
2 changed files with 20 additions and 6 deletions
  1. 1 0
      node/api/metadata_store.go
  2. 19 6
      node/device_manager.go

+ 1 - 0
node/api/metadata_store.go

@@ -12,6 +12,7 @@ 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:"-"`
15 16
 }
16 17
 
17 18
 func NewMetadataStore() *MetadataStore {

+ 19 - 6
node/device_manager.go

@@ -65,6 +65,9 @@ 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
+
68 71
 				// find entitlement for subject
69 72
 				ent, found := d.entitlementStore.Accepted.FindForSubject(subject)
70 73
 
@@ -72,7 +75,9 @@ func (d *device_manager) loop() {
72 75
 					// if the underlying data is discoverable
73 76
 					// send to the metadata service
74 77
 					if ent.IsDiscoverable() {
75
-						err := d.sendDataToMetadataService(message.Schema, subject.String(), k, v)
78
+
79
+						var err error
80
+						catalogUID, err = d.sendDataToMetadataService(message.Schema, subject.String(), k, v)
76 81
 
77 82
 						if err != nil {
78 83
 							log.Println(err.Error())
@@ -99,13 +104,21 @@ func (d *device_manager) loop() {
99 104
 				// is this the first time we have seen this data?
100 105
 				if ss.IsRoot() {
101 106
 
107
+					// make sure we create the correct entitlement and metadata
102 108
 					m.Subject = currentSubject
103 109
 					m.Path = k
110
+					m.CatalogUID = catalogUID
104 111
 					d.metaStore.Add(m)
105 112
 
106 113
 					ent.Subject = currentSubject
107 114
 					ent.UID = uuid.NewV4().String()
108 115
 					d.entitlementStore.Accepted.AppendOrReplaceOnSubject(ent)
116
+
117
+				} else {
118
+					// else we have seen this before
119
+					// ensure we capture the catalogUID
120
+					m.CatalogUID = catalogUID
121
+					d.metaStore.Add(m)
109 122
 				}
110 123
 
111 124
 				// write to the storage service
@@ -131,7 +144,7 @@ func (d *device_manager) sendDataToStorageService(subject string, value interfac
131 144
 	return nil
132 145
 }
133 146
 
134
-func (d *device_manager) sendDataToMetadataService(schema map[string]interface{}, subject, key string, value interface{}) error {
147
+func (d *device_manager) sendDataToMetadataService(schema map[string]interface{}, subject, key string, value interface{}) (string, error) {
135 148
 
136 149
 	// we first need to use the schema for the data to 'expand' out and fully qualify the metadata
137 150
 	// to do this we use the JSON-LD expand function that helpfully drops any unqualified metadata and values
@@ -151,7 +164,7 @@ func (d *device_manager) sendDataToMetadataService(schema map[string]interface{}
151 164
 	expanded, err := proc.Expand(s, options)
152 165
 
153 166
 	if err != nil {
154
-		return err
167
+		return "", err
155 168
 	}
156 169
 
157 170
 	// create our metadata request
@@ -161,13 +174,13 @@ func (d *device_manager) sendDataToMetadataService(schema map[string]interface{}
161 174
 		Tags:   harvestTagData("", expanded),
162 175
 	}
163 176
 
164
-	_, _, err = d.mClient.CatalogItem(d.locationToken, req)
177
+	response, _, err := d.mClient.CatalogItem(d.locationToken, req)
165 178
 
166 179
 	if err != nil {
167
-		return fmt.Errorf("error updating metadata : %s", err.Error())
180
+		return "", fmt.Errorf("error updating metadata : %s", err.Error())
168 181
 	}
169 182
 
170
-	return nil
183
+	return response.Uid, nil
171 184
 }
172 185
 
173 186
 func harvestTagData(parent string, v []interface{}) []string {