Exemple #1
0
func (s *localServerSuite) TestValidateImageMetadata(c *C) {
	params, err := s.Env.(imagemetadata.ImageMetadataValidator).MetadataLookupParams("some-region")
	c.Assert(err, IsNil)
	params.Series = "raring"
	image_ids, err := imagemetadata.ValidateImageMetadata(params)
	c.Assert(err, IsNil)
	c.Assert(image_ids, DeepEquals, []string{"id-y"})
}
Exemple #2
0
func (t *localServerSuite) TestValidateImageMetadata(c *C) {
	params, err := t.env.(imagemetadata.ImageMetadataValidator).MetadataLookupParams("test")
	c.Assert(err, IsNil)
	params.Series = "precise"
	params.Endpoint = "https://ec2.endpoint.com"
	image_ids, err := imagemetadata.ValidateImageMetadata(params)
	c.Assert(err, IsNil)
	sort.Strings(image_ids)
	c.Assert(image_ids, DeepEquals, []string{"ami-00000033", "ami-00000034", "ami-00000035"})
}
func (c *ValidateImageMetadataCommand) Run(context *cmd.Context) error {
	var params *imagemetadata.MetadataLookupParams

	if c.providerType == "" {
		environ, err := environs.NewFromName(c.EnvName)
		if err != nil {
			return err
		}
		mdLookup, ok := environ.(imagemetadata.ImageMetadataValidator)
		if !ok {
			return fmt.Errorf("%s provider does not support image metadata validation", environ.Config().Type())
		}
		params, err = mdLookup.MetadataLookupParams(c.region)
		if err != nil {
			return err
		}
	} else {
		prov, err := environs.Provider(c.providerType)
		if err != nil {
			return err
		}
		mdLookup, ok := prov.(imagemetadata.ImageMetadataValidator)
		if !ok {
			return fmt.Errorf("%s provider does not support image metadata validation", c.providerType)
		}
		params, err = mdLookup.MetadataLookupParams(c.region)
		if err != nil {
			return err
		}
	}

	if c.series != "" {
		params.Series = c.series
	}
	if c.region != "" {
		params.Region = c.region
	}
	if c.endpoint != "" {
		params.Endpoint = c.endpoint
	}
	// If the metadata files are to be loaded from a directory, we need to register
	// a file http transport.
	if c.metadataDir != "" {
		if _, err := os.Stat(c.metadataDir); err != nil {
			return err
		}

		params.BaseURLs = []string{"file://" + c.metadataDir}
		t := &http.Transport{}
		t.RegisterProtocol("file", http.NewFileTransport(http.Dir("/")))
		c := &http.Client{Transport: t}
		imagemetadata.SetHttpClient(c)
	}

	image_ids, err := imagemetadata.ValidateImageMetadata(params)
	if err != nil {
		return err
	}

	if len(image_ids) > 0 {
		fmt.Fprintf(context.Stdout, "matching image ids for region %q:\n%s\n", params.Region, strings.Join(image_ids, "\n"))
	} else {
		return fmt.Errorf("no matching image ids for region %s using URLs:\n%s", params.Region, strings.Join(params.BaseURLs, "\n"))
	}
	return nil
}