本文整理汇总了Golang中github.com/wayneeseguin/rdpg-agent/log.Error函数的典型用法代码示例。如果您正苦于以下问题:Golang Error函数的具体用法?Golang Error怎么用?Golang Error使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Error函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: httpAuth
func httpAuth(h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, request *http.Request) {
if len(request.Header["Authorization"]) == 0 {
log.Trace(fmt.Sprintf("httpAuth(): Authorization Required"))
http.Error(w, "Authorization Required", http.StatusUnauthorized)
return
}
auth := strings.SplitN(request.Header["Authorization"][0], " ", 2)
if len(auth) != 2 || auth[0] != "Basic" {
log.Error(fmt.Sprintf("httpAuth(): Unhandled Authorization Type, Expected Basic"))
http.Error(w, "Unhandled Authroization Type, Expected Basic\n", http.StatusBadRequest)
return
}
payload, err := base64.StdEncoding.DecodeString(auth[1])
if err != nil {
log.Error(fmt.Sprintf("httpAuth(): Authorization Failed"))
http.Error(w, "Authorization Failed\n", http.StatusUnauthorized)
return
}
nv := strings.SplitN(string(payload), ":", 2)
if (len(nv) != 2) || !isAuthorized(nv[0], nv[1]) {
log.Error(fmt.Sprintf("httpAuth(): Authorization Failed"))
http.Error(w, "Authorization Failed\n", http.StatusUnauthorized)
return
}
h(w, request)
}
}
示例2: OpenDB
// TODO: Instead pass back *sql.DB
func (r *RDPG) OpenDB(dbname string) error {
if r.DB == nil {
u, err := url.Parse(r.URI)
if err != nil {
log.Error(fmt.Sprintf("Failed parsing URI %s err: %s", r.URI, err))
}
u.Path = dbname
r.URI = u.String()
db, err := sqlx.Connect("postgres", r.URI)
if err != nil {
log.Error(fmt.Sprintf("Failed connecting to %s err: %s", rdpgURI, err))
return err
}
r.DB = db
} else {
err := r.DB.Ping()
if err != nil {
db, err := sqlx.Connect("postgres", r.URI)
if err != nil {
log.Error(fmt.Sprintf("Failed connecting to %s err: %s", rdpgURI, err))
proc, _ := os.FindProcess(os.Getpid())
proc.Signal(syscall.SIGTERM)
return err
}
r.DB = db
}
}
return nil
}
示例3: Fetch
func (c *Catalog) Fetch() (err error) {
r := rdpg.New()
err = r.OpenDB("rdpg")
if err != nil {
log.Error(fmt.Sprintf("Failed fetching catalog from database: %s", err))
return
}
db := r.DB
err = db.Select(&c.Services, `SELECT service_id,name,description,bindable FROM cfsb.services;`)
if err != nil {
log.Error(fmt.Sprintf("Catalog#Fetch() selecting from cfsb.services %s", err.Error()))
return
}
// TODO: Account for plans being associated with a service.
for i, _ := range c.Services {
service := &c.Services[i]
err = db.Select(&service.Plans, `SELECT plan_id,name,description FROM cfsb.plans;`)
if err != nil {
log.Error(fmt.Sprintf("Catalog#Fetch() Service Plans %s", err.Error()))
return
}
c.Services[i].Tags = []string{"rdpg", "postgresql"}
// c.Services[i].Dashboard = DashboardClient{}
}
return
}
示例4: CreateDatabase
func (n *Node) CreateDatabase(dbname, owner string) (err error) {
n.Database = "postgres"
db, err := n.Connect()
if err != nil {
log.Error(fmt.Sprintf("Node#CreateDatabase(%s) %s ! %s", dbname, n.Host, err))
return
}
defer db.Close()
sq := fmt.Sprintf(`CREATE DATABASE %s WITH OWNER %s TEMPLATE template0 ENCODING 'UTF8'`, dbname, owner)
log.Trace(fmt.Sprintf(`Node#CreateDatabase(%s) %s > %s`, dbname, n.Host, sq))
_, err = db.Query(sq)
if err != nil {
log.Error(fmt.Sprintf("Node#CreateDatabase(%s) %s ! %s", dbname, n.Host, err))
return
}
sq = fmt.Sprintf(`REVOKE ALL ON DATABASE "%s" FROM public`, dbname)
log.Trace(fmt.Sprintf(`Node#CreateDatabase(%s) %s > %s`, dbname, n.Host, sq))
_, err = db.Exec(sq)
if err != nil {
log.Error(fmt.Sprintf("Node#CreateDatabase(%s) %s ! %s", dbname, n.Host, err))
}
sq = fmt.Sprintf(`GRANT ALL PRIVILEGES ON DATABASE %s TO %s`, dbname, owner)
log.Trace(fmt.Sprintf(`Node#CreateDatabase(%s) %s > %s`, dbname, n.Host, sq))
_, err = db.Query(sq)
if err != nil {
log.Error(fmt.Sprintf(`Node#CreateDatabase(%s) %s ! %s`, dbname, n.Host, err))
return
}
return nil
}
示例5: CatalogHandler
/*
(FC) GET /v2/catalog
*/
func CatalogHandler(w http.ResponseWriter, request *http.Request) {
log.Trace(fmt.Sprintf("%s /v2/catalog", request.Method))
switch request.Method {
case "GET":
c := Catalog{}
err := c.Fetch()
if err != nil {
msg := fmt.Sprintf(`{"status": %d, "description": "%s"}`, http.StatusInternalServerError, err)
log.Error(msg)
http.Error(w, msg, http.StatusInternalServerError)
return
}
jsonCatalog, err := json.Marshal(c)
if err != nil {
msg := fmt.Sprintf(`{"status": %d, "description": "%s"}`, http.StatusInternalServerError, err)
log.Error(msg)
http.Error(w, msg, http.StatusInternalServerError)
} else {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
w.Write(jsonCatalog)
}
default:
msg := fmt.Sprintf(`{"status": %d, "description": "Allowed Methods: GET"}`, http.StatusMethodNotAllowed)
log.Error(msg)
http.Error(w, msg, http.StatusMethodNotAllowed)
}
return
}
示例6: CreateUser
func (r *RDPG) CreateUser(username, password string) (err error) {
for _, node := range r.Nodes() {
node.Database = `postgres`
db, err := node.Connect()
if err != nil {
log.Error(fmt.Sprintf(`RDPG#CreateUser(%s) %s ! %s`, username, node.Host, err))
return err
}
// TODO: Check if user exists first
sq := fmt.Sprintf(`CREATE USER %s;`, username)
log.Trace(fmt.Sprintf(`RDPG#CreateUser(%s) %s > %s`, username, node.Host, sq))
_, err = db.Exec(sq)
if err != nil {
log.Error(fmt.Sprintf("RDPG#CreateUser(%s) %s ! %s", username, node.Host, err))
db.Close()
return err
}
sq = fmt.Sprintf(`ALTER USER %s ENCRYPTED PASSWORD '%s'`, username, password)
log.Trace(fmt.Sprintf(`RDPG#CreateUser(%s) %s > %s`, username, node.Host, sq))
_, err = db.Exec(sq)
if err != nil {
log.Error(fmt.Sprintf("RDPG#CreateUser(%s) %s ! %s", username, node.Host, err))
}
db.Close()
}
return nil
}
示例7: ServiceHandler
/*
POST /services/{service}/{action}
*/
func ServiceHandler(w http.ResponseWriter, request *http.Request) {
vars := mux.Vars(request)
log.Trace(fmt.Sprintf("%s /services/%s/%s", request.Method, vars["service"], vars["action"]))
switch request.Method {
case "PUT":
service, err := NewService(vars["service"])
if err != nil {
log.Error(fmt.Sprintf("ServiceHandler(): NewService(%s)"))
http.Error(w, `{"status": %d, "description": "%s"}`, http.StatusInternalServerError)
return
}
switch vars["action"] {
case "configure":
err := service.Configure()
if err != nil {
msg := fmt.Sprintf(`{"status": %d, "description": "%s"}`, http.StatusInternalServerError, err)
log.Error(msg)
http.Error(w, msg, http.StatusInternalServerError)
}
msg := fmt.Sprintf(`{"status": %d, "description": "%s %s"}`, http.StatusOK, vars["service"], vars["action"])
log.Trace(msg)
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, msg)
default:
msg := fmt.Sprintf(`{"status": %d, "description": "Invalid Action %s for %s"}`, http.StatusBadRequest, vars["action"], vars["service"])
log.Error(msg)
http.Error(w, msg, http.StatusBadRequest)
}
default:
msg := fmt.Sprintf(`{"status": %d, "description": "Method not allowed %s"}`, http.StatusMethodNotAllowed, request.Method)
log.Error(msg)
http.Error(w, msg, http.StatusMethodNotAllowed)
}
}
示例8: DropDatabase
func (r *RDPG) DropDatabase(dbname string) (err error) {
nodes := r.Nodes()
for i := len(nodes) - 1; i >= 0; i-- {
node := nodes[i]
node.Database = "postgres"
db, err := node.Connect()
if err != nil {
log.Error(fmt.Sprintf("RDPG#DropDatabase(%s) %s ! %s", dbname, node.Host, err))
return err
}
// sq := fmt.Sprintf(SELECT slot_name FROM pg_replication_slots WHERE database='%s',dbname);
// pg_recvlogical --drop-slot
// TODO: How do we drop a database in bdr properly?
sq := fmt.Sprintf(`DROP DATABASE IF EXISTS %s`, dbname)
log.Trace(fmt.Sprintf(`RDPG#DropDatabase(%s) %s DROP > %s`, dbname, node.Host, sq))
_, err = db.Exec(sq)
if err != nil {
log.Error(fmt.Sprintf("RDPG#DropDatabase(%s) DROP %s ! %s", dbname, node.Host, err))
}
db.Close()
}
return nil
}
示例9: connect
func (r *RDPG) connect() (db *sqlx.DB, err error) {
db, err = sqlx.Connect("postgres", r.URI)
if err != nil {
log.Error(fmt.Sprintf("rdpg.Node#Connect() %s ! %s", r.URI, err))
}
return db, err
}
示例10: NewRDPG
// TODO: RDPG Struct => RDPG Struct, allowing for multiple instances of RDPG
func NewRDPG(uri string) *RDPG {
if uri == "" || uri[0:13] != "postgresql://" {
log.Error(fmt.Sprintf("rdpg.NewRDPG() uri malformed ! %s", uri))
return nil
}
return &RDPG{URI: uri}
}
示例11: RemoveBinding
func RemoveBinding(bindingId string) (binding *Binding, err error) {
binding, err = FindBinding(bindingId)
if err != nil {
log.Error(fmt.Sprintf(`cfsb.CreateBinding(%s) ! %s`, bindingId, err))
return
}
r := rdpg.New()
sq := `UPDATE cfsb.bindings SET ineffective_at = CURRENT_TIMESTAMP WHERE binding_id = $1;`
log.Trace(fmt.Sprintf(`cfsb.RemoveBinding(%s) > %s`, bindingId, sq))
r.OpenDB("rdpg")
_, err = r.DB.Query(sq, bindingId)
if err != nil {
log.Error(fmt.Sprintf(`cfsb.CreateBinding(%s) ! %s`, bindingId, err))
}
r.DB.Close()
return
}
示例12: Connect
func (n *Node) Connect() (db *sqlx.DB, err error) {
uri := n.URI()
db, err = sqlx.Connect(`postgres`, uri) // n.LocalDSN)
if err != nil {
log.Error(fmt.Sprintf(`rdpg.Node#Connect() %s ! %s`, uri, err))
return db, err
}
return db, nil
}
示例13: Provision
func (i *Instance) Provision() (err error) {
i.Pass = strings.ToLower(strings.Replace(rdpg.NewUUID().String(), "-", "", -1))
r := rdpg.New()
// TODO: Alter this logic based on "plan"
err = r.CreateUser(i.User, i.Pass)
if err != nil {
log.Error(fmt.Sprintf("Instance#Provision(%s) CreateUser(%s) ! %s", i.InstanceId, i.User, err))
return err
}
err = r.CreateDatabase(i.Database, i.User)
if err != nil {
log.Error(fmt.Sprintf("Instance#Provision(%s) CreateDatabase(%s,%s) ! %s", i.InstanceId, i.Database, i.User, err))
return err
}
err = r.CreateReplicationGroup(i.Database)
if err != nil {
log.Error(fmt.Sprintf("Instance#Provision(%s) CreateReplicationGroup(%s) ! %s", i.InstanceId, i.Database, err))
return err
}
r.OpenDB("rdpg")
sq := `INSERT INTO cfsb.instances
(instance_id, service_id, plan_id, organization_id, space_id, dbname, uname, pass)
VALUES ($1,$2,$3,$4,$5,$6,$7,$8);
`
_, err = r.DB.Query(sq, i.InstanceId, i.ServiceId, i.PlanId, i.OrganizationId, i.SpaceId, i.Database, i.User, i.Pass)
if err != nil {
log.Error(fmt.Sprintf(`Instance#Provision(%s) ! %s`, i.InstanceId, err))
}
nodes := r.Nodes()
for _, node := range nodes {
err := node.AdminAPI("PUT", "services/pgbouncer/configure")
if err != nil {
log.Error(fmt.Sprintf(`Instance#Provision(%s) %s ! %s`, i.InstanceId, node.Host, err))
}
}
r.DB.Close()
return nil
}
示例14: Remove
func (i *Instance) Remove() (err error) {
r := rdpg.New()
r.OpenDB("rdpg")
_, err = r.DB.Exec(`UPDATE cfsb.instances SET ineffective_at = CURRENT_TIMESTAMP WHERE id=$1`, i.Id)
if err != nil {
log.Error(fmt.Sprintf("Instance#Remove(%s) ! %s", i.InstanceId, err))
}
time.Sleep(1) // Wait for the update to propigate to the other nodes.
for _, node := range r.Nodes() {
err := node.AdminAPI("PUT", "services/pgbouncer/configure")
if err != nil {
log.Error(fmt.Sprintf(`Instance#Provision(%s) %s ! %s`, i.InstanceId, node.Host, err))
}
}
r.DB.Close()
return
}
示例15: CreateUser
func (n *Node) CreateUser(name, password string) (err error) {
if n.User != `postgres` {
return errors.New(fmt.Sprintf(`Node user is not postgres, can not create a user with '%s'`, n.User))
}
uri := n.URI()
db, err := sqlx.Connect(`postgres`, uri)
if err != nil {
log.Error(fmt.Sprintf(`Node#CreateUser(%s) %s ! %s`, name, uri, err))
return err
}
defer db.Close()
err = db.Get(&name, `SELECT rolname FROM pg_roles WHERE rolname=? LIMIT 1;`, name)
if err != nil {
log.Error(fmt.Sprintf(`Node#CreateUser(%s) %s ! %s`, name, n.Host, err))
return err
}
if name != "" {
log.Debug(fmt.Sprintf(`User %s already exists, skipping.`, name))
return nil
}
sq := fmt.Sprintf(`CREATE USER %s WITH SUPERUSER CREATEDB CREATEROLE INHERIT;`, name)
result, err := db.Exec(sq)
rows, _ := result.RowsAffected()
if rows > 0 {
log.Debug(fmt.Sprintf(`Node#CreateUser(%s) %s User Created`, n.Host, name))
}
if err != nil {
log.Error(fmt.Sprintf(`Node#CreateUser(%s) %s ! %s`, name, n.Host, err))
return err
}
sq = fmt.Sprintf(`ALTER USER %s ENCRYPTED PASSWORD %s;`, name, password)
_, err = db.Exec(sq)
if err != nil {
log.Error(fmt.Sprintf(`Node#CreateUser(%s) %s ! %s`, name, n.Host, err))
return err
}
return nil
}