-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_property_data_source.go
More file actions
129 lines (117 loc) · 4 KB
/
config_property_data_source.go
File metadata and controls
129 lines (117 loc) · 4 KB
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
package provider
import (
"context"
"fmt"
dtrack "github.com/DependencyTrack/client-go"
"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"
)
// Interface impl check.
var (
_ datasource.DataSource = &configPropertyDataSource{}
_ datasource.DataSourceWithConfigure = &configPropertyDataSource{}
)
type (
configPropertyDataSource struct {
client *dtrack.Client
semver *Semver
}
configPropertyDataSourceModel struct {
Group types.String `tfsdk:"group"`
Name types.String `tfsdk:"name"`
Value types.String `tfsdk:"value"`
Type types.String `tfsdk:"type"`
Description types.String `tfsdk:"description"`
}
)
func NewConfigPropertyDataSource() datasource.DataSource {
return &configPropertyDataSource{}
}
func (*configPropertyDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_config_property"
}
func (*configPropertyDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Fetch a config property by group and name.",
Attributes: map[string]schema.Attribute{
"group": schema.StringAttribute{
Description: "Group Name of the config property.",
Required: true,
},
"name": schema.StringAttribute{
Description: "Property Name of the config property.",
Required: true,
},
"value": schema.StringAttribute{
Description: "Property Value of the config property.",
Computed: true,
},
"type": schema.StringAttribute{
Description: "Property Type of the config property. See DependencyTrack for valid enum values.",
Computed: true,
},
"description": schema.StringAttribute{
Description: "Description of the config property.",
Computed: true,
},
},
}
}
func (d *configPropertyDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state configPropertyDataSourceModel
diags := req.Config.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
groupName := state.Group.ValueString()
propertyName := state.Name.ValueString()
tflog.Debug(ctx, "Reading Config Property", map[string]any{
"group": groupName,
"name": propertyName,
})
configProperty, err := d.client.Config.Get(ctx, groupName, propertyName)
if err != nil {
resp.Diagnostics.AddError(
"Within Read, unable to fetch config properties.",
"Unexpected error within: "+err.Error(),
)
return
}
propertyState := configPropertyDataSourceModel{
Group: types.StringValue(configProperty.GroupName),
Name: types.StringValue(configProperty.Name),
Value: types.StringValue(configProperty.Value),
Type: types.StringValue(configProperty.Type),
Description: types.StringValue(configProperty.Description),
}
diags = resp.State.Set(ctx, &propertyState)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
tflog.Debug(ctx, "Read Config Property", map[string]any{
"group": propertyState.Group.ValueString(),
"name": propertyState.Name.ValueString(),
"value": propertyState.Value.ValueString(),
"type": propertyState.Type.ValueString(),
"description": propertyState.Description.ValueString(),
})
}
func (d *configPropertyDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
clientInfoData, ok := req.ProviderData.(clientInfo)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Configure Type",
fmt.Sprintf("Expected provider.clientInfo, got %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
d.client = clientInfoData.client
d.semver = clientInfoData.semver
}