Skip to content

Commit

Permalink
Storage functions for users table (#5731)
Browse files Browse the repository at this point in the history
* Storage functions added for users table

Signed-off-by: root <swale@msystechnologies.com>

* Updated with review comments

Signed-off-by: root <swale@msystechnologies.com>
  • Loading branch information
sonali523 authored and himanshi-chhabra committed Sep 27, 2021
1 parent a7b38cb commit dd184a3
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
96 changes: 96 additions & 0 deletions components/infra-proxy-service/storage/postgres/users.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package postgres

import (
"context"
"time"

"github.com/chef/automate/components/infra-proxy-service/storage"
)

// InsertUser saves a user to the DB.
func (p *postgres) InsertUser(ctx context.Context, id, serverId, infraServerUsername, credentialId, connector, automateUserId string, isServerAdmin bool) (storage.User, error) {
var user storage.User
nowTime := time.Now()
err := p.db.QueryRowContext(ctx,
`INSERT INTO users (
id, server_id,
infra_server_username,
credential_id,
connector,
automate_user_id,
is_server_admin,
created_at,updated_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $8)
RETURNING id, server_id, infra_server_username, credential_id, connector, automate_user_id, is_server_admin, created_at, updated_at`,
id, serverId, infraServerUsername, credentialId, connector, automateUserId, isServerAdmin, nowTime).
Scan(&user.ID, &user.ServerID, &user.InfraServerUsername, &user.CredentialID, &user.Connector, &user.AutomateUserID, &user.IsServerAdmin, &user.CreatedAt, &user.UpdatedAt)
if err != nil {
return storage.User{}, p.processError(err)
}

return user, nil
}

// GetUser fetches a user by ID.
func (p *postgres) GetUser(ctx context.Context, id string) (storage.User, error) {
return p.getUser(ctx, p.db, id)
}

func (p *postgres) getUser(ctx context.Context, q querier, id string) (storage.User, error) {
var user storage.User
err := q.QueryRowContext(ctx,
`SELECT
u.id, u.server_id,
u.infra_server_username,
u.credential_id,
u.connector,
u.automate_user_id,
u.is_server_admin,
u.created_at, u.updated_at
FROM users u
WHERE u.id = $1`, id).
Scan(&user.ID, &user.ServerID, &user.InfraServerUsername, &user.CredentialID, &user.Connector, &user.AutomateUserID, &user.IsServerAdmin, &user.CreatedAt, &user.UpdatedAt)
if err != nil {
return storage.User{}, p.processError(err)
}
return user, nil
}

// EditUser does a full update on a database server
func (p *postgres) EditUser(ctx context.Context, id, serverId, infraServerUsername, credentialId, connector, automateUserId string, isServerAdmin bool) (storage.User, error) {
var user storage.User
nowTime := time.Now()
err := p.db.QueryRowContext(ctx,
`UPDATE users SET
server_id = $2,
infra_server_username = $3,
credential_id = $4,
connector = $5,
automate_user_id = $6,
is_server_admin= $7,
updated_at= $8
WHERE id = $1
RETURNING id, server_id, infra_server_username, credential_id, connector, automate_user_id, is_server_admin, created_at, updated_at`,
id, serverId, infraServerUsername, credentialId, connector, automateUserId, isServerAdmin, nowTime).
Scan(&user.ID, &user.ServerID, &user.InfraServerUsername, &user.CredentialID, &user.Connector, &user.AutomateUserID, &user.IsServerAdmin, &user.CreatedAt, &user.UpdatedAt)
if err != nil {
return storage.User{}, p.processError(err)
}

return user, nil
}

// DeleteServer deletes a user from the DB.
func (p *postgres) DeleteUser(ctx context.Context, id string) (storage.User, error) {
var user storage.User
err := p.db.QueryRowContext(ctx,
`DELETE FROM users WHERE id = $1
RETURNING id, server_id, infra_server_username, credential_id, connector, automate_user_id, is_server_admin, created_at, updated_at`,
id).
Scan(&user.ID, &user.ServerID, &user.InfraServerUsername, &user.CredentialID, &user.Connector, &user.AutomateUserID, &user.IsServerAdmin, &user.CreatedAt, &user.UpdatedAt)
if err != nil {
return storage.User{}, p.processError(err)
}

return user, nil
}
18 changes: 18 additions & 0 deletions components/infra-proxy-service/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ type Storage interface {
DeleteOrg(ctx context.Context, orgID string, serverID string) (Org, error)
EditOrg(ctx context.Context, id string, name string, adminUser string, serverID string, projects []string) (Org, error)
TouchOrg(ctx context.Context, id string, serverID string) (Org, error)

InsertUser(ctx context.Context, id, serverID, infraServerUsername, credentialID, Connector, automateUserID string, IsServerAdmin bool) (User, error)
GetUser(ctx context.Context, id string) (User, error)
EditUser(ctx context.Context, id, serverID, infraServerUsername, credentialID, Connector, automateUserID string, IsServerAdmin bool) (User, error)
DeleteUser(ctx context.Context, id string) (User, error)
}

// Resetter is, if exposed, used for tests to reset the storage backend to a
Expand Down Expand Up @@ -51,6 +56,19 @@ type Org struct {
UpdatedAt time.Time
}

// User is the struct ingested and returned by our backend implementations.
type User struct {
ID string
ServerID string
InfraServerUsername string
CredentialID string
Connector string
AutomateUserID string
IsServerAdmin bool
CreatedAt time.Time
UpdatedAt time.Time
}

// Errors returned from the backend
var (
// ErrNotFound is returned when a requested server wasn't found
Expand Down

0 comments on commit dd184a3

Please sign in to comment.