From d1b343faed8864876725de924dff921b7c59af28 Mon Sep 17 00:00:00 2001 From: Brian Flad Date: Wed, 1 Dec 2021 06:39:46 -0500 Subject: [PATCH] Add provider configuration fetching to Configure since it will be a very common use case --- internal/provider/provider.go | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 1a58443..a9d43d6 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -12,6 +12,13 @@ import ( // provider satisfies the tfsdk.Provider interface and usually is included // with all Resource and DataSource implementations. type provider struct { + // client can contain the upstream provider SDK or HTTP client used to + // communicate with the upstream service. Resource and DataSource + // implementations can then make calls using this client. + // + // TODO: If appropriate, implement upstream provider SDK or HTTP client. + // client vendorsdk.ExampleClient + // configured is set to true at the end of the Configure method. // This can be used in Resource and DataSource implementations to verify // that the provider was previously configured. @@ -21,16 +28,25 @@ type provider struct { // provider is built and ran locally, and "test" when running acceptance // testing. version string +} - // client can contain the upstream provider SDK or HTTP client used to - // communicate with the upstream service. Resource and DataSource - // implementations can then make calls using this client. - // - // TODO: If appropriate, implement upstream provider SDK or HTTP client. - // client vendorsdk.ExampleClient +// providerData can be used to store data from the Terraform configuration. +type providerData struct { + Example types.String `tfsdk:"example"` } func (p *provider) Configure(ctx context.Context, req tfsdk.ConfigureProviderRequest, resp *tfsdk.ConfigureProviderResponse) { + var data providerData + diags := req.Config.Get(ctx, &data) + resp.Diagnostics.Append(diags...) + + if resp.Diagnostics.HasError() { + return + } + + // Configuration values are now available. + // if data.Example.Null { /* ... */ } + // If the upstream provider SDK or HTTP client requires configuration, such // as authentication or logging, this is a great opportunity to do so.