-
Notifications
You must be signed in to change notification settings - Fork 2
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
Support attach and detach exporters from service #188
Open
aaronblevy
wants to merge
2
commits into
main
Choose a base branch
from
aaron/attach_detatch_exporter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,172 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"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 | ||
} | ||
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) | ||
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 { | ||
Khyme marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
} |
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,7 @@ | ||
mutation AttachServiceToGenericExporter($projectId: ID!, $serviceId: ID!, $exporterId: String!) { | ||
attachServiceToGenericExporter(data: { | ||
projectId: $projectId, | ||
serviceId: $serviceId, | ||
exporterId: $exporterId} | ||
) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we can have exporters with the same name, this isn't right
There is no issue with having two exporters with the same name since they're managed (ie attached) using the ids
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there was an internal meeting where we decided to go forward with import by name, otherwise we'll have to start showing IDs in the UI. This seems similar to have VPC works? It also fetches VPCs by name and I don't see VPC IDs in the UI
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes but names are unique for vpc by design
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is the solution you're thinking of to show IDs in the UI?