/
routing_zone_constraint.go
120 lines (103 loc) · 4.23 KB
/
routing_zone_constraint.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
118
119
120
package connectivitytemplate
import (
"context"
"encoding/json"
"fmt"
"github.com/Juniper/apstra-go-sdk/apstra"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
dataSourceSchema "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
)
var _ Primitive = &RoutingZoneConstraint{}
type RoutingZoneConstraint struct {
Name types.String `tfsdk:"name"`
RoutingZoneConstraintId types.String `tfsdk:"routing_zone_constraint_id"`
Primitive types.String `tfsdk:"primitive"`
}
func (o RoutingZoneConstraint) DataSourceAttributes() map[string]dataSourceSchema.Attribute {
return map[string]dataSourceSchema.Attribute{
"name": dataSourceSchema.StringAttribute{
MarkdownDescription: "Primitive name displayed in the web UI",
Optional: true,
},
"routing_zone_constraint_id": dataSourceSchema.StringAttribute{
MarkdownDescription: "Apstra Object ID of Routing Zone Constraint to be attached.",
Required: true,
Validators: []validator.String{stringvalidator.LengthAtLeast(1)},
},
"primitive": dataSourceSchema.StringAttribute{
MarkdownDescription: "JSON output for use in the `primitives` field of an " +
"`apstra_datacenter_connectivity_template` resource or a different Connectivity " +
"Template Primitive data source",
Computed: true,
},
}
}
func (o RoutingZoneConstraint) Marshal(_ context.Context, diags *diag.Diagnostics) string {
obj := routingZoneConstraintPrototype{}
if !o.RoutingZoneConstraintId.IsNull() {
id := o.RoutingZoneConstraintId.ValueString()
obj.RoutingZoneConstraintId = &id
}
data, err := json.Marshal(&obj)
if err != nil {
diags.AddError("failed marshaling RoutingZoneConstraint primitive data", err.Error())
return ""
}
data, err = json.Marshal(&tfCfgPrimitive{
PrimitiveType: apstra.CtPrimitivePolicyTypeNameAttachRoutingZoneConstraint.String(),
Label: o.Name.ValueString(),
Data: data,
})
if err != nil {
diags.AddError("failed marshaling primitive", err.Error())
return ""
}
return string(data)
}
func (o *RoutingZoneConstraint) loadSdkPrimitive(ctx context.Context, in apstra.ConnectivityTemplatePrimitive, diags *diag.Diagnostics) {
attributes, ok := in.Attributes.(*apstra.ConnectivityTemplatePrimitiveAttributesAttachRoutingZoneConstraint)
if !ok {
diags.AddError("failed loading SDK primitive due to wrong attribute type", fmt.Sprintf("unexpected type %T", in))
return
}
o.loadSdkPrimitiveAttributes(ctx, attributes, diags)
if diags.HasError() {
return
}
o.Name = types.StringValue(in.Label)
}
func (o *RoutingZoneConstraint) loadSdkPrimitiveAttributes(_ context.Context, in *apstra.ConnectivityTemplatePrimitiveAttributesAttachRoutingZoneConstraint, _ *diag.Diagnostics) {
o.RoutingZoneConstraintId = types.StringNull()
if in.RoutingZoneConstraint != nil {
o.RoutingZoneConstraintId = types.StringValue(in.RoutingZoneConstraint.String())
}
}
var _ JsonPrimitive = &routingZoneConstraintPrototype{}
type routingZoneConstraintPrototype struct {
Label string `json:"label,omitempty"`
RoutingZoneConstraintId *string `json:"routing_zone_constraint_id"`
}
func (o routingZoneConstraintPrototype) attributes(_ context.Context, _ path.Path, _ *diag.Diagnostics) apstra.ConnectivityTemplatePrimitiveAttributes {
rzcId := apstra.ObjectId(*o.RoutingZoneConstraintId)
return &apstra.ConnectivityTemplatePrimitiveAttributesAttachRoutingZoneConstraint{
RoutingZoneConstraint: &rzcId,
}
}
func (o routingZoneConstraintPrototype) ToSdkPrimitive(ctx context.Context, path path.Path, diags *diag.Diagnostics) *apstra.ConnectivityTemplatePrimitive {
attributes := o.attributes(ctx, path, diags)
if diags.HasError() {
return nil
}
return &apstra.ConnectivityTemplatePrimitive{
Id: nil, // calculated later
Label: o.Label,
Attributes: attributes,
Subpolicies: nil, // this primitive has no children
BatchId: nil, // this primitive has no children
PipelineId: nil, // calculated later
}
}