(ctx context.Context, image images.Image)
| 122 | } |
| 123 | |
| 124 | func (s *imageStore) Create(ctx context.Context, image images.Image) (images.Image, error) { |
| 125 | namespace, err := namespaces.NamespaceRequired(ctx) |
| 126 | if err != nil { |
| 127 | return images.Image{}, err |
| 128 | } |
| 129 | |
| 130 | if err := update(ctx, s.db, func(tx *bolt.Tx) error { |
| 131 | if err := validateImage(&image); err != nil { |
| 132 | return err |
| 133 | } |
| 134 | |
| 135 | bkt, err := createImagesBucket(tx, namespace) |
| 136 | if err != nil { |
| 137 | return err |
| 138 | } |
| 139 | |
| 140 | if err := addImageLease(ctx, tx, image.Name, image.Labels); err != nil { |
| 141 | return err |
| 142 | } |
| 143 | |
| 144 | ibkt, err := bkt.CreateBucket([]byte(image.Name)) |
| 145 | if err != nil { |
| 146 | if err != errbolt.ErrBucketExists { |
| 147 | return err |
| 148 | } |
| 149 | |
| 150 | return fmt.Errorf("image %q: %w", image.Name, errdefs.ErrAlreadyExists) |
| 151 | } |
| 152 | |
| 153 | // The value of `image.CreatedAt` passed from the caller is discarded here. |
| 154 | // Ideally we should return an error when the value is already set. |
| 155 | // However, as `image.CreatedAt` is defined as a non-pointer `time.Time`, we can't compare it to nil. |
| 156 | // And we can't compare it to `time.Time{}` either, as `time.Time{}` is a proper timestamp (1970-01-01 00:00:00). |
| 157 | if tm := epoch.FromContext(ctx); tm != nil { |
| 158 | image.CreatedAt = tm.UTC() |
| 159 | } else { |
| 160 | image.CreatedAt = time.Now().UTC() |
| 161 | } |
| 162 | image.UpdatedAt = image.CreatedAt |
| 163 | return writeImage(ibkt, &image) |
| 164 | }); err != nil { |
| 165 | return images.Image{}, err |
| 166 | } |
| 167 | |
| 168 | if publisher := s.db.Publisher(ctx); publisher != nil { |
| 169 | if err := publisher.Publish(ctx, "/images/create", &eventstypes.ImageCreate{ |
| 170 | Name: image.Name, |
| 171 | Labels: image.Labels, |
| 172 | }); err != nil { |
| 173 | return images.Image{}, err |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | return image, nil |
| 178 | } |
| 179 | |
| 180 | func (s *imageStore) Update(ctx context.Context, image images.Image, fieldpaths ...string) (images.Image, error) { |
| 181 | namespace, err := namespaces.NamespaceRequired(ctx) |
nothing calls this directly
no test coverage detected