func getSchemas() []*schema.Schema {
	rawSchemasResponse := getSchemasResponse()
	rawSchemas := rawSchemasResponse.(map[string]interface{})["schemas"].([]map[string]interface{})
	schemas := []*schema.Schema{}
	for _, s := range rawSchemas {
		schema, _ := schema.NewSchemaFromObj(s)
		schemas = append(schemas, schema)
	}
	return schemas
}
Beispiel #2
0
func (gohanClientCLI *GohanClientCLI) getSchemas() ([]*schema.Schema, error) {
	response := extensions.GetResult{}
	url := fmt.Sprintf("%s%s", gohanClientCLI.opts.gohanEndpointURL, gohanClientCLI.opts.gohanSchemaURL)
	gohanClientCLI.logRequest("GET", url, gohanClientCLI.provider.TokenID, nil)
	_, err := gohanClientCLI.provider.Get(url, &response.Body, nil)
	if err != nil {
		return nil, err
	}

	bodyMap := response.Body.(map[string]interface{})
	if _, ok := bodyMap["schemas"]; !ok {
		return nil, fmt.Errorf("No 'schemas' key in response JSON")
	}

	result := []*schema.Schema{}
	for _, rawSchema := range bodyMap["schemas"].([]interface{}) {
		schema, err := schema.NewSchemaFromObj(rawSchema)
		if err != nil {
			return nil, fmt.Errorf("Could not parse schemas: %v", err)
		}
		result = append(result, schema)
	}
	return result, nil
}
Beispiel #3
0
func setupEditor(server *Server) {
	manager := schema.GetManager()
	config := util.GetConfig()
	editableSchemaFile := config.GetString("editable_schema", "")
	golang.RegisterGoCallback("handle_schema",
		func(event string, context map[string]interface{}) error {
			auth := context["auth"].(schema.Authorization)
			if event == "pre_list" {
				list := []interface{}{}
				total := 0
				for _, currentSchema := range manager.OrderedSchemas() {
					trimmedSchema, err := GetSchema(currentSchema, auth)
					if err != nil {
						return err
					}
					if trimmedSchema != nil {
						s := trimmedSchema.Data()
						s["url"] = currentSchema.URL
						list = append(list, s)
						total = total + 1
					}
				}
				context["total"] = total
				context["response"] = map[string]interface{}{
					"schemas": list,
				}
				return nil
			} else if event == "pre_show" {
				ID := context["id"].(string)
				currentSchema, _ := manager.Schema(ID)
				object, _ := GetSchema(currentSchema, auth)
				s := object.Data()
				s["url"] = currentSchema.URL
				context["response"] = map[string]interface{}{
					"schema": s,
				}
				return nil
			}
			if event != "pre_create" && event != "pre_update" && event != "pre_delete" {
				return nil
			}

			if editableSchemaFile == "" {
				return nil
			}

			ID := context["id"].(string)

			schemasInFile, err := util.LoadMap(editableSchemaFile)
			if err != nil {
				return nil
			}
			schemas := schemasInFile["schemas"].([]interface{})
			updatedSchemas := []interface{}{}
			var existingSchema map[string]interface{}
			for _, rawSchema := range schemas {
				s := rawSchema.(map[string]interface{})
				if s["id"] == ID {
					existingSchema = s
				} else {
					updatedSchemas = append(updatedSchemas, s)
				}
			}

			if event == "pre_create" {
				if existingSchema != nil {
					return fmt.Errorf("ID has been taken")
				}
				newSchema := context["resource"].(map[string]interface{})
				_, err := schema.NewSchemaFromObj(newSchema)
				if err != nil {
					return err
				}
				schemasInFile["schemas"] = append(updatedSchemas, context["resource"].(map[string]interface{}))
				context["response"] = map[string]interface{}{
					"schema": context["resource"],
				}
				context["exception"] = map[string]interface{}{
					"name":    "CustomException",
					"message": context["response"],
					"code":    201,
				}
			} else if event == "pre_update" {
				if existingSchema == nil {
					return fmt.Errorf("Not found or Update isn't supported for this schema")
				}
				for key, value := range context["resource"].(map[string]interface{}) {
					existingSchema[key] = value
				}
				_, err := schema.NewSchemaFromObj(existingSchema)
				if err != nil {
					return err
				}
				schemasInFile["schemas"] = append(updatedSchemas, existingSchema)
				context["response"] = map[string]interface{}{
					"schema": context["resource"],
				}
				context["exception"] = map[string]interface{}{
					"name":    "CustomException",
					"message": context["response"],
					"code":    200,
				}

			} else if event == "pre_delete" {
				if existingSchema == nil {
					return fmt.Errorf("Not found or Delete isn't supported for this schema")
				}
				schemasInFile["schemas"] = updatedSchemas
				deletedSchema, ok := manager.Schema(ID)
				if ok {
					manager.UnRegisterSchema(deletedSchema)
				}
				context["exception"] = map[string]interface{}{
					"name":    "CustomException",
					"message": map[string]interface{}{"result": "deleted"},
					"code":    204,
				}
			}
			util.SaveFile(editableSchemaFile, schemasInFile)
			err = manager.LoadSchemaFromFile(editableSchemaFile)
			if err != nil {
				return err
			}
			server.initDB()
			server.resetRouter()
			server.mapRoutes()
			return nil
		})
}
Beispiel #4
0
				It("Should show error - could not write file", func() {
					name, _ := ioutil.TempDir(os.TempDir(), "gohan_test")
					gohanClientCLI.opts.cachePath = name
					err = gohanClientCLI.setCachedSchemas()
					Expect(err).To(MatchError(ContainSubstring("is a directory")))
				})
			})
		})

		Describe("Commands", func() {
			var towerSchema *schema.Schema
			var castleSchema *schema.Schema
			const customCommandsTotal int = 2

			BeforeEach(func() {
				towerSchema, _ = schema.NewSchemaFromObj(getTowerSchema())
				castleSchema, _ = schema.NewSchemaFromObj(getCastleSchema())
			})

			It("Should get all commands", func() {
				gohanClientCLI.schemas = []*schema.Schema{
					towerSchema,
					castleSchema,
				}
				commands := gohanClientCLI.getCommands()
				Expect(commands).To(HaveLen(10 + customCommandsTotal))
				Expect(commands[0].Name).To(Equal("tower list"))
				Expect(commands[1].Name).To(Equal("tower show"))
				Expect(commands[2].Name).To(Equal("tower create"))
				Expect(commands[3].Name).To(Equal("tower set"))
				Expect(commands[4].Name).To(Equal("tower delete"))
Beispiel #5
0
func (server *Server) mapRoutes() {
	schemaManager := schema.GetManager()
	MapNamespacesRoutes(server.martini)
	MapRouteBySchemas(server, server.db)

	tx, err := server.db.Begin()
	if err != nil {
		log.Fatal(err)
	}
	defer tx.Close()
	coreSchema, _ := schemaManager.Schema("schema")
	if coreSchema == nil {
		log.Info("No gohan schema. Disabling schema editing mode")
		return
	}

	policySchema, _ := schemaManager.Schema("policy")
	if coreSchema == nil {
		log.Error("No gohan schema. Disabling schema editing mode")
		return
	}

	policyList, _, err := tx.List(policySchema, nil, nil)
	if err != nil {
		log.Info(err.Error())
	}
	schemaManager.LoadPolicies(policyList)

	extensionSchema, _ := schemaManager.Schema("extension")
	extensionList, _, err := tx.List(extensionSchema, nil, nil)
	if err != nil {
		log.Info(err.Error())
	}
	schemaManager.LoadExtensions(extensionList)

	namespaceSchema, _ := schemaManager.Schema("namespace")
	if namespaceSchema == nil {
		log.Error("No gohan schema. Disabling schema editing mode")
		return
	}
	namespaceList, _, err := tx.List(namespaceSchema, nil, nil)
	if err != nil {
		log.Info(err.Error())
	}
	err = tx.Commit()
	if err != nil {
		log.Info(err.Error())
	}
	schemaManager.LoadNamespaces(namespaceList)

	var schemaHandler = func(resource *schema.Resource) {
		s, err := schema.NewSchemaFromObj(resource.Data())
		if err != nil {
			log.Info(err.Error())
		}
		schemaManager.UnRegisterSchema(s)
		server.db.DropTable(s)
		server.resetRouter()
		server.mapRoutes()
	}

	var extensionHandler = func(resource *schema.Resource) {
		tx, err := server.db.Begin()
		if err != nil {
			log.Fatal(err)
		}
		extensionList, _, err := tx.List(extensionSchema, nil, nil)
		tx.Commit()
		if err != nil {
			log.Info(err.Error())
		}
		schemaManager.ClearExtensions()
		schemaManager.LoadExtensions(extensionList)
		server.resetRouter()
		server.mapRoutes()
	}

	coreSchema.SetCreateHandler(schemaHandler)
	coreSchema.SetUpdateHandler(schemaHandler)
	coreSchema.SetDeleteHandler(schemaHandler)

	extensionSchema.SetCreateHandler(extensionHandler)
	extensionSchema.SetUpdateHandler(extensionHandler)
	extensionSchema.SetDeleteHandler(extensionHandler)
}