/
data_source_policy_types.go
87 lines (76 loc) · 2.8 KB
/
data_source_policy_types.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
package mds
import (
"context"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/svc-bot-mds/terraform-provider-vmds/client/mds"
"github.com/svc-bot-mds/terraform-provider-vmds/constants/common"
)
// Ensure the implementation satisfies the expected interfaces.
var (
_ datasource.DataSource = &policyTypesDataSource{}
_ datasource.DataSourceWithConfigure = &policyTypesDataSource{}
)
type policyTypesDataSourceModel struct {
Id types.String `tfsdk:"id"`
PolicyTypes []string `tfsdk:"policy_types"`
}
func NewPolicyTypesDataSource() datasource.DataSource {
return &policyTypesDataSource{}
}
type policyTypesDataSource struct {
client *mds.Client
}
// Metadata returns the data source type name.
func (d *policyTypesDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_policy_types"
}
func (d *policyTypesDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Used to fetch types of policies supported by MDS.",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
MarkdownDescription: "The testing framework requires an id attribute to be present in every data source and resource.",
},
"policy_types": schema.SetAttribute{
Description: "List of policy types.",
Computed: true,
ElementType: types.StringType,
},
},
}
}
// Configure adds the provider configured client to the data source.
func (d *policyTypesDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
d.client = req.ProviderData.(*mds.Client)
}
// Read refreshes the Terraform state with the latest data.
func (d *policyTypesDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
tflog.Info(ctx, "getPolicyTypes")
var state policyTypesDataSourceModel
//Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &state)...)
tflog.Info(ctx, "getPolicyTypes")
typesList, err := d.client.ServiceMetadata.GetPolicyTypes()
if err != nil {
resp.Diagnostics.AddError(
"Unable to Read MDS Policy Types:",
err.Error(),
)
return
}
state.Id = types.StringValue(common.DataSource + common.PolicyTypesId)
state.PolicyTypes = append(state.PolicyTypes, typesList...)
// Set state
diags := resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}