Skip to content
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

add examples and tests to verify that their schema loads and plans work #16

Merged
merged 2 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 32 additions & 20 deletions chronosphere/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,38 @@ import (
"github.com/stretchr/testify/require"
)

// Sanity test that the provider schema can be loaded by TF.
func TestProviderLoadSchema(t *testing.T) {
p := NewProject(t)
func TestExamples(t *testing.T) {
skips := map[string]string{
"data-bucket": "datasources are not supported without a real tenant",
"data-collection": "datasources are not supported without a real tenant",
"service-datasource": "datasources are not supported without a real tenant",
}

p.Write(t, "main.tf", `
resource "chronosphere_collection" "c" {
name = "C"
exampleRoot := "../examples"
examplesList, err := os.ReadDir(exampleRoot)
require.NoError(t, err)
for _, entry := range examplesList {
if !entry.IsDir() {
continue
}
if reason, ok := skips[entry.Name()]; ok {
t.Logf("skipping %s: %s", entry.Name(), reason)
continue
}
t.Run(entry.Name(), func(t *testing.T) {
p := NewProject(t)
exampleFiles, err := os.ReadDir(filepath.Join(exampleRoot, entry.Name()))
require.NoError(t, err)
for _, fileEntry := range exampleFiles {
path := filepath.Join(exampleRoot, entry.Name(), fileEntry.Name())
contents, err := os.ReadFile(path)
require.NoError(t, err)
p.Write(t, filepath.Base(fileEntry.Name()), string(contents))
}
p.Init(t)
p.Plan(t)
})
}
`)
p.Init(t)
p.Plan(t)
}

// Project represents a Terraform project.
Expand All @@ -52,23 +73,14 @@ func NewProject(t testing.TB) *Project {
dir, err := os.MkdirTemp(t.TempDir(), "tf-work")
require.NoError(t, err)

require.NoError(t, os.WriteFile(filepath.Join(dir, "provider.tf"), []byte(`
require.NoError(t, os.WriteFile(filepath.Join(dir, "provider_config.tf"), []byte(`
`), 0o666))

p := &Project{
dir: dir,
providerCfg: startProvider(t),
}
p.Write(t, "provider.tf", `
terraform {
required_providers {
chronosphere = {
version = "0.0.1-dev"
source = "local/chronosphereio/chronosphere"
}
}
}

p.Write(t, "provider_config.tf", `
provider "chronosphere" {
org = "test"
api_token = "test"
Expand Down
1 change: 1 addition & 0 deletions examples/bucket-inline-policy/email.tf
11 changes: 11 additions & 0 deletions examples/bucket-inline-policy/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
resource "chronosphere_bucket" "b" {
name = "bucket"
notification_policy_data = chronosphere_notification_policy.np.notification_policy_data
}

resource "chronosphere_notification_policy" "np" {
route {
severity = "warn"
notifiers = [chronosphere_email_alert_notifier.email.id]
}
}
1 change: 1 addition & 0 deletions examples/bucket-inline-policy/version.tf
5 changes: 5 additions & 0 deletions examples/bucket/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resource "chronosphere_bucket" "b" {
name = "Bucket"
description = "bucket created by terraform examples"
labels = { "foo" : "bar" }
}
1 change: 1 addition & 0 deletions examples/bucket/version.tf
9 changes: 9 additions & 0 deletions examples/classic-dashboard/collection.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
resource "chronosphere_team" "t" {
name = "team"
}

resource "chronosphere_collection" "c" {
name = "Bucket"
team_id = chronosphere_team.t.id
description = "collection created by terraform examples"
}
22 changes: 22 additions & 0 deletions examples/classic-dashboard/dashboard.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
resource "chronosphere_classic_dashboard" "my_dashboard" {
collection_id = chronosphere_collection.c.id
dashboard_json = jsonencode({
title : "Dashboard",
panels : [{
"gridPos" : {
"h" : 12,
"w" : 24,
"x" : 0,
"y" : 0
},
id : 2,
targets : [
{
expr : "1",
}
],
title : "Panel Title",
type : "graph",
}],
})
}
1 change: 1 addition & 0 deletions examples/classic-dashboard/version.tf
4 changes: 4 additions & 0 deletions examples/collection-no-team/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
resource "chronosphere_collection" "infra" {
name = "Infrastructure Collection"
description = "Collection of resources related to infrastructure services."
}
1 change: 1 addition & 0 deletions examples/collection-no-team/version.tf
1 change: 1 addition & 0 deletions examples/collection-notification-policy/email.tf
17 changes: 17 additions & 0 deletions examples/collection-notification-policy/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
resource "chronosphere_notification_policy" "np" {
team_id = chronosphere_team.t.id
name = "team NP"

route {
severity = "warn"
notifiers = [chronosphere_email_alert_notifier.email.id]
}
}

resource "chronosphere_collection" "infra" {
name = "Infrastructure Collection"
team_id = chronosphere_team.t.id
description = "Collection of resources related to infrastructure services."

notification_policy_id = chronosphere_notification_policy.np.id
}
1 change: 1 addition & 0 deletions examples/collection-notification-policy/team.tf
1 change: 1 addition & 0 deletions examples/collection-notification-policy/version.tf
5 changes: 5 additions & 0 deletions examples/collection/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resource "chronosphere_collection" "infra" {
name = "Infrastructure Collection"
description = "Collection of resources related to infrastructure services."
team_id = chronosphere_team.t.id
}
1 change: 1 addition & 0 deletions examples/collection/teams.tf
1 change: 1 addition & 0 deletions examples/collection/version.tf
5 changes: 5 additions & 0 deletions examples/dashboard/collections.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resource "chronosphere_collection" "c" {
name = "Collection"
description = "collection created by terraform examples."
team_id = chronosphere_team.t.id
}
12 changes: 12 additions & 0 deletions examples/dashboard/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
resource "chronosphere_dashboard" "my_dashboard" {
slug = "slug"
collection_id = chronosphere_collection.c.id
dashboard_json = jsonencode({
kind : "Dashboard",
metadata : {
name : "Chrono Dashboard"
}
spec : {
}
})
}
4 changes: 4 additions & 0 deletions examples/dashboard/teams.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
resource "chronosphere_team" "t" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

optional: could this be a symlink to ../teams/main.tf

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

name = "Team"
description = "Optional Team Description"
}
1 change: 1 addition & 0 deletions examples/dashboard/version.tf
9 changes: 9 additions & 0 deletions examples/data-bucket/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
data "chronosphere_bucket" "default1" {
name = "Default"
}


data "chronosphere_bucket" "default2" {
slug = "default"
}

1 change: 1 addition & 0 deletions examples/data-bucket/version.tf
3 changes: 3 additions & 0 deletions examples/data-collection/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data "chronosphere_collection" "default" {
slug = "default"
}
1 change: 1 addition & 0 deletions examples/data-collection/version.tf
87 changes: 87 additions & 0 deletions examples/datasets/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
resource "chronosphere_dataset" "example_prod_payfe" {
name = "Production paymentfe Traces"
description = "Traces passing through the paymentfe service in production"
configuration {
type = "TRACES"

trace_dataset {
match_criteria {
span {
duration {
max_secs = 99
min_secs = 1
}

error {
value = true
}

match_type = "INCLUDE"

operation {
value = "importantop"
match = "EXACT"
}

parent_operation {
value = "payments/.*"
match = "REGEX"
}

parent_service {
value = "frontdoor-[east|west]"
match = "REGEX"
}

service {
value = "importantsvc"
match = "EXACT"
}

span_count {
max = 2
min = 1
}

tag {
key = "cool_tag"

value {
value = "coolvalue"
match = "EXACT"
}
}

tag {
key = "env_tag"

value {
value = "prod.*"
match = "REGEX"
}
}

tag {
key = "http.status_code"

numeric_value {
comparison = "GREATER_THAN"
value = 299
}
}
}

trace {
duration {
max_secs = 10
min_secs = 5
}

error {
value = true
}
}
}
}
}
}
1 change: 1 addition & 0 deletions examples/datasets/version.tf
68 changes: 68 additions & 0 deletions examples/derived-labels/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
resource "chronosphere_derived_label" "my-constructed-derived-label" {
name = "my-constructed-label"
slug = "my-constructed-label"
description = "this is my derived label"
label_name = "tier"

metric_label {
constructed_label {
value_definitions {
value = "read"
filters {
name = "instance"
value_glob = "reader-*"
}
}
value_definitions {
value = "write"
filters {
name = "instance"
value_glob = "writer-*"
}
}
value_definitions {
value = "admin"
filters {
name = "instance"
value_glob = "admin-*"
}
}
}
}
}

resource "chronosphere_derived_label" "my-mapping-derived-label" {
name = "my-mapping-label"
slug = "my-mapping-label"
description = "this is my derived label"
label_name = "my_derived_label"

metric_label {
mapping_label {
name_mappings {
source_label = "grpc_service"
filters {
name = "__name__"
value_glob = "grpc_*"
}
value_mappings {
target_value = "gateway"
source_value_globs = ["rpcgateway, gateway-service"]
}
}
name_mappings {
source_label = "backend_service"
filters {
name = "__name__"
value_glob = "envoy_*"
}
}
value_mappings {
target_value = "auth"
source_value_globs = ["Auth, auth-service"]
}
}
}

existing_label_policy = "OVERRIDE"
}
1 change: 1 addition & 0 deletions examples/derived-labels/version.tf
Loading
Loading