当编写 TF Provider 时,从 Importer 返回什么?

huangapple go评论81阅读模式
英文:

What should I return from Importer when writing TF Provider?

问题

上下文:我正在开发一个 TF 提供程序。

假设我正在实现一个复杂的导入功能:

func fooResource() *schema.Resource {
	return &schema.Resource{
...
		Importer: &schema.ResourceImporter{
			StateContext: fooImport,
		},

...

func fooImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
	...
    return []*schema.ResourceData{d}, nil
}

所以如果导入成功,我应该返回 []*schema.ResourceData{d}, nil。如果导入失败,我应该返回 nil 还是 []*schema.ResourceData{d} 作为第一个参数?显然,第二个参数将是 err

注意:我在 HashiCorp 的这个教程中找到了相关信息,但它没有提到这个问题(他们使用了 ImportStatePassthrough)。

英文:

Context: I'm developing a TF provider.

Let's say I'm implementing a complex import:

func fooResource() *schema.Resource {
	return &schema.Resource{
...
		Importer: &schema.ResourceImporter{
			StateContext: fooImport,
		},

...

func fooImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
	...
    return []*schema.ResourceData{d}, nil
}

So if the import was successful I should return []*schema.ResourceData{d}, nil. What if my import failed, shall I return nil or []*schema.ResourceData{d} as the first argument? Obviously, the second argument is going to be err.

Note: I found this tutorial from HashiCorp but it doesn't say anything about it (they're using ImportStatePassthrough).

答案1

得分: 1

复杂资源导入的代码通常遵循以下结构:

Importer: &schema.ResourceImporter{
  State: func(d *schema.ResourceData, meta interface{}) (result []*schema.ResourceData, err error) {
    ...
    if err {
      return nil, fmt.Errorf("关于ID(%q)的错误消息", d.Id())
    }

    d.Set("<resource_type>", d.Id())

    return []*schema.ResourceData{d}, nil
  },
},

所以对于你的具体情况,代码看起来应该是这样的:

Importer: &schema.ResourceImporter{
  StateContext: fooImport(ctx context.Context, d *schema.ResourceData, meta interface{}) (result []*schema.ResourceData, error) {
    ...
    if err {
      return nil, fmt.Errorf("关于ID(%q)的错误消息", d.Id())
    }

    d.Set("foo_resource", d.Id())

    return []*schema.ResourceData{d}, nil,
  },
}

所以基本上第一个返回类型应该是 nil

顺便提一下:你提到了 ImportStatePassthrough,在你的情况下,与 context 相关的辅助函数可能是 ImportStatePassthroughContext

英文:

The code for a complex resource import generally follows the structure below:

Importer: &amp;schema.ResourceImporter{
  State: func(d *schema.ResourceData, meta interface{}) (result []*schema.ResourceData, err error) {
    ...
    if err {
      return nil, fmt.Errorf(&quot;my error message about the ID (%q)&quot;, d.Id())
    }

    d.Set(&quot;&lt;resource_type&gt;&quot;, d.Id())

	return []*schema.ResourceData{d}, nil
  },
},

so for your specific situation it would appear like:

Importer: &amp;schema.ResourceImporter{
  StateContext: fooImport(ctx context.Context, d *schema.ResourceData, meta interface{}) (result []*schema.ResourceData, error) {
    ...
    if err {
      return nil, fmt.Errorf(&quot;my error message about the ID (%q)&quot;, d.Id())
    }

    d.Set(&quot;foo_resource&quot;, d.Id())

    return []*schema.ResourceData{d}, nil,
  },
}

So basically the first return type would be nil.

Side note: you mention ImportStatePassthrough, and in your situation with context the relevant helper may be ImportStatePassthroughContext instead.

huangapple
  • 本文由 发表于 2022年1月20日 10:57:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/70780320.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定