diff options
author | Jhon Honce <jhonce@redhat.com> | 2022-05-19 10:27:31 -0700 |
---|---|---|
committer | Jhon Honce <jhonce@redhat.com> | 2022-05-19 15:24:18 -0700 |
commit | 5b79cf15a0226dc3dad5053615ee652823376cd3 (patch) | |
tree | 066e0b6e5fde4af49e7113ab48c277a55a60c22a /pkg/api/handlers/libpod | |
parent | 913caaa9b1de2b63692c9bae15120208194c9eb3 (diff) | |
download | podman-5b79cf15a0226dc3dad5053615ee652823376cd3.tar.gz podman-5b79cf15a0226dc3dad5053615ee652823376cd3.tar.bz2 podman-5b79cf15a0226dc3dad5053615ee652823376cd3.zip |
Swagger refactor/cleanup
* Remove duplicate or unused types and constants
* Move all documetation-only models and responses into swagger package
* Remove all unecessary names, go-swagger will determine names from
struct declarations
* Use Libpod suffix to differentiate between compat and libpod models
and responses. Taken from swagger:operation declarations.
* Models and responses that start with lowercase are for swagger use
only while uppercase are used "as is" in the code and swagger comments
* Used gofumpt on new code
```release-note
```
Signed-off-by: Jhon Honce <jhonce@redhat.com>
Diffstat (limited to 'pkg/api/handlers/libpod')
-rw-r--r-- | pkg/api/handlers/libpod/containers.go | 1 | ||||
-rw-r--r-- | pkg/api/handlers/libpod/generate.go | 8 | ||||
-rw-r--r-- | pkg/api/handlers/libpod/images.go | 16 | ||||
-rw-r--r-- | pkg/api/handlers/libpod/manifests.go | 12 | ||||
-rw-r--r-- | pkg/api/handlers/libpod/pods.go | 8 | ||||
-rw-r--r-- | pkg/api/handlers/libpod/swagger.go | 157 | ||||
-rw-r--r-- | pkg/api/handlers/libpod/swagger_spec.go | 29 | ||||
-rw-r--r-- | pkg/api/handlers/libpod/volumes.go | 15 |
8 files changed, 53 insertions, 193 deletions
diff --git a/pkg/api/handlers/libpod/containers.go b/pkg/api/handlers/libpod/containers.go index 03dd436f6..6b5bee403 100644 --- a/pkg/api/handlers/libpod/containers.go +++ b/pkg/api/handlers/libpod/containers.go @@ -168,6 +168,7 @@ func UnmountContainer(w http.ResponseWriter, r *http.Request) { } utils.WriteResponse(w, http.StatusNoContent, "") } + func MountContainer(w http.ResponseWriter, r *http.Request) { runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime) name := utils.GetName(r) diff --git a/pkg/api/handlers/libpod/generate.go b/pkg/api/handlers/libpod/generate.go index 28785b00d..b1ac6a65a 100644 --- a/pkg/api/handlers/libpod/generate.go +++ b/pkg/api/handlers/libpod/generate.go @@ -41,17 +41,17 @@ func GenerateSystemd(w http.ResponseWriter, r *http.Request) { return } - var ContainerPrefix = "container" + ContainerPrefix := "container" if query.ContainerPrefix != nil { ContainerPrefix = *query.ContainerPrefix } - var PodPrefix = "pod" + PodPrefix := "pod" if query.PodPrefix != nil { PodPrefix = *query.PodPrefix } - var Separator = "-" + Separator := "-" if query.Separator != nil { Separator = *query.Separator } @@ -106,5 +106,7 @@ func GenerateKube(w http.ResponseWriter, r *http.Request) { return } + // FIXME: Content-Type is being set as application/x-tar NOT text/vnd.yaml + // https://mailarchive.ietf.org/arch/msg/media-types/e9ZNC0hDXKXeFlAVRWxLCCaG9GI/ utils.WriteResponse(w, http.StatusOK, report.Reader) } diff --git a/pkg/api/handlers/libpod/images.go b/pkg/api/handlers/libpod/images.go index cddf4c205..efcbe9d77 100644 --- a/pkg/api/handlers/libpod/images.go +++ b/pkg/api/handlers/libpod/images.go @@ -102,9 +102,7 @@ func GetImage(w http.ResponseWriter, r *http.Request) { } func PruneImages(w http.ResponseWriter, r *http.Request) { - var ( - err error - ) + var err error runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime) decoder := r.Context().Value(api.DecoderKey).(*schema.Decoder) query := struct { @@ -129,7 +127,7 @@ func PruneImages(w http.ResponseWriter, r *http.Request) { return } - var libpodFilters = []string{} + libpodFilters := []string{} if _, found := r.URL.Query()["filters"]; found { dangling := (*filterMap)["all"] if len(dangling) > 0 { @@ -162,9 +160,7 @@ func PruneImages(w http.ResponseWriter, r *http.Request) { } func ExportImage(w http.ResponseWriter, r *http.Request) { - var ( - output string - ) + var output string runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime) decoder := r.Context().Value(api.DecoderKey).(*schema.Decoder) query := struct { @@ -243,9 +239,7 @@ func ExportImage(w http.ResponseWriter, r *http.Request) { } func ExportImages(w http.ResponseWriter, r *http.Request) { - var ( - output string - ) + var output string runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime) decoder := r.Context().Value(api.DecoderKey).(*schema.Decoder) query := struct { @@ -566,7 +560,7 @@ func CommitContainer(w http.ResponseWriter, r *http.Request) { utils.Error(w, http.StatusInternalServerError, errors.Wrapf(err, "CommitFailure")) return } - utils.WriteResponse(w, http.StatusOK, handlers.IDResponse{ID: commitImage.ID()}) // nolint + utils.WriteResponse(w, http.StatusOK, entities.IDResponse{ID: commitImage.ID()}) // nolint } func UntagImage(w http.ResponseWriter, r *http.Request) { diff --git a/pkg/api/handlers/libpod/manifests.go b/pkg/api/handlers/libpod/manifests.go index 8dc7c57d5..65b9d6cb5 100644 --- a/pkg/api/handlers/libpod/manifests.go +++ b/pkg/api/handlers/libpod/manifests.go @@ -88,7 +88,7 @@ func ManifestCreate(w http.ResponseWriter, r *http.Request) { // Treat \r\n as empty body if len(buffer) < 3 { - utils.WriteResponse(w, status, handlers.IDResponse{ID: manID}) + utils.WriteResponse(w, status, entities.IDResponse{ID: manID}) return } @@ -113,7 +113,7 @@ func ManifestCreate(w http.ResponseWriter, r *http.Request) { return } - utils.WriteResponse(w, status, handlers.IDResponse{ID: id}) + utils.WriteResponse(w, status, entities.IDResponse{ID: id}) } // ManifestExists return true if manifest list exists. @@ -204,7 +204,7 @@ func ManifestAddV3(w http.ResponseWriter, r *http.Request) { utils.InternalServerError(w, err) return } - utils.WriteResponse(w, http.StatusOK, handlers.IDResponse{ID: newID}) + utils.WriteResponse(w, http.StatusOK, entities.IDResponse{ID: newID}) } // ManifestRemoveDigestV3 remove digest from manifest list @@ -238,7 +238,7 @@ func ManifestRemoveDigestV3(w http.ResponseWriter, r *http.Request) { utils.InternalServerError(w, err) return } - utils.WriteResponse(w, http.StatusOK, handlers.IDResponse{ID: manifestList.ID()}) + utils.WriteResponse(w, http.StatusOK, entities.IDResponse{ID: manifestList.ID()}) } // ManifestPushV3 push image to registry @@ -294,7 +294,7 @@ func ManifestPushV3(w http.ResponseWriter, r *http.Request) { utils.Error(w, http.StatusBadRequest, errors.Wrapf(err, "error pushing image %q", query.Destination)) return } - utils.WriteResponse(w, http.StatusOK, handlers.IDResponse{ID: digest}) + utils.WriteResponse(w, http.StatusOK, entities.IDResponse{ID: digest}) } // ManifestPush push image to registry @@ -353,7 +353,7 @@ func ManifestPush(w http.ResponseWriter, r *http.Request) { utils.Error(w, http.StatusBadRequest, errors.Wrapf(err, "error pushing image %q", destination)) return } - utils.WriteResponse(w, http.StatusOK, handlers.IDResponse{ID: digest}) + utils.WriteResponse(w, http.StatusOK, entities.IDResponse{ID: digest}) } // ManifestModify efficiently updates the named manifest list diff --git a/pkg/api/handlers/libpod/pods.go b/pkg/api/handlers/libpod/pods.go index d522631b7..5b92358fa 100644 --- a/pkg/api/handlers/libpod/pods.go +++ b/pkg/api/handlers/libpod/pods.go @@ -81,7 +81,7 @@ func PodCreate(w http.ResponseWriter, r *http.Request) { utils.Error(w, httpCode, errors.Wrap(err, "failed to make pod")) return } - utils.WriteResponse(w, http.StatusCreated, handlers.IDResponse{ID: pod.ID()}) + utils.WriteResponse(w, http.StatusCreated, entities.IDResponse{ID: pod.ID()}) } func Pods(w http.ResponseWriter, r *http.Request) { @@ -290,9 +290,7 @@ func PodPrune(w http.ResponseWriter, r *http.Request) { } func PodPruneHelper(r *http.Request) ([]*entities.PodPruneReport, error) { - var ( - runtime = r.Context().Value(api.RuntimeKey).(*libpod.Runtime) - ) + runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime) responses, err := runtime.PrunePods(r.Context()) if err != nil { return nil, err @@ -414,7 +412,7 @@ loop: // break out of for/select infinite` loop } if len(output) > 0 { - var body = handlers.PodTopOKBody{} + body := handlers.PodTopOKBody{} body.Titles = strings.Split(output[0], "\t") for i := range body.Titles { body.Titles[i] = strings.TrimSpace(body.Titles[i]) diff --git a/pkg/api/handlers/libpod/swagger.go b/pkg/api/handlers/libpod/swagger.go deleted file mode 100644 index 5f33e6c01..000000000 --- a/pkg/api/handlers/libpod/swagger.go +++ /dev/null @@ -1,157 +0,0 @@ -package libpod - -import ( - "net/http" - "os" - - "github.com/containers/common/libnetwork/types" - "github.com/containers/image/v5/manifest" - "github.com/containers/podman/v4/libpod/define" - "github.com/containers/podman/v4/pkg/api/handlers/utils" - "github.com/containers/podman/v4/pkg/domain/entities" - "github.com/pkg/errors" -) - -// DefaultPodmanSwaggerSpec provides the default path to the podman swagger spec file -const DefaultPodmanSwaggerSpec = "/usr/share/containers/podman/swagger.yaml" - -// List Containers -// swagger:response ListContainers -type swagInspectPodResponse struct { - // in:body - Body []entities.ListContainer -} - -// Inspect Manifest -// swagger:response InspectManifest -type swagInspectManifestResponse struct { - // in:body - Body manifest.Schema2List -} - -// Kill Pod -// swagger:response PodKillReport -type swagKillPodResponse struct { - // in:body - Body entities.PodKillReport -} - -// Pause pod -// swagger:response PodPauseReport -type swagPausePodResponse struct { - // in:body - Body entities.PodPauseReport -} - -// Unpause pod -// swagger:response PodUnpauseReport -type swagUnpausePodResponse struct { - // in:body - Body entities.PodUnpauseReport -} - -// Stop pod -// swagger:response PodStopReport -type swagStopPodResponse struct { - // in:body - Body entities.PodStopReport -} - -// Restart pod -// swagger:response PodRestartReport -type swagRestartPodResponse struct { - // in:body - Body entities.PodRestartReport -} - -// Start pod -// swagger:response PodStartReport -type swagStartPodResponse struct { - // in:body - Body entities.PodStartReport -} - -// Prune pod -// swagger:response PodPruneReport -type swagPrunePodResponse struct { - // in:body - Body entities.PodPruneReport -} - -// Rm pod -// swagger:response PodRmReport -type swagRmPodResponse struct { - // in:body - Body entities.PodRmReport -} - -// Info -// swagger:response InfoResponse -type swagInfoResponse struct { - // in:body - Body define.Info -} - -// Network rm -// swagger:response NetworkRmReport -type swagNetworkRmReport struct { - // in:body - Body []entities.NetworkRmReport -} - -// Network inspect -// swagger:response NetworkInspectReport -type swagNetworkInspectReport struct { - // in:body - Body types.Network -} - -// Network list -// swagger:response NetworkListReport -type swagNetworkListReport struct { - // in:body - Body []types.Network -} - -// Network create -// swagger:model NetworkCreateLibpod -type swagNetworkCreateLibpod struct { - types.Network -} - -// Network create -// swagger:response NetworkCreateReport -type swagNetworkCreateReport struct { - // in:body - Body types.Network -} - -// Network prune -// swagger:response NetworkPruneResponse -type swagNetworkPruneResponse struct { - // in:body - Body []entities.NetworkPruneReport -} - -// Network connect -// swagger:model NetworkConnectRequest -type swagNetworkConnectRequest struct { - entities.NetworkConnectOptions -} - -func ServeSwagger(w http.ResponseWriter, r *http.Request) { - path := DefaultPodmanSwaggerSpec - if p, found := os.LookupEnv("PODMAN_SWAGGER_SPEC"); found { - path = p - } - if _, err := os.Stat(path); err != nil { - if os.IsNotExist(err) { - utils.InternalServerError(w, errors.Errorf("file %q does not exist", path)) - return - } - utils.InternalServerError(w, err) - return - } - w.Header().Set("Content-Type", "text/yaml") - http.ServeFile(w, r, path) -} diff --git a/pkg/api/handlers/libpod/swagger_spec.go b/pkg/api/handlers/libpod/swagger_spec.go new file mode 100644 index 000000000..8eeb041d2 --- /dev/null +++ b/pkg/api/handlers/libpod/swagger_spec.go @@ -0,0 +1,29 @@ +package libpod + +import ( + "net/http" + "os" + + "github.com/containers/podman/v4/pkg/api/handlers/utils" + "github.com/pkg/errors" +) + +// DefaultPodmanSwaggerSpec provides the default path to the podman swagger spec file +const DefaultPodmanSwaggerSpec = "/usr/share/containers/podman/swagger.yaml" + +func ServeSwagger(w http.ResponseWriter, r *http.Request) { + path := DefaultPodmanSwaggerSpec + if p, found := os.LookupEnv("PODMAN_SWAGGER_SPEC"); found { + path = p + } + if _, err := os.Stat(path); err != nil { + if errors.Is(err, os.ErrNotExist) { + utils.InternalServerError(w, errors.Errorf("swagger spec %q does not exist", path)) + return + } + utils.InternalServerError(w, err) + return + } + w.Header().Set("Content-Type", "text/yaml") + http.ServeFile(w, r, path) +} diff --git a/pkg/api/handlers/libpod/volumes.go b/pkg/api/handlers/libpod/volumes.go index e0ea16d82..e792dea35 100644 --- a/pkg/api/handlers/libpod/volumes.go +++ b/pkg/api/handlers/libpod/volumes.go @@ -25,8 +25,7 @@ func CreateVolume(w http.ResponseWriter, r *http.Request) { runtime = r.Context().Value(api.RuntimeKey).(*libpod.Runtime) decoder = r.Context().Value(api.DecoderKey).(*schema.Decoder) ) - query := struct { - }{ + query := struct{}{ // override any golang type defaults } if err := decoder.Decode(&query, r.URL.Query()); err != nil { @@ -86,9 +85,7 @@ func CreateVolume(w http.ResponseWriter, r *http.Request) { } func InspectVolume(w http.ResponseWriter, r *http.Request) { - var ( - runtime = r.Context().Value(api.RuntimeKey).(*libpod.Runtime) - ) + runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime) name := utils.GetName(r) vol, err := runtime.GetVolume(name) if err != nil { @@ -107,9 +104,7 @@ func InspectVolume(w http.ResponseWriter, r *http.Request) { } func ListVolumes(w http.ResponseWriter, r *http.Request) { - var ( - runtime = r.Context().Value(api.RuntimeKey).(*libpod.Runtime) - ) + runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime) filterMap, err := util.PrepareFilters(r) if err != nil { utils.Error(w, http.StatusInternalServerError, @@ -153,9 +148,7 @@ func PruneVolumes(w http.ResponseWriter, r *http.Request) { } func pruneVolumesHelper(r *http.Request) ([]*reports.PruneReport, error) { - var ( - runtime = r.Context().Value(api.RuntimeKey).(*libpod.Runtime) - ) + runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime) filterMap, err := util.PrepareFilters(r) if err != nil { return nil, err |