Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example: New Kind generation #25

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
TRex
---

**TRex** is RH **T**AP's **R**est **Ex**ample
**TRex** is RH **T**AP's **R**est **Ex**ample

![Trexxy](rhtap-trex_sm.png)

Expand All @@ -22,7 +22,7 @@ Some of the features included are:
When looking through the code, anything talking about dinosaurs is business logic, which you
will replace with your business logic. The rest is infrastructure that you will probably want to preserve without change.

It's up to you to port future improvements to this project to your own fork. A goal of this project is to become a
It's up to you to port future improvements to this project to your own fork. A goal of this project is to become a
framework with an upgrade path.


Expand Down
1 change: 1 addition & 0 deletions cmd/trex/environments/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ func (e *Env) LoadServices() {
e.Services.Generic = NewGenericServiceLocator(e)
e.Services.Dinosaurs = NewDinosaurServiceLocator(e)
e.Services.Events = NewEventServiceLocator(e)
e.Services.Subscriptions = NewSubscriptionServiceLocator(e)
}

func (e *Env) LoadClients() error {
Expand Down
10 changes: 10 additions & 0 deletions cmd/trex/environments/service_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,13 @@ func NewEventServiceLocator(env *Env) EventServiceLocator {
return services.NewEventService(dao.NewEventDao(&env.Database.SessionFactory))
}
}

type SubscriptionServiceLocator func() services.SubscriptionService

func NewSubscriptionServiceLocator(env *Env) SubscriptionServiceLocator {
return func() services.SubscriptionService {
return services.NewSubscriptionService(
dao.NewSubscriptionDao(&env.Database.SessionFactory),
)
}
}
7 changes: 4 additions & 3 deletions cmd/trex/environments/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ type Handlers struct {
}

type Services struct {
Dinosaurs DinosaurServiceLocator
Generic GenericServiceLocator
Events EventServiceLocator
Dinosaurs DinosaurServiceLocator
Generic GenericServiceLocator
Events EventServiceLocator
Subscriptions SubscriptionServiceLocator
}

type Clients struct {
Expand Down
13 changes: 12 additions & 1 deletion cmd/trex/server/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func (s *apiServer) routes() *mux.Router {
}

dinosaurHandler := handlers.NewDinosaurHandler(services.Dinosaurs(), services.Generic())
subscriptionHandler := handlers.NewSubscriptionHandler(services.Subscriptions(), services.Generic())
errorsHandler := handlers.NewErrorsHandler()

authMiddleware, err := auth.NewAuthMiddleware()
Expand Down Expand Up @@ -73,7 +74,17 @@ func (s *apiServer) routes() *mux.Router {
apiV1DinosaursRouter.HandleFunc("/{id}", dinosaurHandler.Delete).Methods(http.MethodDelete)
apiV1DinosaursRouter.Use(authMiddleware.AuthenticateAccountJWT)

apiV1DinosaursRouter.Use(authzMiddleware.AuthorizeApi)
// /api/rh-trex/v1/subscriptions
// Add manually
apiV1SubscriptionsRouter := apiV1Router.PathPrefix("/subscriptions").Subrouter()
apiV1SubscriptionsRouter.HandleFunc("", subscriptionHandler.List).Methods(http.MethodGet)
apiV1SubscriptionsRouter.HandleFunc("/{id}", subscriptionHandler.Get).Methods(http.MethodGet)
apiV1SubscriptionsRouter.HandleFunc("", subscriptionHandler.Create).Methods(http.MethodPost)
apiV1SubscriptionsRouter.HandleFunc("/{id}", subscriptionHandler.Patch).Methods(http.MethodPatch)
apiV1SubscriptionsRouter.HandleFunc("/{id}", subscriptionHandler.Delete).Methods(http.MethodDelete)
apiV1SubscriptionsRouter.Use(authMiddleware.AuthenticateAccountJWT)

apiV1SubscriptionsRouter.Use(authzMiddleware.AuthorizeApi)

return mainRouter
}
Expand Down
112 changes: 103 additions & 9 deletions data/generated/openapi/openapi.go

Large diffs are not rendered by default.

Binary file added ocm-example-service
Binary file not shown.
308 changes: 308 additions & 0 deletions openapi/openapi.dinosaurs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,308 @@
paths:
# NEW ENDPOINT START
/api/rh-trex/v1/dinosaurs:
# NEW ENDPOINT END
get:
summary: Returns a list of dinosaurs
security:
- Bearer: []
responses:
'200':
description: A JSON array of dinosaur objects
content:
application/json:
schema:
$ref: '#/components/schemas/DinosaurList'
'401':
description: Auth token is invalid
content:
application/json:
schema:
$ref: 'openapi.yaml#/components/schemas/Error'
'403':
description: Unauthorized to perform operation
content:
application/json:
schema:
$ref: 'openapi.yaml#/components/schemas/Error'
'500':
description: Unexpected error occurred
content:
application/json:
schema:
$ref: 'openapi.yaml#/components/schemas/Error'
parameters:
- $ref: '#/components/parameters/page'
- $ref: '#/components/parameters/size'
- $ref: '#/components/parameters/search'
- $ref: '#/components/parameters/orderBy'
- $ref: '#/components/parameters/fields'
post:
summary: Create a new dinosaur
security:
- Bearer: []
requestBody:
description: Dinosaur data
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/Dinosaur'
responses:
'201':
description: Created
content:
application/json:
schema:
$ref: '#/components/schemas/Dinosaur'
'400':
description: Validation errors occurred
content:
application/json:
schema:
$ref: 'openapi.yaml#/components/schemas/Error'
'401':
description: Auth token is invalid
content:
application/json:
schema:
$ref: 'openapi.yaml#/components/schemas/Error'
'403':
description: Unauthorized to perform operation
content:
application/json:
schema:
$ref: 'openapi.yaml#/components/schemas/Error'
'409':
description: Dinosaur already exists
content:
application/json:
schema:
$ref: 'openapi.yaml#/components/schemas/Error'
'500':
description: An unexpected error occurred creating the dinosaur
content:
application/json:
schema:
$ref: 'openapi.yaml#/components/schemas/Error'
# NEW ENDPOINT START
/api/rh-trex/v1/dinosaurs/{id}:
# NEW ENDPOINT END
get:
summary: Get an dinosaur by id
security:
- Bearer: []
responses:
'200':
description: Dinosaur found by id
content:
application/json:
schema:
$ref: '#/components/schemas/Dinosaur'
'401':
description: Auth token is invalid
content:
application/json:
schema:
$ref: 'openapi.yaml#/components/schemas/Error'
'403':
description: Unauthorized to perform operation
content:
application/json:
schema:
$ref: 'openapi.yaml#/components/schemas/Error'
'404':
description: No dinosaur with specified id exists
content:
application/json:
schema:
$ref: 'openapi.yaml#/components/schemas/Error'
'500':
description: Unexpected error occurred
content:
application/json:
schema:
$ref: 'openapi.yaml#/components/schemas/Error'
patch:
summary: Update an dinosaur
security:
- Bearer: []
requestBody:
description: Updated dinosaur data
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/DinosaurPatchRequest'
responses:
'200':
description: Dinosaur updated successfully
content:
application/json:
schema:
$ref: '#/components/schemas/Dinosaur'
'400':
description: Validation errors occurred
content:
application/json:
schema:
$ref: 'openapi.yaml#/components/schemas/Error'
'401':
description: Auth token is invalid
content:
application/json:
schema:
$ref: 'openapi.yaml#/components/schemas/Error'
'403':
description: Unauthorized to perform operation
content:
application/json:
schema:
$ref: 'openapi.yaml#/components/schemas/Error'
'404':
description: No dinosaur with specified id exists
content:
application/json:
schema:
$ref: 'openapi.yaml#/components/schemas/Error'
'409':
description: Dinosaur already exists
content:
application/json:
schema:
$ref: 'openapi.yaml#/components/schemas/Error'
'500':
description: Unexpected error updating dinosaur
content:
application/json:
schema:
$ref: 'openapi.yaml#/components/schemas/Error'
parameters:
- $ref: '#/components/parameters/id'
components:
schemas:
# NEW SCHEMA START
Dinosaur:
# NEW SCHEMA END
allOf:
- $ref: 'openapi.yaml#/components/schemas/ObjectReference'
- type: object
properties:
species:
type: string
created_at:
type: string
format: date-time
updated_at:
type: string
format: date-time
# NEW SCHEMA START
DinosaurList:
# NEW SCHEMA END
allOf:
- $ref: 'openapi.yaml#/components/schemas/List'
- type: object
properties:
items:
type: array
items:
$ref: '#/components/schemas/Dinosaur'
# NEW SCHEMA START
DinosaurPatchRequest:
# NEW SCHEMA END
type: object
properties:
species:
type: string
parameters:
id:
name: id
in: path
description: The id of record
required: true
schema:
type: string
page:
name: page
in: query
description: Page number of record list when record list exceeds specified page size
schema:
type: integer
default: 1
minimum: 1
required: false
size:
name: size
in: query
description: Maximum number of records to return
schema:
type: integer
default: 100
minimum: 0
required: false
search:
name: search
in: query
required: false
description: |-
Specifies the search criteria. The syntax of this parameter is
similar to the syntax of the _where_ clause of an SQL statement,
using the names of the json attributes / column names of the account.
For example, in order to retrieve all the accounts with a username
starting with `my`:

```sql
username like 'my%'
```

The search criteria can also be applied on related resource.
For example, in order to retrieve all the subscriptions labeled by `foo=bar`,

```sql
subscription_labels.key = 'foo' and subscription_labels.value = 'bar'
```

If the parameter isn't provided, or if the value is empty, then
all the accounts that the user has permission to see will be
returned.
schema:
type: string
orderBy:
name: orderBy
in: query
required: false
description: |-
Specifies the order by criteria. The syntax of this parameter is
similar to the syntax of the _order by_ clause of an SQL statement,
but using the names of the json attributes / column of the account.
For example, in order to retrieve all accounts ordered by username:

```sql
username asc
```

Or in order to retrieve all accounts ordered by username _and_ first name:

```sql
username asc, firstName asc
```

If the parameter isn't provided, or if the value is empty, then
no explicit ordering will be applied.
schema:
type: string
fields:
name: fields
in: query
required: false
description: |-
Supplies a comma-separated list of fields to be returned.
Fields of sub-structures and of arrays use <structure>.<field> notation.
<stucture>.* means all field of a structure
Example: For each Subscription to get id, href, plan(id and kind) and labels (all fields)

```
ocm get subscriptions --parameter fields=id,href,plan.id,plan.kind,labels.* --parameter fetchLabels=true
```
schema:
type: string
Loading