Skip to content

Commit

Permalink
Merge branch 'main' into add-event-query-ids
Browse files Browse the repository at this point in the history
  • Loading branch information
danhurwit authored Feb 12, 2024
2 parents 08ac222 + bb16ff0 commit fb6c662
Show file tree
Hide file tree
Showing 15 changed files with 100 additions and 74 deletions.
2 changes: 1 addition & 1 deletion .go-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.89.1
1.91.0
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ endif
LIGHTSTEP_API_KEY=${LIGHTSTEP_API_KEY_PUBLIC} \
LIGHTSTEP_ORG="terraform-provider" \
LIGHTSTEP_PROJECT="terraform-provider-test" \
LIGHTSTEP_ENV="public" \
LIGHTSTEP_API_BASE_URL="https://api.lightstep.com" \
go test -v ./lightstep

.PHONY: test-local
Expand All @@ -62,7 +62,7 @@ test-local:
LIGHTSTEP_ORG="terraform-provider" \
LIGHTSTEP_PROJECT="terraform-provider-test" \
LIGHTSTEP_API_RATE_LIMIT=100 \
LIGHTSTEP_ENV="public" \
LIGHTSTEP_API_BASE_URL="https://api.lightstep.com" \
go test -v ./lightstep -test.run TestAccSAMLGroupMappings

.PHONY: test-staging
Expand All @@ -72,7 +72,7 @@ test-staging:
LIGHTSTEP_API_KEY=${LIGHTSTEP_STAGING_API_KEY} \
LIGHTSTEP_ORG="terraform-provider" \
LIGHTSTEP_PROJECT="terraform-provider-test" \
LIGHTSTEP_ENV="staging" \
LIGHTSTEP_API_BASE_URL="https://api-staging.lightstep.com" \
go test -v ./lightstep

.PHONY: ensure-clean-repo
Expand Down
8 changes: 1 addition & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,10 @@ It's possible to export an existing Lightstep dashboard to HCL code using the pr

The `exporter` utility is built-in to the provider binary and requires certain environment variables to be set:

For the LIGHTSTEP_ENV variable:

- public = app.lightstep.com
- meta = app-meta.lightstep.com
- staging = app-staging.lightstep.com

```
$ export LIGHTSTEP_API_KEY=....
$ export LIGHTSTEP_ORG=your-org
$ export LIGHTSTEP_ENV=public
$ export LIGHTSTEP_API_BASE_URL='https://api.lightstep.com'
# exports to console dashboard id = rZbPJ33q from project terraform-shop
$ go run github.com/lightstep/terraform-provider-lightstep exporter lightstep_dashboard terraform-shop rZbPJ33q
Expand Down
28 changes: 7 additions & 21 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type genericAPIResponse[T any] struct {

type Client struct {
apiKey string
baseURL string
baseUrl string
orgName string
client *retryablehttp.Client
rateLimiter *rate.Limiter
Expand All @@ -74,26 +74,12 @@ type Client struct {
}

// NewClient gets a client for the public API
func NewClient(apiKey string, orgName string, env string) *Client {
return NewClientWithUserAgent(apiKey, orgName, env, fmt.Sprintf("%s/%s", DefaultUserAgent, version.ProviderVersion))
func NewClient(apiKey string, orgName string, baseUrl string) *Client {
return NewClientWithUserAgent(apiKey, orgName, baseUrl, fmt.Sprintf("%s/%s", DefaultUserAgent, version.ProviderVersion))
}

func NewClientWithUserAgent(apiKey string, orgName string, env string, userAgent string) *Client {
// Let the user override the API base URL.
// e.g. http://localhost:8080
envBaseURL := os.Getenv("LIGHTSTEP_API_BASE_URL")

var baseURL string
if envBaseURL != "" {
// User specified a base URL, let that take priority.
baseURL = envBaseURL
} else if env == "public" {
baseURL = "https://api.lightstep.com"
} else {
baseURL = fmt.Sprintf("https://api-%v.lightstep.com", env)
}

fullBaseURL := fmt.Sprintf("%s/public/v0.2/%v", baseURL, orgName)
func NewClientWithUserAgent(apiKey string, orgName string, baseUrl string, userAgent string) *Client {
fullBaseUrl := fmt.Sprintf("%s/public/v0.2/%v", baseUrl, orgName)

rateLimitStr := os.Getenv("LIGHTSTEP_API_RATE_LIMIT")
rateLimit, err := strconv.Atoi(rateLimitStr)
Expand All @@ -108,7 +94,7 @@ func NewClientWithUserAgent(apiKey string, orgName string, env string, userAgent
return &Client{
apiKey: apiKey,
orgName: orgName,
baseURL: fullBaseURL,
baseUrl: fullBaseUrl,
userAgent: userAgent,
rateLimiter: rate.NewLimiter(rate.Limit(rateLimit), 1),
client: newClient,
Expand All @@ -121,7 +107,7 @@ func (c *Client) CallAPI(ctx context.Context, httpMethod string, suffix string,
return callAPI(
ctx,
c,
fmt.Sprintf("%v/%v", c.baseURL, suffix),
fmt.Sprintf("%v/%v", c.baseUrl, suffix),
httpMethod,
Headers{
"Authorization": fmt.Sprintf("bearer %v", c.apiKey),
Expand Down
21 changes: 4 additions & 17 deletions client/client_test.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
package client

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestNew_public(t *testing.T) {
t.Parallel()

c := NewClient("api-key", "org-name", "public")
assert.Equal(t, "https://api.lightstep.com/public/v0.2/org-name", c.baseURL)
}

func TestNew_other(t *testing.T) {
t.Parallel()
c := NewClient("api-key", "org-name", "other")
assert.Equal(t, "https://api-other.lightstep.com/public/v0.2/org-name", c.baseURL)
}

func TestNew_env_var_provided_baseURL(t *testing.T) {
// Parallel not used here due to t.Setenv.
t.Setenv("LIGHTSTEP_API_BASE_URL", "http://localhost:8080")
c := NewClient("api-key", "org-name", "public")
assert.Equal(t, "http://localhost:8080/public/v0.2/org-name", c.baseURL)
c := NewClient("api-key", "org-name", "https://api.lightstep.com")
assert.Equal(t, "https://api.lightstep.com/public/v0.2/org-name", c.baseUrl)
}
6 changes: 2 additions & 4 deletions client/destinations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ func Test_DeleteDestionation_when_connection_is_closed(t *testing.T) {
}))
defer server.Close()

t.Setenv("LIGHTSTEP_API_BASE_URL", server.URL)
c := NewClient("api", "blars", "staging")
c := NewClient("api", "blars", server.URL)
err := c.DeleteDestination(context.Background(), "tacoman", "hi")

assert.NotNil(t, err)
Expand All @@ -36,8 +35,7 @@ func Test_DeleteDestination_when_connection_has_wrong_content_length(t *testing.
}))
defer server.Close()

t.Setenv("LIGHTSTEP_API_BASE_URL", server.URL)
c := NewClient("api", "blars", "staging")
c := NewClient("api", "blars", server.URL)
err := c.DeleteDestination(context.Background(), "tacoman", "hi")

assert.NotNil(t, err)
Expand Down
3 changes: 1 addition & 2 deletions client/inferred_service_rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ func Test_InferredServiceRule_GetInferredServiceRulesReturnsRule(t *testing.T) {
}))
defer server.Close()

t.Setenv("LIGHTSTEP_API_BASE_URL", server.URL)
apiClient := NewClient("api", "blars", "staging")
apiClient := NewClient("api", "blars", server.URL)
inferredServiceRule, err := apiClient.GetInferredServiceRule(
context.Background(),
"terraform-provider-tests",
Expand Down
6 changes: 2 additions & 4 deletions client/metric_conditions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ func Test_DeleteUnifiedCondition_when_connection_is_closed(t *testing.T) {
}))
defer server.Close()

t.Setenv("LIGHTSTEP_API_BASE_URL", server.URL)
c := NewClient("api", "blars", "staging")
c := NewClient("api", "blars", server.URL)
err := c.DeleteUnifiedCondition(context.Background(), "tacoman", "hi")

assert.NotNil(t, err)
Expand All @@ -36,8 +35,7 @@ func Test_DeleteUnifiedCondition_when_connection_has_wrong_content_length(t *tes
}))
defer server.Close()

t.Setenv("LIGHTSTEP_API_BASE_URL", server.URL)
c := NewClient("api", "blars", "staging")
c := NewClient("api", "blars", server.URL)
err := c.DeleteUnifiedCondition(context.Background(), "tacoman", "hi")

assert.NotNil(t, err)
Expand Down
6 changes: 2 additions & 4 deletions client/metric_dashboards_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ func Test_DeleteMetricDashboard_when_connection_is_closed(t *testing.T) {
}))
defer server.Close()

t.Setenv("LIGHTSTEP_API_BASE_URL", server.URL)
c := NewClient("api", "blars", "staging")
c := NewClient("api", "blars", server.URL)
err := c.DeleteUnifiedDashboard(context.Background(), "tacoman", "hi")
assert.NotNil(t, err)

Expand All @@ -56,8 +55,7 @@ func Test_DeleteMetricDashboard_when_connection_has_wrong_content_length(t *test
}))
defer server.Close()

t.Setenv("LIGHTSTEP_API_BASE_URL", server.URL)
c := NewClient("api", "blars", "staging")
c := NewClient("api", "blars", server.URL)
err := c.DeleteUnifiedDashboard(context.Background(), "tacoman", "hi")

assert.NotNil(t, err)
Expand Down
3 changes: 1 addition & 2 deletions client/saml_group_mappings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ func TestSAMLGroupMappings(t *testing.T) {
}))
defer server.Close()

t.Setenv("LIGHTSTEP_API_BASE_URL", server.URL)
c := NewClient("api", "blars", "staging")
c := NewClient("api", "blars", server.URL)
sgm, err := c.ListSAMLGroupMappings(context.Background())
assert.NoError(t, err)

Expand Down
3 changes: 1 addition & 2 deletions client/user_role_binding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ func Test_UserRoleBinding(t *testing.T) {
}))
defer server.Close()

t.Setenv("LIGHTSTEP_API_BASE_URL", server.URL)
c := NewClient("api", "blars", "staging")
c := NewClient("api", "blars", server.URL)
rb, err := c.ListRoleBinding(context.Background(), "project with spaces", "project editor")
assert.NoError(t, err)

Expand Down
3 changes: 2 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ provider "lightstep" {

- `api_key` (String) The API Key for a Lightstep organization.
- `api_key_env_var` (String) Environment variable for Lightstep API key.
- `environment` (String) The name of the Lightstep environment, must be one of: staging, meta, public.
- `api_url` (String) The base URL for the Lightstep API. This setting takes precedent over 'environment'. For example, https://api.lightstep.com
- `environment` (String, Deprecated) The name of the Lightstep environment, must be one of: staging, meta, public. Deprecated in favor of `api_url`
10 changes: 6 additions & 4 deletions exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,11 @@ func Run(args ...string) error {
}

// default to public API environment
lightstepEnv := "public"
if len(os.Getenv("LIGHTSTEP_ENV")) > 0 {
lightstepEnv = os.Getenv("LIGHTSTEP_ENV")
lightstepUrl := "https://api.lightstep.com"
if len(os.Getenv("LIGHTSTEP_API_URL")) > 0 {
lightstepUrl = os.Getenv("LIGHTSTEP_API_URL")
} else if len(os.Getenv("LIGHTSTEP_API_BASE_URL")) > 0 {
lightstepUrl = os.Getenv("LIGHTSTEP_API_BASE_URL")
}

if len(args) < 4 {
Expand All @@ -182,7 +184,7 @@ func Run(args ...string) error {
log.Fatalf("error: only dashboard resources are supported at this time")
}

c := client.NewClient(os.Getenv("LIGHTSTEP_API_KEY"), os.Getenv("LIGHTSTEP_ORG"), lightstepEnv)
c := client.NewClient(os.Getenv("LIGHTSTEP_API_KEY"), os.Getenv("LIGHTSTEP_ORG"), lightstepUrl)
d, err := c.GetUnifiedDashboard(context.Background(), args[3], args[4])
if err != nil {
log.Fatalf("error: could not get dashboard: %v", err)
Expand Down
43 changes: 43 additions & 0 deletions exporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ package exporter

import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"time"

"github.com/lightstep/terraform-provider-lightstep/client"
"github.com/stretchr/testify/assert"
)

func TestExportToHCL(t *testing.T) {
Expand Down Expand Up @@ -100,3 +106,40 @@ EOT`,
})
}
}

func TestRunExport(t *testing.T) {
done := make(chan struct{})

var server *httptest.Server
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "/public/v0.2/blars-org/projects/blars/metric_dashboards/123abc", r.URL.Path)
body, err := json.Marshal(map[string]client.UnifiedDashboard{
"data": {
ID: "123abc",
Type: "metric_dashboard",
Attributes: client.UnifiedDashboardAttributes{
Name: "blars dashboard",
Charts: []client.UnifiedChart{},
},
},
})
assert.NoError(t, err)
_, err = w.Write(body)
assert.NoError(t, err)
w.WriteHeader(http.StatusOK)
close(done)
}))
defer server.Close()

os.Setenv("LIGHTSTEP_API_KEY", "api-key")
os.Setenv("LIGHTSTEP_ORG", "blars-org")
os.Setenv("LIGHTSTEP_API_URL", server.URL)
err := Run("exporter", "export", "lightstep_dashboard", "blars", "123abc")
assert.NoError(t, err)

select {
case <-time.After(10 * time.Second):
t.Fatal("timeout waiting for HTTP handler to be exercised")
case <-done:
}
}
26 changes: 24 additions & 2 deletions lightstep/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,14 @@ func Provider() *schema.Provider {
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("LIGHTSTEP_ENV", "public"),
ValidateFunc: validation.StringMatch(regexp.MustCompile(`^(staging|meta|public)$`), "Must be one of: staging, meta, public"),
Description: "The name of the Lightstep environment, must be one of: staging, meta, public.",
Description: "The name of the Lightstep environment, must be one of: staging, meta, public. Deprecated in favor of `api_url`",
Deprecated: "This field is deprecated and will be removed in a future release. Please use the `api_url` field instead.",
},
"api_url": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{"LIGHTSTEP_API_URL", "LIGHTSTEP_API_BASE_URL"}, ""),
Description: "The base URL for the Lightstep API. This setting takes precedent over 'environment'. For example, https://api.lightstep.com",
},
"api_key": {
Type: schema.TypeString,
Expand Down Expand Up @@ -94,10 +101,25 @@ func configureProvider(_ context.Context, d *schema.ResourceData) (interface{},
apiKey = apiKeyEnv
}

// TODO remove this code once `environment` is fully deprecated
var baseUrl string
optionalUrl := d.Get("api_url").(string)
if len(optionalUrl) > 0 {
baseUrl = optionalUrl
} else {
// get the url from the `environment` provider attribute
env := d.Get("environment").(string)
if env == "public" {
baseUrl = "https://api.lightstep.com"
} else {
baseUrl = fmt.Sprintf("https://api-%v.lightstep.com", env)
}
}

client := client.NewClientWithUserAgent(
apiKey,
d.Get("organization").(string),
d.Get("environment").(string),
baseUrl,
fmt.Sprintf("%s/%s (terraform %s)", "terraform-provider-lightstep", version.ProviderVersion, meta.SDKVersionString()),
)

Expand Down

0 comments on commit fb6c662

Please sign in to comment.