diff options
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/api/handlers/libpod/manifests.go | 18 | ||||
-rw-r--r-- | pkg/api/server/register_manifest.go | 20 | ||||
-rw-r--r-- | pkg/bindings/manifests/manifests.go | 13 | ||||
-rw-r--r-- | pkg/bindings/manifests/types.go | 6 | ||||
-rw-r--r-- | pkg/bindings/manifests/types_exists_options.go | 88 | ||||
-rw-r--r-- | pkg/domain/entities/engine_image.go | 1 | ||||
-rw-r--r-- | pkg/domain/infra/abi/manifest.go | 12 | ||||
-rw-r--r-- | pkg/domain/infra/tunnel/manifest.go | 9 |
8 files changed, 167 insertions, 0 deletions
diff --git a/pkg/api/handlers/libpod/manifests.go b/pkg/api/handlers/libpod/manifests.go index dce861f6f..35221ecf1 100644 --- a/pkg/api/handlers/libpod/manifests.go +++ b/pkg/api/handlers/libpod/manifests.go @@ -48,6 +48,24 @@ func ManifestCreate(w http.ResponseWriter, r *http.Request) { utils.WriteResponse(w, http.StatusOK, handlers.IDResponse{ID: manID}) } +// ExistsManifest check if a manifest list exists +func ExistsManifest(w http.ResponseWriter, r *http.Request) { + runtime := r.Context().Value("runtime").(*libpod.Runtime) + name := utils.GetName(r) + + ic := abi.ImageEngine{Libpod: runtime} + report, err := ic.ManifestExists(r.Context(), name) + if err != nil { + utils.Error(w, "Something went wrong.", http.StatusInternalServerError, err) + return + } + if !report.Value { + utils.Error(w, "manifest not found", http.StatusNotFound, errors.New("manifest not found")) + return + } + utils.WriteResponse(w, http.StatusNoContent, "") +} + func ManifestInspect(w http.ResponseWriter, r *http.Request) { runtime := r.Context().Value("runtime").(*libpod.Runtime) name := utils.GetName(r) diff --git a/pkg/api/server/register_manifest.go b/pkg/api/server/register_manifest.go index 5e7d94538..eefcc396e 100644 --- a/pkg/api/server/register_manifest.go +++ b/pkg/api/server/register_manifest.go @@ -38,6 +38,26 @@ func (s *APIServer) registerManifestHandlers(r *mux.Router) error { // 500: // $ref: "#/responses/InternalError" r.Handle(VersionedPath("/libpod/manifests/create"), s.APIHandler(libpod.ManifestCreate)).Methods(http.MethodPost) + // swagger:operation GET /libpod/manifests/{name}/exists manifests Exists + // --- + // summary: Exists + // description: Check if manifest list exists + // parameters: + // - in: path + // name: name + // type: string + // required: true + // description: the name of the manifest list + // produces: + // - application/json + // responses: + // 204: + // description: manifest list exists + // 404: + // $ref: '#/responses/NoSuchManifest' + // 500: + // $ref: '#/responses/InternalError' + r.Handle(VersionedPath("/libpod/manifests/{name}/exists"), s.APIHandler(libpod.ExistsManifest)).Methods(http.MethodGet) // swagger:operation GET /libpod/manifests/{name:.*}/json manifests Inspect // --- // summary: Inspect diff --git a/pkg/bindings/manifests/manifests.go b/pkg/bindings/manifests/manifests.go index b6db64b02..fec9832a0 100644 --- a/pkg/bindings/manifests/manifests.go +++ b/pkg/bindings/manifests/manifests.go @@ -49,6 +49,19 @@ func Create(ctx context.Context, names, images []string, options *CreateOptions) return idr.ID, response.Process(&idr) } +// Exists returns true if a given maifest list exists +func Exists(ctx context.Context, name string, options *ExistsOptions) (bool, error) { + conn, err := bindings.GetClient(ctx) + if err != nil { + return false, err + } + response, err := conn.DoRequest(nil, http.MethodGet, "/manifests/%s/exists", nil, nil, name) + if err != nil { + return false, err + } + return response.IsSuccess(), nil +} + // Inspect returns a manifest list for a given name. func Inspect(ctx context.Context, name string, options *InspectOptions) (*manifest.Schema2List, error) { var list manifest.Schema2List diff --git a/pkg/bindings/manifests/types.go b/pkg/bindings/manifests/types.go index 7f84d69fc..fde90a865 100644 --- a/pkg/bindings/manifests/types.go +++ b/pkg/bindings/manifests/types.go @@ -11,6 +11,12 @@ type CreateOptions struct { All *bool } +//go:generate go run ../generator/generator.go ExistsOptions +// ExistsOptions are optional options for checking +// if a manifest list exists +type ExistsOptions struct { +} + //go:generate go run ../generator/generator.go AddOptions // AddOptions are optional options for adding manifests type AddOptions struct { diff --git a/pkg/bindings/manifests/types_exists_options.go b/pkg/bindings/manifests/types_exists_options.go new file mode 100644 index 000000000..fd2cd3ee9 --- /dev/null +++ b/pkg/bindings/manifests/types_exists_options.go @@ -0,0 +1,88 @@ +package manifests + +import ( + "net/url" + "reflect" + "strconv" + "strings" + + jsoniter "github.com/json-iterator/go" + "github.com/pkg/errors" +) + +/* +This file is generated automatically by go generate. Do not edit. +*/ + +// Changed +func (o *ExistsOptions) Changed(fieldName string) bool { + r := reflect.ValueOf(o) + value := reflect.Indirect(r).FieldByName(fieldName) + return !value.IsNil() +} + +// ToParams +func (o *ExistsOptions) ToParams() (url.Values, error) { + params := url.Values{} + if o == nil { + return params, nil + } + json := jsoniter.ConfigCompatibleWithStandardLibrary + s := reflect.ValueOf(o) + if reflect.Ptr == s.Kind() { + s = s.Elem() + } + sType := s.Type() + for i := 0; i < s.NumField(); i++ { + fieldName := sType.Field(i).Name + if !o.Changed(fieldName) { + continue + } + fieldName = strings.ToLower(fieldName) + f := s.Field(i) + if reflect.Ptr == f.Kind() { + f = f.Elem() + } + switch f.Kind() { + case reflect.Bool: + params.Set(fieldName, strconv.FormatBool(f.Bool())) + case reflect.String: + params.Set(fieldName, f.String()) + case reflect.Int, reflect.Int64: + // f.Int() is always an int64 + params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) + case reflect.Uint, reflect.Uint64: + // f.Uint() is always an uint64 + params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) + case reflect.Slice: + typ := reflect.TypeOf(f.Interface()).Elem() + switch typ.Kind() { + case reflect.String: + sl := f.Slice(0, f.Len()) + s, ok := sl.Interface().([]string) + if !ok { + return nil, errors.New("failed to convert to string slice") + } + for _, val := range s { + params.Add(fieldName, val) + } + default: + return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) + } + case reflect.Map: + lowerCaseKeys := make(map[string][]string) + iter := f.MapRange() + for iter.Next() { + lowerCaseKeys[iter.Key().Interface().(string)] = iter.Value().Interface().([]string) + + } + s, err := json.MarshalToString(lowerCaseKeys) + if err != nil { + return nil, err + } + + params.Set(fieldName, s) + } + } + return params, nil +} diff --git a/pkg/domain/entities/engine_image.go b/pkg/domain/entities/engine_image.go index 935ee6f20..ee611502f 100644 --- a/pkg/domain/entities/engine_image.go +++ b/pkg/domain/entities/engine_image.go @@ -32,6 +32,7 @@ type ImageEngine interface { Unmount(ctx context.Context, images []string, options ImageUnmountOptions) ([]*ImageUnmountReport, error) Untag(ctx context.Context, nameOrID string, tags []string, options ImageUntagOptions) error ManifestCreate(ctx context.Context, names, images []string, opts ManifestCreateOptions) (string, error) + ManifestExists(ctx context.Context, name string) (*BoolReport, error) ManifestInspect(ctx context.Context, name string) ([]byte, error) ManifestAdd(ctx context.Context, opts ManifestAddOptions) (string, error) ManifestAnnotate(ctx context.Context, names []string, opts ManifestAnnotateOptions) (string, error) diff --git a/pkg/domain/infra/abi/manifest.go b/pkg/domain/infra/abi/manifest.go index 139032ad6..626f1f7bf 100644 --- a/pkg/domain/infra/abi/manifest.go +++ b/pkg/domain/infra/abi/manifest.go @@ -40,6 +40,18 @@ func (ir *ImageEngine) ManifestCreate(ctx context.Context, names, images []strin return imageID, err } +// ManifestExists checks if a manifest list with the given name exists in local storage +func (ir *ImageEngine) ManifestExists(ctx context.Context, name string) (*entities.BoolReport, error) { + if image, err := ir.Libpod.ImageRuntime().NewFromLocal(name); err == nil { + exists, err := image.ExistsManifest() + if err != nil && errors.Cause(err) != buildahManifests.ErrManifestTypeNotSupported { + return nil, err + } + return &entities.BoolReport{Value: exists}, nil + } + return &entities.BoolReport{Value: false}, nil +} + // ManifestInspect returns the content of a manifest list or image func (ir *ImageEngine) ManifestInspect(ctx context.Context, name string) ([]byte, error) { if newImage, err := ir.Libpod.ImageRuntime().NewFromLocal(name); err == nil { diff --git a/pkg/domain/infra/tunnel/manifest.go b/pkg/domain/infra/tunnel/manifest.go index 22ca44165..c12ba0045 100644 --- a/pkg/domain/infra/tunnel/manifest.go +++ b/pkg/domain/infra/tunnel/manifest.go @@ -23,6 +23,15 @@ func (ir *ImageEngine) ManifestCreate(ctx context.Context, names, images []strin return imageID, err } +// ManifestExists checks if a manifest list with the given name exists +func (ir *ImageEngine) ManifestExists(ctx context.Context, name string) (*entities.BoolReport, error) { + exists, err := manifests.Exists(ir.ClientCtx, name, nil) + if err != nil { + return nil, err + } + return &entities.BoolReport{Value: exists}, nil +} + // ManifestInspect returns contents of manifest list with given name func (ir *ImageEngine) ManifestInspect(ctx context.Context, name string) ([]byte, error) { list, err := manifests.Inspect(ir.ClientCtx, name, nil) |