-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support attach and detach exporters from service
- Loading branch information
Showing
27 changed files
with
845 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "timescale_exporter Data Source - terraform-provider-timescale" | ||
subcategory: "" | ||
description: |- | ||
Exporter data source | ||
--- | ||
|
||
# timescale_exporter (Data Source) | ||
|
||
Exporter data source | ||
|
||
|
||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `name` (String) The name of this exporter. Exporter names must be unique in order to manage them using Terraform. | ||
|
||
### Read-Only | ||
|
||
- `id` (String) exporter id is the unique identifier for an exporter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
terraform { | ||
required_providers { | ||
timescale = { | ||
source = "registry.terraform.io/providers/timescale" | ||
version = "~> 1.0" | ||
} | ||
} | ||
} | ||
|
||
variable "ts_access_key" { | ||
type = string | ||
} | ||
|
||
variable "ts_secret_key" { | ||
type = string | ||
} | ||
|
||
variable "ts_project_id" { | ||
type = string | ||
} | ||
|
||
provider "timescale" { | ||
access_key = var.ts_access_key | ||
secret_key = var.ts_secret_key | ||
project_id = var.ts_project_id | ||
} | ||
|
||
data "timescale_exporter" "exporter" { | ||
name = "exporter_name" | ||
} | ||
|
||
output "products_list" { | ||
value = data.timescale_exporter.exporter.id | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/hashicorp/terraform-plugin-log/tflog" | ||
"github.com/samber/lo" | ||
) | ||
|
||
type Exporter struct { | ||
ID string `json:"id"` | ||
ProjectID string `json:"projectId"` | ||
Created time.Time `json:"created"` | ||
Name string `json:"name"` | ||
Type string `json:"type"` | ||
RegionCode string `json:"regionCode"` | ||
Config json.RawMessage `json:"config"` | ||
} | ||
|
||
type GetAllMetricExportersResponse struct { | ||
Exporters []*Exporter `json:"getAllMetricExporters"` | ||
} | ||
|
||
type GetAllGenericExporterResponse struct { | ||
Exporters []*Exporter `json:"getAllGenericExporters"` | ||
} | ||
|
||
type GetExporterByNameRequest struct { | ||
Name string | ||
} | ||
|
||
type AttachExporterRequest struct { | ||
ServiceID string | ||
ExporterID string | ||
} | ||
|
||
type DetachExporterRequest struct { | ||
ServiceID string | ||
ExporterID string | ||
} | ||
|
||
func (c *Client) getAllMetricExporters(ctx context.Context) ([]*Exporter, error) { | ||
tflog.Trace(ctx, "MetricExporter.GetAll") | ||
req := graphQLRequest{ | ||
operationName: "GetAllMetricExporters", | ||
query: GetAllMetricExporters, | ||
variables: map[string]interface{}{ | ||
"projectId": c.projectID, | ||
}, | ||
} | ||
var resp Response[GetAllMetricExportersResponse] | ||
err := c.do(ctx, req.build(), &resp) | ||
if err = coalesceErrors(resp, err); err != nil { | ||
return nil, err | ||
} | ||
return resp.Data.Exporters, nil | ||
} | ||
|
||
func (c *Client) getAllLogExporters(ctx context.Context) ([]*Exporter, error) { | ||
tflog.Trace(ctx, "MetricExporter.GetAllLogExporters") | ||
req := graphQLRequest{ | ||
operationName: "GetAllGenericExporters", | ||
query: GetAllGenericMetricExporters, | ||
variables: map[string]interface{}{ | ||
"projectId": c.projectID, | ||
}, | ||
} | ||
var resp Response[GetAllGenericExporterResponse] | ||
err := c.do(ctx, req.build(), &resp) | ||
if err = coalesceErrors(resp, err); err != nil { | ||
return nil, err | ||
} | ||
return resp.Data.Exporters, nil | ||
} | ||
|
||
func (c *Client) getAllExporters(ctx context.Context) ([]*Exporter, error) { | ||
tflog.Trace(ctx, "Client.getAllExporters") | ||
metricExporters, err := c.getAllMetricExporters(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
logExporters, err := c.getAllLogExporters(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
tflog.Info(ctx, "found "+strconv.Itoa(len(logExporters))+" log exporters\n\n\n\n\n") | ||
for _, exporter := range logExporters { | ||
tflog.Info(ctx, "LOG EXPORTER: "+exporter.Name) | ||
} | ||
return append(metricExporters, logExporters...), nil | ||
} | ||
|
||
func (c *Client) GetExporterByName(ctx context.Context, request *GetExporterByNameRequest) (*Exporter, error) { | ||
tflog.Trace(ctx, "Client.GetExporterByName") | ||
exporters, err := c.getAllExporters(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
e := lo.Filter(exporters, func(e *Exporter, _ int) bool { | ||
return e.Name == request.Name | ||
}) | ||
if len(e) == 0 { | ||
return nil, errNotFound | ||
} | ||
if len(e) > 1 { | ||
return nil, errors.New("exporter names must be unique for importing") | ||
} | ||
return e[0], nil | ||
} | ||
|
||
func (c *Client) AttachMetricExporter(ctx context.Context, request *AttachExporterRequest) error { | ||
tflog.Trace(ctx, "Client.AttachMetricExporter") | ||
req := &graphQLRequest{ | ||
operationName: "AttachServiceToMetricExporter", | ||
query: AttachMetricExporterMutation, | ||
variables: map[string]interface{}{ | ||
"projectId": c.projectID, | ||
"serviceId": request.ServiceID, | ||
"exporterId": request.ExporterID, | ||
}, | ||
} | ||
var resp Response[any] | ||
err := c.do(ctx, req.build(), &resp) | ||
return coalesceErrors(resp, err) | ||
} | ||
|
||
func (c *Client) AttachLogExporter(ctx context.Context, request *AttachExporterRequest) error { | ||
tflog.Trace(ctx, "Client.AttachLogExporter") | ||
req := &graphQLRequest{ | ||
operationName: "AttachServiceToGenericExporter", | ||
query: AttachGenericExporterMutation, | ||
variables: map[string]interface{}{ | ||
"projectId": c.projectID, | ||
"serviceId": request.ServiceID, | ||
"exporterId": request.ExporterID, | ||
}, | ||
} | ||
var resp Response[any] | ||
err := c.do(ctx, req.build(), &resp) | ||
tflog.Info(ctx, "RESP "+fmt.Sprintf("%+v", resp)) | ||
return coalesceErrors(resp, err) | ||
} | ||
|
||
func (c *Client) DetachLogExporter(ctx context.Context, request *DetachExporterRequest) error { | ||
tflog.Trace(ctx, "Client.DetachLogExporter") | ||
req := &graphQLRequest{ | ||
operationName: "DetachServiceFromGenericExporter", | ||
query: DetachGenericMetricExporterMutation, | ||
variables: map[string]interface{}{ | ||
"projectId": c.projectID, | ||
"serviceId": request.ServiceID, | ||
"exporterId": request.ExporterID, | ||
}, | ||
} | ||
var resp Response[any] | ||
err := c.do(ctx, req.build(), &resp) | ||
return coalesceErrors(resp, err) | ||
} | ||
|
||
func (c *Client) DetachMetricExporter(ctx context.Context, request *DetachExporterRequest) error { | ||
tflog.Trace(ctx, "Client.DetachMetricExporter") | ||
req := &graphQLRequest{ | ||
operationName: "DetachServiceFromMetricExporter", | ||
query: DetachMetricExporterMutation, | ||
variables: map[string]interface{}{ | ||
"projectId": c.projectID, | ||
"serviceId": request.ServiceID, | ||
"exporterId": request.ExporterID, | ||
}, | ||
} | ||
var resp Response[any] | ||
err := c.do(ctx, req.build(), &resp) | ||
return coalesceErrors(resp, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package client | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
) | ||
|
||
type graphQLRequest struct { | ||
operationName string | ||
query string | ||
variables map[string]interface{} | ||
} | ||
|
||
func (g *graphQLRequest) build() map[string]interface{} { | ||
return map[string]interface{}{ | ||
"operationName": g.operationName, | ||
"query": g.query, | ||
"variables": g.variables, | ||
} | ||
} | ||
|
||
type Error struct { | ||
Message string `json:"message"` | ||
Path []string `json:"path"` | ||
} | ||
|
||
func (e *Error) Error() string { | ||
return e.Message + " " + strings.Join(e.Path, ".") | ||
} | ||
|
||
func coalesceErrors[T any](resp Response[T], err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
if len(resp.Errors) > 0 { | ||
errs := make([]error, len(resp.Errors)) | ||
for idx, e := range resp.Errors { | ||
errs[idx] = e | ||
} | ||
return errors.Join(errs...) | ||
} | ||
if resp.Data == nil { | ||
return errNotFound | ||
} | ||
return nil | ||
} |
Oops, something went wrong.