Skip to content

Commit

Permalink
feat(http): expose list of available telegraf plugins (#16233)
Browse files Browse the repository at this point in the history
  • Loading branch information
glinton authored Dec 17, 2019
1 parent bba04e2 commit 41e771a
Show file tree
Hide file tree
Showing 6 changed files with 1,798 additions and 1 deletion.
2 changes: 2 additions & 0 deletions http/api_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ func NewAPIHandler(b *APIBackend, opts ...APIHandlerOptFn) *APIHandler {

telegrafBackend := NewTelegrafBackend(b.Logger.With(zap.String("handler", "telegraf")), b)
telegrafBackend.TelegrafService = authorizer.NewTelegrafConfigService(b.TelegrafService, b.UserResourceMappingService)
h.Mount(prefixTelegrafPlugins, NewTelegrafHandler(b.Logger, telegrafBackend))
h.Mount(prefixTelegraf, NewTelegrafHandler(b.Logger, telegrafBackend))

userBackend := NewUserBackend(b.Logger.With(zap.String("handler", "user")), b)
Expand Down Expand Up @@ -241,6 +242,7 @@ var apiLinks = map[string]interface{}{
"tasks": "/api/v2/tasks",
"checks": "/api/v2/checks",
"telegrafs": "/api/v2/telegrafs",
"plugins": "/api/v2/telegraf/plugins",
"users": "/api/v2/users",
"write": "/api/v2/write",
"delete": "/api/v2/delete",
Expand Down
45 changes: 45 additions & 0 deletions http/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,29 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/Error"
/telegraf/plugins:
get:
operationId: GetTelegrafPlugins
parameters:
- $ref: '#/components/parameters/TraceSpan'
- in: query
name: type
description: The type of plugin desired.
schema:
type: string
responses:
'200':
description: A list of Telegraf plugins.
content:
application/json:
schema:
$ref: "#/components/schemas/TelegrafPlugins"
default:
description: Unexpected error
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
/telegrafs:
get:
operationId: GetTelegrafs
Expand Down Expand Up @@ -9635,6 +9658,28 @@ components:
type: array
items:
$ref: "#/components/schemas/Telegraf"
TelegrafPlugin:
type: object
properties:
type:
type: string
name:
type: string
description:
type: string
config:
type: string
TelegrafPlugins:
type: object
properties:
version:
type: string
os:
type: string
plugins:
type: array
items:
$ref: "#/components/schemas/TelegrafPlugin"
TelegrafPluginInputDockerConfig:
type: object
required:
Expand Down
28 changes: 28 additions & 0 deletions http/telegraf.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ const (
telegrafsIDOwnersIDPath = "/api/v2/telegrafs/:id/owners/:userID"
telegrafsIDLabelsPath = "/api/v2/telegrafs/:id/labels"
telegrafsIDLabelsIDPath = "/api/v2/telegrafs/:id/labels/:lid"

prefixTelegrafPlugins = "/api/v2/telegraf"
telegrafPluginsPath = "/api/v2/telegraf/plugins"
)

// NewTelegrafHandler returns a new instance of TelegrafHandler.
Expand All @@ -86,6 +89,8 @@ func NewTelegrafHandler(log *zap.Logger, b *TelegrafBackend) *TelegrafHandler {
h.HandlerFunc("DELETE", telegrafsIDPath, h.handleDeleteTelegraf)
h.HandlerFunc("PUT", telegrafsIDPath, h.handlePutTelegraf)

h.HandlerFunc("GET", telegrafPluginsPath, h.handleGetTelegrafPlugins)

memberBackend := MemberBackend{
HTTPErrorHandler: b.HTTPErrorHandler,
log: b.log.With(zap.String("handler", "member")),
Expand Down Expand Up @@ -189,6 +194,29 @@ type telegrafResponses struct {
TelegrafConfigs []*telegrafResponse `json:"configurations"`
}

func getTelegrafPlugins(t string) (*plugins.TelegrafPlugins, error) {
if len(t) == 0 {
return plugins.AvailablePlugins()
}

return plugins.ListAvailablePlugins(t)
}

func (h *TelegrafHandler) handleGetTelegrafPlugins(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

telPlugins, err := getTelegrafPlugins(r.URL.Query().Get("type"))
if err != nil {
h.HandleHTTPError(ctx, err, w)
return
}

if err := encodeResponse(ctx, w, http.StatusOK, telPlugins); err != nil {
logEncodingError(h.log, r, err)
return
}
}

func newTelegrafResponse(tc *platform.TelegrafConfig, labels []*platform.Label) *telegrafResponse {
res := &telegrafResponse{
TelegrafConfig: tc,
Expand Down
2 changes: 1 addition & 1 deletion storage/reads/datatypes/storage_common.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 41e771a

Please sign in to comment.