func CleanupCreatedPoolRefs(internal bool, app, sha, env string) error { helper.SetRouterRoot(internal) // remove static rule, cleanup rule from trie if needed ruleName := helper.GetAppShaEnvStaticRuleName(app, sha, env) trieName := helper.GetAppEnvTrieName(app, env) // remove static rule from trie trie, err := routerzk.GetTrie(Zk.Conn, trieName) if err != nil { return err } newRules := []string{} for _, rule := range trie.Rules { if rule != ruleName { newRules = append(newRules, rule) } } if len(trie.Rules) != len(newRules) { trie.Rules = newRules err = routerzk.SetTrie(Zk.Conn, trie) if err != nil { return err } } // delete static rule err = routerzk.DelRule(Zk.Conn, ruleName) if err != nil { return err } return nil }
func UpdateAppEnvTrie(internal bool, app, sha, env string) (string, error) { helper.SetRouterRoot(internal) // create trie (if it doesn't exist) trieName := helper.GetAppEnvTrieName(app, env) if exists, err := routerzk.TrieExists(Zk.Conn, trieName); !exists || err != nil { err = routerzk.SetTrie(Zk.Conn, routercfg.Trie{ Name: trieName, Rules: []string{}, Internal: internal, }) if err != nil { return trieName, err } } // if sha != "" attach pool as static rule (if trie is empty) if sha != "" { // if static rule does not exist, create it ruleName := helper.GetAppShaEnvStaticRuleName(app, sha, env) poolName := helper.CreatePoolName(app, sha, env) if exists, err := routerzk.RuleExists(Zk.Conn, ruleName); !exists || err != nil { err = routerzk.SetRule(Zk.Conn, routercfg.Rule{ Name: ruleName, Type: "static", Value: "true", Pool: poolName, Internal: internal, }) if err != nil { return trieName, err } } trie, err := routerzk.GetTrie(Zk.Conn, trieName) if err != nil { return trieName, err } if len(trie.Rules) == 0 { trie.Rules = []string{ruleName} } else { trie.Rules = append(trie.Rules, ruleName) } if err = routerzk.SetTrie(Zk.Conn, trie); err != nil { return trieName, err } } return trieName, nil }