coffees_data_source.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package provider
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/hashicorp-demoapp/hashicups-client-go"
  6. "github.com/hashicorp/terraform-plugin-framework/datasource"
  7. "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
  8. "github.com/hashicorp/terraform-plugin-framework/types"
  9. )
  10. // Ensure the implementation satisfies the expected interfaces.
  11. var (
  12. _ datasource.DataSource = &coffeesDataSource{}
  13. _ datasource.DataSourceWithConfigure = &coffeesDataSource{}
  14. )
  15. func NewCoffeesDataSource() datasource.DataSource {
  16. return &coffeesDataSource{}
  17. }
  18. type coffeesDataSource struct {
  19. client *hashicups.Client
  20. }
  21. func (d *coffeesDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
  22. // Add a nil check when handling ProviderData because Terraform
  23. // sets that data after it calls the ConfigureProvider RPC.
  24. if req.ProviderData == nil {
  25. return
  26. }
  27. client, ok := req.ProviderData.(*hashicups.Client)
  28. if !ok {
  29. resp.Diagnostics.AddError("Unexpected Data Source Configure Type",
  30. fmt.Sprintf("Expected *hashicups.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData))
  31. return
  32. }
  33. d.client = client
  34. }
  35. func (d *coffeesDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
  36. resp.TypeName = req.ProviderTypeName + "_coffees"
  37. }
  38. func (d *coffeesDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
  39. resp.Schema = schema.Schema{
  40. Attributes: map[string]schema.Attribute{
  41. "coffees": schema.ListNestedAttribute{
  42. Computed: true,
  43. NestedObject: schema.NestedAttributeObject{
  44. Attributes: map[string]schema.Attribute{
  45. "id": schema.Int64Attribute{
  46. Computed: true,
  47. },
  48. "name": schema.StringAttribute{
  49. Computed: true,
  50. },
  51. "teaser": schema.StringAttribute{
  52. Computed: true,
  53. },
  54. "description": schema.StringAttribute{
  55. Computed: true,
  56. },
  57. "price": schema.Float64Attribute{
  58. Computed: true,
  59. },
  60. "image": schema.StringAttribute{
  61. Computed: true,
  62. },
  63. "ingredients": schema.ListNestedAttribute{
  64. Computed: true,
  65. NestedObject: schema.NestedAttributeObject{
  66. Attributes: map[string]schema.Attribute{
  67. "id": schema.Int64Attribute{
  68. Computed: true,
  69. },
  70. },
  71. },
  72. },
  73. },
  74. },
  75. },
  76. },
  77. }
  78. }
  79. // Read is used by the data source to refresh the Terraform state based on the schema data.
  80. func (d *coffeesDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
  81. // 1. Reads coffees list. The method invokes the API client's GetCoffees method.
  82. var state coffeesDataSourceModel
  83. coffees, err := d.client.GetCoffees()
  84. if err != nil {
  85. resp.Diagnostics.AddError(
  86. "Unable to Read HashiCups Coffees",
  87. err.Error(),
  88. )
  89. return
  90. }
  91. // 2. Maps response body to schema attributes.
  92. // After the method reads the coffees, it maps the []hashicups.Coffee response
  93. // to coffeesModel so the data source can set the Terraform state.
  94. for _, coffee := range coffees {
  95. coffeeState := coffeesModel{
  96. ID: types.Int64Value(int64(coffee.ID)),
  97. Name: types.StringValue(coffee.Name),
  98. Teaser: types.StringValue(coffee.Teaser),
  99. Description: types.StringValue(coffee.Description),
  100. Price: types.Float64Value(coffee.Price),
  101. Image: types.StringValue(coffee.Image),
  102. Ingredients: make([]coffeeIngredientsModel, 0, len(coffee.Ingredient)), // Not in tutorial, why ?
  103. }
  104. for _, ingredient := range coffee.Ingredient {
  105. coffeeState.Ingredients = append(coffeeState.Ingredients, coffeeIngredientsModel{
  106. ID: types.Int64Value(int64(ingredient.ID)),
  107. })
  108. }
  109. state.Coffees = append(state.Coffees, coffeeState)
  110. }
  111. // 3. Sets state with coffees list.
  112. diags := resp.State.Set(ctx, state)
  113. resp.Diagnostics.Append(diags...)
  114. // Useless code from tutorial, why ?
  115. if resp.Diagnostics.HasError() {
  116. return
  117. }
  118. }
  119. type coffeesDataSourceModel struct {
  120. Coffees []coffeesModel `tfsdk:"coffees"`
  121. }
  122. type coffeesModel struct {
  123. ID types.Int64 `tfsdk:"id"`
  124. Name types.String `tfsdk:"name"`
  125. Teaser types.String `tfsdk:"teaser"`
  126. Description types.String `tfsdk:"description"`
  127. Price types.Float64 `tfsdk:"price"`
  128. Image types.String `tfsdk:"image"`
  129. Ingredients []coffeeIngredientsModel `tfsdk:"ingredients"`
  130. }
  131. type coffeeIngredientsModel struct {
  132. ID types.Int64 `tfsdk:"id"`
  133. }