aboutsummaryrefslogtreecommitdiff
path: root/util.go
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2021-03-09 19:00:45 +0100
committerAlex Auvolat <alex@adnab.me>2021-03-09 19:00:45 +0100
commit99d8955ab3b5e24552d1f2b621744d07013f58ac (patch)
tree5820b5b594aeea4d4bb366055efef62046221119 /util.go
parent1a20a64eff34336789c0e3b41db1de727e3fe998 (diff)
downloadbottin-99d8955ab3b5e24552d1f2b621744d07013f58ac.tar.gz
bottin-99d8955ab3b5e24552d1f2b621744d07013f58ac.zip
Refactor & add case normalization logic to putAttributes
Diffstat (limited to 'util.go')
-rw-r--r--util.go92
1 files changed, 49 insertions, 43 deletions
diff --git a/util.go b/util.go
index 30d7a67..cbb6b0f 100644
--- a/util.go
+++ b/util.go
@@ -11,6 +11,13 @@ import (
log "github.com/sirupsen/logrus"
)
+// DNs ----
+
+type dnComponent struct {
+ Type string
+ Value string
+}
+
func dnToConsul(dn string) (string, error) {
if strings.Contains(dn, "/") {
return "", fmt.Errorf("DN %s contains a /", dn)
@@ -45,6 +52,48 @@ func consulToDN(key string) (string, string, error) {
return "", "", fmt.Errorf("Consul key %s does not end with attribute=something", key)
}
+func parseDN(dn string) ([]dnComponent, error) {
+ rdns := strings.Split(dn, ",")
+
+ ret := []dnComponent{}
+
+ for _, rdn := range rdns {
+ splits := strings.Split(rdn, "=")
+ if len(splits) != 2 {
+ return nil, fmt.Errorf("Wrong DN component: %s (expected type=value)", rdn)
+ }
+ ret = append(ret, dnComponent{
+ Type: strings.ToLower(strings.TrimSpace(splits[0])),
+ Value: strings.ToLower(strings.TrimSpace(splits[1])),
+ })
+ }
+ return ret, nil
+}
+
+func unparseDN(path []dnComponent) string {
+ ret := ""
+ for _, c := range path {
+ if ret != "" {
+ ret = ret + ","
+ }
+ ret = ret + c.Type + "=" + c.Value
+ }
+ return ret
+}
+
+func canonicalDN(dn string) (string, error) {
+ path, err := parseDN(dn)
+ if err != nil {
+ return "", err
+ }
+
+ return unparseDN(path), nil
+}
+
+// Values
+
+type Entry map[string][]string
+
func parseValue(value []byte) ([]string, error) {
val := []string{}
err := json.Unmarshal(value, &val)
@@ -82,49 +131,6 @@ func parseConsulResult(data []*consul.KVPair) (map[string]Entry, error) {
return aggregator, nil
}
-type DNComponent struct {
- Type string
- Value string
-}
-
-func parseDN(dn string) ([]DNComponent, error) {
- rdns := strings.Split(dn, ",")
-
- ret := []DNComponent{}
-
- for _, rdn := range rdns {
- splits := strings.Split(rdn, "=")
- if len(splits) != 2 {
- return nil, fmt.Errorf("Wrong DN component: %s (expected type=value)", rdn)
- }
- ret = append(ret, DNComponent{
- Type: strings.ToLower(strings.TrimSpace(splits[0])),
- Value: strings.ToLower(strings.TrimSpace(splits[1])),
- })
- }
- return ret, nil
-}
-
-func unparseDN(path []DNComponent) string {
- ret := ""
- for _, c := range path {
- if ret != "" {
- ret = ret + ","
- }
- ret = ret + c.Type + "=" + c.Value
- }
- return ret
-}
-
-func canonicalDN(dn string) (string, error) {
- path, err := parseDN(dn)
- if err != nil {
- return "", err
- }
-
- return unparseDN(path), nil
-}
-
func checkRestrictedAttr(attr string) error {
RESTRICTED_ATTRS := []string{
ATTR_MEMBEROF,