-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathassets_datasource.go
More file actions
159 lines (128 loc) · 5.62 KB
/
Copy pathassets_datasource.go
File metadata and controls
159 lines (128 loc) · 5.62 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright 2025 BeyondTrust. All rights reserved.
// Package provider_framework implements a terraform provider that can talk with Beyondtrust Secret Safe API.
package provider_framework
import (
"context"
"terraform-provider-passwordsafe/providers/utils"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/BeyondTrust/go-client-library-passwordsafe/api/assets"
)
var _ datasource.DataSource = &AssetDataSource{}
func NewAssetDataSource() datasource.DataSource {
return &AssetDataSource{}
}
type AssetDataSource struct {
providerInfo *ProviderData
}
type AssetDataSourceModel struct {
Assets []AssetModel `tfsdk:"assets"`
Parameter types.String `tfsdk:"parameter"`
}
func (d *AssetDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_asset_datasource"
}
type AssetModel struct {
WorkgroupID types.Int32 `tfsdk:"workgroup_id"`
AssetID types.Int32 `tfsdk:"asset_id"`
AssetName types.String `tfsdk:"asset_name"`
DnsName types.String `tfsdk:"dns_name"`
DomainName types.String `tfsdk:"domain_name"`
IPAddress types.String `tfsdk:"ip_address"`
MacAddress types.String `tfsdk:"mac_address"`
AssetType types.String `tfsdk:"asset_type"`
OperatingSystem types.String `tfsdk:"operating_system"`
CreateDate types.String `tfsdk:"create_date"`
LastUpdateDate types.String `tfsdk:"last_update_date"`
Description types.String `tfsdk:"description"`
}
func (d *AssetDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Asset Datasource, get assets list.",
Attributes: map[string]schema.Attribute{
"parameter": schema.StringAttribute{
MarkdownDescription: "Parameter",
Required: true,
},
},
Blocks: map[string]schema.Block{
"assets": schema.ListNestedBlock{
Description: "Asset Datasource Attributes",
NestedObject: schema.NestedBlockObject{
Attributes: d.getAssetDataSourceSchemaAttributes(),
},
},
},
}
}
// getAssetDataSourceSchemaAttributes get schema attributes.
func (d *AssetDataSource) getAssetDataSourceSchemaAttributes() map[string]schema.Attribute {
return map[string]schema.Attribute{
"workgroup_id": utils.GetInt32Attribute("Workgroup ID", false, false, true),
"asset_id": utils.GetInt32Attribute("Asset ID", false, false, true),
"asset_name": utils.GetStringAttribute("Asset Name", false, false, true),
"dns_name": utils.GetStringAttribute("DNS Name", false, false, true),
"domain_name": utils.GetStringAttribute("Domain Name", false, false, true),
"ip_address": utils.GetStringAttribute("IP Address", false, false, true),
"mac_address": utils.GetStringAttribute("MAC Address", false, false, true),
"asset_type": utils.GetStringAttribute("Asset Type", false, false, true),
"operating_system": utils.GetStringAttribute("Operating System", false, false, true),
"create_date": utils.GetStringAttribute("Creation Date (ISO 8601 format)", false, false, true),
"last_update_date": utils.GetStringAttribute("Last Update Date (ISO 8601 format)", false, false, true),
"description": utils.GetStringAttribute("Description", false, false, true),
}
}
func (d *AssetDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
// getting data from Provider
c, _ := req.ProviderData.(ProviderData)
d.providerInfo = &c
if d.providerInfo.userName == "" {
return
}
}
func (d *AssetDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var inputData AssetDataSourceModel
resp.Diagnostics.Append(req.Config.Get(ctx, &inputData)...)
if resp.Diagnostics.HasError() {
return
}
// instantiating asset obj
asssetObj, _ := assets.NewAssetObj(*d.providerInfo.authenticationObj, zapLogger)
// get assets list using workgroup id.
itemsByWorkgroupId, err := asssetObj.GetAssetsListByWorkgroupIdFlow(inputData.Parameter.ValueString())
if err != nil {
resp.Diagnostics.AddError("Error getting assets list by workgroup id", err.Error())
return
}
items := itemsByWorkgroupId
// if there is not assets by workgroup id so it will search by workgroup name.
if len(items) == 0 {
// get assets list using workgroup name.
itemsByWorkgroupName, err := asssetObj.GetAssetsListByWorkgroupNameFlow(inputData.Parameter.ValueString())
if err != nil {
resp.Diagnostics.AddError("Error getting assets list by workgroup name", err.Error())
return
}
items = itemsByWorkgroupName
}
var assetsList []AssetModel
for _, item := range items {
assetsList = append(assetsList, AssetModel{
WorkgroupID: types.Int32Value(int32(item.WorkgroupID)),
AssetID: types.Int32Value(int32(item.AssetID)),
AssetName: types.StringValue(item.AssetName),
AssetType: types.StringValue(item.AssetType),
DnsName: types.StringValue(item.DnsName),
DomainName: types.StringValue(item.DomainName),
IPAddress: types.StringValue(item.IPAddress),
OperatingSystem: types.StringValue(item.OperatingSystem),
CreateDate: types.StringValue(item.CreateDate),
LastUpdateDate: types.StringValue(item.LastUpdateDate),
Description: types.StringValue(item.Description),
})
}
responseData := AssetDataSourceModel{}
responseData.Assets = assetsList
resp.Diagnostics.Append(resp.State.Set(ctx, &responseData)...)
}