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

     1  package containerserver
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  	"time"
     7  
     8  	"code.cloudfoundry.org/lager"
     9  	"github.com/pf-qiu/concourse/v6/atc/api/present"
    10  	"github.com/pf-qiu/concourse/v6/atc/db"
    11  )
    12  
    13  func (s *Server) GetContainer(team db.Team) http.Handler {
    14  	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    15  		handle := r.FormValue(":id")
    16  
    17  		hLog := s.logger.Session("container", lager.Data{
    18  			"handle": handle,
    19  		})
    20  
    21  		container, found, err := team.FindContainerByHandle(handle)
    22  		if err != nil {
    23  			hLog.Error("failed-to-lookup-container", err)
    24  			w.WriteHeader(http.StatusInternalServerError)
    25  			return
    26  		}
    27  
    28  		if !found {
    29  			hLog.Debug("container-not-found")
    30  			w.WriteHeader(http.StatusNotFound)
    31  			return
    32  		}
    33  
    34  		isCheckContainer, err := team.IsCheckContainer(handle)
    35  		if err != nil {
    36  			hLog.Error("failed-to-find-container", err)
    37  			w.WriteHeader(http.StatusInternalServerError)
    38  			return
    39  		}
    40  
    41  		ok, err := team.IsContainerWithinTeam(handle, isCheckContainer)
    42  		if err != nil {
    43  			hLog.Error("failed-to-find-container-within-team", err)
    44  			w.WriteHeader(http.StatusInternalServerError)
    45  			return
    46  		}
    47  
    48  		if !ok {
    49  			hLog.Error("container-not-found-within-team", err)
    50  			w.WriteHeader(http.StatusNotFound)
    51  			return
    52  		}
    53  
    54  		hLog.Debug("found-container")
    55  
    56  		presentedContainer := present.Container(container, time.Time{})
    57  
    58  		w.Header().Set("Content-Type", "application/json")
    59  		err = json.NewEncoder(w).Encode(presentedContainer)
    60  		if err != nil {
    61  			hLog.Error("failed-to-encode-container", err)
    62  			w.WriteHeader(http.StatusInternalServerError)
    63  		}
    64  	})
    65  }