Skip to content

Commit b08ec5a

Browse files
committed
feat(tfdatasource): add ConnectionDetailsFn support
Add an optional ConnectionDetailsFn field to the tfdatasource.Spec that allows observed resources to publish connection details to a Secret via writeConnectionSecretToRef. The function is called in both Observe (when up-to-date) and Update (after a successful read). Extend the generate-observed emitter to automatically generate a ConnectionDetailsFn for every observed resource that has scalar atProvider fields. Each field is exported using its Terraform attribute name as the secret key.
1 parent 5a00f6c commit b08ec5a

2 files changed

Lines changed: 108 additions & 8 deletions

File tree

pkg/generateobserved/emit.go

Lines changed: 88 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/tfdatasource/controller.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ type Spec struct {
5757
// IsUpToDate optionally checks whether the resource status is already
5858
// current. If nil, the resource is always re-read.
5959
IsUpToDate func(resource.Managed) bool
60+
61+
// ConnectionDetailsFn optionally returns connection details to publish
62+
// after a successful data source read. The returned details are written
63+
// to the Secret referenced by writeConnectionSecretToRef. If nil, no
64+
// connection details are published.
65+
ConnectionDetailsFn func(mg resource.Managed) managed.ConnectionDetails
6066
}
6167

6268
// Setup adds a controller that reconciles an observe-only resource.
@@ -135,9 +141,15 @@ func (e *external) Observe(_ context.Context, mg resource.Managed) (managed.Exte
135141
tjresource.SetUpToDateCondition(mg, true)
136142
}
137143

144+
var cd managed.ConnectionDetails
145+
if upToDate && e.spec.ConnectionDetailsFn != nil {
146+
cd = e.spec.ConnectionDetailsFn(mg)
147+
}
148+
138149
return managed.ExternalObservation{
139-
ResourceExists: true,
140-
ResourceUpToDate: upToDate,
150+
ResourceExists: true,
151+
ResourceUpToDate: upToDate,
152+
ConnectionDetails: cd,
141153
}, nil
142154
}
143155

@@ -151,7 +163,12 @@ func (e *external) Update(ctx context.Context, mg resource.Managed) (managed.Ext
151163
}
152164
mg.(interface{ SetConditions(...xpv1.Condition) }).SetConditions(xpv1.Available())
153165
tjresource.SetUpToDateCondition(mg, true)
154-
return managed.ExternalUpdate{}, nil
166+
167+
var cd managed.ConnectionDetails
168+
if e.spec.ConnectionDetailsFn != nil {
169+
cd = e.spec.ConnectionDetailsFn(mg)
170+
}
171+
return managed.ExternalUpdate{ConnectionDetails: cd}, nil
155172
}
156173

157174
func (e *external) Delete(_ context.Context, _ resource.Managed) (managed.ExternalDelete, error) {

0 commit comments

Comments
 (0)