github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/core/resources/resources.go (about)

     1  // Copyright 2018 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package resources
     5  
     6  import (
     7  	// Import shas that are used for docker image validation.
     8  	_ "crypto/sha256"
     9  	_ "crypto/sha512"
    10  
    11  	"github.com/docker/distribution/reference"
    12  	"github.com/juju/errors"
    13  )
    14  
    15  // DockerImageDetails holds the details for a Docker resource type.
    16  type DockerImageDetails struct {
    17  	// RegistryPath holds the path of the Docker image (including host and sha256) in a docker registry.
    18  	RegistryPath string `json:"ImageName" yaml:"registrypath"`
    19  
    20  	// Username holds the username used to gain access to a non-public image.
    21  	Username string `json:"Username" yaml:"username"`
    22  
    23  	// Password holds the password used to gain access to a non-public image.
    24  	Password string `json:"Password,omitempty" yaml:"password"`
    25  }
    26  
    27  // ValidateDockerRegistryPath ensures the registry path is valid (i.e. api.jujucharms.com@sha256:deadbeef)
    28  func ValidateDockerRegistryPath(path string) error {
    29  	_, err := reference.ParseNormalizedNamed(path)
    30  	if err != nil {
    31  		return errors.NotValidf("docker image path %q", path)
    32  	}
    33  	return nil
    34  }
    35  
    36  // CheckDockerDetails validates the provided resource is suitable for use.
    37  func CheckDockerDetails(name string, details DockerImageDetails) error {
    38  	// TODO (veebers): Validate the URL actually works.
    39  	return ValidateDockerRegistryPath(details.RegistryPath)
    40  }