github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/core/description/cloudimagemetadata.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package description
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"github.com/juju/schema"
     9  )
    10  
    11  type cloudimagemetadataset struct {
    12  	Version             int                   `yaml:"version"`
    13  	CloudImageMetadata_ []*cloudimagemetadata `yaml:"cloudimagemetadata"`
    14  }
    15  
    16  type cloudimagemetadata struct {
    17  	Stream_          string  `yaml:"stream"`
    18  	Region_          string  `yaml:"region"`
    19  	Version_         string  `yaml:"version"`
    20  	Series_          string  `yaml:"series"`
    21  	Arch_            string  `yaml:"arch"`
    22  	VirtType_        string  `yaml:"virt-type"`
    23  	RootStorageType_ string  `yaml:"root-storage-type"`
    24  	RootStorageSize_ *uint64 `yaml:"root-storage-size,omitempty"`
    25  	DateCreated_     int64   `yaml:"date-created"`
    26  	Source_          string  `yaml:"source"`
    27  	Priority_        int     `yaml:"priority"`
    28  	ImageId_         string  `yaml:"image-id"`
    29  }
    30  
    31  // Stream implements CloudImageMetadata.
    32  func (i *cloudimagemetadata) Stream() string {
    33  	return i.Stream_
    34  }
    35  
    36  // Region implements CloudImageMetadata.
    37  func (i *cloudimagemetadata) Region() string {
    38  	return i.Region_
    39  }
    40  
    41  // Version implements CloudImageMetadata.
    42  func (i *cloudimagemetadata) Version() string {
    43  	return i.Version_
    44  }
    45  
    46  // Series implements CloudImageMetadata.
    47  func (i *cloudimagemetadata) Series() string {
    48  	return i.Series_
    49  }
    50  
    51  // Arch implements CloudImageMetadata.
    52  func (i *cloudimagemetadata) Arch() string {
    53  	return i.Arch_
    54  }
    55  
    56  // VirtType implements CloudImageMetadata.
    57  func (i *cloudimagemetadata) VirtType() string {
    58  	return i.VirtType_
    59  }
    60  
    61  // RootStorageType implements CloudImageMetadata.
    62  func (i *cloudimagemetadata) RootStorageType() string {
    63  	return i.RootStorageType_
    64  }
    65  
    66  // RootStorageSize implements CloudImageMetadata.
    67  func (i *cloudimagemetadata) RootStorageSize() (uint64, bool) {
    68  	if i.RootStorageSize_ == nil {
    69  		return 0, false
    70  	}
    71  	return *i.RootStorageSize_, true
    72  }
    73  
    74  // DateCreated implements CloudImageMetadata.
    75  func (i *cloudimagemetadata) DateCreated() int64 {
    76  	return i.DateCreated_
    77  }
    78  
    79  // Source implements CloudImageMetadata.
    80  func (i *cloudimagemetadata) Source() string {
    81  	return i.Source_
    82  }
    83  
    84  // Priority implements CloudImageMetadata.
    85  func (i *cloudimagemetadata) Priority() int {
    86  	return i.Priority_
    87  }
    88  
    89  //ImageId implements CloudImageMetadata.
    90  func (i *cloudimagemetadata) ImageId() string {
    91  	return i.ImageId_
    92  }
    93  
    94  // CloudImageMetadataArgs is an argument struct used to create a
    95  // new internal cloudimagemetadata type that supports the CloudImageMetadata interface.
    96  type CloudImageMetadataArgs struct {
    97  	Stream          string
    98  	Region          string
    99  	Version         string
   100  	Series          string
   101  	Arch            string
   102  	VirtType        string
   103  	RootStorageType string
   104  	RootStorageSize *uint64
   105  	DateCreated     int64
   106  	Source          string
   107  	Priority        int
   108  	ImageId         string
   109  }
   110  
   111  func newCloudImageMetadata(args CloudImageMetadataArgs) *cloudimagemetadata {
   112  	cloudimagemetadata := &cloudimagemetadata{
   113  		Stream_:          args.Stream,
   114  		Region_:          args.Region,
   115  		Version_:         args.Version,
   116  		Series_:          args.Series,
   117  		Arch_:            args.Arch,
   118  		VirtType_:        args.VirtType,
   119  		RootStorageType_: args.RootStorageType,
   120  		RootStorageSize_: args.RootStorageSize,
   121  		DateCreated_:     args.DateCreated,
   122  		Source_:          args.Source,
   123  		Priority_:        args.Priority,
   124  		ImageId_:         args.ImageId,
   125  	}
   126  	return cloudimagemetadata
   127  }
   128  
   129  func importCloudImageMetadata(source map[string]interface{}) ([]*cloudimagemetadata, error) {
   130  	checker := versionedChecker("cloudimagemetadata")
   131  	coerced, err := checker.Coerce(source, nil)
   132  	if err != nil {
   133  		return nil, errors.Annotatef(err, "cloudimagemetadata version schema check failed")
   134  	}
   135  	valid := coerced.(map[string]interface{})
   136  
   137  	version := int(valid["version"].(int64))
   138  	importFunc, ok := cloudimagemetadataDeserializationFuncs[version]
   139  	if !ok {
   140  		return nil, errors.NotValidf("version %d", version)
   141  	}
   142  	sourceList := valid["cloudimagemetadata"].([]interface{})
   143  	return importCloudImageMetadataList(sourceList, importFunc)
   144  }
   145  
   146  func importCloudImageMetadataList(sourceList []interface{}, importFunc cloudimagemetadataDeserializationFunc) ([]*cloudimagemetadata, error) {
   147  	result := make([]*cloudimagemetadata, 0, len(sourceList))
   148  	for i, value := range sourceList {
   149  		source, ok := value.(map[string]interface{})
   150  		if !ok {
   151  			return nil, errors.Errorf("unexpected type for cloudimagemetadata %d, %#v", i, value)
   152  		}
   153  		cloudimagemetadata, err := importFunc(source)
   154  		if err != nil {
   155  			return nil, errors.Annotatef(err, "cloudimagemetadata %d", i)
   156  		}
   157  		result = append(result, cloudimagemetadata)
   158  	}
   159  	return result, nil
   160  }
   161  
   162  type cloudimagemetadataDeserializationFunc func(map[string]interface{}) (*cloudimagemetadata, error)
   163  
   164  var cloudimagemetadataDeserializationFuncs = map[int]cloudimagemetadataDeserializationFunc{
   165  	1: importCloudImageMetadataV1,
   166  }
   167  
   168  func importCloudImageMetadataV1(source map[string]interface{}) (*cloudimagemetadata, error) {
   169  	fields := schema.Fields{
   170  		"stream":            schema.String(),
   171  		"region":            schema.String(),
   172  		"version":           schema.String(),
   173  		"series":            schema.String(),
   174  		"arch":              schema.String(),
   175  		"virt-type":         schema.String(),
   176  		"root-storage-type": schema.String(),
   177  		"root-storage-size": schema.Uint(),
   178  		"date-created":      schema.Int(),
   179  		"source":            schema.String(),
   180  		"priority":          schema.Int(),
   181  		"image-id":          schema.String(),
   182  	}
   183  	// Some values don't have to be there.
   184  	defaults := schema.Defaults{
   185  		"root-storage-size": schema.Omit,
   186  	}
   187  	checker := schema.FieldMap(fields, defaults)
   188  
   189  	coerced, err := checker.Coerce(source, nil)
   190  	if err != nil {
   191  		return nil, errors.Annotatef(err, "cloudimagemetadata v1 schema check failed")
   192  	}
   193  	valid := coerced.(map[string]interface{})
   194  	_, ok := valid["root-storage-size"]
   195  	var pointerSize *uint64
   196  	if ok {
   197  		rootStorageSize := valid["root-storage-size"].(uint64)
   198  		pointerSize = &rootStorageSize
   199  	}
   200  
   201  	cloudimagemetadata := &cloudimagemetadata{
   202  		Stream_:          valid["stream"].(string),
   203  		Region_:          valid["region"].(string),
   204  		Version_:         valid["version"].(string),
   205  		Series_:          valid["series"].(string),
   206  		Arch_:            valid["arch"].(string),
   207  		VirtType_:        valid["virt-type"].(string),
   208  		RootStorageType_: valid["root-storage-type"].(string),
   209  		RootStorageSize_: pointerSize,
   210  		DateCreated_:     valid["date-created"].(int64),
   211  		Source_:          valid["source"].(string),
   212  		Priority_:        int(valid["priority"].(int64)),
   213  		ImageId_:         valid["image-id"].(string),
   214  	}
   215  
   216  	return cloudimagemetadata, nil
   217  }