Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
gbdubs committed Dec 15, 2023
1 parent 233c5a5 commit 207b5ad
Show file tree
Hide file tree
Showing 6 changed files with 285 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cmd/runner/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ If you do want to actually run the full `runner` image on Azure, you can use:

```bash
# Run the backend, tell it to create tasks as real Azure Container Apps Jobs.
bazel run //scripts:run_apiserver -- --use_azure_runner
bazel run //scripts:run_server -- --use_azure_runner
```

### Creating a new docker image to run locally
Expand Down
1 change: 1 addition & 0 deletions cmd/server/pactasrv/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ go_library(
"pactasrv.go",
"parallel.go",
"portfolio.go",
"portfolio_group.go",
"upload.go",
"user.go",
],
Expand Down
8 changes: 8 additions & 0 deletions cmd/server/pactasrv/pactasrv.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ type DB interface {

GetOrCreateOwnerForUser(tx db.Tx, uID pacta.UserID) (pacta.OwnerID, error)

PortfolioGroup(tx db.Tx, id pacta.PortfolioGroupID) (*pacta.PortfolioGroup, error)
PortfolioGroupsByOwner(tx db.Tx, owner pacta.OwnerID) ([]*pacta.PortfolioGroup, error)
CreatePortfolioGroup(tx db.Tx, p *pacta.PortfolioGroup) (pacta.PortfolioGroupID, error)
UpdatePortfolioGroup(tx db.Tx, id pacta.PortfolioGroupID, mutations ...db.UpdatePortfolioGroupFn) error
DeletePortfolioGroup(tx db.Tx, id pacta.PortfolioGroupID) error
CreatePortfolioGroupMembership(tx db.Tx, pgID pacta.PortfolioGroupID, pID pacta.PortfolioID) error
DeletePortfolioGroupMembership(tx db.Tx, pgID pacta.PortfolioGroupID, pID pacta.PortfolioID) error

GetOrCreateUserByAuthn(tx db.Tx, mech pacta.AuthnMechanism, authnID, email, canonicalEmail string) (*pacta.User, error)
User(tx db.Tx, id pacta.UserID) (*pacta.User, error)
Users(tx db.Tx, ids []pacta.UserID) (map[pacta.UserID]*pacta.User, error)
Expand Down
103 changes: 103 additions & 0 deletions cmd/server/pactasrv/portfolio_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package pactasrv

import (
"context"

"github.com/RMI/pacta/db"
"github.com/RMI/pacta/oapierr"
api "github.com/RMI/pacta/openapi/pacta"
"github.com/RMI/pacta/pacta"
"go.uber.org/zap"
)

// Returns a portfolio group by ID
// (GET /portfolio-group/{id})
func (s *Server) FindPortfolioGroupById(ctx context.Context, request api.FindPortfolioGroupByIdRequestObject) (api.FindPortfolioGroupByIdResponseObject, error) {
// TODO(#12) Implement Authorization
pg, err := s.DB.PortfolioGroup(s.DB.NoTxn(ctx), pacta.PortfolioGroupID(request.Id))
if err != nil {
if db.IsNotFound(err) {
return nil, oapierr.NotFound("portfolio group not found", zap.String("portfolio_group_id", request.Id))
}
return nil, oapierr.Internal("failed to load portfolio_group", zap.String("portfolio_group_id", request.Id), zap.Error(err))
}
resp, err := conv.PortfolioGroupToOAPI(pg)
if err != nil {
return nil, err
}
return api.FindPortfolioGroupById200JSONResponse(*resp), nil
}

// Returns the portfolio groups that the user has access to
// (GET /portfolio-groups)
func (s *Server) ListPortfolioGroups(ctx context.Context, request api.ListPortfolioGroupsRequestObject) (api.ListPortfolioGroupsResponseObject, error) {
// TODO(#12) Implement Authorization
ownerID, err := s.getUserOwnerID(ctx)
if err != nil {
return nil, err
}
pgs, err := s.DB.PortfolioGroupsByOwner(s.DB.NoTxn(ctx), ownerID)
if err != nil {
return nil, oapierr.Internal("failed to query portfolio groups", zap.Error(err))
}
items, err := dereference(conv.PortfolioGroupsToOAPI(pgs))
if err != nil {
return nil, err
}
return api.ListPortfolioGroups200JSONResponse{Items: items}, nil
}

// Creates a portfolio group
// (POST /portfolio-groups)
func (s *Server) CreatePortfolioGroup(ctx context.Context, request api.CreatePortfolioGroupRequestObject) (api.CreatePortfolioGroupResponseObject, error) {
userID, err := getUserID(ctx)
if err != nil {
return nil, err
}
pg, err := conv.PortfolioGroupCreateFromOAPI(request.Body, userID)
if err != nil {
return nil, err
}
id, err := s.DB.CreatePortfolioGroup(s.DB.NoTxn(ctx), pg)
if err != nil {
return nil, oapierr.Internal("failed to create portfolio group", zap.Error(err))
}
pg.ID = id
pg.CreatedAt = s.Now()
resp, err := conv.PortfolioGroupToOAPI(pg)
if err != nil {
return nil, err
}
return api.CreatePortfolioGroup200Response{Body: resp}, nil
}

// Updates portfolio group properties
// (PATCH /portfolio-group/{id})
func (s *Server) UpdatePortfolioGroup(ctx context.Context, request api.UpdatePortfolioGroupRequestObject) (api.UpdatePortfolioGroupResponseObject, error) {
// TODO(#12) Implement Authorization
id := pacta.PortfolioGroupID(request.Id)
mutations := []db.UpdatePortfolioGroupFn{}
b := request.Body
if b.Name != nil {
mutations = append(mutations, db.SetPortfolioGroupName(*b.Name))
}
if b.Description != nil {
mutations = append(mutations, db.SetPortfolioGroupDescription(*b.Description))
}
err := s.DB.UpdatePortfolioGroup(s.DB.NoTxn(ctx), id, mutations...)
if err != nil {
return nil, oapierr.Internal("failed to update portfolio group", zap.String("portfolio_group_id", string(id)), zap.Error(err))
}
return api.UpdatePortfolioGroup204Response{}, nil
}

// Deletes a portfolio group by ID
// (DELETE /portfolio-group/{id})
func (s *Server) DeletePortfolioGroup(ctx context.Context, request api.DeletePortfolioGroupRequestObject) (api.DeletePortfolioGroupResponseObject, error) {
// TODO(#12) Implement Authorization
err := s.DB.DeletePortfolioGroup(s.DB.NoTxn(ctx), pacta.PortfolioGroupID(request.Id))
if err != nil {
return nil, oapierr.Internal("failed to delete portfolio group", zap.String("portfolio_group_id", request.Id), zap.Error(err))
}
return api.DeletePortfolioGroup204Response{}, nil
}
170 changes: 170 additions & 0 deletions openapi/pacta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,89 @@ paths:
responses:
'204':
description: the relationship changes were applied successfully
/portfolio-groups:
get:
summary: Returns the portfolio groups that the user has access to
operationId: listPortfolioGroups
responses:
'200':
content:
application/json:
schema:
$ref: '#/components/schemas/ListPortfolioGroupsResp'
post:
summary: Creates a portfolio group
description: Creates a new portfolio group
operationId: createPortfolioGroup
requestBody:
description: Initial portfolio group object properties
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/PortfolioGroupCreate'

responses:
'200':
description: portfolio group created successfully
content:
application/json:
schema:
$ref: '#/components/schemas/PortfolioGroup'
/portfolio-group/{id}:
get:
summary: Returns a portfolio group by ID
description: Returns a portfolio group based on a single ID
operationId: findPortfolioGroupById
parameters:
- name: id
in: path
description: ID of portfolio group to fetch
required: true
schema:
type: string
responses:
'200':
description: portfolio group response
content:
application/json:
schema:
$ref: '#/components/schemas/PortfolioGroup'
patch:
summary: Updates portfolio group properties
description: Updates a portfolio group's settable properties
operationId: updatePortfolioGroup
parameters:
- name: id
in: path
description: ID of the portfolio group to update
required: true
schema:
type: string
requestBody:
description: Portfolio Group object properties to update
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/PortfolioGroupChanges'
responses:
'204':
description: the changes were applied successfully
delete:
summary: Deletes a portfolio group by ID
description: deletes a portfolio group based on the ID supplied - note this does not delete the portfolios that are members to this group
operationId: deletePortfolioGroup
parameters:
- name: id
in: path
description: ID of portfolio group to delete
required: true
schema:
type: string
responses:
'204':
description: portfolio group deleted
/incomplete-uploads:
get:
description: Gets the incomplete uploads that the user is the owner of
Expand Down Expand Up @@ -720,6 +803,77 @@ components:
digest:
type: string
description: The hash (typically SHA256) that uniquely identifies this version of the PACTA model.
PortfolioGroupMembershipPortfolio:
type: object
required:
- portfolio
- createdAt
properties:
portfolio:
$ref: '#/components/schemas/Portfolio'
createdAt:
type: string
format: date-time
description: The time at which this membership was created.
PortfolioGroupMembershipPortfolioGroup:
type: object
required:
- portfolioGroup
- createdAt
properties:
portfolioGroup:
$ref: '#/components/schemas/PortfolioGroup'
createdAt:
type: string
format: date-time
description: The time at which this membership was created.
PortfolioGroupCreate:
type: object
required:
- name
- description
properties:
name:
type: string
description: the human meaningful name of the portfolio group
description:
type: string
description: an optional description of the contents or purpose of the portfolio group
PortfolioGroup:
type: object
required:
- id
- name
- description
- createdAt
properties:
id:
type: string
description: the system assigned id of the portfolio group
name:
type: string
description: the human meaningful name of the portfoio group
description:
type: string
description: the description of the contents or purpose of the portfolio group
members:
type: array
description: the list of portfolios that are members of this portfolio group
items:
$ref: '#/components/schemas/PortfolioGroupMembershipPortfolio'
createdAt:
type: string
format: date-time
description: The time at which this initiative was created.
PortfolioGroupChanges:
type: object
properties:
name:
type: string
description: the human meaningful name of the portfolio group
description:
type: string
description: the description of the contents or purpose of the portfolio group
InitiativeCreate:
type: object
required:
Expand Down Expand Up @@ -1128,6 +1282,11 @@ components:
numberOfRows:
type: integer
description: The number of rows in the portfolio
groups:
type: array
description: The list of portfolio groups that this portfolio is a member of
items:
$ref: '#/components/schemas/PortfolioGroupMembershipPortfolioGroup'
PortfolioChanges:
type: object
properties:
Expand Down Expand Up @@ -1162,6 +1321,17 @@ components:
type: array
items:
$ref: '#/components/schemas/Portfolio'
ListPortfolioGroupsReq:
type: object
ListPortfolioGroupsResp:
type: object
required:
- items
properties:
items:
type: array
items:
$ref: '#/components/schemas/PortfolioGroup'
ParsePortfolioReq:
type: object
required:
Expand Down
2 changes: 2 additions & 0 deletions pacta/pacta.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ type Portfolio struct {
Blob *Blob
AdminDebugEnabled bool
NumberOfRows int
MemberOf []*PortfolioGroupMembership
}

func (o *Portfolio) Clone() *Portfolio {
Expand All @@ -372,6 +373,7 @@ func (o *Portfolio) Clone() *Portfolio {
Blob: o.Blob.Clone(),
AdminDebugEnabled: o.AdminDebugEnabled,
NumberOfRows: o.NumberOfRows,
MemberOf: cloneAll(o.MemberOf),
}
}

Expand Down

0 comments on commit 207b5ad

Please sign in to comment.