/
edgegateway_datasource.go
86 lines (66 loc) · 2.69 KB
/
edgegateway_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
// Package edgegw provides a Terraform resource to manage edge gateways.
package edgegw
import (
"context"
"fmt"
"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/pkg/uuid"
)
var (
_ datasource.DataSource = &edgeGatewaysDataSource{}
_ datasource.DataSourceWithConfigure = &edgeGatewaysDataSource{}
)
// NewEdgeGatewayDataSource returns a new datasource implementing the edge_gateway data source.
func NewEdgeGatewayDataSource() datasource.DataSource {
return &edgeGatewayDataSource{}
}
type edgeGatewayDataSource struct {
client *client.CloudAvenue
}
func (d *edgeGatewayDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_" + categoryName
}
func (d *edgeGatewayDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = edgegwSchema().GetDataSource(ctx)
}
func (d *edgeGatewayDataSource) 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 *edgeGatewayDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
defer metrics.New("data.cloudavenue_edgegateway", d.client.GetOrgName(), metrics.Read)()
config := &edgeGatewayDatasourceModel{}
// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, config)...)
if resp.Diagnostics.HasError() {
return
}
data := config.Copy()
// Read data from the API
edgegw, err := d.client.CAVSDK.V1.EdgeGateway.GetByName(config.Name.Get())
if err != nil {
resp.Diagnostics.AddError("Error retrieving edge gateway", err.Error())
return
}
data.ID.Set(uuid.Normalize(uuid.Gateway, edgegw.GetID()).String())
data.Tier0VrfID.Set(edgegw.GetTier0VrfID())
data.OwnerName.Set(edgegw.GetOwnerName())
data.OwnerType.Set(string(edgegw.GetOwnerType()))
data.Description.Set(edgegw.GetDescription())
data.Bandwidth.SetInt(int(edgegw.GetBandwidth()))
// Set refreshed state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}