-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.go
64 lines (55 loc) · 1.63 KB
/
image.go
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
package imagespy
import (
"fmt"
"net/http"
"time"
)
// Image is a Docker image.
type Image struct {
Created time.Time `json:"created"`
Digest string `json:"digest"`
Name string `json:"name"`
Tag string `json:"tag"`
}
// ImageSpy is a ImageSpy.
type ImageSpy struct {
CurrentImage *Image `json:"current_image"`
LatestImage *Image `json:"latest_image"`
Name string `json:"name"`
}
// ImageSpyService handles interactions.
type ImageSpyService struct {
cacheUnknownImages bool
registryWhitelist map[string]struct{}
requester *requester
}
// Get retrieves an ImageSpy.
// Creates the ImageSpy if it does not exist.
func (is *ImageSpyService) Get(name string) (*ImageSpy, error) {
whitelisted, err := isRegistryWhitelisted(name, is.registryWhitelist)
if err != nil {
return nil, err
}
if whitelisted == false {
return nil, fmt.Errorf("Registry domain of image %s is not whitelisted", name)
}
resp, err := is.requester.readAsJSON("/v1/images/" + name)
if err != nil {
return nil, err
}
switch resp.StatusCode {
case http.StatusOK:
imageSpy := &ImageSpy{}
err = is.requester.parseJSON(resp.Body, imageSpy)
if err != nil {
return nil, err
}
return imageSpy, nil
case http.StatusNotFound:
// TODO: This is only necessary because httpcache only caches when the body is read. Another solution possible?
is.requester.parseJSON(resp.Body, struct{}{})
return nil, &NotFoundError{message: fmt.Sprintf("Error retrieving ImageSpy: API returned status code %d", resp.StatusCode)}
default:
return nil, fmt.Errorf("Error retrieving ImageSpy: API returned status code %d", resp.StatusCode)
}
}