provider.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. // configured is set to true at the end of the Configure method.
  13. // This can be used in Resource and DataSource implementations to verify
  14. // that the provider was previously configured.
  15. configured bool
  16. // version is set to the provider version on release, "dev" when the
  17. // provider is built and ran locally, and "test" when running acceptance
  18. // testing.
  19. version string
  20. // client can contain the upstream provider SDK or HTTP client used to
  21. // communicate with the upstream service. Resource and DataSource
  22. // implementations can then make calls using this client.
  23. //
  24. // TODO: If appropriate, implement upstream provider SDK or HTTP client.
  25. // client vendorsdk.ExampleClient
  26. }
  27. func (p *provider) Configure(ctx context.Context, req tfsdk.ConfigureProviderRequest, resp *tfsdk.ConfigureProviderResponse) {
  28. // If the upstream provider SDK or HTTP client requires configuration, such
  29. // as authentication or logging, this is a great opportunity to do so.
  30. p.configured = true
  31. }
  32. func (p *provider) GetResources(ctx context.Context) (map[string]tfsdk.ResourceType, diag.Diagnostics) {
  33. return map[string]tfsdk.ResourceType{
  34. "scaffolding_example": exampleResourceType{},
  35. }, nil
  36. }
  37. func (p *provider) GetDataSources(ctx context.Context) (map[string]tfsdk.DataSourceType, diag.Diagnostics) {
  38. return map[string]tfsdk.DataSourceType{
  39. "scaffolding_example": exampleDataSourceType{},
  40. }, nil
  41. }
  42. func (p *provider) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) {
  43. return tfsdk.Schema{
  44. Attributes: map[string]tfsdk.Attribute{
  45. "example": {
  46. MarkdownDescription: "Example provider attribute",
  47. Optional: true,
  48. Type: types.StringType,
  49. },
  50. },
  51. }, nil
  52. }
  53. func New(version string) func() tfsdk.Provider {
  54. return func() tfsdk.Provider {
  55. return &provider{
  56. version: version,
  57. }
  58. }
  59. }
  60. // convertProviderType is a helper function for NewResource and NewDataSource
  61. // implementations to associate the concrete provider type. Alternatively,
  62. // this helper can be skipped and the provider type can be directly type
  63. // asserted (e.g. provider: in.(*provider)), however using this can prevent
  64. // potential panics.
  65. func convertProviderType(in tfsdk.Provider) (provider, diag.Diagnostics) {
  66. var diags diag.Diagnostics
  67. p, ok := in.(*provider)
  68. if !ok {
  69. diags.AddError(
  70. "Unexpected Provider Instance Type",
  71. 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),
  72. )
  73. return provider{}, diags
  74. }
  75. if p == nil {
  76. diags.AddError(
  77. "Unexpected Provider Instance Type",
  78. "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.",
  79. )
  80. return provider{}, diags
  81. }
  82. return *p, diags
  83. }