auth

Paddy 2015-03-24 Child:3e8964a914ef

152:de5e09680f6b Go to Latest

auth/scope_postgres.go

Implement postgres version of scopeStore. Update the authd server to use postgres as its scopeStore, instead of memstore. panic when starting the authd server if the CreateScopes call fails. This should, ideally, ignore ErrScopeAlreadyExists errors, but does not as of this commit. Update the simple.gotmpl template to properly display scopes, after switching to the Scope type instead of simply passing around the string the client supplied broke the template and I never bothered fixing it. Update the updateScopes method on the scopeStore (and the corresponding UpdateScopes method on the Context type) to be updateScope/UpdateScope. Operating on several scopes at a time like that is simply too challenging in SQL and I can't justify the complexity with a use case. Add a helper method to ScopeChange called Empty(), which returns true if the ScopeChange is full of nil values. Remove the ID from the ScopeChange type, because we're no longer accepting multiple ScopeChange types in UpdateScope, so we can supply that information outside the ScopeChange, which matches the rest of our update* methods. Correct our tests in scope_test.go to correctly use the updateScope method instead of the old updateScopes method. This generally just resulted in calling updateScope multiple times, as opposed to just once. Add a scope table initialization to the sql/postgres_init.sql script.

History
paddy@152 1 package auth
paddy@152 2
paddy@152 3 import (
paddy@152 4 "github.com/secondbit/pan"
paddy@152 5 )
paddy@152 6
paddy@152 7 func (s Scope) GetSQLTableName() string {
paddy@152 8 return "scopes"
paddy@152 9 }
paddy@152 10
paddy@152 11 func (p *postgres) createScopesSQL(scopes []Scope) *pan.Query {
paddy@152 12 fields, _ := pan.GetFields(scopes[0])
paddy@152 13 query := pan.New(pan.POSTGRES, "INSERT INTO "+pan.GetTableName(scopes[0]))
paddy@152 14 query.Include("(" + pan.QueryList(fields) + ")")
paddy@152 15 query.Include("VALUES")
paddy@152 16 for _, scope := range scopes {
paddy@152 17 _, values := pan.GetFields(scope)
paddy@152 18 query.Include("("+pan.VariableList(len(values))+")", values...)
paddy@152 19 }
paddy@152 20 return query.FlushExpressions(" ")
paddy@152 21 }
paddy@152 22
paddy@152 23 func (p *postgres) createScopes(scopes []Scope) error {
paddy@152 24 if len(scopes) < 1 {
paddy@152 25 return nil
paddy@152 26 }
paddy@152 27 query := p.createScopesSQL(scopes)
paddy@152 28 _, err := p.db.Exec(query.String(), query.Args...)
paddy@152 29 return err
paddy@152 30 }
paddy@152 31
paddy@152 32 func (p *postgres) getScopesSQL(ids []string) *pan.Query {
paddy@152 33 var scope Scope
paddy@152 34 intids := make([]interface{}, len(ids))
paddy@152 35 for pos, id := range ids {
paddy@152 36 intids[pos] = id
paddy@152 37 }
paddy@152 38 fields, _ := pan.GetFields(scope)
paddy@152 39 query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(scope))
paddy@152 40 query.IncludeWhere()
paddy@152 41 query.Include(pan.GetUnquotedColumn(scope, "ID") + " IN")
paddy@152 42 query.Include("("+pan.VariableList(len(ids))+")", intids...)
paddy@152 43 return query.FlushExpressions(" ")
paddy@152 44 }
paddy@152 45
paddy@152 46 func (p *postgres) getScopes(ids []string) ([]Scope, error) {
paddy@152 47 query := p.getScopesSQL(ids)
paddy@152 48 rows, err := p.db.Query(query.String(), query.Args...)
paddy@152 49 if err != nil {
paddy@152 50 return []Scope{}, err
paddy@152 51 }
paddy@152 52 var scopes []Scope
paddy@152 53 for rows.Next() {
paddy@152 54 var scope Scope
paddy@152 55 err := pan.Unmarshal(rows, &scope)
paddy@152 56 if err != nil {
paddy@152 57 return scopes, err
paddy@152 58 }
paddy@152 59 scopes = append(scopes, scope)
paddy@152 60 }
paddy@152 61 if err = rows.Err(); err != nil {
paddy@152 62 return scopes, err
paddy@152 63 }
paddy@152 64 return scopes, nil
paddy@152 65 }
paddy@152 66
paddy@152 67 func (p *postgres) updateScopeSQL(id string, change ScopeChange) *pan.Query {
paddy@152 68 var scope Scope
paddy@152 69 query := pan.New(pan.POSTGRES, "UPDATE "+pan.GetTableName(scope)+" SET ")
paddy@152 70 query.IncludeIfNotNil(pan.GetUnquotedColumn(scope, "Name")+" = ?", change.Name)
paddy@152 71 query.IncludeIfNotNil(pan.GetUnquotedColumn(scope, "Description")+" = ?", change.Description)
paddy@152 72 query.FlushExpressions(", ")
paddy@152 73 query.IncludeWhere()
paddy@152 74 query.Include(pan.GetUnquotedColumn(scope, "ID")+" = ?", id)
paddy@152 75 return query.FlushExpressions(" ")
paddy@152 76 }
paddy@152 77
paddy@152 78 func (p *postgres) updateScope(id string, change ScopeChange) error {
paddy@152 79 if change.Empty() {
paddy@152 80 return nil
paddy@152 81 }
paddy@152 82 query := p.updateScopeSQL(id, change)
paddy@152 83 _, err := p.db.Exec(query.String(), query.Args...)
paddy@152 84 return err
paddy@152 85 }
paddy@152 86
paddy@152 87 func (p *postgres) removeScopesSQL(ids []string) *pan.Query {
paddy@152 88 var scope Scope
paddy@152 89 intids := make([]interface{}, len(ids))
paddy@152 90 for pos, id := range ids {
paddy@152 91 intids[pos] = id
paddy@152 92 }
paddy@152 93 query := pan.New(pan.POSTGRES, "DELETE FROM "+pan.GetTableName(scope))
paddy@152 94 query.IncludeWhere()
paddy@152 95 query.Include(pan.GetUnquotedColumn(scope, "ID") + " IN")
paddy@152 96 query.Include("("+pan.VariableList(len(ids))+")", intids...)
paddy@152 97 return query.FlushExpressions(" ")
paddy@152 98 }
paddy@152 99
paddy@152 100 func (p *postgres) removeScopes(ids []string) error {
paddy@152 101 query := p.removeScopesSQL(ids)
paddy@152 102 _, err := p.db.Exec(query.String(), query.Args...)
paddy@152 103 if err != nil {
paddy@152 104 return err
paddy@152 105 }
paddy@152 106 return nil
paddy@152 107 }
paddy@152 108
paddy@152 109 func (p *postgres) listScopesSQL() *pan.Query {
paddy@152 110 var scope Scope
paddy@152 111 fields, _ := pan.GetFields(scope)
paddy@152 112 query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(scope))
paddy@152 113 return query.FlushExpressions(" ")
paddy@152 114 }
paddy@152 115
paddy@152 116 func (p *postgres) listScopes() ([]Scope, error) {
paddy@152 117 query := p.listScopesSQL()
paddy@152 118 rows, err := p.db.Query(query.String(), query.Args...)
paddy@152 119 if err != nil {
paddy@152 120 return []Scope{}, err
paddy@152 121 }
paddy@152 122 var scopes []Scope
paddy@152 123 for rows.Next() {
paddy@152 124 var scope Scope
paddy@152 125 err = pan.Unmarshal(rows, &scope)
paddy@152 126 if err != nil {
paddy@152 127 return scopes, err
paddy@152 128 }
paddy@152 129 scopes = append(scopes, scope)
paddy@152 130 }
paddy@152 131 if err = rows.Err(); err != nil {
paddy@152 132 return scopes, err
paddy@152 133 }
paddy@152 134 return scopes, nil
paddy@152 135 }