agola/internal/services/configstore/readdb/org.go

399 lines
11 KiB
Go
Raw Permalink Normal View History

2019-02-28 14:52:35 +00:00
// Copyright 2019 Sorint.lab
//
// 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 readdb
import (
"database/sql"
"encoding/json"
2019-07-01 09:40:20 +00:00
"agola.io/agola/internal/db"
"agola.io/agola/internal/errors"
2019-07-01 09:40:20 +00:00
"agola.io/agola/internal/services/configstore/common"
"agola.io/agola/internal/util"
"agola.io/agola/services/configstore/types"
2019-02-28 14:52:35 +00:00
sq "github.com/Masterminds/squirrel"
)
var (
orgSelect = sb.Select("org.id", "org.data").From("org")
orgInsert = sb.Insert("org").Columns("id", "name", "data")
orgmemberSelect = sb.Select("orgmember.id", "orgmember.data").From("orgmember")
orgmemberInsert = sb.Insert("orgmember").Columns("id", "orgid", "userid", "role", "data")
2019-02-28 14:52:35 +00:00
)
func (r *ReadDB) insertOrg(tx *db.Tx, data []byte) error {
org := types.Organization{}
if err := json.Unmarshal(data, &org); err != nil {
return errors.Wrapf(err, "failed to unmarshal org")
2019-02-28 14:52:35 +00:00
}
r.log.Debug().Msgf("inserting org: %s", util.Dump(org))
2019-02-28 14:52:35 +00:00
// poor man insert or update...
if err := r.deleteOrg(tx, org.ID); err != nil {
return errors.WithStack(err)
2019-02-28 14:52:35 +00:00
}
q, args, err := orgInsert.Values(org.ID, org.Name, data).ToSql()
if err != nil {
return errors.Wrapf(err, "failed to build query")
2019-02-28 14:52:35 +00:00
}
if _, err := tx.Exec(q, args...); err != nil {
return errors.Wrapf(err, "failed to insert org")
2019-02-28 14:52:35 +00:00
}
return nil
}
func (r *ReadDB) deleteOrg(tx *db.Tx, orgID string) error {
if _, err := tx.Exec("delete from org where id = $1", orgID); err != nil {
return errors.Wrapf(err, "failed to delete org")
2019-02-28 14:52:35 +00:00
}
return nil
}
func (r *ReadDB) GetOrg(tx *db.Tx, orgRef string) (*types.Organization, error) {
refType, err := common.ParseNameRef(orgRef)
if err != nil {
return nil, errors.WithStack(err)
}
var org *types.Organization
switch refType {
case common.RefTypeID:
org, err = r.GetOrgByID(tx, orgRef)
case common.RefTypeName:
org, err = r.GetOrgByName(tx, orgRef)
}
return org, errors.WithStack(err)
}
func (r *ReadDB) GetOrgByID(tx *db.Tx, orgID string) (*types.Organization, error) {
2019-02-28 14:52:35 +00:00
q, args, err := orgSelect.Where(sq.Eq{"id": orgID}).ToSql()
r.log.Debug().Msgf("q: %s, args: %s", q, util.Dump(args))
2019-02-28 14:52:35 +00:00
if err != nil {
return nil, errors.Wrapf(err, "failed to build query")
2019-02-28 14:52:35 +00:00
}
orgs, _, err := fetchOrgs(tx, q, args...)
if err != nil {
return nil, errors.WithStack(err)
2019-02-28 14:52:35 +00:00
}
if len(orgs) > 1 {
return nil, errors.Errorf("too many rows returned")
}
if len(orgs) == 0 {
return nil, nil
}
return orgs[0], nil
}
func (r *ReadDB) GetOrgByName(tx *db.Tx, name string) (*types.Organization, error) {
q, args, err := orgSelect.Where(sq.Eq{"name": name}).ToSql()
r.log.Debug().Msgf("q: %s, args: %s", q, util.Dump(args))
2019-02-28 14:52:35 +00:00
if err != nil {
return nil, errors.Wrapf(err, "failed to build query")
2019-02-28 14:52:35 +00:00
}
orgs, _, err := fetchOrgs(tx, q, args...)
if err != nil {
return nil, errors.WithStack(err)
2019-02-28 14:52:35 +00:00
}
if len(orgs) > 1 {
return nil, errors.Errorf("too many rows returned")
}
if len(orgs) == 0 {
return nil, nil
}
return orgs[0], nil
}
func getOrgsFilteredQuery(startOrgName string, limit int, asc bool) sq.SelectBuilder {
fields := []string{"id", "data"}
s := sb.Select(fields...).From("org as org")
if asc {
s = s.OrderBy("org.name asc")
} else {
s = s.OrderBy("org.name desc")
}
if startOrgName != "" {
if asc {
s = s.Where(sq.Gt{"org.name": startOrgName})
} else {
s = s.Where(sq.Lt{"org.name": startOrgName})
}
}
if limit > 0 {
s = s.Limit(uint64(limit))
}
return s
}
func (r *ReadDB) GetOrgs(tx *db.Tx, startOrgName string, limit int, asc bool) ([]*types.Organization, error) {
var orgs []*types.Organization
s := getOrgsFilteredQuery(startOrgName, limit, asc)
q, args, err := s.ToSql()
r.log.Debug().Msgf("q: %s, args: %s", q, util.Dump(args))
2019-02-28 14:52:35 +00:00
if err != nil {
return nil, errors.Wrapf(err, "failed to build query")
2019-02-28 14:52:35 +00:00
}
rows, err := tx.Query(q, args...)
if err != nil {
return nil, errors.WithStack(err)
2019-02-28 14:52:35 +00:00
}
orgs, _, err = scanOrgs(rows)
return orgs, errors.WithStack(err)
2019-02-28 14:52:35 +00:00
}
func fetchOrgs(tx *db.Tx, q string, args ...interface{}) ([]*types.Organization, []string, error) {
rows, err := tx.Query(q, args...)
if err != nil {
return nil, nil, errors.WithStack(err)
2019-02-28 14:52:35 +00:00
}
defer rows.Close()
return scanOrgs(rows)
}
func scanOrg(rows *sql.Rows, additionalFields ...interface{}) (*types.Organization, string, error) {
var id string
var data []byte
if err := rows.Scan(&id, &data); err != nil {
return nil, "", errors.Wrapf(err, "failed to scan rows")
2019-02-28 14:52:35 +00:00
}
org := types.Organization{}
if len(data) > 0 {
if err := json.Unmarshal(data, &org); err != nil {
return nil, "", errors.Wrapf(err, "failed to unmarshal org")
2019-02-28 14:52:35 +00:00
}
}
return &org, id, nil
}
func scanOrgs(rows *sql.Rows) ([]*types.Organization, []string, error) {
orgs := []*types.Organization{}
ids := []string{}
for rows.Next() {
org, id, err := scanOrg(rows)
2019-02-28 14:52:35 +00:00
if err != nil {
rows.Close()
return nil, nil, errors.WithStack(err)
2019-02-28 14:52:35 +00:00
}
orgs = append(orgs, org)
2019-02-28 14:52:35 +00:00
ids = append(ids, id)
}
if err := rows.Err(); err != nil {
return nil, nil, errors.WithStack(err)
2019-02-28 14:52:35 +00:00
}
return orgs, ids, nil
}
func (r *ReadDB) insertOrgMember(tx *db.Tx, data []byte) error {
orgmember := types.OrganizationMember{}
if err := json.Unmarshal(data, &orgmember); err != nil {
return errors.Wrapf(err, "failed to unmarshal orgmember")
}
r.log.Debug().Msgf("inserting orgmember: %s", util.Dump(orgmember))
// poor man insert or update...
if err := r.deleteOrgMember(tx, orgmember.ID); err != nil {
return errors.WithStack(err)
}
q, args, err := orgmemberInsert.Values(orgmember.ID, orgmember.OrganizationID, orgmember.UserID, orgmember.MemberRole, data).ToSql()
if err != nil {
return errors.Wrapf(err, "failed to build query")
}
if _, err := tx.Exec(q, args...); err != nil {
return errors.Wrapf(err, "failed to insert orgmember")
}
return nil
}
func (r *ReadDB) deleteOrgMember(tx *db.Tx, orgmemberID string) error {
if _, err := tx.Exec("delete from orgmember where id = $1", orgmemberID); err != nil {
return errors.Wrapf(err, "failed to delete orgmember")
}
return nil
}
2019-05-09 14:47:22 +00:00
func (r *ReadDB) GetOrgMemberByOrgUserID(tx *db.Tx, orgID, userID string) (*types.OrganizationMember, error) {
q, args, err := orgmemberSelect.Where(sq.Eq{"orgmember.orgid": orgID, "orgmember.userid": userID}).ToSql()
r.log.Debug().Msgf("q: %s, args: %s", q, util.Dump(args))
2019-05-09 14:47:22 +00:00
if err != nil {
return nil, errors.Wrapf(err, "failed to build query")
2019-05-09 14:47:22 +00:00
}
oms, _, err := fetchOrgMembers(tx, q, args...)
if err != nil {
return nil, errors.WithStack(err)
2019-05-09 14:47:22 +00:00
}
if len(oms) > 1 {
return nil, errors.Errorf("too many rows returned")
}
if len(oms) == 0 {
return nil, nil
}
return oms[0], nil
}
func fetchOrgMembers(tx *db.Tx, q string, args ...interface{}) ([]*types.OrganizationMember, []string, error) {
rows, err := tx.Query(q, args...)
if err != nil {
return nil, nil, errors.WithStack(err)
}
defer rows.Close()
return scanOrgMembers(rows)
}
func scanOrgMember(rows *sql.Rows, additionalFields ...interface{}) (*types.OrganizationMember, string, error) {
var id string
var data []byte
if err := rows.Scan(&id, &data); err != nil {
return nil, "", errors.Wrapf(err, "failed to scan rows")
}
orgmember := types.OrganizationMember{}
if len(data) > 0 {
if err := json.Unmarshal(data, &orgmember); err != nil {
return nil, "", errors.Wrapf(err, "failed to unmarshal org")
}
}
return &orgmember, id, nil
}
func scanOrgMembers(rows *sql.Rows) ([]*types.OrganizationMember, []string, error) {
orgmembers := []*types.OrganizationMember{}
ids := []string{}
for rows.Next() {
orgmember, id, err := scanOrgMember(rows)
if err != nil {
rows.Close()
return nil, nil, errors.WithStack(err)
}
orgmembers = append(orgmembers, orgmember)
ids = append(ids, id)
}
if err := rows.Err(); err != nil {
return nil, nil, errors.WithStack(err)
}
return orgmembers, ids, nil
}
type OrgUser struct {
User *types.User
Role types.MemberRole
}
// TODO(sgotti) implement cursor fetching
func (r *ReadDB) GetOrgUsers(tx *db.Tx, orgID string) ([]*OrgUser, error) {
s := sb.Select("orgmember.data", "user.data").From("orgmember")
s = s.Where(sq.Eq{"orgmember.orgid": orgID})
s = s.Join("user on user.id = orgmember.userid")
s = s.OrderBy("user.name")
q, args, err := s.ToSql()
r.log.Debug().Msgf("q: %s, args: %s", q, util.Dump(args))
if err != nil {
return nil, errors.Wrapf(err, "failed to build query")
}
rows, err := tx.Query(q, args...)
if err != nil {
return nil, errors.WithStack(err)
}
defer rows.Close()
orgusers := []*OrgUser{}
for rows.Next() {
var orgmember *types.OrganizationMember
var user *types.User
var orgmemberdata []byte
var userdata []byte
if err := rows.Scan(&orgmemberdata, &userdata); err != nil {
return nil, errors.Wrapf(err, "failed to scan rows")
}
if err := json.Unmarshal(orgmemberdata, &orgmember); err != nil {
return nil, errors.Wrapf(err, "failed to unmarshal orgmember")
}
if err := json.Unmarshal(userdata, &user); err != nil {
return nil, errors.Wrapf(err, "failed to unmarshal org")
}
orgusers = append(orgusers, &OrgUser{
User: user,
Role: orgmember.MemberRole,
})
}
if err := rows.Err(); err != nil {
return nil, errors.WithStack(err)
}
return orgusers, nil
}
type UserOrg struct {
Organization *types.Organization
Role types.MemberRole
}
// TODO(sgotti) implement cursor fetching
func (r *ReadDB) GetUserOrgs(tx *db.Tx, userID string) ([]*UserOrg, error) {
s := sb.Select("orgmember.data", "org.data").From("orgmember")
s = s.Where(sq.Eq{"orgmember.userid": userID})
s = s.Join("org on org.id = orgmember.orgid")
s = s.OrderBy("org.name")
q, args, err := s.ToSql()
r.log.Debug().Msgf("q: %s, args: %s", q, util.Dump(args))
if err != nil {
return nil, errors.Wrapf(err, "failed to build query")
}
rows, err := tx.Query(q, args...)
if err != nil {
return nil, errors.WithStack(err)
}
defer rows.Close()
userorgs := []*UserOrg{}
for rows.Next() {
var orgmember *types.OrganizationMember
var org *types.Organization
var orgmemberdata []byte
var orgdata []byte
if err := rows.Scan(&orgmemberdata, &orgdata); err != nil {
return nil, errors.Wrapf(err, "failed to scan rows")
}
if err := json.Unmarshal(orgmemberdata, &orgmember); err != nil {
return nil, errors.Wrapf(err, "failed to unmarshal orgmember")
}
if err := json.Unmarshal(orgdata, &org); err != nil {
return nil, errors.Wrapf(err, "failed to unmarshal org")
}
userorgs = append(userorgs, &UserOrg{
Organization: org,
Role: orgmember.MemberRole,
})
}
if err := rows.Err(); err != nil {
return nil, errors.WithStack(err)
}
return userorgs, nil
}