github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/atc/api/configserver/get.go (about)

     1  package configserver
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net/http"
     7  
     8  	"code.cloudfoundry.org/lager"
     9  
    10  	"github.com/pf-qiu/concourse/v6/atc"
    11  	"github.com/tedsuo/rata"
    12  )
    13  
    14  func (s *Server) GetConfig(w http.ResponseWriter, r *http.Request) {
    15  	logger := s.logger.Session("get-config")
    16  	teamName := rata.Param(r, "team_name")
    17  	pipelineName := rata.Param(r, "pipeline_name")
    18  	pipelineRef := atc.PipelineRef{Name: pipelineName}
    19  	if instanceVars := r.URL.Query().Get("instance_vars"); instanceVars != "" {
    20  		err := json.Unmarshal([]byte(instanceVars), &pipelineRef.InstanceVars)
    21  		if err != nil {
    22  			logger.Error("malformed-instance-vars", err)
    23  			s.handleBadRequest(w, fmt.Sprintf("instance_vars is malformed: %s", err))
    24  			return
    25  		}
    26  	}
    27  
    28  	team, found, err := s.teamFactory.FindTeam(teamName)
    29  	if err != nil {
    30  		logger.Error("failed-to-find-team", err)
    31  		w.WriteHeader(http.StatusInternalServerError)
    32  		return
    33  	}
    34  
    35  	if !found {
    36  		logger.Debug("team-not-found", lager.Data{"team": teamName})
    37  		w.WriteHeader(http.StatusNotFound)
    38  		return
    39  	}
    40  
    41  	pipeline, found, err := team.Pipeline(pipelineRef)
    42  	if err != nil {
    43  		logger.Error("failed-to-find-pipeline", err)
    44  		w.WriteHeader(http.StatusInternalServerError)
    45  		return
    46  	}
    47  
    48  	if !found {
    49  		logger.Debug("pipeline-not-found", lager.Data{"pipeline": pipelineName})
    50  		w.WriteHeader(http.StatusNotFound)
    51  		return
    52  	}
    53  
    54  	if pipeline.Archived() {
    55  		logger.Debug("pipeline-is-archived", lager.Data{"pipeline": pipelineName})
    56  		w.WriteHeader(http.StatusNotFound)
    57  		return
    58  	}
    59  
    60  	config, err := pipeline.Config()
    61  	if err != nil {
    62  		logger.Error("failed-to-get-pipeline-config", err)
    63  		w.WriteHeader(http.StatusInternalServerError)
    64  		return
    65  	}
    66  
    67  	w.Header().Set(atc.ConfigVersionHeader, fmt.Sprintf("%d", pipeline.ConfigVersion()))
    68  	w.Header().Set("Content-Type", "application/json")
    69  
    70  	err = json.NewEncoder(w).Encode(atc.ConfigResponse{
    71  		Config: config,
    72  	})
    73  	if err != nil {
    74  		logger.Error("failed-to-encode-config", err)
    75  		w.WriteHeader(http.StatusInternalServerError)
    76  	}
    77  }