Exemple #1
0
func importConstraintsV1(source map[string]interface{}) (*constraints, error) {
	fields := schema.Fields{
		"architecture":  schema.String(),
		"container":     schema.String(),
		"cpu-cores":     schema.Uint(),
		"cpu-power":     schema.Uint(),
		"instance-type": schema.String(),
		"memory":        schema.Uint(),
		"root-disk":     schema.Uint(),

		"spaces": schema.List(schema.String()),
		"tags":   schema.List(schema.String()),
	}
	// Some values don't have to be there.
	defaults := schema.Defaults{
		"architecture":  "",
		"container":     "",
		"cpu-cores":     uint64(0),
		"cpu-power":     uint64(0),
		"instance-type": "",
		"memory":        uint64(0),
		"root-disk":     uint64(0),

		"spaces": schema.Omit,
		"tags":   schema.Omit,
	}
	checker := schema.FieldMap(fields, defaults)

	coerced, err := checker.Coerce(source, nil)
	if err != nil {
		return nil, errors.Annotatef(err, "constraints v1 schema check failed")
	}
	valid := coerced.(map[string]interface{})
	// From here we know that the map returned from the schema coercion
	// contains fields of the right type.

	return &constraints{
		Version:       1,
		Architecture_: valid["architecture"].(string),
		Container_:    valid["container"].(string),
		CpuCores_:     valid["cpu-cores"].(uint64),
		CpuPower_:     valid["cpu-power"].(uint64),
		InstanceType_: valid["instance-type"].(string),
		Memory_:       valid["memory"].(uint64),
		RootDisk_:     valid["root-disk"].(uint64),

		Spaces_: convertToStringSlice(valid["spaces"]),
		Tags_:   convertToStringSlice(valid["tags"]),
	}, nil
}
Exemple #2
0
func importStorageConstraintV1(source map[string]interface{}) (*storageconstraint, error) {
	fields := schema.Fields{
		"pool":  schema.String(),
		"size":  schema.Uint(),
		"count": schema.Uint(),
	}
	checker := schema.FieldMap(fields, nil)

	coerced, err := checker.Coerce(source, nil)
	if err != nil {
		return nil, errors.Annotatef(err, "storageconstraint v1 schema check failed")
	}
	valid := coerced.(map[string]interface{})
	// From here we know that the map returned from the schema coercion
	// contains fields of the right type.

	return &storageconstraint{
		Version: 1,
		Pool_:   valid["pool"].(string),
		Size_:   valid["size"].(uint64),
		Count_:  valid["count"].(uint64),
	}, nil
}
Exemple #3
0
func importCloudImageMetadataV1(source map[string]interface{}) (*cloudimagemetadata, error) {
	fields := schema.Fields{
		"stream":            schema.String(),
		"region":            schema.String(),
		"version":           schema.String(),
		"series":            schema.String(),
		"arch":              schema.String(),
		"virt-type":         schema.String(),
		"root-storage-type": schema.String(),
		"root-storage-size": schema.Uint(),
		"date-created":      schema.Int(),
		"source":            schema.String(),
		"priority":          schema.Int(),
		"image-id":          schema.String(),
	}
	// Some values don't have to be there.
	defaults := schema.Defaults{
		"root-storage-size": schema.Omit,
	}
	checker := schema.FieldMap(fields, defaults)

	coerced, err := checker.Coerce(source, nil)
	if err != nil {
		return nil, errors.Annotatef(err, "cloudimagemetadata v1 schema check failed")
	}
	valid := coerced.(map[string]interface{})
	_, ok := valid["root-storage-size"]
	var pointerSize *uint64
	if ok {
		rootStorageSize := valid["root-storage-size"].(uint64)
		pointerSize = &rootStorageSize
	}

	cloudimagemetadata := &cloudimagemetadata{
		Stream_:          valid["stream"].(string),
		Region_:          valid["region"].(string),
		Version_:         valid["version"].(string),
		Series_:          valid["series"].(string),
		Arch_:            valid["arch"].(string),
		VirtType_:        valid["virt-type"].(string),
		RootStorageType_: valid["root-storage-type"].(string),
		RootStorageSize_: pointerSize,
		DateCreated_:     valid["date-created"].(int64),
		Source_:          valid["source"].(string),
		Priority_:        int(valid["priority"].(int64)),
		ImageId_:         valid["image-id"].(string),
	}

	return cloudimagemetadata, nil
}