예제 #1
0
func (handler *servicesHandler) DoAction(w http.ResponseWriter, req *http.Request) {
	vars := mux.Vars(req)
	serviceName := vars["name"]

	serviceConfigurations, err := cf.GetServiceConfigurations()
	if err != nil {
		http.Error(w, err.Error(), 500)
		return
	}

	serviceConfiguration := findServiceWithName(serviceConfigurations, serviceName)
	if serviceConfiguration == nil {
		http.Error(w, fmt.Sprintf("Unknown service '%s'", serviceName), 404)
		return
	}

	actionName := vars["action_name"]

	switch actionName {
	case "export":
		provider := &mysql.MysqlProvider{}

		uuid := uuid.New()
		status := &serviceActionStatus{
			lock:   new(sync.Mutex),
			buffer: &bytes.Buffer{},
			exit:   make(chan int),
		}

		handler.addStatus(uuid, status)

		go func() {
			err := provider.Export(serviceConfiguration.Credentials, status)
			if err != nil {
				exitCode, ok := getExitCode(err)
				if ok {
					status.exit <- exitCode
				} else {
					fmt.Printf("ERROR: Unable to execute action: %s\n", err.Error())
					status.exit <- -1
				}
			} else {
				status.exit <- 0
			}
			close(status.exit)
		}()

		w.Header().Set("Location", fmt.Sprintf("/api/actions/%s/status", uuid))
		w.WriteHeader(201)
		return

	default:
		http.Error(w, fmt.Sprintf("The action '%s' is not supported", actionName), 400)
		return
	}
}
예제 #2
0
func (handler *servicesHandler) List(w http.ResponseWriter, req *http.Request) {
	serviceConfigurations, err := cf.GetServiceConfigurations()
	if err != nil {
		http.Error(w, err.Error(), 500)
		return
	}

	serviceApiModels := getServiceApiModelsFromServiceConfigurations(serviceConfigurations)
	sort.Sort(serviceApiModels)

	data, err := json.Marshal(serviceApiModels)
	if err != nil {
		http.Error(w, err.Error(), 500)
		return
	}

	w.WriteHeader(200)
	w.Write(data)
}