Ejemplo n.º 1
0
func generatePath(resourcePath string, body string) string {
	var curSchema = schema.GetSchemaByURLPath(resourcePath)
	path := resourcePath
	if _, ok := curSchema.SyncKeyTemplate(); ok {
		var data map[string]interface{}
		err := json.Unmarshal(([]byte)(body), &data)
		if err != nil {
			log.Error(fmt.Sprintf("Error %v during unmarshaling data %v", err, data))
		} else {
			path, err = curSchema.GenerateCustomPath(data)
			if err != nil {
				path = resourcePath
				log.Error(fmt.Sprintf("%v", err))
			}
		}
	}
	path = configPrefix + path
	log.Info("Generated path: %s", path)
	return path
}
Ejemplo n.º 2
0
func (server *Server) syncEvent(resource *schema.Resource) error {
	schemaManager := schema.GetManager()
	eventSchema, _ := schemaManager.Schema("event")
	tx, err := server.db.Begin()
	if err != nil {
		return err
	}
	defer tx.Close()
	eventType := resource.Get("type").(string)
	resourcePath := resource.Get("path").(string)
	body := resource.Get("body").(string)

	path := generatePath(resourcePath, body)

	version, ok := resource.Get("version").(int)
	if !ok {
		log.Debug("cannot cast version value in int for %s", path)
	}
	log.Debug("event %s", eventType)

	if eventType == "create" || eventType == "update" {
		log.Debug("set %s on sync", path)
		content, err := json.Marshal(map[string]interface{}{
			"body":    body,
			"version": version,
		})
		if err != nil {
			log.Error(fmt.Sprintf("When marshalling sync object: %s", err))
			return err
		}
		err = server.sync.Update(path, string(content))
		if err != nil {
			log.Error(fmt.Sprintf("%s on sync", err))
			return err
		}
	} else if eventType == "delete" {
		log.Debug("delete %s", resourcePath)
		deletePath := resourcePath
		resourceSchema := schema.GetSchemaByURLPath(resourcePath)
		if _, ok := resourceSchema.SyncKeyTemplate(); ok {
			var data map[string]interface{}
			err := json.Unmarshal(([]byte)(body), &data)
			deletePath, err = resourceSchema.GenerateCustomPath(data)
			if err != nil {
				log.Error(fmt.Sprintf("Delete from sync failed %s - generating of custom path failed", err))
				return err
			}
		}
		log.Debug("deleting %s", statePrefix+deletePath)
		err = server.sync.Delete(statePrefix + deletePath)
		if err != nil {
			log.Error(fmt.Sprintf("Delete from sync failed %s", err))
		}
		log.Debug("deleting %s", monitoringPrefix+deletePath)
		err = server.sync.Delete(monitoringPrefix + deletePath)
		if err != nil {
			log.Error(fmt.Sprintf("Delete from sync failed %s", err))
		}
		log.Debug("deleting %s", resourcePath)
		err = server.sync.Delete(path)
		if err != nil {
			log.Error(fmt.Sprintf("Delete from sync failed %s", err))
			return err
		}
	}
	log.Debug("delete event %d", resource.Get("id"))
	id := resource.Get("id")
	err = tx.Delete(eventSchema, id)
	if err != nil {
		log.Error(fmt.Sprintf("delete failed: %s", err))
		return err
	}

	err = tx.Commit()
	if err != nil {
		log.Error(fmt.Sprintf("commit failed: %s", err))
		return err
	}
	return nil
}
Ejemplo n.º 3
0
func (server *Server) syncEvent(resource *schema.Resource) error {
	schemaManager := schema.GetManager()
	eventSchema, _ := schemaManager.Schema("event")
	tx, err := server.db.Begin()
	if err != nil {
		return err
	}
	defer tx.Close()
	eventType := resource.Get("type").(string)
	resourcePath := resource.Get("path").(string)
	body := resource.Get("body").(string)
	syncPlain := resource.Get("sync_plain").(bool)
	syncProperty := resource.Get("sync_property").(string)

	path := generatePath(resourcePath, body)

	version, ok := resource.Get("version").(int)
	if !ok {
		log.Debug("cannot cast version value in int for %s", path)
	}
	log.Debug("event %s", eventType)

	if eventType == "create" || eventType == "update" {
		log.Debug("set %s on sync", path)

		content := body

		var data map[string]interface{}
		if syncProperty != "" {
			err = json.Unmarshal(([]byte)(body), &data)
			if err != nil {
				log.Error(fmt.Sprintf("failed to unmarshal body on sync: %s", err))
				return err
			}
			target, ok := data[syncProperty]
			if !ok {
				return fmt.Errorf("could not find property `%s`", syncProperty)
			}
			jsonData, err := json.Marshal(target)
			if err != nil {
				return err
			}
			content = string(jsonData)
		}

		if syncPlain {
			var target interface{}
			json.Unmarshal([]byte(content), &target)
			switch target.(type) {
			case string:
				content = fmt.Sprintf("%v", target)
			}
		} else {
			data, err := json.Marshal(map[string]interface{}{
				"body":    content,
				"version": version,
			})
			if err != nil {
				log.Error(fmt.Sprintf("When marshalling sync object: %s", err))
				return err
			}
			content = string(data)
		}

		err = server.sync.Update(path, content)
		if err != nil {
			log.Error(fmt.Sprintf("%s on sync", err))
			return err
		}
	} else if eventType == "delete" {
		log.Debug("delete %s", resourcePath)
		deletePath := resourcePath
		resourceSchema := schema.GetSchemaByURLPath(resourcePath)
		if _, ok := resourceSchema.SyncKeyTemplate(); ok {
			var data map[string]interface{}
			json.Unmarshal(([]byte)(body), &data)
			deletePath, err = resourceSchema.GenerateCustomPath(data)
			if err != nil {
				log.Error(fmt.Sprintf("Delete from sync failed %s - generating of custom path failed", err))
				return err
			}
		}
		log.Debug("deleting %s", statePrefix+deletePath)
		err = server.sync.Delete(statePrefix + deletePath)
		if err != nil {
			log.Error(fmt.Sprintf("Delete from sync failed %s", err))
		}
		log.Debug("deleting %s", monitoringPrefix+deletePath)
		err = server.sync.Delete(monitoringPrefix + deletePath)
		if err != nil {
			log.Error(fmt.Sprintf("Delete from sync failed %s", err))
		}
		log.Debug("deleting %s", resourcePath)
		err = server.sync.Delete(path)
		if err != nil {
			log.Error(fmt.Sprintf("Delete from sync failed %s", err))
			return err
		}
	}
	log.Debug("delete event %d", resource.Get("id"))
	id := resource.Get("id")
	err = tx.Delete(eventSchema, id)
	if err != nil {
		log.Error(fmt.Sprintf("delete failed: %s", err))
		return err
	}

	err = tx.Commit()
	if err != nil {
		log.Error(fmt.Sprintf("commit failed: %s", err))
		return err
	}
	return nil
}