本文整理汇总了Golang中github.com/wallyworld/core/environs/config.Config.ValidateUnknownAttrs方法的典型用法代码示例。如果您正苦于以下问题:Golang Config.ValidateUnknownAttrs方法的具体用法?Golang Config.ValidateUnknownAttrs怎么用?Golang Config.ValidateUnknownAttrs使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/wallyworld/core/environs/config.Config
的用法示例。
在下文中一共展示了Config.ValidateUnknownAttrs方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Validate
func (p *environProvider) Validate(cfg, old *config.Config) (valid *config.Config, err error) {
// Check for valid changes for the base config values.
if err := config.Validate(cfg, old); err != nil {
return nil, err
}
validated, err := cfg.ValidateUnknownAttrs(configFields, configDefaults)
if err != nil {
return nil, err
}
if idStr, ok := validated["state-id"].(string); ok {
if _, err := strconv.Atoi(idStr); err != nil {
return nil, fmt.Errorf("invalid state-id %q", idStr)
}
}
// Apply the coerced unknown values back into the config.
return cfg.Apply(validated)
}
示例2: Validate
func (prov maasEnvironProvider) Validate(cfg, oldCfg *config.Config) (*config.Config, error) {
// Validate base configuration change before validating MAAS specifics.
err := config.Validate(cfg, oldCfg)
if err != nil {
return nil, err
}
validated, err := cfg.ValidateUnknownAttrs(configFields, configDefaults)
if err != nil {
return nil, err
}
if oldCfg != nil {
oldAttrs := oldCfg.UnknownAttrs()
validMaasAgentName := false
if oldName, ok := oldAttrs["maas-agent-name"]; !ok || oldName == nil {
// If maas-agent-name was nil (because the config was
// generated pre-1.16.2 the only correct value for it is ""
// See bug #1256179
validMaasAgentName = (validated["maas-agent-name"] == "")
} else {
validMaasAgentName = (validated["maas-agent-name"] == oldName)
}
if !validMaasAgentName {
return nil, fmt.Errorf("cannot change maas-agent-name")
}
}
envCfg := new(maasEnvironConfig)
envCfg.Config = cfg
envCfg.attrs = validated
server := envCfg.maasServer()
serverURL, err := url.Parse(server)
if err != nil || serverURL.Scheme == "" || serverURL.Host == "" {
return nil, fmt.Errorf("malformed maas-server URL '%v': %s", server, err)
}
oauth := envCfg.maasOAuth()
if strings.Count(oauth, ":") != 2 {
return nil, errMalformedMaasOAuth
}
return cfg.Apply(envCfg.attrs)
}
示例3: validate
func (p manualProvider) validate(cfg, old *config.Config) (*environConfig, error) {
// Check for valid changes for the base config values.
if err := config.Validate(cfg, old); err != nil {
return nil, err
}
validated, err := cfg.ValidateUnknownAttrs(configFields, configDefaults)
if err != nil {
return nil, err
}
envConfig := newEnvironConfig(cfg, validated)
if envConfig.bootstrapHost() == "" {
return nil, errNoBootstrapHost
}
// Check various immutable attributes.
if old != nil {
oldEnvConfig, err := p.validate(old, nil)
if err != nil {
return nil, err
}
for _, key := range [...]string{
"bootstrap-user",
"bootstrap-host",
"storage-listen-ip",
} {
if err = checkImmutableString(envConfig, oldEnvConfig, key); err != nil {
return nil, err
}
}
oldPort, newPort := oldEnvConfig.storagePort(), envConfig.storagePort()
if oldPort != newPort {
return nil, fmt.Errorf("cannot change storage-port from %q to %q", oldPort, newPort)
}
oldUseSSHStorage, newUseSSHStorage := oldEnvConfig.useSSHStorage(), envConfig.useSSHStorage()
if oldUseSSHStorage != newUseSSHStorage && newUseSSHStorage == true {
return nil, fmt.Errorf("cannot change use-sshstorage from %v to %v", oldUseSSHStorage, newUseSSHStorage)
}
}
return envConfig, nil
}
示例4: validateConfig
func validateConfig(cfg, old *config.Config) (*environConfig, error) {
// Check for valid changes for the base config values.
if err := config.Validate(cfg, old); err != nil {
return nil, err
}
newAttrs, err := cfg.ValidateUnknownAttrs(configFields, configDefaults)
if err != nil {
return nil, err
}
envConfig := &environConfig{cfg, newAttrs}
// If an old config was supplied, check any immutable fields have not changed.
if old != nil {
oldEnvConfig, err := validateConfig(old, nil)
if err != nil {
return nil, err
}
for _, field := range configImmutableFields {
if oldEnvConfig.attrs[field] != envConfig.attrs[field] {
return nil, fmt.Errorf(
"%s: cannot change from %v to %v",
field, oldEnvConfig.attrs[field], envConfig.attrs[field],
)
}
}
}
// Read env variables to fill in any missing fields.
for field, envVar := range environmentVariables {
// If field is not set, get it from env variables
if fieldValue, ok := envConfig.attrs[field]; !ok || fieldValue == "" {
localEnvVariable := os.Getenv(envVar)
if localEnvVariable != "" {
envConfig.attrs[field] = localEnvVariable
} else {
if field != "private-key-path" {
return nil, fmt.Errorf("cannot get %s value from environment variable %s", field, envVar)
}
}
}
}
// Ensure private-key-path is set - if it's not in config or an env var, use a default value.
if v, ok := envConfig.attrs["private-key-path"]; !ok || v == "" {
v = os.Getenv(environmentVariables["private-key-path"])
if v == "" {
v = DefaultPrivateKey
}
envConfig.attrs["private-key-path"] = v
}
// Now that we've ensured private-key-path is properly set, we go back and set
// up the private key - this is used to sign requests.
if fieldValue, ok := envConfig.attrs["private-key"]; !ok || fieldValue == "" {
keyFile, err := utils.NormalizePath(envConfig.attrs["private-key-path"].(string))
if err != nil {
return nil, err
}
privateKey, err := ioutil.ReadFile(keyFile)
if err != nil {
return nil, err
}
envConfig.attrs["private-key"] = string(privateKey)
}
// Check for missing fields.
for field := range configFields {
if envConfig.attrs[field] == "" {
return nil, fmt.Errorf("%s: must not be empty", field)
}
}
return envConfig, nil
}
示例5: Validate
// Validate implements environs.EnvironProvider.Validate.
func (provider environProvider) Validate(cfg, old *config.Config) (valid *config.Config, err error) {
// Check for valid changes for the base config values.
if err := config.Validate(cfg, old); err != nil {
return nil, err
}
validated, err := cfg.ValidateUnknownAttrs(configFields, configDefaults)
if err != nil {
return nil, fmt.Errorf("failed to validate unknown attrs: %v", err)
}
localConfig := newEnvironConfig(cfg, validated)
// Before potentially creating directories, make sure that the
// root directory has not changed.
containerType := localConfig.container()
if old != nil {
oldLocalConfig, err := provider.newConfig(old)
if err != nil {
return nil, fmt.Errorf("old config is not a valid local config: %v", old)
}
if containerType != oldLocalConfig.container() {
return nil, fmt.Errorf("cannot change container from %q to %q",
oldLocalConfig.container(), containerType)
}
if localConfig.rootDir() != oldLocalConfig.rootDir() {
return nil, fmt.Errorf("cannot change root-dir from %q to %q",
oldLocalConfig.rootDir(),
localConfig.rootDir())
}
if localConfig.networkBridge() != oldLocalConfig.networkBridge() {
return nil, fmt.Errorf("cannot change network-bridge from %q to %q",
oldLocalConfig.rootDir(),
localConfig.rootDir())
}
if localConfig.storagePort() != oldLocalConfig.storagePort() {
return nil, fmt.Errorf("cannot change storage-port from %v to %v",
oldLocalConfig.storagePort(),
localConfig.storagePort())
}
}
// Currently only supported containers are "lxc" and "kvm".
if containerType != instance.LXC && containerType != instance.KVM {
return nil, fmt.Errorf("unsupported container type: %q", containerType)
}
dir, err := utils.NormalizePath(localConfig.rootDir())
if err != nil {
return nil, err
}
if dir == "." {
dir = osenv.JujuHomePath(cfg.Name())
}
// Always assign the normalized path.
localConfig.attrs["root-dir"] = dir
if containerType != instance.KVM {
fastOptionAvailable := useFastLXC(containerType)
if _, found := localConfig.attrs["lxc-clone"]; !found {
localConfig.attrs["lxc-clone"] = fastOptionAvailable
}
}
// Apply the coerced unknown values back into the config.
return cfg.Apply(localConfig.attrs)
}