Skip to content

Latest commit

 

History

History
95 lines (66 loc) · 4.16 KB

adding-a-new-endpoint.md

File metadata and controls

95 lines (66 loc) · 4.16 KB

Adding a New Endpoint

This document covers the steps on how to add a new endpoint to the service.

Modify the OpenAPI spec

The OpenAPI spec needs to be modified and generated to make new schemas available to be used by the service.

Follow the Modifying the API definition guide on how to do this.

Add required types for your new endpoint

Note: This step is only required if your new endpoint requires new internal types to be defined.

Types used by the service are located in the pkg/api directory. Please add new types or modify existing types here.

Modify or add new converters/presenters

Note: This step is only required if your new endpoint requires addition or modification to the existing type converters/presenters

Converters are functions responsible for converting the incoming request using an openapi model to an internal type used by the service.

Presenters are functions responsible for converting internal types to the endpoint's response model as defined in the openapi specification.

Converters/presenters are defined in the pkg/api/presenters directory. Please add/modify existing converters and presenters here.

Add a new handler

Handlers are defined in the one of the handlers directory.

Format

All handlers should follow a specific format as defined in this framework. See existing handlers as an example.

Request Validation

Any request validation should be specified in the handler config's Validate field as seen below.

cfg := &handlerConfig{
    Validate: []validate{
        ...
        validateLength(&someVar, "sampleVal", &minRequiredFieldLength, nil),
        ...
    },
    ...
}

Validation functions are available in validation.go. Please add any new validations in this file if required.

Services

Any backend functionality called from your handler should be specified in services or it's subdirectory.

Add your new endpoint to the Route Loader

The route_loader.go contains the definition of the service's endpoints. Add your new endpoint to the router and attach your handler using HandleFunc() here.

For example

...
router := apiV1Router.PathPrefix("/your_new_endpoint").Subrouter()
router.HandleFunc("/{id}", handler.GetResource).Methods(http.MethodGet)
...

If your endpoint requires authentication, please add the following line as the last line of the router definition:

router.Use(authMiddleware.AuthenticateAccountJWT)

Add a new command to the CLI

The CLI will only be used for local development and testing. If a new endpoint was added, a new command should be added to the CLI to reflect that new endpoint.

The CLI is built using Cobra. All of the commands and sub commands are located at:

/cloudprovider - command definition for the /cloudprovider endpoint
/cluster - command definition for the /cluster endpoint
/flags - util functions for flags validation
/dinosaur - command definition for the /dinosaur endpoint

If your endpoint is using a new resource, a new folder should be created here with the following files:

  • cmd.go: The main entry point for your command
  • flags.go: Definition for common flags used across your command.

Any subcommands (i.e. get, list, create) should be added as separate files.