forked from harbur/docker-multi-tenancy
/
image.go
73 lines (54 loc) · 1.79 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
65
66
67
68
69
70
71
72
73
package main
import (
"net/http"
"regexp"
"encoding/json"
"bytes"
"bufio"
"io/ioutil"
"encoding/binary"
"log"
)
// APIImages represent an image returned in the ListImages call.
type APIImages struct {
ID string `json:"Id" yaml:"Id"`
RepoTags []string `json:"RepoTags,omitempty" yaml:"RepoTags,omitempty"`
Created int64 `json:"Created,omitempty" yaml:"Created,omitempty"`
Size int64 `json:"Size,omitempty" yaml:"Size,omitempty"`
VirtualSize int64 `json:"VirtualSize,omitempty" yaml:"VirtualSize,omitempty"`
ParentID string `json:"ParentId,omitempty" yaml:"ParentId,omitempty"`
RepoDigests []string `json:"RepoDigests,omitempty" yaml:"RepoDigests,omitempty"`
Labels map[string]string `json:"Labels,omitempty" yaml:"Labels,omitempty"`
}
type imageTransformer struct{}
func NewImageListTransformer() *Transformer {
t := &Transformer{}
t.Regexp = regexp.MustCompile("/v.*/images/json")
t.MultiTenancyTransformer = &imageTransformer{}
return t
}
func (c *imageTransformer) transformRequest(r *http.Request) {
log.Println("NewImageListTransformer.transformRequest")
}
func (c *imageTransformer) transformResponse(r *http.Response) {
var images []APIImages
if err := json.NewDecoder(r.Body).Decode(&images); err != nil {
return
}
for _, im := range images{
if im.Labels == nil {
im.Labels = make(map[string]string)
}
im.Labels["hola"] = "world"
}
var b bytes.Buffer
w := bufio.NewWriter(&b)
// Now take the struct and encode it
if err := json.NewEncoder(w).Encode(&images); err != nil {
return
}
// Restore the io.ReadCloser to its original state
r.Body = ioutil.NopCloser(bytes.NewBuffer(b.Bytes()))
// Set size of modified body
r.ContentLength = int64(binary.Size(b))
}