func (s *ConfigSuite) TestValidateUnknownAttrs(c *gc.C) { s.addJujuFiles(c) cfg, err := config.New(config.UseDefaults, map[string]interface{}{ "name": "myenv", "type": "other", "known": "this", "unknown": "that", }) // No fields: all attrs passed through. attrs, err := cfg.ValidateUnknownAttrs(nil, nil) c.Assert(err, gc.IsNil) 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, gc.IsNil) 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, gc.IsNil) 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"\)`) }
// SetStorageDelay causes any storage download operation in any current // environment to be delayed for the given duration. func SetStorageDelay(d time.Duration) { p := &providerInstance p.mu.Lock() defer p.mu.Unlock() for _, st := range p.state { st.mu.Lock() st.storageDelay = d st.mu.Unlock() } } var configFields = schema.Fields{ "state-server": schema.Bool(), "broken": schema.String(), "secret": schema.String(), "state-id": schema.String(), } var configDefaults = schema.Defaults{ "broken": "", "secret": "pork", "state-id": schema.Omit, } type environConfig struct { *config.Config attrs map[string]interface{} } func (c *environConfig) stateServer() bool {
// interface: mysql // limit: // optional: false // // In all input cases, the output is the fully specified interface // representation as seen in the mysql interface description above. func ifaceExpander(limit interface{}) schema.Checker { return ifaceExpC{limit} } type ifaceExpC struct { limit interface{} } var ( stringC = schema.String() mapC = schema.StringMap(schema.Any()) ) func (c ifaceExpC) Coerce(v interface{}, path []string) (newv interface{}, err error) { s, err := stringC.Coerce(v, path) if err == nil { newv = map[string]interface{}{ "interface": s, "limit": c.limit, "optional": false, "scope": string(ScopeGlobal), } return }
func (option Option) validate(name string, value interface{}) (_ interface{}, err error) { if value == nil { return nil, nil } defer option.error(&err, name, value) if checker := optionTypeCheckers[option.Type]; checker != nil { if value, err = checker.Coerce(value, nil); err != nil { return nil, err } return value, nil } panic(fmt.Errorf("option %q has unknown type %q", name, option.Type)) } var optionTypeCheckers = map[string]schema.Checker{ "string": schema.String(), "int": schema.Int(), "float": schema.Float(), "boolean": schema.Bool(), } // parse returns an appropriately-typed value for the supplied string, or // returns an error if it cannot be parsed to the correct type. func (option Option) parse(name, str string) (_ interface{}, err error) { defer option.error(&err, name, str) switch option.Type { case "string": return str, nil case "int": return strconv.ParseInt(str, 10, 64) case "float":
"os" "path/filepath" "github.com/juju/core/agent" "github.com/juju/core/environs/config" "github.com/juju/core/instance" "github.com/juju/core/schema" ) var checkIfRoot = func() bool { return os.Getuid() == 0 } var ( configFields = schema.Fields{ "root-dir": schema.String(), "bootstrap-ip": schema.String(), "network-bridge": schema.String(), "container": schema.String(), "storage-port": schema.ForceInt(), "namespace": schema.String(), } // The port defaults below are not entirely arbitrary. Local user web // frameworks often use 8000 or 8080, so I didn't want to use either of // these, but did want the familiarity of using something in the 8000 // range. configDefaults = schema.Defaults{ "root-dir": "", "network-bridge": "lxcbr0", "container": string(instance.LXC), "bootstrap-ip": schema.Omit,