1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
|
// Copyright (c) 2018-2023, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
package client
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
jsonresp "github.com/sylabs/json-resp"
)
// getEntity returns the specified entity; returns ErrNotFound if entity is not
// found, otherwise error
func (c *Client) getEntity(ctx context.Context, entityRef string) (*Entity, error) {
entJSON, err := c.apiGet(ctx, "v1/entities/"+entityRef)
if err != nil {
return nil, err
}
var res EntityResponse
if err := json.Unmarshal(entJSON, &res); err != nil {
return nil, fmt.Errorf("error decoding entity: %w", err)
}
return &res.Data, nil
}
// getCollection returns the specified collection; returns ErrNotFound if
// collection is not found, otherwise error.
func (c *Client) getCollection(ctx context.Context, collectionRef string) (*Collection, error) {
colJSON, err := c.apiGet(ctx, "v1/collections/"+collectionRef)
if err != nil {
return nil, err
}
var res CollectionResponse
if err := json.Unmarshal(colJSON, &res); err != nil {
return nil, fmt.Errorf("error decoding collection: %w", err)
}
return &res.Data, nil
}
// getContainer returns container by ref id; returns ErrNotFound if container
// is not found, otherwise error.
func (c *Client) getContainer(ctx context.Context, containerRef string) (*Container, error) {
conJSON, err := c.apiGet(ctx, "v1/containers/"+containerRef)
if err != nil {
return nil, err
}
var res ContainerResponse
if err := json.Unmarshal(conJSON, &res); err != nil {
return nil, fmt.Errorf("error decoding container: %w", err)
}
return &res.Data, nil
}
// createEntity creates an entity (must be authorized)
func (c *Client) createEntity(ctx context.Context, name string) (*Entity, error) {
e := Entity{
Name: name,
Description: "No description",
}
entJSON, err := c.apiCreate(ctx, "v1/entities", e)
if err != nil {
return nil, err
}
var res EntityResponse
if err := json.Unmarshal(entJSON, &res); err != nil {
return nil, fmt.Errorf("error decoding entity: %w", err)
}
return &res.Data, nil
}
// createCollection creates a new collection
func (c *Client) createCollection(ctx context.Context, name string, entityID string) (*Collection, error) {
newCollection := Collection{
Name: name,
Description: "No description",
Entity: entityID,
}
colJSON, err := c.apiCreate(ctx, "v1/collections", newCollection)
if err != nil {
return nil, err
}
var res CollectionResponse
if err := json.Unmarshal(colJSON, &res); err != nil {
return nil, fmt.Errorf("error decoding collection: %w", err)
}
return &res.Data, nil
}
// createContainer creates a container in the specified collection
func (c *Client) createContainer(ctx context.Context, name string, collectionID string) (*Container, error) {
newContainer := Container{
Name: name,
Description: "No description",
Collection: collectionID,
}
conJSON, err := c.apiCreate(ctx, "v1/containers", newContainer)
if err != nil {
return nil, err
}
var res ContainerResponse
if err := json.Unmarshal(conJSON, &res); err != nil {
return nil, fmt.Errorf("error decoding container: %w", err)
}
return &res.Data, nil
}
// createImage creates a new image
func (c *Client) createImage(ctx context.Context, hash string, containerID string, description string) (*Image, error) {
i := Image{
Hash: hash,
Description: description,
Container: containerID,
}
imgJSON, err := c.apiCreate(ctx, "v1/images", i)
if err != nil {
return nil, err
}
var res ImageResponse
if err := json.Unmarshal(imgJSON, &res); err != nil {
return nil, fmt.Errorf("error decoding image: %w", err)
}
return &res.Data, nil
}
// setTags applies tags to the specified container
func (c *Client) setTags(ctx context.Context, containerID, imageID string, tags []string) error {
// Get existing tags, so we know which will be replaced
existingTags, err := c.getTags(ctx, containerID)
if err != nil {
return err
}
for _, tag := range tags {
c.Logger.Logf("Setting tag %s", tag)
if _, ok := existingTags[tag]; ok {
c.Logger.Logf("%s replaces an existing tag", tag)
}
imgTag := ImageTag{
tag,
imageID,
}
err := c.setTag(ctx, containerID, imgTag)
if err != nil {
return err
}
}
return nil
}
// getTags returns a tag map for the specified containerID
func (c *Client) getTags(ctx context.Context, containerID string) (TagMap, error) {
url := fmt.Sprintf("v1/tags/%s", containerID)
c.Logger.Logf("getTags calling %s", url)
req, err := c.newRequest(ctx, http.MethodGet, url, "", nil)
if err != nil {
return nil, fmt.Errorf("error creating request to server:\n\t%w", err)
}
res, err := c.HTTPClient.Do(req)
if err != nil {
return nil, fmt.Errorf("error making request to server:\n\t%w", err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
err := jsonresp.ReadError(res.Body)
if err != nil {
return nil, fmt.Errorf("creation did not succeed: %w", err)
}
return nil, fmt.Errorf("%w: unexpected http status code: %d", errHTTP, res.StatusCode)
}
var tagRes TagsResponse
err = json.NewDecoder(res.Body).Decode(&tagRes)
if err != nil {
return nil, fmt.Errorf("error decoding tags: %w", err)
}
return tagRes.Data, nil
}
// setTag sets tag on specified containerID
func (c *Client) setTag(ctx context.Context, containerID string, t ImageTag) error {
url := "v1/tags/" + containerID
c.Logger.Logf("setTag calling %s", url)
s, err := json.Marshal(t)
if err != nil {
return fmt.Errorf("error encoding object to JSON:\n\t%w", err)
}
req, err := c.newRequest(ctx, http.MethodPost, url, "", bytes.NewBuffer(s))
if err != nil {
return fmt.Errorf("error creating POST request:\n\t%w", err)
}
res, err := c.HTTPClient.Do(req)
if err != nil {
return fmt.Errorf("error making request to server:\n\t%w", err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
err := jsonresp.ReadError(res.Body)
if err != nil {
return fmt.Errorf("creation did not succeed: %w", err)
}
return fmt.Errorf("%w: creation did not succeed: http status code: %d", errHTTP, res.StatusCode)
}
return nil
}
// setTags applies tags to the specified container
func (c *Client) setTagsV2(ctx context.Context, containerID, arch string, imageID string, tags []string) error {
// Get existing tags, so we know which will be replaced
existingTags, err := c.getTagsV2(ctx, containerID)
if err != nil {
return err
}
for _, tag := range tags {
c.Logger.Logf("Setting tag %s", tag)
if _, ok := existingTags[arch][tag]; ok {
c.Logger.Logf("%s replaces an existing tag for arch %s", tag, arch)
}
imgTag := ArchImageTag{
Arch: arch,
Tag: tag,
ImageID: imageID,
}
err := c.setTagV2(ctx, containerID, imgTag)
if err != nil {
return err
}
}
return nil
}
// getTagsV2 returns a arch->tag map for the specified containerID
func (c *Client) getTagsV2(ctx context.Context, containerID string) (ArchTagMap, error) {
url := fmt.Sprintf("v2/tags/%s", containerID)
c.Logger.Logf("getTagsV2 calling %s", url)
req, err := c.newRequest(ctx, http.MethodGet, url, "", nil)
if err != nil {
return nil, fmt.Errorf("error creating request to server:\n\t%w", err)
}
res, err := c.HTTPClient.Do(req)
if err != nil {
return nil, fmt.Errorf("error making request to server:\n\t%w", err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
err := jsonresp.ReadError(res.Body)
if err != nil {
return nil, fmt.Errorf("creation did not succeed: %w", err)
}
return nil, fmt.Errorf("%w: unexpected http status code: %d", errHTTP, res.StatusCode)
}
var tagRes ArchTagsResponse
err = json.NewDecoder(res.Body).Decode(&tagRes)
if err != nil {
return nil, fmt.Errorf("error decoding tags: %w", err)
}
return tagRes.Data, nil
}
// setTag sets an arch->tag on specified containerID
func (c *Client) setTagV2(ctx context.Context, containerID string, t ArchImageTag) error {
url := "v2/tags/" + containerID
c.Logger.Logf("setTag calling %s", url)
s, err := json.Marshal(t)
if err != nil {
return fmt.Errorf("error encoding object to JSON:\n\t%w", err)
}
req, err := c.newRequest(ctx, http.MethodPost, url, "", bytes.NewBuffer(s))
if err != nil {
return fmt.Errorf("error creating POST request:\n\t%w", err)
}
res, err := c.HTTPClient.Do(req)
if err != nil {
return fmt.Errorf("error making request to server:\n\t%w", err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
err := jsonresp.ReadError(res.Body)
if err != nil {
return fmt.Errorf("creation did not succeed: %w", err)
}
return fmt.Errorf("%w: creation did not succeed: http status code: %d", errHTTP, res.StatusCode)
}
return nil
}
// GetImage returns the Image object if exists; returns ErrNotFound if image is
// not found, otherwise error.
func (c *Client) GetImage(ctx context.Context, arch string, imageRef string) (*Image, error) {
q := url.Values{}
q.Add("arch", arch)
apiURL := &url.URL{
Path: "v1/images/" + imageRef,
RawQuery: q.Encode(),
}
imgJSON, err := c.apiGet(ctx, apiURL.String())
if err != nil {
return nil, err
}
var res ImageResponse
if err := json.Unmarshal(imgJSON, &res); err != nil {
return nil, fmt.Errorf("error decoding image: %w", err)
}
return &res.Data, nil
}
|