provider.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package provider
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/hashicorp/terraform-plugin-framework/diag"
  6. "github.com/hashicorp/terraform-plugin-framework/tfsdk"
  7. "github.com/hashicorp/terraform-plugin-framework/types"
  8. )
  9. // provider satisfies the tfsdk.Provider interface and usually is included
  10. // with all Resource and DataSource implementations.
  11. type provider struct {
  12. // client can contain the upstream provider SDK or HTTP client used to
  13. // communicate with the upstream service. Resource and DataSource
  14. // implementations can then make calls using this client.
  15. //
  16. // TODO: If appropriate, implement upstream provider SDK or HTTP client.
  17. // client vendorsdk.ExampleClient
  18. // configured is set to true at the end of the Configure method.
  19. // This can be used in Resource and DataSource implementations to verify
  20. // that the provider was previously configured.
  21. configured bool
  22. // version is set to the provider version on release, "dev" when the
  23. // provider is built and ran locally, and "test" when running acceptance
  24. // testing.
  25. version string
  26. }
  27. // providerData can be used to store data from the Terraform configuration.
  28. type providerData struct {
  29. Example types.String `tfsdk:"example"`
  30. }
  31. func (p *provider) Configure(ctx context.Context, req tfsdk.ConfigureProviderRequest, resp *tfsdk.ConfigureProviderResponse) {
  32. var data providerData
  33. diags := req.Config.Get(ctx, &data)
  34. resp.Diagnostics.Append(diags...)
  35. if resp.Diagnostics.HasError() {
  36. return
  37. }
  38. // Configuration values are now available.
  39. // if data.Example.Null { /* ... */ }
  40. // If the upstream provider SDK or HTTP client requires configuration, such
  41. // as authentication or logging, this is a great opportunity to do so.
  42. p.configured = true
  43. }
  44. func (p *provider) GetResources(ctx context.Context) (map[string]tfsdk.ResourceType, diag.Diagnostics) {
  45. return map[string]tfsdk.ResourceType{
  46. "scaffolding_example": exampleResourceType{},
  47. }, nil
  48. }
  49. func (p *provider) GetDataSources(ctx context.Context) (map[string]tfsdk.DataSourceType, diag.Diagnostics) {
  50. return map[string]tfsdk.DataSourceType{
  51. "scaffolding_example": exampleDataSourceType{},
  52. }, nil
  53. }
  54. func (p *provider) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) {
  55. return tfsdk.Schema{
  56. Attributes: map[string]tfsdk.Attribute{
  57. "example": {
  58. MarkdownDescription: "Example provider attribute",
  59. Optional: true,
  60. Type: types.StringType,
  61. },
  62. },
  63. }, nil
  64. }
  65. func New(version string) func() tfsdk.Provider {
  66. return func() tfsdk.Provider {
  67. return &provider{
  68. version: version,
  69. }
  70. }
  71. }
  72. // convertProviderType is a helper function for NewResource and NewDataSource
  73. // implementations to associate the concrete provider type. Alternatively,
  74. // this helper can be skipped and the provider type can be directly type
  75. // asserted (e.g. provider: in.(*provider)), however using this can prevent
  76. // potential panics.
  77. func convertProviderType(in tfsdk.Provider) (provider, diag.Diagnostics) {
  78. var diags diag.Diagnostics
  79. p, ok := in.(*provider)
  80. if !ok {
  81. diags.AddError(
  82. "Unexpected Provider Instance Type",
  83. fmt.Sprintf("While creating the data source or resource, an unexpected provider type (%T) was received. This is always a bug in the provider code and should be reported to the provider developers.", p),
  84. )
  85. return provider{}, diags
  86. }
  87. if p == nil {
  88. diags.AddError(
  89. "Unexpected Provider Instance Type",
  90. "While creating the data source or resource, an unexpected empty provider instance was received. This is always a bug in the provider code and should be reported to the provider developers.",
  91. )
  92. return provider{}, diags
  93. }
  94. return *p, diags
  95. }