Skip to content

Commit 04cef66

Browse files
committed
chore: Example analytics support for resource creation and update
1 parent fb284b1 commit 04cef66

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

internal/config/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func (c *Config) NewClient(ctx context.Context) (any, error) {
111111
tfLoggingTransport := logging.NewTransport("Atlas", digestTransport)
112112
// Add UserAgentExtra fields to the User-Agent header, see wrapper_provider_server.go
113113
userAgentTransport := NewUserAgentTransport(tfLoggingTransport, c.AnalyticsEnabled)
114-
client := &http.Client{Transport: userAgentTransport.Transport}
114+
client := &http.Client{Transport: userAgentTransport}
115115

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

internal/config/resource_base.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,30 @@ func (r *RSCommon) ReadProviderMetaCreate(ctx context.Context, req *resource.Cre
3636
return meta
3737
}
3838

39+
func (r *RSCommon) ReadProviderMetaUpdate(ctx context.Context, req *resource.UpdateRequest, diags *diag.Diagnostics) ProviderMeta {
40+
var meta ProviderMeta
41+
diags.Append(req.ProviderMeta.Get(ctx, &meta)...)
42+
return meta
43+
}
44+
45+
func (r *RSCommon) AddAnalyticsCreate(ctx context.Context, req *resource.CreateRequest, diags *diag.Diagnostics) context.Context {
46+
meta := r.ReadProviderMetaCreate(ctx, req, diags)
47+
return AddUserAgentExtra(ctx, UserAgentExtra{
48+
ScriptLocation: meta.ScriptLocation.ValueString(),
49+
Name: r.ResourceName,
50+
Operation: "create",
51+
})
52+
}
53+
54+
func (r *RSCommon) AddAnalyticsUpdate(ctx context.Context, req *resource.UpdateRequest, diags *diag.Diagnostics) context.Context {
55+
meta := r.ReadProviderMetaUpdate(ctx, req, diags)
56+
return AddUserAgentExtra(ctx, UserAgentExtra{
57+
ScriptLocation: meta.ScriptLocation.ValueString(),
58+
Name: r.ResourceName,
59+
Operation: "create",
60+
})
61+
}
62+
3963
func (r *RSCommon) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
4064
resp.TypeName = fmt.Sprintf("%s_%s", req.ProviderTypeName, r.ResourceName)
4165
}

internal/service/project/resource_project.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,7 @@ func (r *projectRS) Create(ctx context.Context, req resource.CreateRequest, resp
6060

6161
connV2 := r.Client.AtlasV2
6262
diags := &resp.Diagnostics
63-
meta := r.ReadProviderMetaCreate(ctx, &req, diags)
64-
scriptLocation := meta.ScriptLocation
65-
fmt.Println("found script location: " + scriptLocation.ValueString())
66-
63+
ctx = r.AddAnalyticsCreate(ctx, &req, diags)
6764
diags.Append(req.Plan.Get(ctx, &projectPlan)...)
6865
if diags.HasError() {
6966
return
@@ -246,6 +243,7 @@ func (r *projectRS) Update(ctx context.Context, req resource.UpdateRequest, resp
246243
var projectState TFProjectRSModel
247244
var projectPlan TFProjectRSModel
248245
connV2 := r.Client.AtlasV2
246+
ctx = r.AddAnalyticsUpdate(ctx, &req, &resp.Diagnostics)
249247

250248
// get current state
251249
resp.Diagnostics.Append(req.State.Get(ctx, &projectState)...)

0 commit comments

Comments
 (0)