Skip to content

Commit fb284b1

Browse files
committed
chore: Adds support for enable_analytics and provider_meta
1 parent 00c9bf7 commit fb284b1

File tree

9 files changed

+86
-23
lines changed

9 files changed

+86
-23
lines changed

internal/config/client.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ type Config struct {
7272
RealmBaseURL string
7373
TerraformVersion string
7474
PreviewV2AdvancedClusterEnabled bool
75+
AnalyticsEnabled bool
7576
}
7677

7778
type AssumeRole struct {
@@ -109,10 +110,8 @@ func (c *Config) NewClient(ctx context.Context) (any, error) {
109110
// Don't change logging.NewTransport to NewSubsystemLoggingHTTPTransport until all resources are in TPF.
110111
tfLoggingTransport := logging.NewTransport("Atlas", digestTransport)
111112
// Add UserAgentExtra fields to the User-Agent header, see wrapper_provider_server.go
112-
userAgentTransport := UserAgentTransport{
113-
Transport: tfLoggingTransport,
114-
}
115-
client := &http.Client{Transport: &userAgentTransport}
113+
userAgentTransport := NewUserAgentTransport(tfLoggingTransport, c.AnalyticsEnabled)
114+
client := &http.Client{Transport: userAgentTransport.Transport}
116115

117116
optsAtlas := []matlasClient.ClientOpt{matlasClient.SetUserAgent(userAgent(c))}
118117
if c.BaseURL != "" {

internal/config/resource_base.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import (
55
"fmt"
66

77
"github.com/hashicorp/terraform-plugin-framework/datasource"
8+
"github.com/hashicorp/terraform-plugin-framework/diag"
89
"github.com/hashicorp/terraform-plugin-framework/resource"
10+
"github.com/hashicorp/terraform-plugin-framework/types"
911
)
1012

1113
const (
@@ -18,11 +20,22 @@ const (
1820
// - Configure
1921
// Client is left empty and populated by the framework when envoking Configure method.
2022
// ResourceName must be defined when creating an instance of a resource.
23+
24+
type ProviderMeta struct {
25+
ScriptLocation types.String `tfsdk:"script_location"`
26+
}
27+
2128
type RSCommon struct {
2229
Client *MongoDBClient
2330
ResourceName string
2431
}
2532

33+
func (r *RSCommon) ReadProviderMetaCreate(ctx context.Context, req *resource.CreateRequest, diags *diag.Diagnostics) ProviderMeta {
34+
var meta ProviderMeta
35+
diags.Append(req.ProviderMeta.Get(ctx, &meta)...)
36+
return meta
37+
}
38+
2639
func (r *RSCommon) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
2740
resp.TypeName = fmt.Sprintf("%s_%s", req.ProviderTypeName, r.ResourceName)
2841
}

internal/config/transport.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,20 @@ func AddUserAgentExtra(ctx context.Context, extra UserAgentExtra) context.Contex
101101
// UserAgentTransport wraps an http.RoundTripper to add User-Agent header with additional metadata.
102102
type UserAgentTransport struct {
103103
Transport http.RoundTripper
104+
Enabled bool
105+
}
106+
107+
func NewUserAgentTransport(transport http.RoundTripper, enabled bool) *UserAgentTransport {
108+
return &UserAgentTransport{
109+
Transport: transport,
110+
Enabled: enabled,
111+
}
104112
}
105113

106114
func (t *UserAgentTransport) RoundTrip(req *http.Request) (*http.Response, error) {
115+
if !t.Enabled {
116+
return t.Transport.RoundTrip(req)
117+
}
107118
extra := ReadUserAgentExtra(req.Context())
108119
if extra != nil {
109120
userAgent := req.Header.Get(UserAgentHeader)

internal/provider/provider.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/hashicorp/terraform-plugin-framework/datasource"
1414
"github.com/hashicorp/terraform-plugin-framework/diag"
1515
"github.com/hashicorp/terraform-plugin-framework/provider"
16+
"github.com/hashicorp/terraform-plugin-framework/provider/metaschema"
1617
"github.com/hashicorp/terraform-plugin-framework/provider/schema"
1718
"github.com/hashicorp/terraform-plugin-framework/providerserver"
1819
"github.com/hashicorp/terraform-plugin-framework/resource"
@@ -74,6 +75,7 @@ type tfMongodbAtlasProviderModel struct {
7475
AwsSecretAccessKeyID types.String `tfsdk:"aws_secret_access_key"`
7576
AwsSessionToken types.String `tfsdk:"aws_session_token"`
7677
IsMongodbGovCloud types.Bool `tfsdk:"is_mongodbgov_cloud"`
78+
EnableAnalytics types.Bool `tfsdk:"enable_analytics"`
7779
}
7880

7981
type tfAssumeRoleModel struct {
@@ -105,6 +107,17 @@ func (p *MongodbtlasProvider) Metadata(ctx context.Context, req provider.Metadat
105107
resp.Version = version.ProviderVersion
106108
}
107109

110+
func (p *MongodbtlasProvider) MetaSchema(ctx context.Context, req provider.MetaSchemaRequest, resp *provider.MetaSchemaResponse) {
111+
resp.Schema = metaschema.Schema{
112+
Attributes: map[string]metaschema.Attribute{
113+
"script_location": metaschema.StringAttribute{
114+
Description: "Example metadata field for analytics/usage.",
115+
Optional: true,
116+
},
117+
},
118+
}
119+
}
120+
108121
func (p *MongodbtlasProvider) Schema(ctx context.Context, req provider.SchemaRequest, resp *provider.SchemaResponse) {
109122
resp.Schema = schema.Schema{
110123
Blocks: map[string]schema.Block{
@@ -156,6 +169,10 @@ func (p *MongodbtlasProvider) Schema(ctx context.Context, req provider.SchemaReq
156169
Optional: true,
157170
Description: "AWS Security Token Service provided session token.",
158171
},
172+
"enable_analytics": schema.BoolAttribute{
173+
Optional: true,
174+
Description: "Allow extra user agent headers such as script_location specified in provider_meta blocks.",
175+
},
159176
},
160177
}
161178
}
@@ -245,6 +262,7 @@ func (p *MongodbtlasProvider) Configure(ctx context.Context, req provider.Config
245262
RealmBaseURL: data.RealmBaseURL.ValueString(),
246263
TerraformVersion: req.TerraformVersion,
247264
PreviewV2AdvancedClusterEnabled: config.PreviewProviderV2AdvancedCluster(),
265+
AnalyticsEnabled: data.EnableAnalytics.ValueBool(),
248266
}
249267

250268
var assumeRoles []tfAssumeRoleModel

internal/provider/provider_sdk2.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,23 @@ func NewSdkV2Provider() *schema.Provider {
126126
Optional: true,
127127
Description: "AWS Security Token Service provided session token.",
128128
},
129+
"enable_analytics": {
130+
Type: schema.TypeBool,
131+
Optional: true,
132+
Description: "Allow extra user agent headers such as script_location specified in provider_meta blocks.",
133+
},
129134
},
130135
DataSourcesMap: getDataSourcesMap(),
131136
ResourcesMap: getResourcesMap(),
132137
}
133138
provider.ConfigureContextFunc = providerConfigure(provider)
139+
provider.ProviderMetaSchema = map[string]*schema.Schema{
140+
"script_location": {
141+
Type: schema.TypeString,
142+
Description: "Example metadata field for analytics/usage.",
143+
Optional: true,
144+
},
145+
}
134146
return provider
135147
}
136148

@@ -287,6 +299,7 @@ func providerConfigure(provider *schema.Provider) func(ctx context.Context, d *s
287299
BaseURL: d.Get("base_url").(string),
288300
RealmBaseURL: d.Get("realm_base_url").(string),
289301
TerraformVersion: provider.TerraformVersion,
302+
AnalyticsEnabled: d.Get("enable_analytics").(bool),
290303
}
291304

292305
assumeRoleValue, ok := d.GetOk("assume_role")

internal/service/organization/resource_organization.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ func resourceCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.
122122
PrivateKey: *organization.ApiKey.PrivateKey,
123123
BaseURL: meta.(*config.MongoDBClient).Config.BaseURL,
124124
TerraformVersion: meta.(*config.MongoDBClient).Config.TerraformVersion,
125+
AnalyticsEnabled: meta.(*config.MongoDBClient).Config.AnalyticsEnabled,
125126
}
126127

127128
clients, _ := cfg.NewClient(ctx)
@@ -163,6 +164,7 @@ func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Di
163164
PrivateKey: d.Get("private_key").(string),
164165
BaseURL: meta.(*config.MongoDBClient).Config.BaseURL,
165166
TerraformVersion: meta.(*config.MongoDBClient).Config.TerraformVersion,
167+
AnalyticsEnabled: meta.(*config.MongoDBClient).Config.AnalyticsEnabled,
166168
}
167169

168170
clients, _ := cfg.NewClient(ctx)
@@ -222,6 +224,7 @@ func resourceUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.
222224
PrivateKey: d.Get("private_key").(string),
223225
BaseURL: meta.(*config.MongoDBClient).Config.BaseURL,
224226
TerraformVersion: meta.(*config.MongoDBClient).Config.TerraformVersion,
227+
AnalyticsEnabled: meta.(*config.MongoDBClient).Config.AnalyticsEnabled,
225228
}
226229

227230
clients, _ := cfg.NewClient(ctx)
@@ -262,6 +265,7 @@ func resourceDelete(ctx context.Context, d *schema.ResourceData, meta any) diag.
262265
PrivateKey: d.Get("private_key").(string),
263266
BaseURL: meta.(*config.MongoDBClient).Config.BaseURL,
264267
TerraformVersion: meta.(*config.MongoDBClient).Config.TerraformVersion,
268+
AnalyticsEnabled: meta.(*config.MongoDBClient).Config.AnalyticsEnabled,
265269
}
266270

267271
clients, _ := cfg.NewClient(ctx)

internal/service/organization/resource_organization_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,10 @@ func getTestClientWithNewOrgCreds(rs *terraform.ResourceState) (*admin.APIClient
353353
}
354354

355355
cfg := config.Config{
356-
PublicKey: rs.Primary.Attributes["public_key"],
357-
PrivateKey: rs.Primary.Attributes["private_key"],
358-
BaseURL: acc.MongoDBClient.Config.BaseURL,
356+
PublicKey: rs.Primary.Attributes["public_key"],
357+
PrivateKey: rs.Primary.Attributes["private_key"],
358+
BaseURL: acc.MongoDBClient.Config.BaseURL,
359+
AnalyticsEnabled: acc.MongoDBClient.Config.AnalyticsEnabled,
359360
}
360361

361362
ctx := context.Background()

internal/service/project/resource_project.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,13 @@ func (r *projectRS) Create(ctx context.Context, req resource.CreateRequest, resp
5959
var limits []TFLimitModel
6060

6161
connV2 := r.Client.AtlasV2
62+
diags := &resp.Diagnostics
63+
meta := r.ReadProviderMetaCreate(ctx, &req, diags)
64+
scriptLocation := meta.ScriptLocation
65+
fmt.Println("found script location: " + scriptLocation.ValueString())
6266

63-
diags := req.Plan.Get(ctx, &projectPlan)
64-
resp.Diagnostics.Append(diags...)
65-
if resp.Diagnostics.HasError() {
67+
diags.Append(req.Plan.Get(ctx, &projectPlan)...)
68+
if diags.HasError() {
6669
return
6770
}
6871
projectGroup := &admin.Group{
@@ -174,17 +177,16 @@ func (r *projectRS) Create(ctx context.Context, req resource.CreateRequest, resp
174177
filteredLimits := FilterUserDefinedLimits(projectProps.Limits, limits)
175178
projectProps.Limits = filteredLimits
176179

177-
projectPlanNew, diags := NewTFProjectResourceModel(ctx, projectRes, *projectProps)
178-
resp.Diagnostics.Append(diags...)
179-
if resp.Diagnostics.HasError() {
180+
projectPlanNew, localDiags := NewTFProjectResourceModel(ctx, projectRes, *projectProps)
181+
diags.Append(localDiags...)
182+
if diags.HasError() {
180183
return
181184
}
182185
updatePlanFromConfig(projectPlanNew, &projectPlan)
183186

184187
// set state to fully populated data
185-
diags = resp.State.Set(ctx, projectPlanNew)
186-
resp.Diagnostics.Append(diags...)
187-
if resp.Diagnostics.HasError() {
188+
diags.Append(resp.State.Set(ctx, projectPlanNew)...)
189+
if diags.HasError() {
188190
return
189191
}
190192
}

internal/testutil/acc/factory.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ func ConnV220241113() *admin20241113.APIClient {
4343

4444
func ConnV2UsingGov() *admin.APIClient {
4545
cfg := config.Config{
46-
PublicKey: os.Getenv("MONGODB_ATLAS_GOV_PUBLIC_KEY"),
47-
PrivateKey: os.Getenv("MONGODB_ATLAS_GOV_PRIVATE_KEY"),
48-
BaseURL: os.Getenv("MONGODB_ATLAS_GOV_BASE_URL"),
46+
PublicKey: os.Getenv("MONGODB_ATLAS_GOV_PUBLIC_KEY"),
47+
PrivateKey: os.Getenv("MONGODB_ATLAS_GOV_PRIVATE_KEY"),
48+
BaseURL: os.Getenv("MONGODB_ATLAS_GOV_BASE_URL"),
49+
AnalyticsEnabled: true,
4950
}
5051
client, _ := cfg.NewClient(context.Background())
5152
return client.(*config.MongoDBClient).AtlasV2
@@ -58,10 +59,11 @@ func init() {
5859
},
5960
}
6061
cfg := config.Config{
61-
PublicKey: os.Getenv("MONGODB_ATLAS_PUBLIC_KEY"),
62-
PrivateKey: os.Getenv("MONGODB_ATLAS_PRIVATE_KEY"),
63-
BaseURL: os.Getenv("MONGODB_ATLAS_BASE_URL"),
64-
RealmBaseURL: os.Getenv("MONGODB_REALM_BASE_URL"),
62+
PublicKey: os.Getenv("MONGODB_ATLAS_PUBLIC_KEY"),
63+
PrivateKey: os.Getenv("MONGODB_ATLAS_PRIVATE_KEY"),
64+
BaseURL: os.Getenv("MONGODB_ATLAS_BASE_URL"),
65+
RealmBaseURL: os.Getenv("MONGODB_REALM_BASE_URL"),
66+
AnalyticsEnabled: true,
6567
}
6668
client, _ := cfg.NewClient(context.Background())
6769
MongoDBClient = client.(*config.MongoDBClient)

0 commit comments

Comments
 (0)