diff --git a/Makefile b/Makefile index 85391e1b..e65d168b 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ HOSTNAME=nobl9.com NAMESPACE=nobl9 NAME=nobl9 BINARY=terraform-provider-${NAME} -VERSION=0.6.3 +VERSION=0.6.4 BUILD_FLAGS="-X github.com/nobl9/terraform-provider-nobl9/nobl9.Version=$(VERSION)" OS_ARCH?=linux_amd64 diff --git a/README.md b/README.md index ea688ce8..cfb9c8ba 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ terraform { required_providers { nobl9 = { source = "nobl9/nobl9" - version = "0.6.3" + version = "0.6.4" } } } diff --git a/docs/index.md b/docs/index.md index 938a426e..3b4586d0 100644 --- a/docs/index.md +++ b/docs/index.md @@ -17,7 +17,7 @@ terraform { required_providers { nobl9 = { source = "nobl9/nobl9" - version = "0.6.3" + version = "0.6.4" } } } diff --git a/examples/provider/provider.tf b/examples/provider/provider.tf index a382fb8b..60d699ce 100644 --- a/examples/provider/provider.tf +++ b/examples/provider/provider.tf @@ -2,7 +2,7 @@ terraform { required_providers { nobl9 = { source = "nobl9/nobl9" - version = "0.6.3" + version = "0.6.4" } } } diff --git a/nobl9/provider.go b/nobl9/provider.go index 6b8e4ab5..581b63e2 100644 --- a/nobl9/provider.go +++ b/nobl9/provider.go @@ -117,14 +117,18 @@ func providerConfigure(_ context.Context, data *schema.ResourceData) (interface{ //nolint:gochecknoglobals var ( - client *nobl9.Client + // The N9 TF Provider supports the creation of project kind. This means that the project can be different (or even + // missing during TF import) between requests to the N9 API. The N9 SDK requires a project to be passed when + // creating a new API client, and reuses this project for each call. To avoid breaking the current SDK interface, + // provider creates the client per project. + clients = make(map[string]*nobl9.Client) clientErr error - once sync.Once + mu sync.Mutex ) func getClient(config ProviderConfig, project string) (*nobl9.Client, diag.Diagnostics) { - once.Do(func() { - client, clientErr = nobl9.NewClient( + var newClient = func() (*nobl9.Client, error) { + return nobl9.NewClient( config.IngestURL, config.Organization, project, @@ -134,7 +138,16 @@ func getClient(config ProviderConfig, project string) (*nobl9.Client, diag.Diagn config.OktaOrgURL, config.OktaAuthServer, ) - }) + } + mu.Lock() + defer mu.Unlock() + + client, clientInitialized := clients[project] + if !clientInitialized { + client, clientErr = newClient() + clients[project] = client + } + if clientErr != nil { return nil, diag.Diagnostics{ diag.Diagnostic{ diff --git a/nobl9/provider_test.go b/nobl9/provider_test.go index 1d7563cd..237285d1 100644 --- a/nobl9/provider_test.go +++ b/nobl9/provider_test.go @@ -75,7 +75,7 @@ func CheckObjectCreated(name string) resource.TestCheckFunc { } } -func CheckDestory(rsType string, objectType n9api.Object) func(s *terraform.State) error { +func CheckDestroy(rsType string, objectType n9api.Object) func(s *terraform.State) error { return func(s *terraform.State) error { config, ok := testProvider.Meta().(ProviderConfig) if !ok { diff --git a/nobl9/resource_agent_test.go b/nobl9/resource_agent_test.go index c50d91c9..fe6ed99b 100644 --- a/nobl9/resource_agent_test.go +++ b/nobl9/resource_agent_test.go @@ -43,7 +43,7 @@ func TestAcc_Nobl9Agent(t *testing.T) { resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, ProviderFactories: ProviderFactory(), - CheckDestroy: CheckDestory("nobl9_agent", n9api.ObjectAgent), + CheckDestroy: CheckDestroy("nobl9_agent", n9api.ObjectAgent), Steps: []resource.TestStep{ { Config: tc.configFunc(tc.name), diff --git a/nobl9/resource_alert_policy_test.go b/nobl9/resource_alert_policy_test.go index 343203b7..ce367f0c 100644 --- a/nobl9/resource_alert_policy_test.go +++ b/nobl9/resource_alert_policy_test.go @@ -6,6 +6,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" + n9api "github.com/nobl9/nobl9-go" ) @@ -53,7 +54,7 @@ func destroyMultiple(rsTypes []string, objectTypes []n9api.Object) resource.Test return fmt.Errorf("resource_types (%v) must match objectTypes (%v)", rsTypes, objectTypes) } for i := 0; i < len(rsTypes); i++ { - CheckDestory(rsTypes[i], objectTypes[i]) + CheckDestroy(rsTypes[i], objectTypes[i]) } return nil } diff --git a/nobl9/resource_alertmethod_test.go b/nobl9/resource_alertmethod_test.go index fb10a1a5..f5db6989 100644 --- a/nobl9/resource_alertmethod_test.go +++ b/nobl9/resource_alertmethod_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + n9api "github.com/nobl9/nobl9-go" ) @@ -31,7 +32,7 @@ func TestAcc_Nobl9AlertMethod(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, ProviderFactories: ProviderFactory(), - CheckDestroy: CheckDestory("nobl9_alert_method_"+tc.resourceSuffix, n9api.ObjectAlertMethod), + CheckDestroy: CheckDestroy("nobl9_alert_method_"+tc.resourceSuffix, n9api.ObjectAlertMethod), Steps: []resource.TestStep{ { Config: tc.configFunc(tc.name), diff --git a/nobl9/resource_project_test.go b/nobl9/resource_project_test.go index d8fae22f..360b42da 100644 --- a/nobl9/resource_project_test.go +++ b/nobl9/resource_project_test.go @@ -15,7 +15,7 @@ func TestAcc_Nobl9Project(t *testing.T) { resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, ProviderFactories: ProviderFactory(), - CheckDestroy: CheckDestory("nobl9_project", n9api.ObjectProject), + CheckDestroy: CheckDestroy("nobl9_project", n9api.ObjectProject), Steps: []resource.TestStep{ { Config: testProjectConfig(name), @@ -29,6 +29,40 @@ func TestAcc_Nobl9Project(t *testing.T) { }) } +func TestAcc_NewNobl9ProjectReference(t *testing.T) { + name := "test-project" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProviderFactories: ProviderFactory(), + CheckDestroy: resource.ComposeTestCheckFunc( + CheckDestroy("nobl9_agent", n9api.ObjectAgent), + CheckDestroy("nobl9_project", n9api.ObjectProject), + ), + Steps: []resource.TestStep{ + { + Config: fmt.Sprintf(` + resource "nobl9_project" "%s" { + display_name = "%s" + name = "%s" + description = "A terraform project" + } + resource "nobl9_agent" "%s" { + name = "%s" + project = nobl9_project.%s.name + source_of = ["Metrics", "Services"] + agent_type = "bigquery" + } + `, name, name, name, name, name, name), + Check: resource.ComposeTestCheckFunc( + CheckObjectCreated("nobl9_project."+name), + CheckObjectCreated("nobl9_agent."+name), + ), + }, + }, + }) +} + func testProjectConfig(name string) string { return fmt.Sprintf(` resource "nobl9_project" "%s" { diff --git a/nobl9/resource_role_binding_test.go b/nobl9/resource_role_binding_test.go index a3214813..7ee22240 100644 --- a/nobl9/resource_role_binding_test.go +++ b/nobl9/resource_role_binding_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + n9api "github.com/nobl9/nobl9-go" ) @@ -25,7 +26,7 @@ func TestAcc_Nobl9RoleBinding(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, ProviderFactories: ProviderFactory(), - CheckDestroy: CheckDestory("nobl9_role_binding", n9api.ObjectRoleBinding), + CheckDestroy: CheckDestroy("nobl9_role_binding", n9api.ObjectRoleBinding), Steps: []resource.TestStep{ { Config: tc.configFunc(tc.name), diff --git a/nobl9/resource_service_test.go b/nobl9/resource_service_test.go index 158b5712..8463f558 100644 --- a/nobl9/resource_service_test.go +++ b/nobl9/resource_service_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + n9api "github.com/nobl9/nobl9-go" ) @@ -13,7 +14,7 @@ func TestAcc_Nobl9Service(t *testing.T) { resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, ProviderFactories: ProviderFactory(), - CheckDestroy: CheckDestory("nobl9_service", n9api.ObjectService), + CheckDestroy: CheckDestroy("nobl9_service", n9api.ObjectService), Steps: []resource.TestStep{ { Config: testService("test-service"), diff --git a/nobl9/resource_slo_test.go b/nobl9/resource_slo_test.go index 29472cdf..d9c3af66 100644 --- a/nobl9/resource_slo_test.go +++ b/nobl9/resource_slo_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + n9api "github.com/nobl9/nobl9-go" ) @@ -54,7 +55,7 @@ func TestAcc_Nobl9SLO(t *testing.T) { resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, ProviderFactories: ProviderFactory(), - CheckDestroy: CheckDestory("nobl9_slo", n9api.ObjectSLO), + CheckDestroy: CheckDestroy("nobl9_slo", n9api.ObjectSLO), Steps: []resource.TestStep{ { Config: tc.configFunc(tc.name),