provider.go 3.9 KB

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