/
bundles_data_source.go
136 lines (120 loc) · 4.17 KB
/
bundles_data_source.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package adcm
import (
"context"
adcmClient "github.com/giggsoff/terraform-provider-adcm/client"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
)
// Ensure the implementation satisfies the expected interfaces.
var (
_ datasource.DataSource = &bundleDataSource{}
_ datasource.DataSourceWithConfigure = &bundleDataSource{}
)
// NewBundleDataSource is a helper function to simplify the provider implementation.
func NewBundleDataSource() datasource.DataSource {
return &bundleDataSource{}
}
// bundleDataSource is the data source implementation.
type bundleDataSource struct {
client *adcmClient.Client
}
// bundleModel maps bundle schema data.
type bundleDataSourceModel struct {
ID types.Int64 `tfsdk:"id"`
Name types.String `tfsdk:"name"`
DisplayName types.String `tfsdk:"display_name"`
Description types.String `tfsdk:"description"`
Edition types.String `tfsdk:"edition"`
License types.String `tfsdk:"license"`
Version types.String `tfsdk:"version"`
}
// Metadata returns the data source type name.
func (d *bundleDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_bundle"
}
// Schema defines the schema for the data source.
func (d *bundleDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Fetches the bundle.",
Attributes: map[string]schema.Attribute{
"id": schema.Int64Attribute{
Description: "Numeric identifier of the bundle.",
Optional: true,
Computed: true,
},
"name": schema.StringAttribute{
Description: "Product name of the bundle.",
Optional: true,
Computed: true,
},
"display_name": schema.StringAttribute{
Description: "Product display name of the bundle.",
Optional: true,
Computed: true,
},
"description": schema.StringAttribute{
Description: "Product description of the bundle.",
Optional: true,
Computed: true,
},
"edition": schema.StringAttribute{
Description: "Product edition of the bundle.",
Optional: true,
Computed: true,
},
"license": schema.StringAttribute{
Description: "Product license acceptance state of the bundle.",
Optional: true,
Computed: true,
},
"version": schema.StringAttribute{
Description: "Product version of the bundle.",
Optional: true,
Computed: true,
},
},
}
}
// Configure adds the provider configured client to the data source.
func (d *bundleDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
d.client = req.ProviderData.(*adcmClient.Client)
}
// Read refreshes the Terraform state with the latest data.
func (d *bundleDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state bundleDataSourceModel
var requestOptions bundleDataSourceModel
req.Config.Get(ctx, &requestOptions)
opts := adcmClient.BundleSearch{
Name: requestOptions.Name.ValueString(),
DisplayName: requestOptions.DisplayName.ValueString(),
Description: requestOptions.Description.ValueString(),
Edition: requestOptions.Edition.ValueString(),
License: requestOptions.License.ValueString(),
Version: requestOptions.Version.ValueString(),
}
bundle, err := d.client.GetBundle(opts)
if err != nil {
resp.Diagnostics.AddError(
"Unable to Read ADCM Bundle",
err.Error(),
)
return
}
state.ID = types.Int64Value(int64(bundle.ID))
state.Name = types.StringValue(bundle.Name)
state.DisplayName = types.StringValue(bundle.DisplayName)
state.Description = types.StringValue(bundle.Description)
state.Edition = types.StringValue(bundle.Edition)
state.License = types.StringValue(bundle.License)
state.Version = types.StringValue(bundle.Version)
// Set state
diags := resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}