英文:
How to configure provider level attributes using the Terraform provider framework
问题
我想实现一个提供者级别的配置功能,这样我就可以在特殊情况下传递一个可选的特定 ARN 给提供者使用。
例如:
provider "custom_provider" {
arn = "arn:aws:service:region-1:123456789012"
}
我创建了提供者的模式(Schema)。
func (p *customProvider) Schema(_ context.Context, _ provider.SchemaRequest, resp *provider.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"arn": schema.StringAttribute{
Optional: true,
},
},
}
}
并实现了提供者的数据模型。
type customProviderModel struct {
Arn types.String `tfsdk:"arn"`
}
实现配置功能。
func (p *customProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
var config customProviderModel
var arn string
diags := req.Config.Get(ctx, &config)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
if !config.Arn.IsNull() {
arn = config.Arn.ValueString()
}
}
但是我不知道如何从配置中将 ARN 传递给数据源,以便我可以在数据源内部执行读取功能时使用 ARN 来扮演角色。
基本上,我希望能够像 AWS 提供者一样,在某些特殊情况下传递区域。
provider "aws" {
region = "us-east-1"
}
英文:
I wanted to implement a provider level configuration functionality so that I can pass an optional specific ARN for the provider to use in special cases.
for eg.
provider "custom_provider" {
arn = "arn:aws:service:region-1:123456789012"
}
I created providers Schema.
func (p *customProvider) Schema(_ context.Context, _ provider.SchemaRequest, resp *provider.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"arn": schema.StringAttribute{
Optional: true,
},
}
}
and implement provider data model
type customProviderModel struct {
Arn types.String `tfsdk:"arn"`
}
Implement configuration functionality
func (p *customProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
var config customProviderModel
var arn string
diags := req.Config.Get(ctx, &config)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
if !config.Arn.IsNull() {
arn = config.Arn.ValueString()
}
but I don't know how to pass the arn to Data Source from the configuration so that I can use the arn to assume a role while I'm doing a Read functionality inside the Data source.
Basically I wanted to have something similar to aws provider where we can pass the region on some special cases.
provider "aws" {
region = "us-east-1"
}
答案1
得分: 0
Configure
方法的 resp *provider.ConfigureResponse
参数是一个 ConfigureResponse
对象,你需要在函数中填充该对象。
DataSourceData
和 ResourceData
字段都接受任意类型的对象,框架将存储这两个值,并将它们原样传递给你的 DataSource
或 Resource
实现的 Configure
方法。
为了接收这些值,你的 DataSource
和 Resource
对象必须分别实现 DataSourceWithConfigure
或 ResourceWithConfigure
。传递给这两个方法的 ConfigureRequest
对象包含了你在 DataSourceData
或 ResourceData
中返回的相同数据对象。
你可以利用这个机制来保存数据源和资源类型实现所需的提供程序配置信息。一个典型的设计是,提供程序级别的 Configure
方法返回一个预配置的客户端,用于与真实远程系统进行交互,然后数据源和资源类型实现将使用该客户端进行请求。
英文:
The resp *provider.ConfigureResponse
argument to your Configure
implementation is a ConfigureResponse
object that you need to populate in your function.
The DataSourceData
and ResourceData
fields both accept an object of any type, and the framework will store those two values and then pass them verbatim to the Configure
methods of your DataSource
or Resource
implementations.
To recieve those values your DataSource
and Resource
objects must implement DataSourceWithConfigure
or ResourceWithConfigure
respectively. The ConfigureRequest
objects passed to each of those includes the same data object you returned in DataSourceData
or ResourceData
.
You can use this mechanism to save whatever information your data source and resource type implementations will need from the provider configuration. A typical design is for the provider-level Configure
to return a pre-configured client for whatever client library you are using to interact with the real remote system, and then the data source and resource type implementations will make their requests using that client.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论