瀏覽代碼

Add provider configuration fetching to Configure since it will be a very common use case

Brian Flad 3 年之前
父節點
當前提交
d1b343faed
共有 1 個文件被更改,包括 22 次插入6 次删除
  1. 22 6
      internal/provider/provider.go

+ 22 - 6
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.