예제 #1
0
func (s *S) TestMap(c *C) {
	sch := schema.Map(schema.String(), schema.Int())
	out, err := sch.Coerce(map[string]interface{}{"a": 1, "b": int8(2)}, aPath)
	c.Assert(err, IsNil)
	c.Assert(out, DeepEquals, map[interface{}]interface{}{"a": int64(1), "b": int64(2)})

	out, err = sch.Coerce(42, aPath)
	c.Assert(out, IsNil)
	c.Assert(err, ErrorMatches, "<path>: expected map, got 42")

	out, err = sch.Coerce(nil, aPath)
	c.Assert(out, IsNil)
	c.Assert(err, ErrorMatches, "<path>: expected map, got nothing")

	out, err = sch.Coerce(map[int]int{1: 1}, aPath)
	c.Assert(out, IsNil)
	c.Assert(err, ErrorMatches, "<path>: expected string, got 1")

	out, err = sch.Coerce(map[string]bool{"a": true}, aPath)
	c.Assert(out, IsNil)
	c.Assert(err, ErrorMatches, `<path>\.a: expected int, got true`)

	// First path entry shouldn't have dots in an error message.
	out, err = sch.Coerce(map[string]bool{"a": true}, nil)
	c.Assert(out, IsNil)
	c.Assert(err, ErrorMatches, `a: expected int, got true`)
}
예제 #2
0
func (*ConfigSuite) TestValidateUnknownAttrs(c *gc.C) {
	defer testing.MakeFakeHomeWithFiles(c, []testing.TestFile{
		{".ssh/id_rsa.pub", "rsa\n"},
		{".juju/myenv-cert.pem", caCert},
		{".juju/myenv-private-key.pem", caKey},
	}).Restore()
	cfg, err := config.New(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 "this"`)
}
예제 #3
0
func (s *S) TestString(c *C) {
	sch := schema.String()

	out, err := sch.Coerce("foo", aPath)
	c.Assert(err, IsNil)
	c.Assert(out, Equals, "foo")

	out, err = sch.Coerce(true, aPath)
	c.Assert(out, IsNil)
	c.Assert(err, ErrorMatches, "<path>: expected string, got true")

	out, err = sch.Coerce(nil, aPath)
	c.Assert(out, IsNil)
	c.Assert(err, ErrorMatches, "<path>: expected string, got nothing")
}
예제 #4
0
파일: config.go 프로젝트: rif/golang-stuff
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package maas

import (
	"errors"
	"fmt"
	"launchpad.net/juju-core/environs/config"
	"launchpad.net/juju-core/schema"
	"net/url"
	"strings"
)

var configFields = schema.Fields{
	"maas-server": schema.String(),
	// maas-oauth is a colon-separated triplet of:
	// consumer-key:resource-token:resource-secret
	"maas-oauth": schema.String(),
}
var configDefaults = schema.Defaults{}

type maasEnvironConfig struct {
	*config.Config
	attrs map[string]interface{}
}

func (cfg *maasEnvironConfig) MAASServer() string {
	return cfg.attrs["maas-server"].(string)
}
예제 #5
0
// 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(),
}
var configDefaults = schema.Defaults{
	"broken": "",
	"secret": "pork",
}

type environConfig struct {
	*config.Config
	attrs map[string]interface{}
}

func (c *environConfig) stateServer() bool {
	return c.attrs["state-server"].(bool)
}
예제 #6
0
package openstack

import (
	"fmt"
	"launchpad.net/goose/identity"
	"launchpad.net/juju-core/environs/config"
	"launchpad.net/juju-core/schema"
	"net/url"
)

var configChecker = schema.StrictFieldMap(
	schema.Fields{
		"username":          schema.String(),
		"password":          schema.String(),
		"tenant-name":       schema.String(),
		"auth-url":          schema.String(),
		"auth-method":       schema.String(),
		"region":            schema.String(),
		"control-bucket":    schema.String(),
		"public-bucket":     schema.String(),
		"public-bucket-url": schema.String(),
	},
	schema.Defaults{
		"username":          "",
		"password":          "",
		"tenant-name":       "",
		"auth-url":          "",
		"auth-method":       string(AuthUserPass),
		"region":            "",
		"control-bucket":    "",
		"public-bucket":     "juju-dist",
예제 #7
0
파일: config.go 프로젝트: rif/golang-stuff
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package azure

import (
	"fmt"
	"io/ioutil"
	"launchpad.net/juju-core/environs/config"
	"launchpad.net/juju-core/schema"
)

var configFields = schema.Fields{
	"location":                      schema.String(),
	"management-subscription-id":    schema.String(),
	"management-certificate-path":   schema.String(),
	"management-certificate":        schema.String(),
	"storage-account-name":          schema.String(),
	"storage-account-key":           schema.String(),
	"storage-container-name":        schema.String(),
	"public-storage-account-name":   schema.String(),
	"public-storage-container-name": schema.String(),
	"force-image-name":              schema.String(),
}
var configDefaults = schema.Defaults{
	"location":                      "",
	"management-certificate":        "",
	"management-certificate-path":   "",
	"storage-container-name":        "",
	"public-storage-account-name":   "",
	"public-storage-container-name": "",
예제 #8
0
//       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
	}
예제 #9
0
파일: config.go 프로젝트: rif/golang-stuff
// Copyright 2011, 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package ec2

import (
	"fmt"
	"launchpad.net/goamz/aws"
	"launchpad.net/juju-core/environs/config"
	"launchpad.net/juju-core/schema"
)

var configFields = schema.Fields{
	"access-key":           schema.String(),
	"secret-key":           schema.String(),
	"region":               schema.String(),
	"control-bucket":       schema.String(),
	"public-bucket":        schema.String(),
	"public-bucket-region": schema.String(),
}

var configDefaults = schema.Defaults{
	"access-key":           "",
	"secret-key":           "",
	"region":               "us-east-1",
	"public-bucket":        "juju-dist",
	"public-bucket-region": "us-east-1",
}

type environConfig struct {
	*config.Config
예제 #10
0
// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package openstack

import (
	"fmt"
	"launchpad.net/goose/identity"
	"launchpad.net/juju-core/environs/config"
	"launchpad.net/juju-core/log"
	"launchpad.net/juju-core/schema"
	"net/url"
)

var configFields = schema.Fields{
	"username":          schema.String(),
	"password":          schema.String(),
	"tenant-name":       schema.String(),
	"auth-url":          schema.String(),
	"auth-mode":         schema.String(),
	"access-key":        schema.String(),
	"secret-key":        schema.String(),
	"region":            schema.String(),
	"control-bucket":    schema.String(),
	"public-bucket":     schema.String(),
	"public-bucket-url": schema.String(),
	"use-floating-ip":   schema.Bool(),
	// These next keys are deprecated and ignored. We keep them them in the schema
	// so existing configs do not error.
	"default-image-id":      schema.String(),
	"default-instance-type": schema.String(),
예제 #11
0
		}
	}
	return out, nil
}

var validTypes = map[string]reflect.Kind{
	"string":  reflect.String,
	"int":     reflect.Int64,
	"boolean": reflect.Bool,
	"float":   reflect.Float64,
}

var optionSchema = schema.FieldMap(
	schema.Fields{
		"type":        schema.OneOf(schema.Const("string"), schema.Const("int"), schema.Const("float"), schema.Const("boolean")),
		"default":     schema.OneOf(schema.String(), schema.Int(), schema.Float(), schema.Bool()),
		"description": schema.String(),
	},
	schema.Defaults{
		"default":     schema.Omit,
		"description": schema.Omit,
	},
)

var configSchema = schema.FieldMap(
	schema.Fields{
		"options": schema.StringMap(optionSchema),
	},
	nil,
)
예제 #12
0
		m[k] = v
	}
	return m
}

// Apply returns a new configuration that has the attributes of c plus attrs.
func (c *Config) Apply(attrs map[string]interface{}) (*Config, error) {
	m := c.AllAttrs()
	for k, v := range attrs {
		m[k] = v
	}
	return New(m)
}

var fields = schema.Fields{
	"type":                      schema.String(),
	"name":                      schema.String(),
	"default-series":            schema.String(),
	"authorized-keys":           schema.String(),
	"authorized-keys-path":      schema.String(),
	"firewall-mode":             schema.String(),
	"agent-version":             schema.String(),
	"development":               schema.Bool(),
	"admin-secret":              schema.String(),
	"ca-cert":                   schema.String(),
	"ca-cert-path":              schema.String(),
	"ca-private-key":            schema.String(),
	"ca-private-key-path":       schema.String(),
	"ssl-hostname-verification": schema.Bool(),
	"state-port":                schema.ForceInt(),
	"api-port":                  schema.ForceInt(),
예제 #13
0
	"fmt"
	"os"
	"path/filepath"
	"strconv"

	"launchpad.net/juju-core/environs/config"
	"launchpad.net/juju-core/schema"
)

var checkIfRoot = func() bool {
	return os.Getuid() == 0
}

var (
	configFields = schema.Fields{
		"root-dir":            schema.String(),
		"bootstrap-ip":        schema.String(),
		"storage-port":        schema.Int(),
		"shared-storage-port": schema.Int(),
	}
	// 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":            "",
		"bootstrap-ip":        schema.Omit,
		"storage-port":        8040,
		"shared-storage-port": 8041,
	}
)
예제 #14
0
파일: config.go 프로젝트: rif/golang-stuff
		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
		} else if value == "" {
			value = nil
		}
		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. Empty
// string values are returned as nil.
func (option Option) parse(name, str string) (_ interface{}, err error) {
	if str == "" {
		return nil, nil
	}
	defer option.error(&err, name, str)
	switch option.Type {
	case "string":
예제 #15
0
//       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
	}