Exemplo n.º 1
0
Arquivo: list.go Projeto: utako/atc
func (s *Server) ListResources(pipelineDB db.PipelineDB) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		var resources []atc.Resource

		config, _, err := pipelineDB.GetConfig()
		if err != nil {
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		showCheckErr := s.validator.IsAuthenticated(r)

		for _, resource := range config.Resources {
			dbResource, err := pipelineDB.GetResource(resource.Name)
			if err != nil {
				w.WriteHeader(http.StatusInternalServerError)
				return
			}

			resources = append(
				resources,
				present.Resource(
					resource,
					config.Groups,
					dbResource,
					showCheckErr,
				),
			)
		}

		w.WriteHeader(http.StatusOK)

		json.NewEncoder(w).Encode(resources)
	})
}
Exemplo n.º 2
0
func (s *Server) GetResource(pipelineDB db.PipelineDB) http.Handler {
	logger := s.logger.Session("get-resource")
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		config, _, found, err := pipelineDB.GetConfig()
		if err != nil {
			logger.Error("failed-to-get-config", err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		if !found {
			logger.Info("config-not-found")
			w.WriteHeader(http.StatusNotFound)
			return
		}

		resourceName := r.FormValue(":resource_name")

		resourceConfig, resourceFound := config.Resources.Lookup(resourceName)
		if !resourceFound {
			logger.Info("resource-not-in-config")
			w.WriteHeader(http.StatusNotFound)
			return
		}

		dbResource, found, err := pipelineDB.GetResource(resourceName)
		if err != nil {
			logger.Error("failed-to-get-resource", err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		if !found {
			logger.Debug("resource-not-found", lager.Data{"resource": resourceName})
			w.WriteHeader(http.StatusNotFound)
			return
		}

		resource := present.Resource(
			resourceConfig,
			config.Groups,
			dbResource,
			auth.IsAuthenticated(r),
		)

		w.WriteHeader(http.StatusOK)

		json.NewEncoder(w).Encode(resource)
	})
}
Exemplo n.º 3
0
func (s *Server) ListResources(pipelineDB db.PipelineDB) http.Handler {
	logger := s.logger.Session("list-resources")
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		var resources []atc.Resource

		config, _, found, err := pipelineDB.GetConfig()
		if err != nil {
			logger.Error("failed-to-get-config", err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		if !found {
			w.WriteHeader(http.StatusNotFound)
			return
		}

		showCheckErr := auth.IsAuthenticated(r)

		for _, resource := range config.Resources {
			dbResource, found, err := pipelineDB.GetResource(resource.Name)
			if err != nil {
				logger.Error("failed-to-get-resource", err)
				w.WriteHeader(http.StatusInternalServerError)
				return
			}

			if !found {
				logger.Debug("resource-not-found", lager.Data{"resource": resource})
				w.WriteHeader(http.StatusNotFound)
				return
			}

			resources = append(
				resources,
				present.Resource(
					resource,
					config.Groups,
					dbResource,
					showCheckErr,
				),
			)
		}

		w.WriteHeader(http.StatusOK)

		json.NewEncoder(w).Encode(resources)
	})
}
Exemplo n.º 4
0
Arquivo: handler.go Projeto: utako/atc
func FetchTemplateData(resourceDB ResourcesDB, authenticated bool, resourceName string, id int, newerResourceVersions bool) (TemplateData, error) {
	config, _, err := resourceDB.GetConfig()
	if err != nil {
		return TemplateData{}, err
	}

	configResource, found := config.Resources.Lookup(resourceName)
	if !found {
		return TemplateData{}, ErrResourceConfigNotFound
	}

	dbResource, err := resourceDB.GetResource(configResource.Name)
	if err != nil {
		return TemplateData{}, err
	}

	maxID, err := resourceDB.GetResourceHistoryMaxID(dbResource.ID)
	if err != nil {
		return TemplateData{}, err
	}

	startingID := maxID

	if id < maxID && id != 0 {
		startingID = id
	}

	history, hasNext, err := resourceDB.GetResourceHistoryCursor(configResource.Name, startingID, newerResourceVersions, 100)
	if err != nil {
		return TemplateData{}, err
	}

	resource := present.Resource(configResource, config.Groups, dbResource, authenticated)

	maxIDFromResults := maxID
	var olderStartID int
	var newerStartID int

	if len(history) > 0 {
		maxIDFromResults = history[0].VersionedResource.ID
		minIDFromResults := history[len(history)-1].VersionedResource.ID
		olderStartID = minIDFromResults - 1
		newerStartID = maxIDFromResults + 1
	}

	hasNewer := maxID > maxIDFromResults
	hasOlder := newerResourceVersions || hasNext
	hasPagination := hasOlder || hasNewer

	templateData := TemplateData{
		Resource: resource,
		History:  history,
		PaginationData: PaginationData{
			HasPagination: hasPagination,
			HasOlder:      hasOlder,
			HasNewer:      hasNewer,
			OlderStartID:  olderStartID,
			NewerStartID:  newerStartID,
		},
		PipelineName: resourceDB.GetPipelineName(),
		GroupStates: group.States(config.Groups, func(g atc.GroupConfig) bool {
			for _, groupResource := range g.Resources {
				if groupResource == configResource.Name {
					return true
				}
			}

			return false
		}),
	}

	return templateData, nil
}
Exemplo n.º 5
0
Arquivo: handler.go Projeto: ACPK/atc
func FetchTemplateData(resourceDB ResourcesDB, authenticated bool, resourceName string, startingID int, resultsGreaterThanStartingID bool) (TemplateData, error) {
	config, _, found, err := resourceDB.GetConfig()
	if err != nil {
		return TemplateData{}, err
	}

	if !found {
		return TemplateData{}, ErrResourceConfigNotFound
	}

	configResource, found := config.Resources.Lookup(resourceName)
	if !found {
		return TemplateData{}, ErrResourceConfigNotFound
	}

	dbResource, err := resourceDB.GetResource(configResource.Name)
	if err != nil {
		return TemplateData{}, err
	}

	maxID, err := resourceDB.GetResourceHistoryMaxID(dbResource.ID)
	if err != nil {
		return TemplateData{}, err
	}

	if startingID == 0 && !resultsGreaterThanStartingID {
		startingID = maxID
	}

	history, moreResultsInGivenDirection, err := resourceDB.GetResourceHistoryCursor(configResource.Name, startingID, resultsGreaterThanStartingID, 100)
	if err != nil {
		return TemplateData{}, err
	}

	var paginationData pagination.PaginationData

	if len(history) > 0 {
		paginationData = pagination.NewPaginationData(
			resultsGreaterThanStartingID,
			moreResultsInGivenDirection,
			maxID,
			history[0].VersionedResource.ID,
			history[len(history)-1].VersionedResource.ID,
		)
	} else {
		paginationData = pagination.PaginationData{}
	}

	resource := present.Resource(configResource, config.Groups, dbResource, authenticated)

	templateData := TemplateData{
		Resource:       resource,
		History:        history,
		PaginationData: paginationData,
		PipelineName:   resourceDB.GetPipelineName(),
		GroupStates: group.States(config.Groups, func(g atc.GroupConfig) bool {
			for _, groupResource := range g.Resources {
				if groupResource == configResource.Name {
					return true
				}
			}

			return false
		}),
	}

	return templateData, nil
}