func importSpaceV1(source map[string]interface{}) (*space, error) { fields := schema.Fields{ "name": schema.String(), "public": schema.Bool(), "provider-id": schema.String(), } // Some values don't have to be there. defaults := schema.Defaults{ "provider-id": "", } checker := schema.FieldMap(fields, defaults) coerced, err := checker.Coerce(source, nil) if err != nil { return nil, errors.Annotatef(err, "space 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 &space{ Name_: valid["name"].(string), Public_: valid["public"].(bool), ProviderID_: valid["provider-id"].(string), }, nil }
func importAgentToolsV1(source map[string]interface{}) (*agentTools, error) { fields := schema.Fields{ "tools-version": schema.String(), "url": schema.String(), "sha256": schema.String(), "size": schema.Int(), } checker := schema.FieldMap(fields, nil) // no defaults coerced, err := checker.Coerce(source, nil) if err != nil { return nil, errors.Annotatef(err, "agentTools 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. verString := valid["tools-version"].(string) toolsVersion, err := version.ParseBinary(verString) if err != nil { return nil, errors.Annotatef(err, "agentTools tools-version") } return &agentTools{ Version_: 1, ToolsVersion_: toolsVersion, URL_: valid["url"].(string), SHA256_: valid["sha256"].(string), Size_: valid["size"].(int64), }, nil }
func file_2_0(source map[string]interface{}) (*file, error) { fields := schema.Fields{ "resource_uri": schema.String(), "filename": schema.String(), "anon_resource_uri": schema.String(), "content": schema.String(), } defaults := schema.Defaults{ "content": "", } checker := schema.FieldMap(fields, defaults) coerced, err := checker.Coerce(source, nil) if err != nil { return nil, WrapWithDeserializationError(err, "file 2.0 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. anonURI, err := url.ParseRequestURI(valid["anon_resource_uri"].(string)) if err != nil { return nil, NewUnexpectedError(err) } result := &file{ resourceURI: valid["resource_uri"].(string), filename: valid["filename"].(string), anonymousURI: anonURI, content: valid["content"].(string), } return result, nil }
func importStatusV1(source map[string]interface{}) (StatusPoint_, error) { fields := schema.Fields{ "value": schema.String(), "message": schema.String(), "data": schema.StringMap(schema.Any()), "updated": schema.Time(), } // Some values don't have to be there. defaults := schema.Defaults{ "message": "", "data": schema.Omit, } checker := schema.FieldMap(fields, defaults) coerced, err := checker.Coerce(source, nil) if err != nil { return StatusPoint_{}, errors.Annotatef(err, "status 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. var data map[string]interface{} if sourceData, set := valid["data"]; set { data = sourceData.(map[string]interface{}) } return StatusPoint_{ Value_: valid["value"].(string), Message_: valid["message"].(string), Data_: data, Updated_: valid["updated"].(time.Time), }, nil }
func space_2_0(source map[string]interface{}) (*space, error) { fields := schema.Fields{ "resource_uri": schema.String(), "id": schema.ForceInt(), "name": schema.String(), "subnets": schema.List(schema.StringMap(schema.Any())), } checker := schema.FieldMap(fields, nil) // no defaults coerced, err := checker.Coerce(source, nil) if err != nil { return nil, errors.Annotatef(err, "space 2.0 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. subnets, err := readSubnetList(valid["subnets"].([]interface{}), subnet_2_0) if err != nil { return nil, errors.Trace(err) } result := &space{ resourceURI: valid["resource_uri"].(string), id: valid["id"].(int), name: valid["name"].(string), subnets: subnets, } return result, nil }
func importPayloadV1(source map[string]interface{}) (*payload, error) { fields := schema.Fields{ "name": schema.String(), "type": schema.String(), "raw-id": schema.String(), "state": schema.String(), "labels": schema.List(schema.String()), } // Some values don't have to be there. defaults := schema.Defaults{ "labels": schema.Omit, } checker := schema.FieldMap(fields, defaults) coerced, err := checker.Coerce(source, nil) if err != nil { return nil, errors.Annotatef(err, "payload v1 schema check failed") } valid := coerced.(map[string]interface{}) return &payload{ Name_: valid["name"].(string), Type_: valid["type"].(string), RawID_: valid["raw-id"].(string), State_: valid["state"].(string), Labels_: convertToStringSlice(valid["labels"]), }, nil }
func importAddressV1(source map[string]interface{}) (*address, error) { fields := schema.Fields{ "value": schema.String(), "type": schema.String(), "scope": schema.String(), "origin": schema.String(), } // Some values don't have to be there. defaults := schema.Defaults{ "scope": "", "origin": "", } checker := schema.FieldMap(fields, defaults) coerced, err := checker.Coerce(source, nil) if err != nil { return nil, errors.Annotatef(err, "address 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 &address{ Version: 1, Value_: valid["value"].(string), Type_: valid["type"].(string), Scope_: valid["scope"].(string), Origin_: valid["origin"].(string), }, nil }
func importVolumeAttachmentV1(source map[string]interface{}) (*volumeAttachment, error) { fields := schema.Fields{ "machine-id": schema.String(), "provisioned": schema.Bool(), "read-only": schema.Bool(), "device-name": schema.String(), "device-link": schema.String(), "bus-address": schema.String(), } checker := schema.FieldMap(fields, nil) // no defaults coerced, err := checker.Coerce(source, nil) if err != nil { return nil, errors.Annotatef(err, "volumeAttachment 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. result := &volumeAttachment{ MachineID_: valid["machine-id"].(string), Provisioned_: valid["provisioned"].(bool), ReadOnly_: valid["read-only"].(bool), DeviceName_: valid["device-name"].(string), DeviceLink_: valid["device-link"].(string), BusAddress_: valid["bus-address"].(string), } return result, nil }
func importPortRangeV1(source map[string]interface{}) (*portRange, error) { fields := schema.Fields{ "unit-name": schema.String(), "from-port": schema.Int(), "to-port": schema.Int(), "protocol": schema.String(), } checker := schema.FieldMap(fields, nil) // no defaults coerced, err := checker.Coerce(source, nil) if err != nil { return nil, errors.Annotatef(err, "port-range 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 &portRange{ UnitName_: valid["unit-name"].(string), FromPort_: int(valid["from-port"].(int64)), ToPort_: int(valid["to-port"].(int64)), Protocol_: valid["protocol"].(string), }, nil }
func importStorageV1(source map[string]interface{}) (*storage, error) { fields := schema.Fields{ "id": schema.String(), "kind": schema.String(), "owner": schema.String(), "name": schema.String(), "attachments": schema.List(schema.String()), } // Normally a list would have defaults, but in this case storage // should always have at least one attachment. checker := schema.FieldMap(fields, nil) // no defaults coerced, err := checker.Coerce(source, nil) if err != nil { return nil, errors.Annotatef(err, "storage 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. result := &storage{ ID_: valid["id"].(string), Kind_: valid["kind"].(string), Owner_: valid["owner"].(string), Name_: valid["name"].(string), Attachments_: convertToStringSlice(valid["attachments"]), } return result, nil }
func importFilesystemAttachmentV1(source map[string]interface{}) (*filesystemAttachment, error) { fields := schema.Fields{ "machine-id": schema.String(), "provisioned": schema.Bool(), "read-only": schema.Bool(), "mount-point": schema.String(), } defaults := schema.Defaults{ "mount-point": "", } checker := schema.FieldMap(fields, defaults) coerced, err := checker.Coerce(source, nil) if err != nil { return nil, errors.Annotatef(err, "filesystemAttachment 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. result := &filesystemAttachment{ MachineID_: valid["machine-id"].(string), Provisioned_: valid["provisioned"].(bool), ReadOnly_: valid["read-only"].(bool), MountPoint_: valid["mount-point"].(string), } return result, nil }
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 }
func importActionV1(source map[string]interface{}) (*action, error) { fields := schema.Fields{ "receiver": schema.String(), "name": schema.String(), "parameters": schema.StringMap(schema.Any()), "enqueued": schema.Time(), "started": schema.Time(), "completed": schema.Time(), "status": schema.String(), "message": schema.String(), "results": schema.StringMap(schema.Any()), "id": schema.String(), } // Some values don't have to be there. defaults := schema.Defaults{ "started": time.Time{}, "completed": time.Time{}, } checker := schema.FieldMap(fields, defaults) coerced, err := checker.Coerce(source, nil) if err != nil { return nil, errors.Annotatef(err, "action v1 schema check failed") } valid := coerced.(map[string]interface{}) action := &action{ Id_: valid["id"].(string), Receiver_: valid["receiver"].(string), Name_: valid["name"].(string), Status_: valid["status"].(string), Message_: valid["message"].(string), Parameters_: valid["parameters"].(map[string]interface{}), Enqueued_: valid["enqueued"].(time.Time).UTC(), Results_: valid["results"].(map[string]interface{}), } started := valid["started"].(time.Time) if !started.IsZero() { started = started.UTC() action.Started_ = &started } completed := valid["completed"].(time.Time) if !started.IsZero() { completed = completed.UTC() action.Completed_ = &completed } return action, nil }
func (s *S) TestStringified(c *gc.C) { s.sch = schema.Stringified() out, err := s.sch.Coerce(true, aPath) c.Assert(err, gc.IsNil) c.Check(out, gc.Equals, "true") out, err = s.sch.Coerce(10, aPath) c.Assert(err, gc.IsNil) c.Check(out, gc.Equals, "10") out, err = s.sch.Coerce(1.1, aPath) c.Assert(err, gc.IsNil) c.Check(out, gc.Equals, "1.1") out, err = s.sch.Coerce("spam", aPath) c.Assert(err, gc.IsNil) c.Check(out, gc.Equals, "spam") _, err = s.sch.Coerce(map[string]string{}, aPath) c.Check(err, gc.ErrorMatches, ".* unexpected value .*") _, err = s.sch.Coerce([]string{}, aPath) c.Check(err, gc.ErrorMatches, ".* unexpected value .*") s.sch = schema.Stringified(schema.StringMap(schema.String())) out, err = s.sch.Coerce(map[string]string{"a": "b"}, aPath) c.Assert(err, gc.IsNil) c.Check(out, gc.Equals, `map[string]string{"a":"b"}`) }
func (c *controller) readAPIVersion(apiVersion version.Number) (set.Strings, version.Number, error) { parsed, err := c.get("version") if err != nil { return nil, apiVersion, errors.Trace(err) } // As we care about other fields, add them. fields := schema.Fields{ "capabilities": schema.List(schema.String()), } checker := schema.FieldMap(fields, nil) // no defaults coerced, err := checker.Coerce(parsed, nil) if err != nil { return nil, apiVersion, WrapWithDeserializationError(err, "version response") } // For now, we don't append any subversion, but as it becomes used, we // should parse and check. valid := coerced.(map[string]interface{}) // From here we know that the map returned from the schema coercion // contains fields of the right type. capabilities := set.NewStrings() capabilityValues := valid["capabilities"].([]interface{}) for _, value := range capabilityValues { capabilities.Add(value.(string)) } return capabilities, apiVersion, nil }
func importOpenedPortsV1(source map[string]interface{}) (*openedPorts, error) { fields := schema.Fields{ "subnet-id": schema.String(), "opened-ports": schema.StringMap(schema.Any()), } checker := schema.FieldMap(fields, nil) // no defaults coerced, err := checker.Coerce(source, nil) if err != nil { return nil, errors.Annotatef(err, "opened-ports 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. ports, err := importPortRanges(valid["opened-ports"].(map[string]interface{})) if err != nil { return nil, errors.Trace(err) } result := &openedPorts{ SubnetID_: valid["subnet-id"].(string), } result.setOpenedPorts(ports) return result, nil }
func importRelationV1(source map[string]interface{}) (*relation, error) { fields := schema.Fields{ "id": schema.Int(), "key": schema.String(), "endpoints": schema.StringMap(schema.Any()), } checker := schema.FieldMap(fields, nil) // no defaults coerced, err := checker.Coerce(source, nil) if err != nil { return nil, errors.Annotatef(err, "relation 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. result := &relation{ Id_: int(valid["id"].(int64)), Key_: valid["key"].(string), } endpoints, err := importEndpoints(valid["endpoints"].(map[string]interface{})) if err != nil { return nil, errors.Trace(err) } result.setEndpoints(endpoints) return result, nil }
func (s *S) TestMap(c *gc.C) { s.sch = schema.Map(schema.String(), schema.Int()) out, err := s.sch.Coerce(map[string]interface{}{"a": 1, "b": int8(2)}, aPath) c.Assert(err, gc.IsNil) c.Assert(out, gc.DeepEquals, map[interface{}]interface{}{"a": int64(1), "b": int64(2)}) out, err = s.sch.Coerce(42, aPath) c.Assert(out, gc.IsNil) c.Assert(err, gc.ErrorMatches, "<path>: expected map, got int\\(42\\)") out, err = s.sch.Coerce(nil, aPath) c.Assert(out, gc.IsNil) c.Assert(err, gc.ErrorMatches, "<path>: expected map, got nothing") out, err = s.sch.Coerce(map[int]int{1: 1}, aPath) c.Assert(out, gc.IsNil) c.Assert(err, gc.ErrorMatches, "<path>: expected string, got int\\(1\\)") out, err = s.sch.Coerce(map[string]bool{"a": true}, aPath) c.Assert(out, gc.IsNil) c.Assert(err, gc.ErrorMatches, `<path>\.a: expected int, got bool\(true\)`) // First path entry shouldn't have dots in an error message. out, err = s.sch.Coerce(map[string]bool{"a": true}, nil) c.Assert(out, gc.IsNil) c.Assert(err, gc.ErrorMatches, `a: expected int, got bool\(true\)`) // Error should work even when path is nil. out, err = s.sch.Coerce(nil, nil) c.Assert(out, gc.IsNil) c.Assert(err, gc.ErrorMatches, `expected map, got nothing`) }
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 }
func (s *ConfigSuite) TestValidateUnknownAttrs(c *gc.C) { s.addJujuFiles(c) cfg, err := config.New(config.UseDefaults, map[string]interface{}{ "name": "myenv", "type": "other", "uuid": testing.ModelTag.Id(), "known": "this", "unknown": "that", }) c.Assert(err, jc.ErrorIsNil) // No fields: all attrs passed through. attrs, err := cfg.ValidateUnknownAttrs(nil, nil) c.Assert(err, jc.ErrorIsNil) c.Assert(attrs, gc.DeepEquals, map[string]interface{}{ "known": "this", "unknown": "that", }) // Valid field: that and other attrs passed through. fields := schema.Fields{"known": schema.String()} attrs, err = cfg.ValidateUnknownAttrs(fields, nil) c.Assert(err, jc.ErrorIsNil) c.Assert(attrs, gc.DeepEquals, map[string]interface{}{ "known": "this", "unknown": "that", }) // Default field: inserted. fields["default"] = schema.String() defaults := schema.Defaults{"default": "the other"} attrs, err = cfg.ValidateUnknownAttrs(fields, defaults) c.Assert(err, jc.ErrorIsNil) c.Assert(attrs, gc.DeepEquals, map[string]interface{}{ "known": "this", "unknown": "that", "default": "the other", }) // Invalid field: failure. fields["known"] = schema.Int() _, err = cfg.ValidateUnknownAttrs(fields, defaults) c.Assert(err, gc.ErrorMatches, `known: expected int, got string\("this"\)`) }
func vlan_2_0(source map[string]interface{}) (*vlan, error) { fields := schema.Fields{ "id": schema.ForceInt(), "resource_uri": schema.String(), "name": schema.OneOf(schema.Nil(""), schema.String()), "fabric": schema.String(), "vid": schema.ForceInt(), "mtu": schema.ForceInt(), "dhcp_on": schema.Bool(), // racks are not always set. "primary_rack": schema.OneOf(schema.Nil(""), schema.String()), "secondary_rack": schema.OneOf(schema.Nil(""), schema.String()), } checker := schema.FieldMap(fields, nil) coerced, err := checker.Coerce(source, nil) if err != nil { return nil, errors.Annotatef(err, "vlan 2.0 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. // Since the primary and secondary racks are optional, we use the two // part cast assignment. If the case fails, then we get the default value // we care about, which is the empty string. primary_rack, _ := valid["primary_rack"].(string) secondary_rack, _ := valid["secondary_rack"].(string) name, _ := valid["name"].(string) result := &vlan{ resourceURI: valid["resource_uri"].(string), id: valid["id"].(int), name: name, fabric: valid["fabric"].(string), vid: valid["vid"].(int), mtu: valid["mtu"].(int), dhcp: valid["dhcp_on"].(bool), primaryRack: primary_rack, secondaryRack: secondary_rack, } return result, nil }
func importUserV1(source map[string]interface{}) (*user, error) { fields := schema.Fields{ "name": schema.String(), "display-name": schema.String(), "created-by": schema.String(), "read-only": schema.Bool(), "date-created": schema.Time(), "last-connection": schema.Time(), "access": schema.String(), } // Some values don't have to be there. defaults := schema.Defaults{ "display-name": "", "last-connection": time.Time{}, "read-only": false, } checker := schema.FieldMap(fields, defaults) coerced, err := checker.Coerce(source, nil) if err != nil { return nil, errors.Annotatef(err, "user 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. result := &user{ Name_: valid["name"].(string), DisplayName_: valid["display-name"].(string), CreatedBy_: valid["created-by"].(string), DateCreated_: valid["date-created"].(time.Time), Access_: valid["access"].(string), } lastConn := valid["last-connection"].(time.Time) if !lastConn.IsZero() { result.LastConnection_ = &lastConn } return result, nil }
func partition_2_0(source map[string]interface{}) (*partition, error) { fields := schema.Fields{ "resource_uri": schema.String(), "id": schema.ForceInt(), "path": schema.String(), "uuid": schema.String(), "used_for": schema.String(), "size": schema.ForceUint(), "filesystem": schema.OneOf(schema.Nil(""), schema.StringMap(schema.Any())), } checker := schema.FieldMap(fields, nil) coerced, err := checker.Coerce(source, nil) if err != nil { return nil, WrapWithDeserializationError(err, "partition 2.0 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. var filesystem *filesystem if fsSource := valid["filesystem"]; fsSource != nil { filesystem, err = filesystem2_0(fsSource.(map[string]interface{})) if err != nil { return nil, errors.Trace(err) } } result := &partition{ resourceURI: valid["resource_uri"].(string), id: valid["id"].(int), path: valid["path"].(string), uuid: valid["uuid"].(string), usedFor: valid["used_for"].(string), size: valid["size"].(uint64), filesystem: filesystem, } return result, nil }
func (c accessC) Coerce(v interface{}, path []string) (interface{}, error) { s := schema.String() in, err := s.Coerce(v, path) if err != nil { return nil, err } access := Access(in.(string)) if err := access.Validate(); err != nil { return nil, errors.Trace(err) } return access, nil }
func bootResource_2_0(source map[string]interface{}) (*bootResource, error) { fields := schema.Fields{ "resource_uri": schema.String(), "id": schema.ForceInt(), "name": schema.String(), "type": schema.String(), "architecture": schema.String(), "subarches": schema.String(), "kflavor": schema.String(), } checker := schema.FieldMap(fields, nil) // no defaults coerced, err := checker.Coerce(source, nil) if err != nil { return nil, WrapWithDeserializationError(err, "boot resource 2.0 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. result := &bootResource{ resourceURI: valid["resource_uri"].(string), id: valid["id"].(int), name: valid["name"].(string), type_: valid["type"].(string), architecture: valid["architecture"].(string), subArches: valid["subarches"].(string), kernelFlavor: valid["kflavor"].(string), } return result, nil }
func importLinkLayerDeviceV1(source map[string]interface{}) (*linklayerdevice, error) { fields := schema.Fields{ "provider-id": schema.String(), "machine-id": schema.String(), "name": schema.String(), "mtu": schema.Int(), "type": schema.String(), "mac-address": schema.String(), "is-autostart": schema.Bool(), "is-up": schema.Bool(), "parent-name": schema.String(), } // Some values don't have to be there. defaults := schema.Defaults{ "provider-id": "", } checker := schema.FieldMap(fields, defaults) coerced, err := checker.Coerce(source, nil) if err != nil { return nil, errors.Annotatef(err, "linklayerdevice v1 schema check failed") } valid := coerced.(map[string]interface{}) return &linklayerdevice{ ProviderID_: valid["provider-id"].(string), MachineID_: valid["machine-id"].(string), Name_: valid["name"].(string), MTU_: uint(valid["mtu"].(int64)), Type_: valid["type"].(string), MACAddress_: valid["mac-address"].(string), IsAutoStart_: valid["is-autostart"].(bool), IsUp_: valid["is-up"].(bool), ParentName_: valid["parent-name"].(string), }, nil }
func zone_2_0(source map[string]interface{}) (*zone, error) { fields := schema.Fields{ "name": schema.String(), "description": schema.String(), "resource_uri": schema.String(), } checker := schema.FieldMap(fields, nil) // no defaults coerced, err := checker.Coerce(source, nil) if err != nil { return nil, errors.Annotatef(err, "zone 2.0 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. result := &zone{ name: valid["name"].(string), description: valid["description"].(string), resourceURI: valid["resource_uri"].(string), } return result, nil }
func importEndpointV1(source map[string]interface{}) (*endpoint, error) { fields := schema.Fields{ "service-name": schema.String(), "name": schema.String(), "role": schema.String(), "interface": schema.String(), "optional": schema.Bool(), "limit": schema.Int(), "scope": schema.String(), "unit-settings": schema.StringMap(schema.StringMap(schema.Any())), } checker := schema.FieldMap(fields, nil) // No defaults. coerced, err := checker.Coerce(source, nil) if err != nil { return nil, errors.Annotatef(err, "endpoint 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. result := &endpoint{ ServiceName_: valid["service-name"].(string), Name_: valid["name"].(string), Role_: valid["role"].(string), Interface_: valid["interface"].(string), Optional_: valid["optional"].(bool), Limit_: int(valid["limit"].(int64)), Scope_: valid["scope"].(string), UnitSettings_: make(map[string]map[string]interface{}), } for unitname, settings := range valid["unit-settings"].(map[string]interface{}) { result.UnitSettings_[unitname] = settings.(map[string]interface{}) } return result, nil }
// FinalizeAuthorizedKeys takes a set of configuration attributes and // ensures that it has an authorized-keys setting, or returns // ErrNoAuthorizedKeys if it cannot. // // If the attributes contains a non-empty value for "authorized-keys", // then it is left alone. If there is an "authorized-keys-path" setting, // its contents will be loaded into "authorized-keys". Otherwise, the // contents of standard public keys will be used: ~/.ssh/id_dsa.pub, // ~/.ssh/id_rsa.pub, and ~/.ssh/identity.pub. func FinalizeAuthorizedKeys(ctx *cmd.Context, attrs map[string]interface{}) error { const authorizedKeysPathKey = "authorized-keys-path" checker := schema.FieldMap(schema.Fields{ config.AuthorizedKeysKey: schema.String(), authorizedKeysPathKey: schema.String(), }, schema.Defaults{ config.AuthorizedKeysKey: schema.Omit, authorizedKeysPathKey: schema.Omit, }) coerced, err := checker.Coerce(attrs, nil) if err != nil { return errors.Trace(err) } coercedAttrs := coerced.(map[string]interface{}) authorizedKeys, haveAuthorizedKeys := coercedAttrs[config.AuthorizedKeysKey].(string) authorizedKeysPath, haveAuthorizedKeysPath := coercedAttrs[authorizedKeysPathKey].(string) if haveAuthorizedKeys && haveAuthorizedKeysPath { return errors.Errorf( "%q and %q may not both be specified", config.AuthorizedKeysKey, authorizedKeysPathKey, ) } if haveAuthorizedKeys { // We have authorized-keys already; nothing to do. return nil } authorizedKeys, err = ReadAuthorizedKeys(ctx, authorizedKeysPath) if err != nil { return errors.Annotate(err, "reading authorized-keys") } if haveAuthorizedKeysPath { delete(attrs, authorizedKeysPathKey) } attrs[config.AuthorizedKeysKey] = authorizedKeys return nil }
func importBlockDeviceV1(source map[string]interface{}) (*blockdevice, error) { fields := schema.Fields{ "name": schema.String(), "links": schema.List(schema.String()), "label": schema.String(), "uuid": schema.String(), "hardware-id": schema.String(), "bus-address": schema.String(), "size": schema.ForceUint(), "fs-type": schema.String(), "in-use": schema.Bool(), "mount-point": schema.String(), } defaults := schema.Defaults{ "links": schema.Omit, "label": "", "uuid": "", "hardware-id": "", "bus-address": "", "fs-type": "", "mount-point": "", } checker := schema.FieldMap(fields, defaults) coerced, err := checker.Coerce(source, nil) if err != nil { return nil, errors.Annotatef(err, "block device 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. result := &blockdevice{ Name_: valid["name"].(string), Links_: convertToStringSlice(valid["links"]), Label_: valid["label"].(string), UUID_: valid["uuid"].(string), HardwareID_: valid["hardware-id"].(string), BusAddress_: valid["bus-address"].(string), Size_: valid["size"].(uint64), FilesystemType_: valid["fs-type"].(string), InUse_: valid["in-use"].(bool), MountPoint_: valid["mount-point"].(string), } return result, nil }