launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/environs/imagemetadata/validation.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package imagemetadata
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"launchpad.net/juju-core/environs/simplestreams"
    10  )
    11  
    12  // ValidateImageMetadata attempts to load image metadata for the specified cloud attributes and stream
    13  // and returns any image ids found, or an error if the metadata could not be loaded.
    14  func ValidateImageMetadata(params *simplestreams.MetadataLookupParams) ([]string, error) {
    15  	if params.Series == "" {
    16  		return nil, fmt.Errorf("required parameter series not specified")
    17  	}
    18  	if params.Region == "" {
    19  		return nil, fmt.Errorf("required parameter region not specified")
    20  	}
    21  	if params.Endpoint == "" {
    22  		return nil, fmt.Errorf("required parameter endpoint not specified")
    23  	}
    24  	if len(params.Architectures) == 0 {
    25  		return nil, fmt.Errorf("required parameter arches not specified")
    26  	}
    27  	if len(params.Sources) == 0 {
    28  		return nil, fmt.Errorf("required parameter sources not specified")
    29  	}
    30  	imageConstraint := NewImageConstraint(simplestreams.LookupParams{
    31  		CloudSpec: simplestreams.CloudSpec{params.Region, params.Endpoint},
    32  		Series:    []string{params.Series},
    33  		Arches:    params.Architectures,
    34  		Stream:    params.Stream,
    35  	})
    36  	matchingImages, err := Fetch(params.Sources, simplestreams.DefaultIndexPath, imageConstraint, false)
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  	if len(matchingImages) == 0 {
    41  		return nil, fmt.Errorf("no matching images found for constraint %+v", imageConstraint)
    42  	}
    43  	image_ids := make([]string, len(matchingImages))
    44  	for i, im := range matchingImages {
    45  		image_ids[i] = im.Id
    46  	}
    47  	return image_ids, nil
    48  }