Skip to content

Commit

Permalink
[Internal] Set SDK used in the useragent in context (#4092)
Browse files Browse the repository at this point in the history
## Changes
<!-- Summary of your changes that are easy to understand -->
There are 2 SDKs -- sdkv2 and pluginframework. We set this information
while setting useragent in the context for a resource and data source

Context:
#4085 (review)

## Tests
<!-- 
How is this tested? Please see the checklist below and also describe any
other relevant tests
-->
Unit tests
- [ ] `make test` run locally
- [ ] relevant change in `docs/` folder
- [ ] covered with integration tests in `internal/acceptance`
- [ ] relevant acceptance tests are passing
- [ ] using Go SDK
  • Loading branch information
tanmay-db authored Oct 10, 2024
1 parent 2ad3f1b commit 2c5dc8a
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 22 deletions.
9 changes: 7 additions & 2 deletions common/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ import (
"strings"

"github.com/databricks/databricks-sdk-go/useragent"
"github.com/databricks/terraform-provider-databricks/internal/providers/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

const sdkName = "sdkv2"

// AddContextToAllResources ...
func AddContextToAllResources(p *schema.Provider, prefix string) {
for k, r := range p.DataSourcesMap {
name := strings.ReplaceAll(k, prefix+"_", "")
wrap := op(r.ReadContext).addContext(ResourceName, name).addContext(IsData, "yes")
wrap := op(r.ReadContext).addContext(ResourceName, name).addContext(IsData, "yes").addContext(Sdk, sdkName)
r.ReadContext = schema.ReadContextFunc(wrap)
}
for k, r := range p.ResourcesMap {
Expand All @@ -33,6 +36,8 @@ func (f op) addContext(k contextKey, v string) op {
ctx = useragent.InContext(ctx, "resource", v)
case IsData:
ctx = useragent.InContext(ctx, "data", v)
case Sdk:
ctx = common.SetSDKInContext(ctx, v)
}
ctx = context.WithValue(ctx, k, v)
return f(ctx, d, m)
Expand All @@ -41,7 +46,7 @@ func (f op) addContext(k contextKey, v string) op {

func addContextToResource(name string, r *schema.Resource) {
addName := func(a op) func(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
return a.addContext(ResourceName, name)
return a.addContext(ResourceName, name).addContext(Sdk, sdkName)
}
if r.CreateContext != nil {
r.CreateContext = addName(op(r.CreateContext))
Expand Down
1 change: 1 addition & 0 deletions common/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
func TestAddContextToAllResources(t *testing.T) {
check := func(ctx context.Context, rd *schema.ResourceData, i any) diag.Diagnostics {
assert.Equal(t, "bar", ResourceName.GetOrUnknown(ctx))
assert.Equal(t, "sdkv2", Sdk.GetOrUnknown(ctx))
return nil
}
p := &schema.Provider{
Expand Down
2 changes: 2 additions & 0 deletions common/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ var (
IsData contextKey = 4
// apiVersion
Api contextKey = 5
// SDK used
Sdk contextKey = 6
)

type contextKey int
Expand Down
12 changes: 11 additions & 1 deletion internal/providers/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
// Note: This is different from internal/providers which contains the changes that *depends* on both:
// internal/providers/sdkv2 and internal/providers/pluginfw packages. Whereas, internal/providers/common package contains
// the changes *used* by both internal/providers/sdkv2 and internal/providers/pluginfw packages.
package internal
package common

import (
"context"

"github.com/databricks/databricks-sdk-go/useragent"
)

const ProviderName = "databricks-tf-provider"

func SetSDKInContext(ctx context.Context, sdkUsed string) context.Context {
return useragent.InContext(ctx, "sdk", sdkUsed)
}
11 changes: 8 additions & 3 deletions internal/providers/pluginfw/context/context.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package common
package context

import (
"context"

"github.com/databricks/databricks-sdk-go/useragent"
"github.com/databricks/terraform-provider-databricks/internal/providers/common"
)

func SetResourceNameInContext(ctx context.Context, resourceName string) context.Context {
const sdkName = "pluginframework"

func SetUserAgentInResourceContext(ctx context.Context, resourceName string) context.Context {
ctx = common.SetSDKInContext(ctx, sdkName)
return useragent.InContext(ctx, "resource", resourceName)
}

func SetDataSourceNameInContext(ctx context.Context, dataSourceName string) context.Context {
func SetUserAgentInDataSourceContext(ctx context.Context, dataSourceName string) context.Context {
ctx = common.SetSDKInContext(ctx, sdkName)
return useragent.InContext(ctx, "data", dataSourceName)
}
16 changes: 9 additions & 7 deletions internal/providers/pluginfw/context/context_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package common
package context

import (
"context"
Expand All @@ -8,20 +8,22 @@ import (
"github.com/stretchr/testify/assert"
)

func TestSetResourceNameInContext(t *testing.T) {
func TestSetUserAgentInResourceContext(t *testing.T) {
ctx := context.Background()
resourceKey := "resource"
resourceName := "test-resource"
actualContext := SetResourceNameInContext(ctx, resourceName)
expectedContext := useragent.InContext(ctx, resourceKey, resourceName)
actualContext := SetUserAgentInResourceContext(ctx, resourceName)
expectedContext := useragent.InContext(ctx, "sdk", "pluginframework")
expectedContext = useragent.InContext(expectedContext, resourceKey, resourceName)
assert.Equal(t, expectedContext, actualContext)
}

func TestSetDataSourceNameInContext(t *testing.T) {
func TestSetUserAgentInDataSourceContext(t *testing.T) {
ctx := context.Background()
dataSourceKey := "data"
dataSourceName := "test-datasource"
actualContext := SetDataSourceNameInContext(ctx, dataSourceName)
expectedContext := useragent.InContext(ctx, dataSourceKey, dataSourceName)
actualContext := SetUserAgentInDataSourceContext(ctx, dataSourceName)
expectedContext := useragent.InContext(ctx, "sdk", "pluginframework")
expectedContext = useragent.InContext(expectedContext, dataSourceKey, dataSourceName)
assert.Equal(t, expectedContext, actualContext)
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func validateClustersList(ctx context.Context, clusters []compute_tf.ClusterDeta
}

func (d *ClusterDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
ctx = pluginfwcontext.SetDataSourceNameInContext(ctx, dataSourceName)
ctx = pluginfwcontext.SetUserAgentInDataSourceContext(ctx, dataSourceName)
w, diags := d.Client.GetWorkspaceClient()
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (r *LibraryResource) Configure(ctx context.Context, req resource.ConfigureR
}

func (r *LibraryResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
ctx = pluginfwcontext.SetResourceNameInContext(ctx, resourceName)
ctx = pluginfwcontext.SetUserAgentInResourceContext(ctx, resourceName)
w, diags := r.Client.GetWorkspaceClient()
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand Down Expand Up @@ -147,7 +147,7 @@ func (r *LibraryResource) Create(ctx context.Context, req resource.CreateRequest
}

func (r *LibraryResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
ctx = pluginfwcontext.SetResourceNameInContext(ctx, resourceName)
ctx = pluginfwcontext.SetUserAgentInResourceContext(ctx, resourceName)
w, diags := r.Client.GetWorkspaceClient()
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand Down Expand Up @@ -185,7 +185,7 @@ func (r *LibraryResource) Update(ctx context.Context, req resource.UpdateRequest
}

func (r *LibraryResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
ctx = pluginfwcontext.SetResourceNameInContext(ctx, resourceName)
ctx = pluginfwcontext.SetUserAgentInResourceContext(ctx, resourceName)
w, diags := r.Client.GetWorkspaceClient()
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (d *QualityMonitorResource) ImportState(ctx context.Context, req resource.I
}

func (r *QualityMonitorResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
ctx = pluginfwcontext.SetResourceNameInContext(ctx, resourceName)
ctx = pluginfwcontext.SetUserAgentInResourceContext(ctx, resourceName)
w, diags := r.Client.GetWorkspaceClient()
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand Down Expand Up @@ -135,7 +135,7 @@ func (r *QualityMonitorResource) Create(ctx context.Context, req resource.Create
}

func (r *QualityMonitorResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
ctx = pluginfwcontext.SetResourceNameInContext(ctx, resourceName)
ctx = pluginfwcontext.SetUserAgentInResourceContext(ctx, resourceName)
w, diags := r.Client.GetWorkspaceClient()
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand Down Expand Up @@ -166,7 +166,7 @@ func (r *QualityMonitorResource) Read(ctx context.Context, req resource.ReadRequ
}

func (r *QualityMonitorResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
ctx = pluginfwcontext.SetResourceNameInContext(ctx, resourceName)
ctx = pluginfwcontext.SetUserAgentInResourceContext(ctx, resourceName)
w, diags := r.Client.GetWorkspaceClient()
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand Down Expand Up @@ -209,7 +209,7 @@ func (r *QualityMonitorResource) Update(ctx context.Context, req resource.Update
}

func (r *QualityMonitorResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
ctx = pluginfwcontext.SetResourceNameInContext(ctx, resourceName)
ctx = pluginfwcontext.SetUserAgentInResourceContext(ctx, resourceName)
w, diags := r.Client.GetWorkspaceClient()
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (d *VolumesDataSource) Configure(_ context.Context, req datasource.Configur
}

func (d *VolumesDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
ctx = pluginfwcontext.SetDataSourceNameInContext(ctx, dataSourceName)
ctx = pluginfwcontext.SetUserAgentInDataSourceContext(ctx, dataSourceName)
w, diags := d.Client.GetWorkspaceClient()
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand Down

0 comments on commit 2c5dc8a

Please sign in to comment.