/
static_route_types.go
114 lines (94 loc) · 3.79 KB
/
static_route_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
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
package edgegw
import (
"context"
"fmt"
govcdtypes "github.com/vmware/go-vcloud-director/v2/types/v56"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
supertypes "github.com/FrangipaneTeam/terraform-plugin-framework-supertypes"
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/pkg/utils"
)
type StaticRouteModel struct {
Description supertypes.StringValue `tfsdk:"description"`
EdgeGatewayID supertypes.StringValue `tfsdk:"edge_gateway_id"`
EdgeGatewayName supertypes.StringValue `tfsdk:"edge_gateway_name"`
ID supertypes.StringValue `tfsdk:"id"`
Name supertypes.StringValue `tfsdk:"name"`
NetworkCidr supertypes.StringValue `tfsdk:"network_cidr"`
NextHops supertypes.SetNestedValue `tfsdk:"next_hops"`
}
// * NextHops.
type StaticRouteModelNextHops []StaticRouteModelNextHop
// * NextHop.
type StaticRouteModelNextHop struct {
AdminDistance supertypes.Int64Value `tfsdk:"admin_distance"`
IPAddress supertypes.StringValue `tfsdk:"ip_address"`
}
func NewStaticRoute(t any) *StaticRouteModel {
switch x := t.(type) {
case tfsdk.State:
return &StaticRouteModel{
Description: supertypes.NewStringNull(),
EdgeGatewayID: supertypes.NewStringUnknown(),
EdgeGatewayName: supertypes.NewStringUnknown(),
ID: supertypes.NewStringUnknown(),
Name: supertypes.NewStringNull(),
NetworkCidr: supertypes.NewStringNull(),
NextHops: supertypes.NewSetNestedNull(x.Schema.GetAttributes()["next_hops"].GetType().(supertypes.SetNestedType).ElementType()),
}
case tfsdk.Plan:
return &StaticRouteModel{
Description: supertypes.NewStringNull(),
EdgeGatewayID: supertypes.NewStringUnknown(),
EdgeGatewayName: supertypes.NewStringUnknown(),
ID: supertypes.NewStringUnknown(),
Name: supertypes.NewStringNull(),
NetworkCidr: supertypes.NewStringNull(),
NextHops: supertypes.NewSetNestedNull(x.Schema.GetAttributes()["next_hops"].GetType().(supertypes.SetNestedType).ElementType()),
}
case tfsdk.Config:
return &StaticRouteModel{
Description: supertypes.NewStringNull(),
EdgeGatewayID: supertypes.NewStringUnknown(),
EdgeGatewayName: supertypes.NewStringUnknown(),
ID: supertypes.NewStringUnknown(),
Name: supertypes.NewStringNull(),
NetworkCidr: supertypes.NewStringNull(),
NextHops: supertypes.NewSetNestedNull(x.Schema.GetAttributes()["next_hops"].GetType().(supertypes.SetNestedType).ElementType()),
}
default:
panic(fmt.Sprintf("unexpected type %T", t))
}
}
func (rm *StaticRouteModel) Copy() *StaticRouteModel {
x := &StaticRouteModel{}
utils.ModelCopy(rm, x)
return x
}
// GetNextHops returns the value of the NextHops field.
func (rm *StaticRouteModel) GetNextHops(ctx context.Context) (values StaticRouteModelNextHops, diags diag.Diagnostics) {
values = make(StaticRouteModelNextHops, 0)
d := rm.NextHops.Get(ctx, &values, false)
return values, d
}
// * CustomFuncs
func (rm *StaticRouteModel) ToNsxtEdgeGatewayStaticRoute(ctx context.Context) (*govcdtypes.NsxtEdgeGatewayStaticRoute, diag.Diagnostics) {
staticRouteConfig := &govcdtypes.NsxtEdgeGatewayStaticRoute{
Name: rm.Name.Get(),
Description: rm.Description.Get(),
NetworkCidr: rm.NetworkCidr.Get(),
NextHops: make([]govcdtypes.NsxtEdgeGatewayStaticRouteNextHops, 0),
}
nextHops, d := rm.GetNextHops(ctx)
if d.HasError() {
return nil, d
}
for _, nextHop := range nextHops {
nH := govcdtypes.NsxtEdgeGatewayStaticRouteNextHops{
IPAddress: nextHop.IPAddress.Get(),
AdminDistance: nextHop.AdminDistance.GetInt(),
}
staticRouteConfig.NextHops = append(staticRouteConfig.NextHops, nH)
}
return staticRouteConfig, d
}