Skip to content
This repository has been archived by the owner on Oct 14, 2024. It is now read-only.

Commit

Permalink
change: owner_id to domain_id
Browse files Browse the repository at this point in the history
Signed-off-by: Arvindh <[email protected]>
  • Loading branch information
arvindh123 committed Jan 8, 2024
1 parent b3e2063 commit 5b26bf2
Show file tree
Hide file tree
Showing 30 changed files with 103 additions and 225 deletions.
5 changes: 0 additions & 5 deletions internal/groups/api/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,6 @@ func decodePageMeta(r *http.Request) (mggroups.PageMeta, error) {
if err != nil {
return mggroups.PageMeta{}, errors.Wrap(apiutil.ErrValidation, err)
}
ownerID, err := apiutil.ReadStringQuery(r, api.OwnerKey, "")
if err != nil {
return mggroups.PageMeta{}, errors.Wrap(apiutil.ErrValidation, err)
}
name, err := apiutil.ReadStringQuery(r, api.NameKey, "")
if err != nil {
return mggroups.PageMeta{}, errors.Wrap(apiutil.ErrValidation, err)
Expand All @@ -284,7 +280,6 @@ func decodePageMeta(r *http.Request) (mggroups.PageMeta, error) {
Offset: offset,
Limit: limit,
Name: name,
OwnerID: ownerID,
Metadata: meta,
Status: st,
}
Expand Down
16 changes: 8 additions & 8 deletions internal/groups/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ func (cge createGroupEvent) Encode() (map[string]interface{}, error) {
"created_at": cge.CreatedAt,
}

if cge.Owner != "" {
val["owner"] = cge.Owner
if cge.Domain != "" {
val["domain"] = cge.Domain
}
if cge.Parent != "" {
val["parent"] = cge.Parent
Expand Down Expand Up @@ -87,8 +87,8 @@ func (uge updateGroupEvent) Encode() (map[string]interface{}, error) {
if uge.ID != "" {
val["id"] = uge.ID
}
if uge.Owner != "" {
val["owner"] = uge.Owner
if uge.Domain != "" {
val["domain"] = uge.Domain
}
if uge.Parent != "" {
val["parent"] = uge.Parent
Expand Down Expand Up @@ -144,8 +144,8 @@ func (vge viewGroupEvent) Encode() (map[string]interface{}, error) {
"id": vge.ID,
}

if vge.Owner != "" {
val["owner"] = vge.Owner
if vge.Domain != "" {
val["domain"] = vge.Domain
}
if vge.Parent != "" {
val["parent"] = vge.Parent
Expand Down Expand Up @@ -207,8 +207,8 @@ func (lge listGroupEvent) Encode() (map[string]interface{}, error) {
if lge.Name != "" {
val["name"] = lge.Name
}
if lge.OwnerID != "" {
val["owner_id"] = lge.OwnerID
if lge.DomainID != "" {
val["domain_id"] = lge.DomainID
}
if lge.Tag != "" {
val["tag"] = lge.Tag
Expand Down
2 changes: 1 addition & 1 deletion internal/groups/mocks/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (m *Repository) Save(ctx context.Context, g mggroups.Group) (mggroups.Group
return mggroups.Group{}, repoerr.ErrCreateEntity
}

if g.Owner == WrongID {
if g.Domain == WrongID {
return mggroups.Group{}, repoerr.ErrCreateEntity
}

Expand Down
40 changes: 20 additions & 20 deletions internal/groups/postgres/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ func New(db postgres.Database) mggroups.Repository {
}

func (repo groupRepository) Save(ctx context.Context, g mggroups.Group) (mggroups.Group, error) {
q := `INSERT INTO groups (name, description, id, owner_id, parent_id, metadata, created_at, status)
VALUES (:name, :description, :id, :owner_id, :parent_id, :metadata, :created_at, :status)
RETURNING id, name, description, owner_id, COALESCE(parent_id, '') AS parent_id, metadata, created_at, status;`
q := `INSERT INTO groups (name, description, id, domain_id, parent_id, metadata, created_at, status)
VALUES (:name, :description, :id, :domain_id, :parent_id, :metadata, :created_at, :status)
RETURNING id, name, description, domain_id, COALESCE(parent_id, '') AS parent_id, metadata, created_at, status;`
dbg, err := toDBGroup(g)
if err != nil {
return mggroups.Group{}, err
Expand Down Expand Up @@ -74,7 +74,7 @@ func (repo groupRepository) Update(ctx context.Context, g mggroups.Group) (mggro
g.Status = mgclients.EnabledStatus
q := fmt.Sprintf(`UPDATE groups SET %s updated_at = :updated_at, updated_by = :updated_by
WHERE id = :id AND status = :status
RETURNING id, name, description, owner_id, COALESCE(parent_id, '') AS parent_id, metadata, created_at, updated_at, updated_by, status`, upq)
RETURNING id, name, description, domain_id, COALESCE(parent_id, '') AS parent_id, metadata, created_at, updated_at, updated_by, status`, upq)

dbu, err := toDBGroup(g)
if err != nil {
Expand All @@ -99,7 +99,7 @@ func (repo groupRepository) Update(ctx context.Context, g mggroups.Group) (mggro

func (repo groupRepository) ChangeStatus(ctx context.Context, group mggroups.Group) (mggroups.Group, error) {
qc := `UPDATE groups SET status = :status, updated_at = :updated_at, updated_by = :updated_by WHERE id = :id
RETURNING id, name, description, owner_id, COALESCE(parent_id, '') AS parent_id, metadata, created_at, updated_at, updated_by, status`
RETURNING id, name, description, domain_id, COALESCE(parent_id, '') AS parent_id, metadata, created_at, updated_at, updated_by, status`

dbg, err := toDBGroup(group)
if err != nil {
Expand All @@ -122,7 +122,7 @@ func (repo groupRepository) ChangeStatus(ctx context.Context, group mggroups.Gro
}

func (repo groupRepository) RetrieveByID(ctx context.Context, id string) (mggroups.Group, error) {
q := `SELECT id, name, owner_id, COALESCE(parent_id, '') AS parent_id, description, metadata, created_at, updated_at, updated_by, status FROM groups
q := `SELECT id, name, domain_id, COALESCE(parent_id, '') AS parent_id, description, metadata, created_at, updated_at, updated_by, status FROM groups
WHERE id = :id`

dbg := dbGroup{
Expand Down Expand Up @@ -153,7 +153,7 @@ func (repo groupRepository) RetrieveAll(ctx context.Context, gm mggroups.Page) (
q = buildHierachy(gm)
}
if gm.ID == "" {
q = `SELECT DISTINCT g.id, g.owner_id, COALESCE(g.parent_id, '') AS parent_id, g.name, g.description,
q = `SELECT DISTINCT g.id, g.domain_id, COALESCE(g.parent_id, '') AS parent_id, g.name, g.description,
g.metadata, g.created_at, g.updated_at, g.updated_by, g.status FROM groups g`
}
q = fmt.Sprintf("%s %s ORDER BY g.created_at LIMIT :limit OFFSET :offset;", q, query)
Expand Down Expand Up @@ -192,7 +192,7 @@ func (repo groupRepository) RetrieveAll(ctx context.Context, gm mggroups.Page) (

func (repo groupRepository) RetrieveByIDs(ctx context.Context, gm mggroups.Page, ids ...string) (mggroups.Page, error) {
var q string
if (len(ids) <= 0) && (gm.PageMeta.OwnerID == "") {
if (len(ids) <= 0) && (gm.PageMeta.DomainID == "") {
return mggroups.Page{PageMeta: mggroups.PageMeta{Offset: gm.Offset, Limit: gm.Limit}}, nil
}
query := buildQuery(gm, ids...)
Expand All @@ -201,7 +201,7 @@ func (repo groupRepository) RetrieveByIDs(ctx context.Context, gm mggroups.Page,
q = buildHierachy(gm)
}
if gm.ID == "" {
q = `SELECT DISTINCT g.id, g.owner_id, COALESCE(g.parent_id, '') AS parent_id, g.name, g.description,
q = `SELECT DISTINCT g.id, g.domain_id, COALESCE(g.parent_id, '') AS parent_id, g.name, g.description,
g.metadata, g.created_at, g.updated_at, g.updated_by, g.status FROM groups g`
}
q = fmt.Sprintf("%s %s ORDER BY g.created_at LIMIT :limit OFFSET :offset;", q, query)
Expand Down Expand Up @@ -310,15 +310,15 @@ func buildHierachy(gm mggroups.Page) string {
switch {
case gm.Direction >= 0: // ancestors
query = `WITH RECURSIVE groups_cte as (
SELECT id, COALESCE(parent_id, '') AS parent_id, owner_id, name, description, metadata, created_at, updated_at, updated_by, status, 0 as level from groups WHERE id = :id
UNION SELECT x.id, COALESCE(x.parent_id, '') AS parent_id, x.owner_id, x.name, x.description, x.metadata, x.created_at, x.updated_at, x.updated_by, x.status, level - 1 from groups x
SELECT id, COALESCE(parent_id, '') AS parent_id, domain_id, name, description, metadata, created_at, updated_at, updated_by, status, 0 as level from groups WHERE id = :id
UNION SELECT x.id, COALESCE(x.parent_id, '') AS parent_id, x.domain_id, x.name, x.description, x.metadata, x.created_at, x.updated_at, x.updated_by, x.status, level - 1 from groups x
INNER JOIN groups_cte a ON a.parent_id = x.id
) SELECT * FROM groups_cte g`

case gm.Direction < 0: // descendants
query = `WITH RECURSIVE groups_cte as (
SELECT id, COALESCE(parent_id, '') AS parent_id, owner_id, name, description, metadata, created_at, updated_at, updated_by, status, 0 as level, CONCAT('', '', id) as path from groups WHERE id = :id
UNION SELECT x.id, COALESCE(x.parent_id, '') AS parent_id, x.owner_id, x.name, x.description, x.metadata, x.created_at, x.updated_at, x.updated_by, x.status, level + 1, CONCAT(path, '.', x.id) as path from groups x
SELECT id, COALESCE(parent_id, '') AS parent_id, domain_id, name, description, metadata, created_at, updated_at, updated_by, status, 0 as level, CONCAT('', '', id) as path from groups WHERE id = :id
UNION SELECT x.id, COALESCE(x.parent_id, '') AS parent_id, x.domain_id, x.name, x.description, x.metadata, x.created_at, x.updated_at, x.updated_by, x.status, level + 1, CONCAT(path, '.', x.id) as path from groups x
INNER JOIN groups_cte d ON d.id = x.parent_id
) SELECT * FROM groups_cte g`
}
Expand All @@ -337,8 +337,8 @@ func buildQuery(gm mggroups.Page, ids ...string) string {
if gm.Status != mgclients.AllStatus {
queries = append(queries, "g.status = :status")
}
if gm.OwnerID != "" {
queries = append(queries, "g.owner_id = :owner_id")
if gm.DomainID != "" {
queries = append(queries, "g.domain_id = :domain_id")
}
if len(gm.Metadata) > 0 {
queries = append(queries, "g.metadata @> :metadata")
Expand All @@ -353,7 +353,7 @@ func buildQuery(gm mggroups.Page, ids ...string) string {
type dbGroup struct {
ID string `db:"id"`
ParentID *string `db:"parent_id,omitempty"`
OwnerID string `db:"owner_id,omitempty"`
DomainID string `db:"domain_id,omitempty"`
Name string `db:"name"`
Description string `db:"description,omitempty"`
Level int `db:"level"`
Expand Down Expand Up @@ -390,7 +390,7 @@ func toDBGroup(g mggroups.Group) (dbGroup, error) {
ID: g.ID,
Name: g.Name,
ParentID: parentID,
OwnerID: g.Owner,
DomainID: g.Domain,
Description: g.Description,
Metadata: data,
Path: g.Path,
Expand Down Expand Up @@ -425,7 +425,7 @@ func toGroup(g dbGroup) (mggroups.Group, error) {
ID: g.ID,
Name: g.Name,
Parent: parentID,
Owner: g.OwnerID,
Domain: g.DomainID,
Description: g.Description,
Metadata: metadata,
Level: g.Level,
Expand Down Expand Up @@ -460,7 +460,7 @@ func toDBGroupPage(pm mggroups.Page) (dbGroupPage, error) {
Offset: pm.Offset,
Limit: pm.Limit,
ParentID: pm.ID,
OwnerID: pm.OwnerID,
DomainID: pm.DomainID,
Status: pm.Status,
}, nil
}
Expand All @@ -470,7 +470,7 @@ type dbGroupPage struct {
ID string `db:"id"`
Name string `db:"name"`
ParentID string `db:"parent_id"`
OwnerID string `db:"owner_id"`
DomainID string `db:"domain_id"`
Metadata []byte `db:"metadata"`
Path string `db:"path"`
Level uint64 `db:"level"`
Expand Down
4 changes: 2 additions & 2 deletions internal/groups/postgres/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ func Migration() *migrate.MemoryMigrationSource {
`CREATE TABLE IF NOT EXISTS groups (
id VARCHAR(36) PRIMARY KEY,
parent_id VARCHAR(36),
owner_id VARCHAR(36) NOT NULL,
domain_id VARCHAR(36) NOT NULL,
name VARCHAR(1024) NOT NULL,
description VARCHAR(1024),
metadata JSONB,
created_at TIMESTAMP,
updated_at TIMESTAMP,
updated_by VARCHAR(254),
status SMALLINT NOT NULL DEFAULT 0 CHECK (status >= 0),
UNIQUE (owner_id, name),
UNIQUE (domain_id, name),
FOREIGN KEY (parent_id) REFERENCES groups (id) ON DELETE SET NULL
)`,
},
Expand Down
4 changes: 2 additions & 2 deletions internal/groups/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (svc service) CreateGroup(ctx context.Context, token, kind string, g groups

g.ID = groupID
g.CreatedAt = time.Now()
g.Owner = res.GetDomainId()
g.Domain = res.GetDomainId()
if g.Parent != "" {
_, err := svc.authorizeToken(ctx, auth.UserType, token, auth.EditPermission, auth.GroupType, g.Parent)
if err != nil {
Expand Down Expand Up @@ -221,7 +221,7 @@ func (svc service) ListGroups(ctx context.Context, token, memberKind, memberID s
if res.GetDomainId() == "" {
return groups.Page{}, errors.ErrMalformedEntity
}
gm.PageMeta.OwnerID = res.GetDomainId()
gm.PageMeta.DomainID = res.GetDomainId()
default:
// If domain is disabled , then this authorization will fail for all non-admin domain users
if _, err := svc.authorizeKind(ctx, "", auth.UserType, auth.UsersKind, res.GetId(), auth.MembershipPermission, auth.DomainType, res.GetDomainId()); err != nil {
Expand Down
5 changes: 1 addition & 4 deletions pkg/clients/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type Client struct {
ID string `json:"id"`
Name string `json:"name,omitempty"`
Tags []string `json:"tags,omitempty"`
Owner string `json:"owner,omitempty"` // nullable
Domain string `json:"domain,omitempty"`
Credentials Credentials `json:"credentials,omitempty"`
Metadata Metadata `json:"metadata,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
Expand Down Expand Up @@ -96,9 +96,6 @@ type Repository interface {
// UpdateSecret updates secret for client with given identity.
UpdateSecret(ctx context.Context, client Client) (Client, error)

// UpdateOwner updates owner for client with given id.
UpdateOwner(ctx context.Context, client Client) (Client, error)

// UpdateRole updates role for client with given id.
UpdateRole(ctx context.Context, client Client) (Client, error)

Expand Down
2 changes: 1 addition & 1 deletion pkg/clients/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type Page struct {
Order string `json:"order,omitempty"`
Dir string `json:"dir,omitempty"`
Metadata Metadata `json:"metadata,omitempty"`
Owner string `json:"owner,omitempty"`
Domain string `json:"domain,omitempty"`
Tag string `json:"tag,omitempty"`
Permission string `json:"permission,omitempty"`
Status Status `json:"status,omitempty"`
Expand Down
Loading

0 comments on commit 5b26bf2

Please sign in to comment.