Skip to content

Commit

Permalink
Storage functions added for users table
Browse files Browse the repository at this point in the history
Signed-off-by: root <swale@msystechnologies.com>
  • Loading branch information
sonali523 committed Sep 15, 2021
1 parent 4c7c371 commit 10a2332
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
73 changes: 73 additions & 0 deletions components/infra-proxy-service/storage/postgres/users.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package postgres

import (
"context"

"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
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, now(), now())
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).
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 s.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

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, created_at = now(),updated_at= now()
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).
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 10a2332

Please sign in to comment.