예제 #1
0
func (s *Server) runtimeCreate(w http.ResponseWriter, r *http.Request) {
	var rt types.Runtime
	s.parseBody(w, r, &rt)

	// Validate runtime
	if rt.Name == "" {
		exc := fmt.Errorf("[runtimeCreate] Attribute \"name\" is required")
		s.failure(w, r, http.StatusBadRequest, exc)
		return
	}
	if rt.Image == "" {
		exc := fmt.Errorf("[runtimeCreate] Attribute \"image\" is required")
		s.failure(w, r, http.StatusBadRequest, exc)
		return
	}
	if rt.Command == "" {
		exc := fmt.Errorf("[runtimeCreate] Attribute \"command\" is required")
		s.failure(w, r, http.StatusBadRequest, exc)
		return
	}
	if rt.Driver == "" {
		exc := fmt.Errorf("[runtimeCreate] Attribute \"driver\" is required")
		s.failure(w, r, http.StatusBadRequest, exc)
		return
	}
	_, err := driver.Open(rt.Driver)
	if err != nil {
		exc := fmt.Errorf("[runtimeCreate] Invalid driver: %s", err)
		s.failure(w, r, http.StatusBadRequest, exc)
		return
	}

	// Set other values
	now := time.Now()
	rt.ID = types.NewID()
	rt.Created = now
	rt.Updated = now

	// Save runtime configuration
	created, err := s.s.Runtimes().Create(rt)
	if err != nil {
		exc := fmt.Errorf("[runtimeCreate] Store error caused by: %s", err)
		s.failure(w, r, http.StatusInternalServerError, exc)
		return
	}
	logger.Printf("Runtime %q created with image %q and command %q.\n", created.Name, created.Image, created.Command)

	s.success(w, r, http.StatusCreated, created)
}
예제 #2
0
func (s *Server) functionCreate(w http.ResponseWriter, r *http.Request) {
	var f types.Function
	s.parseBody(w, r, &f)

	// Validate function
	if f.Name == "" {
		exc := fmt.Errorf("[functionCreate] Attribute \"name\" is required")
		s.failure(w, r, http.StatusBadRequest, exc)
		return
	}
	if f.Runtime == "" {
		exc := fmt.Errorf("[functionCreate] Attribute \"runtime\" is required")
		s.failure(w, r, http.StatusBadRequest, exc)
		return
	} else {
		// Check if runtime exists
		if rt, err := s.s.Runtimes().FindByIDOrName(f.Runtime); err == store.ErrNotFound {
			exc := fmt.Errorf("[functionCreate] Runtime %q does not exists", f.Runtime)
			s.failure(w, r, http.StatusBadRequest, exc)
			return
		} else {
			f.Runtime = rt.ID
		}
	}
	if f.Timeout <= 0 {
		exc := fmt.Errorf("[functionCreate] Attribute \"timeout\" must be greater than 0")
		s.failure(w, r, http.StatusBadRequest, exc)
		return
	}
	if f.Memory < 32 {
		exc := fmt.Errorf("[functionCreate] Attribute \"memory\" must be greater than 32 MB")
		s.failure(w, r, http.StatusBadRequest, exc)
		return
	}
	if f.Instances <= 0 {
		exc := fmt.Errorf("[functionCreate] Attribute \"instances\" must be greater than 0")
		s.failure(w, r, http.StatusBadRequest, exc)
		return
	}
	for _, e := range f.Env {
		if _, _, err := env.ParseLine(e); err != nil {
			exc := fmt.Errorf("Invalid environment variable %q. Pattern must be VARIABLE=VALUE", e)
			s.failure(w, r, http.StatusBadRequest, exc)
			return
		}
	}

	now := time.Now()
	f.ID = types.NewID()
	f.Created = now
	f.Updated = now

	created, err := s.s.Functions().Create(f)
	if err != nil {
		exc := fmt.Errorf("[functionCreate] Store error caused by: %s", err)
		s.failure(w, r, http.StatusInternalServerError, exc)
		return
	}
	logger.Printf("Function %q created with runtime %q", created.Name, created.Runtime)

	s.success(w, r, http.StatusCreated, created)
}