Skip to content

Commit

Permalink
reuse n9 client per project (#63)
Browse files Browse the repository at this point in the history
Reusing N9 client for all API calls caused using an invalid project
for some of the requests. Now clients are created per individual
project.

Resolves: PC-6512
  • Loading branch information
mkaras-nobl9 authored Oct 5, 2022
1 parent bc7bf9d commit cd84076
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ terraform {
required_providers {
nobl9 = {
source = "nobl9/nobl9"
version = "0.6.3"
version = "0.6.4"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ terraform {
required_providers {
nobl9 = {
source = "nobl9/nobl9"
version = "0.6.3"
version = "0.6.4"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/provider/provider.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ terraform {
required_providers {
nobl9 = {
source = "nobl9/nobl9"
version = "0.6.3"
version = "0.6.4"
}
}
}
Expand Down
23 changes: 18 additions & 5 deletions nobl9/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion nobl9/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion nobl9/resource_agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
3 changes: 2 additions & 1 deletion nobl9/resource_alert_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion nobl9/resource_alertmethod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"

n9api "github.com/nobl9/nobl9-go"
)

Expand All @@ -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),
Expand Down
36 changes: 35 additions & 1 deletion nobl9/resource_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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" {
Expand Down
3 changes: 2 additions & 1 deletion nobl9/resource_role_binding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"

n9api "github.com/nobl9/nobl9-go"
)

Expand All @@ -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),
Expand Down
3 changes: 2 additions & 1 deletion nobl9/resource_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"

n9api "github.com/nobl9/nobl9-go"
)

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"),
Expand Down
3 changes: 2 additions & 1 deletion nobl9/resource_slo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"

n9api "github.com/nobl9/nobl9-go"
)

Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit cd84076

Please sign in to comment.