github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/pkg/api/handlers/compat/images_search.go (about)

     1  package compat
     2  
     3  import (
     4  	"net/http"
     5  	"strconv"
     6  
     7  	"github.com/containers/image/v5/types"
     8  	"github.com/containers/podman/v2/libpod/image"
     9  	"github.com/containers/podman/v2/pkg/api/handlers/utils"
    10  	"github.com/containers/podman/v2/pkg/auth"
    11  	"github.com/gorilla/schema"
    12  	"github.com/pkg/errors"
    13  )
    14  
    15  func SearchImages(w http.ResponseWriter, r *http.Request) {
    16  	decoder := r.Context().Value("decoder").(*schema.Decoder)
    17  	query := struct {
    18  		Term      string              `json:"term"`
    19  		Limit     int                 `json:"limit"`
    20  		Filters   map[string][]string `json:"filters"`
    21  		TLSVerify bool                `json:"tlsVerify"`
    22  	}{
    23  		// This is where you can override the golang default value for one of fields
    24  	}
    25  
    26  	if err := decoder.Decode(&query, r.URL.Query()); err != nil {
    27  		utils.Error(w, "Something went wrong.", http.StatusBadRequest, errors.Wrapf(err, "failed to parse parameters for %s", r.URL.String()))
    28  		return
    29  	}
    30  
    31  	filter := image.SearchFilter{}
    32  	if len(query.Filters) > 0 {
    33  		if len(query.Filters["stars"]) > 0 {
    34  			stars, err := strconv.Atoi(query.Filters["stars"][0])
    35  			if err != nil {
    36  				utils.InternalServerError(w, err)
    37  				return
    38  			}
    39  			filter.Stars = stars
    40  		}
    41  		if len(query.Filters["is-official"]) > 0 {
    42  			isOfficial, err := strconv.ParseBool(query.Filters["is-official"][0])
    43  			if err != nil {
    44  				utils.InternalServerError(w, err)
    45  				return
    46  			}
    47  			filter.IsOfficial = types.NewOptionalBool(isOfficial)
    48  		}
    49  		if len(query.Filters["is-automated"]) > 0 {
    50  			isAutomated, err := strconv.ParseBool(query.Filters["is-automated"][0])
    51  			if err != nil {
    52  				utils.InternalServerError(w, err)
    53  				return
    54  			}
    55  			filter.IsAutomated = types.NewOptionalBool(isAutomated)
    56  		}
    57  	}
    58  	options := image.SearchOptions{
    59  		Filter: filter,
    60  		Limit:  query.Limit,
    61  	}
    62  
    63  	if _, found := r.URL.Query()["tlsVerify"]; found {
    64  		options.InsecureSkipTLSVerify = types.NewOptionalBool(!query.TLSVerify)
    65  	}
    66  
    67  	_, authfile, key, err := auth.GetCredentials(r)
    68  	if err != nil {
    69  		utils.Error(w, "failed to retrieve repository credentials", http.StatusBadRequest, errors.Wrapf(err, "failed to parse %q header for %s", key, r.URL.String()))
    70  		return
    71  	}
    72  	defer auth.RemoveAuthfile(authfile)
    73  	options.Authfile = authfile
    74  
    75  	results, err := image.SearchImages(query.Term, options)
    76  	if err != nil {
    77  		utils.BadRequest(w, "term", query.Term, err)
    78  		return
    79  	}
    80  	utils.WriteResponse(w, http.StatusOK, results)
    81  }