func (api *API) parseMetadataFromParams(p params.CloudImageMetadata, env environs.Environ) (cloudimagemetadata.Metadata, error) { result := cloudimagemetadata.Metadata{ cloudimagemetadata.MetadataAttributes{ Stream: p.Stream, Region: p.Region, Version: p.Version, Series: p.Series, Arch: p.Arch, VirtType: p.VirtType, RootStorageType: p.RootStorageType, RootStorageSize: p.RootStorageSize, Source: p.Source, }, p.Priority, p.ImageId, } // Fill in any required default values. if p.Stream == "" { result.Stream = env.Config().ImageStream() } if p.Source == "" { result.Source = "custom" } if result.Arch == "" { result.Arch = "amd64" } if result.Series == "" { result.Series = config.PreferredSeries(env.Config()) } if result.Region == "" { // If the env supports regions, use the env default. if r, ok := env.(simplestreams.HasRegion); ok { spec, err := r.Region() if err != nil { return cloudimagemetadata.Metadata{}, errors.Annotatef(err, "getting cloud region") } result.Region = spec.Region } } return result, nil }
// storeImageMetadataInState writes image metadata into state store. func storeImageMetadataInState(st *state.State, env environs.Environ, source string, priority int, existingMetadata []*imagemetadata.ImageMetadata) error { if len(existingMetadata) == 0 { return nil } cfg := env.Config() metadataState := make([]cloudimagemetadata.Metadata, len(existingMetadata)) for i, one := range existingMetadata { m := cloudimagemetadata.Metadata{ MetadataAttributes: cloudimagemetadata.MetadataAttributes{ Stream: one.Stream, Region: one.RegionName, Arch: one.Arch, VirtType: one.VirtType, RootStorageType: one.Storage, Source: source, Version: one.Version, }, Priority: priority, ImageId: one.Id, } s, err := seriesFromVersion(one.Version) if err != nil { return errors.Annotatef(err, "cannot determine series for version %v", one.Version) } m.Series = s if m.Stream == "" { m.Stream = cfg.ImageStream() } if m.Source == "" { m.Source = "custom" } metadataState[i] = m } if err := st.CloudImageMetadataStorage.SaveMetadata(metadataState); err != nil { return errors.Annotatef(err, "cannot cache image metadata") } return nil }