provider.go 3.7 KB

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