2019-10-23 07:59:13 +02:00
|
|
|
/*
|
|
|
|
* MinIO Cloud Storage, (C) 2019 MinIO, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-11-14 23:19:57 +01:00
|
|
|
"regexp"
|
2019-10-23 07:59:13 +02:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/minio/minio-go/pkg/set"
|
|
|
|
"github.com/minio/minio/pkg/auth"
|
|
|
|
"github.com/minio/minio/pkg/env"
|
2019-11-05 15:18:26 +01:00
|
|
|
"github.com/minio/minio/pkg/madmin"
|
2019-10-23 07:59:13 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Error config error type
|
|
|
|
type Error string
|
|
|
|
|
2019-10-31 07:39:09 +01:00
|
|
|
// Errorf - formats according to a format specifier and returns
|
|
|
|
// the string as a value that satisfies error of type config.Error
|
|
|
|
func Errorf(format string, a ...interface{}) error {
|
|
|
|
return Error(fmt.Sprintf(format, a...))
|
|
|
|
}
|
|
|
|
|
2019-10-23 07:59:13 +02:00
|
|
|
func (e Error) Error() string {
|
|
|
|
return string(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default keys
|
|
|
|
const (
|
2019-11-05 15:18:26 +01:00
|
|
|
Default = madmin.Default
|
2019-10-23 07:59:13 +02:00
|
|
|
State = "state"
|
|
|
|
Comment = "comment"
|
|
|
|
|
|
|
|
// State values
|
|
|
|
StateOn = "on"
|
|
|
|
StateOff = "off"
|
|
|
|
|
|
|
|
RegionName = "name"
|
|
|
|
AccessKey = "access_key"
|
|
|
|
SecretKey = "secret_key"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Top level config constants.
|
|
|
|
const (
|
2019-11-14 02:38:05 +01:00
|
|
|
CredentialsSubSys = "credentials"
|
|
|
|
PolicyOPASubSys = "policy_opa"
|
|
|
|
IdentityOpenIDSubSys = "identity_openid"
|
|
|
|
IdentityLDAPSubSys = "identity_ldap"
|
|
|
|
CacheSubSys = "cache"
|
|
|
|
RegionSubSys = "region"
|
|
|
|
EtcdSubSys = "etcd"
|
|
|
|
StorageClassSubSys = "storageclass"
|
|
|
|
CompressionSubSys = "compression"
|
|
|
|
KmsVaultSubSys = "kms_vault"
|
|
|
|
LoggerWebhookSubSys = "logger_webhook"
|
|
|
|
AuditWebhookSubSys = "audit_webhook"
|
2019-10-23 07:59:13 +02:00
|
|
|
|
|
|
|
// Add new constants here if you add new fields to config.
|
|
|
|
)
|
|
|
|
|
|
|
|
// Notification config constants.
|
|
|
|
const (
|
|
|
|
NotifyKafkaSubSys = "notify_kafka"
|
|
|
|
NotifyMQTTSubSys = "notify_mqtt"
|
|
|
|
NotifyMySQLSubSys = "notify_mysql"
|
|
|
|
NotifyNATSSubSys = "notify_nats"
|
|
|
|
NotifyNSQSubSys = "notify_nsq"
|
|
|
|
NotifyESSubSys = "notify_elasticsearch"
|
|
|
|
NotifyAMQPSubSys = "notify_amqp"
|
|
|
|
NotifyPostgresSubSys = "notify_postgres"
|
|
|
|
NotifyRedisSubSys = "notify_redis"
|
|
|
|
NotifyWebhookSubSys = "notify_webhook"
|
|
|
|
|
|
|
|
// Add new constants here if you add new fields to config.
|
|
|
|
)
|
|
|
|
|
|
|
|
// SubSystems - all supported sub-systems
|
|
|
|
var SubSystems = set.CreateStringSet([]string{
|
|
|
|
CredentialsSubSys,
|
|
|
|
RegionSubSys,
|
2019-10-30 08:04:39 +01:00
|
|
|
EtcdSubSys,
|
2019-10-23 07:59:13 +02:00
|
|
|
CacheSubSys,
|
|
|
|
StorageClassSubSys,
|
|
|
|
CompressionSubSys,
|
|
|
|
KmsVaultSubSys,
|
2019-11-14 02:38:05 +01:00
|
|
|
LoggerWebhookSubSys,
|
|
|
|
AuditWebhookSubSys,
|
2019-10-23 07:59:13 +02:00
|
|
|
PolicyOPASubSys,
|
|
|
|
IdentityLDAPSubSys,
|
|
|
|
IdentityOpenIDSubSys,
|
|
|
|
NotifyAMQPSubSys,
|
|
|
|
NotifyESSubSys,
|
|
|
|
NotifyKafkaSubSys,
|
|
|
|
NotifyMQTTSubSys,
|
|
|
|
NotifyMySQLSubSys,
|
|
|
|
NotifyNATSSubSys,
|
|
|
|
NotifyNSQSubSys,
|
|
|
|
NotifyPostgresSubSys,
|
|
|
|
NotifyRedisSubSys,
|
|
|
|
NotifyWebhookSubSys,
|
|
|
|
}...)
|
|
|
|
|
|
|
|
// SubSystemsSingleTargets - subsystems which only support single target.
|
|
|
|
var SubSystemsSingleTargets = set.CreateStringSet([]string{
|
|
|
|
CredentialsSubSys,
|
|
|
|
RegionSubSys,
|
2019-10-30 08:04:39 +01:00
|
|
|
EtcdSubSys,
|
2019-10-23 07:59:13 +02:00
|
|
|
CacheSubSys,
|
|
|
|
StorageClassSubSys,
|
|
|
|
CompressionSubSys,
|
|
|
|
KmsVaultSubSys,
|
|
|
|
PolicyOPASubSys,
|
|
|
|
IdentityLDAPSubSys,
|
|
|
|
IdentityOpenIDSubSys,
|
|
|
|
}...)
|
|
|
|
|
|
|
|
// Constant separators
|
|
|
|
const (
|
2019-11-05 15:18:26 +01:00
|
|
|
SubSystemSeparator = madmin.SubSystemSeparator
|
|
|
|
KvSeparator = madmin.KvSeparator
|
|
|
|
KvSpaceSeparator = madmin.KvSpaceSeparator
|
2019-11-14 02:38:05 +01:00
|
|
|
KvComment = `#`
|
2019-11-05 15:18:26 +01:00
|
|
|
KvNewline = madmin.KvNewline
|
|
|
|
KvDoubleQuote = madmin.KvDoubleQuote
|
|
|
|
KvSingleQuote = madmin.KvSingleQuote
|
2019-10-30 08:04:39 +01:00
|
|
|
|
|
|
|
// Env prefix used for all envs in MinIO
|
|
|
|
EnvPrefix = "MINIO_"
|
|
|
|
EnvWordDelimiter = `_`
|
2019-10-23 07:59:13 +02:00
|
|
|
)
|
|
|
|
|
2019-11-21 00:10:24 +01:00
|
|
|
// KV - is a shorthand of each key value.
|
|
|
|
type KV struct {
|
|
|
|
Key string `json:"key"`
|
|
|
|
Value string `json:"value"`
|
|
|
|
}
|
|
|
|
|
2019-10-23 07:59:13 +02:00
|
|
|
// KVS - is a shorthand for some wrapper functions
|
|
|
|
// to operate on list of key values.
|
2019-11-21 00:10:24 +01:00
|
|
|
type KVS []KV
|
2019-10-23 07:59:13 +02:00
|
|
|
|
2019-11-14 02:38:05 +01:00
|
|
|
// Empty - return if kv is empty
|
|
|
|
func (kvs KVS) Empty() bool {
|
|
|
|
return len(kvs) == 0
|
|
|
|
}
|
|
|
|
|
2019-10-23 07:59:13 +02:00
|
|
|
func (kvs KVS) String() string {
|
|
|
|
var s strings.Builder
|
2019-11-21 00:10:24 +01:00
|
|
|
for _, kv := range kvs {
|
2019-11-14 02:38:05 +01:00
|
|
|
// Do not need to print if state is on
|
2019-11-21 00:10:24 +01:00
|
|
|
if kv.Key == State && kv.Value == StateOn {
|
2019-11-14 02:38:05 +01:00
|
|
|
continue
|
|
|
|
}
|
2019-11-21 00:10:24 +01:00
|
|
|
s.WriteString(kv.Key)
|
2019-10-23 07:59:13 +02:00
|
|
|
s.WriteString(KvSeparator)
|
|
|
|
s.WriteString(KvDoubleQuote)
|
2019-11-21 00:10:24 +01:00
|
|
|
s.WriteString(kv.Value)
|
2019-10-23 07:59:13 +02:00
|
|
|
s.WriteString(KvDoubleQuote)
|
|
|
|
s.WriteString(KvSpaceSeparator)
|
|
|
|
}
|
|
|
|
return s.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get - returns the value of a key, if not found returns empty.
|
|
|
|
func (kvs KVS) Get(key string) string {
|
2019-11-21 00:10:24 +01:00
|
|
|
v, ok := kvs.Lookup(key)
|
|
|
|
if ok {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup - lookup a key in a list of KVS
|
|
|
|
func (kvs KVS) Lookup(key string) (string, bool) {
|
|
|
|
for _, kv := range kvs {
|
|
|
|
if kv.Key == key {
|
|
|
|
return kv.Value, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", false
|
2019-10-23 07:59:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Config - MinIO server config structure.
|
|
|
|
type Config map[string]map[string]KVS
|
|
|
|
|
|
|
|
func (c Config) String() string {
|
|
|
|
var s strings.Builder
|
|
|
|
for k, v := range c {
|
|
|
|
for target, kv := range v {
|
2019-10-30 08:04:39 +01:00
|
|
|
s.WriteString(k)
|
2019-10-23 07:59:13 +02:00
|
|
|
if target != Default {
|
|
|
|
s.WriteString(SubSystemSeparator)
|
|
|
|
s.WriteString(target)
|
|
|
|
}
|
|
|
|
s.WriteString(KvSpaceSeparator)
|
|
|
|
s.WriteString(kv.String())
|
|
|
|
s.WriteString(KvNewline)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default KV configs for worm and region
|
|
|
|
var (
|
|
|
|
DefaultCredentialKVS = KVS{
|
2019-11-21 00:10:24 +01:00
|
|
|
KV{
|
|
|
|
Key: State,
|
|
|
|
Value: StateOff,
|
|
|
|
},
|
|
|
|
KV{
|
|
|
|
Key: AccessKey,
|
|
|
|
Value: auth.DefaultAccessKey,
|
|
|
|
},
|
|
|
|
KV{
|
|
|
|
Key: SecretKey,
|
|
|
|
Value: auth.DefaultSecretKey,
|
|
|
|
},
|
2019-10-23 07:59:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DefaultRegionKVS = KVS{
|
2019-11-21 00:10:24 +01:00
|
|
|
KV{
|
|
|
|
Key: State,
|
|
|
|
Value: StateOff,
|
|
|
|
},
|
|
|
|
KV{
|
|
|
|
Key: RegionName,
|
|
|
|
Value: "",
|
|
|
|
},
|
2019-10-23 07:59:13 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// LookupCreds - lookup credentials from config.
|
|
|
|
func LookupCreds(kv KVS) (auth.Credentials, error) {
|
|
|
|
if err := CheckValidKeys(CredentialsSubSys, kv, DefaultCredentialKVS); err != nil {
|
|
|
|
return auth.Credentials{}, err
|
|
|
|
}
|
2019-11-14 02:38:05 +01:00
|
|
|
accessKey := env.Get(EnvAccessKey, kv.Get(AccessKey))
|
|
|
|
secretKey := env.Get(EnvSecretKey, kv.Get(SecretKey))
|
|
|
|
if accessKey == "" && secretKey == "" {
|
|
|
|
accessKey = auth.DefaultAccessKey
|
|
|
|
secretKey = auth.DefaultSecretKey
|
|
|
|
}
|
|
|
|
return auth.CreateCredentials(accessKey, secretKey)
|
2019-10-23 07:59:13 +02:00
|
|
|
}
|
|
|
|
|
2019-11-14 23:19:57 +01:00
|
|
|
var validRegionRegex = regexp.MustCompile("^[a-zA-Z][a-zA-Z0-9-_-]+$")
|
|
|
|
|
2019-10-23 07:59:13 +02:00
|
|
|
// LookupRegion - get current region.
|
|
|
|
func LookupRegion(kv KVS) (string, error) {
|
|
|
|
if err := CheckValidKeys(RegionSubSys, kv, DefaultRegionKVS); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
region := env.Get(EnvRegion, "")
|
|
|
|
if region == "" {
|
|
|
|
region = env.Get(EnvRegionName, kv.Get(RegionName))
|
|
|
|
}
|
2019-11-14 23:19:57 +01:00
|
|
|
if region != "" {
|
|
|
|
if validRegionRegex.MatchString(region) {
|
|
|
|
return region, nil
|
|
|
|
}
|
|
|
|
return "", Errorf("region '%s' is invalid, expected simple characters such as [us-east-1, myregion...]",
|
|
|
|
region)
|
|
|
|
}
|
|
|
|
return "", nil
|
2019-10-23 07:59:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CheckValidKeys - checks if inputs KVS has the necessary keys,
|
|
|
|
// returns error if it find extra or superflous keys.
|
|
|
|
func CheckValidKeys(subSys string, kv KVS, validKVS KVS) error {
|
|
|
|
nkv := KVS{}
|
2019-11-21 00:10:24 +01:00
|
|
|
for _, kv := range kv {
|
|
|
|
if _, ok := validKVS.Lookup(kv.Key); !ok {
|
|
|
|
nkv = append(nkv, kv)
|
2019-10-23 07:59:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(nkv) > 0 {
|
2019-11-21 00:10:24 +01:00
|
|
|
return Errorf("found invalid keys (%s) for '%s' sub-system", nkv.String(), subSys)
|
2019-10-23 07:59:13 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LookupWorm - check if worm is enabled
|
2019-11-21 00:10:24 +01:00
|
|
|
func LookupWorm() (bool, error) {
|
|
|
|
return ParseBool(env.Get(EnvWorm, StateOff))
|
2019-10-23 07:59:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// New - initialize a new server config.
|
|
|
|
func New() Config {
|
|
|
|
srvCfg := make(Config)
|
|
|
|
for _, k := range SubSystems.ToSlice() {
|
|
|
|
srvCfg[k] = map[string]KVS{}
|
|
|
|
}
|
|
|
|
return srvCfg
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetKVS - get kvs from specific subsystem.
|
2019-11-21 00:10:24 +01:00
|
|
|
func (c Config) GetKVS(s string, defaultKVS map[string]KVS) (map[string]KVS, error) {
|
2019-10-23 07:59:13 +02:00
|
|
|
if len(s) == 0 {
|
|
|
|
return nil, Error("input cannot be empty")
|
|
|
|
}
|
|
|
|
inputs := strings.Fields(s)
|
|
|
|
if len(inputs) > 1 {
|
2019-11-21 00:10:24 +01:00
|
|
|
return nil, Errorf("invalid number of arguments %s", s)
|
2019-10-23 07:59:13 +02:00
|
|
|
}
|
|
|
|
subSystemValue := strings.SplitN(inputs[0], SubSystemSeparator, 2)
|
|
|
|
if len(subSystemValue) == 0 {
|
2019-11-21 00:10:24 +01:00
|
|
|
return nil, Errorf("invalid number of arguments %s", s)
|
2019-10-23 07:59:13 +02:00
|
|
|
}
|
|
|
|
found := SubSystems.Contains(subSystemValue[0])
|
|
|
|
if !found {
|
2019-11-14 23:19:57 +01:00
|
|
|
// Check for sub-prefix only if the input value is only a
|
|
|
|
// single value, this rejects invalid inputs if any.
|
2019-10-23 07:59:13 +02:00
|
|
|
found = !SubSystems.FuncMatch(strings.HasPrefix, subSystemValue[0]).IsEmpty() && len(subSystemValue) == 1
|
|
|
|
}
|
|
|
|
if !found {
|
2019-11-21 00:10:24 +01:00
|
|
|
return nil, Errorf("unknown sub-system %s", s)
|
2019-10-23 07:59:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
kvs := make(map[string]KVS)
|
|
|
|
var ok bool
|
2019-11-21 00:10:24 +01:00
|
|
|
subSysPrefix := subSystemValue[0]
|
2019-10-23 07:59:13 +02:00
|
|
|
if len(subSystemValue) == 2 {
|
|
|
|
if len(subSystemValue[1]) == 0 {
|
2019-11-21 00:10:24 +01:00
|
|
|
return nil, Errorf("sub-system target '%s' cannot be empty", s)
|
2019-10-23 07:59:13 +02:00
|
|
|
}
|
2019-11-21 00:10:24 +01:00
|
|
|
kvs[inputs[0]], ok = c[subSysPrefix][subSystemValue[1]]
|
2019-10-23 07:59:13 +02:00
|
|
|
if !ok {
|
2019-11-21 00:10:24 +01:00
|
|
|
return nil, Errorf("sub-system target '%s' doesn't exist", s)
|
2019-10-23 07:59:13 +02:00
|
|
|
}
|
2019-11-21 00:10:24 +01:00
|
|
|
} else {
|
|
|
|
for subSys, subSysTgts := range c {
|
|
|
|
if !strings.HasPrefix(subSys, subSysPrefix) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for k, kv := range subSysTgts {
|
|
|
|
if k != Default {
|
|
|
|
kvs[subSys+SubSystemSeparator+k] = kv
|
|
|
|
} else {
|
|
|
|
kvs[subSys] = kv
|
|
|
|
}
|
2019-10-23 07:59:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-21 00:10:24 +01:00
|
|
|
if len(kvs) == 0 {
|
|
|
|
kvs[subSysPrefix] = defaultKVS[subSysPrefix]
|
|
|
|
return kvs, nil
|
|
|
|
}
|
2019-10-23 07:59:13 +02:00
|
|
|
return kvs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DelKVS - delete a specific key.
|
|
|
|
func (c Config) DelKVS(s string) error {
|
|
|
|
if len(s) == 0 {
|
|
|
|
return Error("input arguments cannot be empty")
|
|
|
|
}
|
|
|
|
inputs := strings.Fields(s)
|
|
|
|
if len(inputs) > 1 {
|
2019-11-21 00:10:24 +01:00
|
|
|
return Errorf("invalid number of arguments %s", s)
|
2019-10-23 07:59:13 +02:00
|
|
|
}
|
|
|
|
subSystemValue := strings.SplitN(inputs[0], SubSystemSeparator, 2)
|
|
|
|
if len(subSystemValue) == 0 {
|
2019-11-21 00:10:24 +01:00
|
|
|
return Errorf("invalid number of arguments %s", s)
|
2019-10-23 07:59:13 +02:00
|
|
|
}
|
|
|
|
if !SubSystems.Contains(subSystemValue[0]) {
|
2019-11-21 00:10:24 +01:00
|
|
|
return Errorf("unknown sub-system %s", s)
|
2019-10-23 07:59:13 +02:00
|
|
|
}
|
2019-11-21 00:10:24 +01:00
|
|
|
tgt := Default
|
|
|
|
subSys := subSystemValue[0]
|
2019-10-23 07:59:13 +02:00
|
|
|
if len(subSystemValue) == 2 {
|
|
|
|
if len(subSystemValue[1]) == 0 {
|
2019-11-21 00:10:24 +01:00
|
|
|
return Errorf("sub-system target '%s' cannot be empty", s)
|
2019-10-23 07:59:13 +02:00
|
|
|
}
|
2019-11-21 00:10:24 +01:00
|
|
|
tgt = subSystemValue[1]
|
|
|
|
}
|
|
|
|
_, ok := c[subSys][tgt]
|
|
|
|
if !ok {
|
|
|
|
return Errorf("sub-system %s already deleted", s)
|
2019-10-23 07:59:13 +02:00
|
|
|
}
|
2019-11-21 00:10:24 +01:00
|
|
|
delete(c[subSys], tgt)
|
2019-11-14 02:38:05 +01:00
|
|
|
return nil
|
2019-10-23 07:59:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// This function is needed, to trim off single or double quotes, creeping into the values.
|
|
|
|
func sanitizeValue(v string) string {
|
|
|
|
v = strings.TrimSuffix(strings.TrimPrefix(strings.TrimSpace(v), KvDoubleQuote), KvDoubleQuote)
|
|
|
|
return strings.TrimSuffix(strings.TrimPrefix(v, KvSingleQuote), KvSingleQuote)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clone - clones a config map entirely.
|
|
|
|
func (c Config) Clone() Config {
|
|
|
|
cp := New()
|
|
|
|
for subSys, tgtKV := range c {
|
|
|
|
cp[subSys] = make(map[string]KVS)
|
|
|
|
for tgt, kv := range tgtKV {
|
2019-11-21 00:10:24 +01:00
|
|
|
cp[subSys][tgt] = append(cp[subSys][tgt], kv...)
|
2019-10-23 07:59:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return cp
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetKVS - set specific key values per sub-system.
|
2019-11-21 00:10:24 +01:00
|
|
|
func (c Config) SetKVS(s string, defaultKVS map[string]KVS) error {
|
2019-10-23 07:59:13 +02:00
|
|
|
if len(s) == 0 {
|
|
|
|
return Error("input arguments cannot be empty")
|
|
|
|
}
|
|
|
|
inputs := strings.SplitN(s, KvSpaceSeparator, 2)
|
|
|
|
if len(inputs) <= 1 {
|
2019-11-21 00:10:24 +01:00
|
|
|
return Errorf("invalid number of arguments '%s'", s)
|
2019-10-23 07:59:13 +02:00
|
|
|
}
|
|
|
|
subSystemValue := strings.SplitN(inputs[0], SubSystemSeparator, 2)
|
|
|
|
if len(subSystemValue) == 0 {
|
2019-11-21 00:10:24 +01:00
|
|
|
return Errorf("invalid number of arguments %s", s)
|
2019-10-23 07:59:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if !SubSystems.Contains(subSystemValue[0]) {
|
2019-11-21 00:10:24 +01:00
|
|
|
return Errorf("unknown sub-system %s", s)
|
2019-10-23 07:59:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if SubSystemsSingleTargets.Contains(subSystemValue[0]) && len(subSystemValue) == 2 {
|
2019-11-21 00:10:24 +01:00
|
|
|
return Errorf("sub-system '%s' only supports single target", subSystemValue[0])
|
2019-10-23 07:59:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var kvs = KVS{}
|
|
|
|
var prevK string
|
|
|
|
for _, v := range strings.Fields(inputs[1]) {
|
|
|
|
kv := strings.SplitN(v, KvSeparator, 2)
|
|
|
|
if len(kv) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if len(kv) == 1 && prevK != "" {
|
2019-11-21 00:10:24 +01:00
|
|
|
kvs = append(kvs, KV{
|
|
|
|
Key: prevK,
|
|
|
|
Value: strings.Join([]string{kvs.Get(prevK), sanitizeValue(kv[0])}, KvSpaceSeparator),
|
|
|
|
})
|
2019-10-23 07:59:13 +02:00
|
|
|
continue
|
|
|
|
}
|
2019-11-12 12:16:25 +01:00
|
|
|
if len(kv) == 1 {
|
2019-11-21 00:10:24 +01:00
|
|
|
return Errorf("key '%s', cannot have empty value", kv[0])
|
2019-11-12 12:16:25 +01:00
|
|
|
}
|
2019-10-23 07:59:13 +02:00
|
|
|
prevK = kv[0]
|
2019-11-21 00:10:24 +01:00
|
|
|
kvs = append(kvs, KV{
|
|
|
|
Key: kv[0],
|
|
|
|
Value: sanitizeValue(kv[1]),
|
|
|
|
})
|
2019-10-23 07:59:13 +02:00
|
|
|
}
|
|
|
|
|
2019-10-30 08:04:39 +01:00
|
|
|
tgt := Default
|
2019-11-21 00:10:24 +01:00
|
|
|
subSys := subSystemValue[0]
|
2019-10-23 07:59:13 +02:00
|
|
|
if len(subSystemValue) == 2 {
|
2019-10-30 08:04:39 +01:00
|
|
|
tgt = subSystemValue[1]
|
|
|
|
}
|
2019-11-14 23:19:57 +01:00
|
|
|
|
2019-11-22 22:46:05 +01:00
|
|
|
// Save client sent kvs
|
|
|
|
c[subSys][tgt] = kvs
|
2019-11-14 23:19:57 +01:00
|
|
|
|
2019-11-22 22:46:05 +01:00
|
|
|
_, ok := c[subSys][tgt].Lookup(State)
|
2019-11-14 23:19:57 +01:00
|
|
|
if !ok {
|
|
|
|
// implicit state "on" if not specified.
|
2019-11-21 00:10:24 +01:00
|
|
|
c[subSys][tgt] = append(c[subSys][tgt], KV{
|
|
|
|
Key: State,
|
|
|
|
Value: StateOn,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
for _, kv := range defaultKVS[subSys] {
|
|
|
|
_, ok := c[subSys][tgt].Lookup(kv.Key)
|
|
|
|
if !ok {
|
|
|
|
c[subSys][tgt] = append(c[subSys][tgt], kv)
|
|
|
|
}
|
2019-10-23 07:59:13 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|