/
dockerresource.go
58 lines (48 loc) · 1.85 KB
/
dockerresource.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
// Copyright 2018 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package resources
import (
// Import shas that are used for docker image validation.
_ "crypto/sha256"
_ "crypto/sha512"
"encoding/json"
"github.com/docker/distribution/reference"
"github.com/juju/errors"
"gopkg.in/yaml.v2"
"github.com/juju/juju/docker"
)
// DockerImageDetails holds the details for a Docker resource type.
type DockerImageDetails struct {
// RegistryPath holds the path of the Docker image (including host and sha256) in a docker registry.
RegistryPath string `json:"ImageName" yaml:"registrypath"`
docker.ImageRepoDetails `json:",inline" yaml:",inline"`
}
// IsPrivate shows if the image repo is private or not.
func (did DockerImageDetails) IsPrivate() bool {
return did.ImageRepoDetails.IsPrivate()
}
// ValidateDockerRegistryPath ensures the registry path is valid (i.e. api.jujucharms.com@sha256:deadbeef)
func ValidateDockerRegistryPath(path string) error {
_, err := reference.ParseNormalizedNamed(path)
if err != nil {
return errors.NotValidf("docker image path %q", path)
}
return nil
}
// CheckDockerDetails validates the provided resource is suitable for use.
func CheckDockerDetails(name string, details DockerImageDetails) error {
// TODO (veebers): Validate the URL actually works.
return ValidateDockerRegistryPath(details.RegistryPath)
}
// UnmarshalDockerResource unmarshals the docker resource file from data.
func UnmarshalDockerResource(data []byte) (DockerImageDetails, error) {
var resourceBody DockerImageDetails
// Older clients sent the resources as a json string.
err := json.Unmarshal(data, &resourceBody)
if err != nil {
if err := yaml.Unmarshal(data, &resourceBody); err != nil {
return DockerImageDetails{}, errors.Annotate(err, "docker resource is neither valid json or yaml")
}
}
return resourceBody, nil
}