/
static_route_datasource.go
117 lines (94 loc) · 3.33 KB
/
static_route_datasource.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
// Package edgegw provides a Terraform datasource.
package edgegw //nolint:dupl
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/client"
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/metrics"
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/provider/common/edgegw"
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/provider/common/org"
)
var (
_ datasource.DataSource = &staticRouteDataSource{}
_ datasource.DataSourceWithConfigure = &staticRouteDataSource{}
)
func NewStaticRouteDataSource() datasource.DataSource {
return &staticRouteDataSource{}
}
type staticRouteDataSource struct {
client *client.CloudAvenue
org org.Org
edgegw edgegw.EdgeGateway
}
// Init Initializes the data source.
func (d *staticRouteDataSource) Init(ctx context.Context, dm *StaticRouteModel) (diags diag.Diagnostics) {
var err error
d.org, diags = org.Init(d.client)
if diags.HasError() {
return
}
d.edgegw, err = d.org.GetEdgeGateway(edgegw.BaseEdgeGW{
ID: types.StringValue(dm.EdgeGatewayID.Get()),
Name: types.StringValue(dm.EdgeGatewayName.Get()),
})
if err != nil {
diags.AddError("Error retrieving Edge Gateway", err.Error())
return
}
return
}
func (d *staticRouteDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_" + categoryName + "_static_route"
}
func (d *staticRouteDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = staticRouteSchema(ctx).GetDataSource(ctx)
}
func (d *staticRouteDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}
client, ok := req.ProviderData.(*client.CloudAvenue)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *client.CloudAvenue, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
d.client = client
}
func (d *staticRouteDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
defer metrics.New("data.cloudavenue_edgegateway_static_route", d.client.GetOrgName(), metrics.Read)()
config := &StaticRouteModel{}
// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, config)...)
if resp.Diagnostics.HasError() {
return
}
// Init the resource
resp.Diagnostics.Append(d.Init(ctx, config)...)
if resp.Diagnostics.HasError() {
return
}
/*
Implement the data source read logic here.
*/
// If read function is identical to the resource, you can use the following code:
s := &staticRouteResource{
client: d.client,
org: d.org,
edgegw: d.edgegw,
}
// Read data from the API
data, _, diags := s.read(ctx, config)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}