Skip to content

Commit

Permalink
add ID attribute to primitives
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismarget-j committed Dec 17, 2024
1 parent 58e6f2f commit 4e95790
Show file tree
Hide file tree
Showing 20 changed files with 328 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ func (o ConnectivityTemplateInterface) Request(ctx context.Context, diags *diag.
}

func (o *ConnectivityTemplateInterface) LoadApiData(ctx context.Context, in *apstra.ConnectivityTemplate, diags *diag.Diagnostics) {
o.Id = types.StringPointerValue((*string)(in.Id))
o.Name = types.StringValue(in.Label)
o.Description = utils.StringValueOrNull(ctx, in.Description, diags)
o.Tags = utils.SetValueOrNull(ctx, types.StringType, in.Tags, diags)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func (o ConnectivityTemplateLoopback) Request(ctx context.Context, diags *diag.D
}

func (o *ConnectivityTemplateLoopback) LoadApiData(ctx context.Context, in *apstra.ConnectivityTemplate, diags *diag.Diagnostics) {
o.Id = types.StringPointerValue((*string)(in.Id))
o.Name = types.StringValue(in.Label)
o.Description = utils.StringValueOrNull(ctx, in.Description, diags)
o.Tags = utils.SetValueOrNull(ctx, types.StringType, in.Tags, diags)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func (o *ConnectivityTemplateSvi) Request(ctx context.Context, diags *diag.Diagn
}

func (o *ConnectivityTemplateSvi) LoadApiData(ctx context.Context, in *apstra.ConnectivityTemplate, diags *diag.Diagnostics) {
o.Id = types.StringPointerValue((*string)(in.Id))
o.Name = types.StringValue(in.Label)
o.Description = utils.StringValueOrNull(ctx, in.Description, diags)
o.Tags = utils.SetValueOrNull(ctx, types.StringType, in.Tags, diags)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ func (o ConnectivityTemplateSystem) Request(ctx context.Context, diags *diag.Dia
}

func (o *ConnectivityTemplateSystem) LoadApiData(ctx context.Context, in *apstra.ConnectivityTemplate, diags *diag.Diagnostics) {
o.Id = types.StringPointerValue((*string)(in.Id))
o.Name = types.StringValue(in.Label)
o.Description = utils.StringValueOrNull(ctx, in.Description, diags)
o.Tags = utils.SetValueOrNull(ctx, types.StringType, in.Tags, diags)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ import (
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
resourceSchema "github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
)

type BgpPeeringGenericSystem struct {
Id types.String `tfsdk:"id"`
Ttl types.Int64 `tfsdk:"ttl"`
BfdEnabled types.Bool `tfsdk:"bfd_enabled"`
Password types.String `tfsdk:"password"`
Expand All @@ -41,6 +44,7 @@ type BgpPeeringGenericSystem struct {

func (o BgpPeeringGenericSystem) AttrTypes() map[string]attr.Type {
return map[string]attr.Type{
"id": types.StringType,
"ttl": types.Int64Type,
"bfd_enabled": types.BoolType,
"password": types.StringType,
Expand All @@ -58,6 +62,11 @@ func (o BgpPeeringGenericSystem) AttrTypes() map[string]attr.Type {

func (o BgpPeeringGenericSystem) ResourceAttributes() map[string]resourceSchema.Attribute {
return map[string]resourceSchema.Attribute{
"id": resourceSchema.StringAttribute{
MarkdownDescription: "Unique identifier for this CT Primitive element",
Computed: true,
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
},
"ttl": resourceSchema.Int64Attribute{
MarkdownDescription: "BGP Time To Live. Omit to use device defaults.",
Optional: true,
Expand Down Expand Up @@ -202,7 +211,15 @@ func (o BgpPeeringGenericSystem) attributes(_ context.Context, diags *diag.Diagn
}

func (o BgpPeeringGenericSystem) primitive(ctx context.Context, diags *diag.Diagnostics) *apstra.ConnectivityTemplatePrimitive {
if !utils.HasValue(o.Id) {
o.Id = utils.NewUuidStringVal(diags)
if diags.HasError() {
return nil
}
}

result := apstra.ConnectivityTemplatePrimitive{
Id: (*apstra.ObjectId)(o.Id.ValueStringPointer()),
// Label: // set by caller
Attributes: o.attributes(ctx, diags),
// Subpolicies: // set below
Expand All @@ -224,6 +241,9 @@ func BgpPeeringGenericSystemSubpolicies(ctx context.Context, bgpPeeringGenericSy
i := 0
for k, v := range bgpPeeringGenericSystems {
subpolicies[i] = v.primitive(ctx, diags)
if diags.HasError() {
return nil
}
subpolicies[i].Label = k
i++
}
Expand Down Expand Up @@ -260,7 +280,7 @@ func newBgpPeeringGenericSystem(_ context.Context, in *apstra.ConnectivityTempla
}

func BgpPeeringGenericSystemPrimitivesFromSubpolicies(ctx context.Context, subpolicies []*apstra.ConnectivityTemplatePrimitive, diags *diag.Diagnostics) types.Map {
result := make(map[string]BgpPeeringGenericSystem, len(subpolicies))
result := make(map[string]BgpPeeringGenericSystem)

for i, subpolicy := range subpolicies {
if subpolicy == nil {
Expand All @@ -278,6 +298,7 @@ func BgpPeeringGenericSystemPrimitivesFromSubpolicies(ctx context.Context, subpo
}

newPrimitive := newBgpPeeringGenericSystem(ctx, p, diags)
newPrimitive.Id = types.StringPointerValue((*string)(subpolicy.Id))
newPrimitive.RoutingPolicies = RoutingPolicyPrimitivesFromSubpolicies(ctx, subpolicy.Subpolicies, diags)
result[subpolicy.Label] = newPrimitive
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ import (
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
resourceSchema "github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
)

type BgpPeeringIpEndpoint struct {
Id types.String `tfsdk:"id"`
NeighborAsn types.Int64 `tfsdk:"neighbor_asn"`
Ttl types.Int64 `tfsdk:"ttl"`
BfdEnabled types.Bool `tfsdk:"bfd_enabled"`
Expand All @@ -38,6 +41,7 @@ type BgpPeeringIpEndpoint struct {

func (o BgpPeeringIpEndpoint) AttrTypes() map[string]attr.Type {
return map[string]attr.Type{
"id": types.StringType,
"neighbor_asn": types.Int64Type,
"ttl": types.Int64Type,
"bfd_enabled": types.BoolType,
Expand All @@ -53,6 +57,11 @@ func (o BgpPeeringIpEndpoint) AttrTypes() map[string]attr.Type {

func (o BgpPeeringIpEndpoint) ResourceAttributes() map[string]resourceSchema.Attribute {
return map[string]resourceSchema.Attribute{
"id": resourceSchema.StringAttribute{
MarkdownDescription: "Unique identifier for this CT Primitive element",
Computed: true,
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
},
"neighbor_asn": resourceSchema.Int64Attribute{
MarkdownDescription: "Neighbor ASN. Omit for *Neighbor ASN Type Dynamic*.",
Optional: true,
Expand Down Expand Up @@ -172,7 +181,15 @@ func (o BgpPeeringIpEndpoint) attributes(_ context.Context, _ *diag.Diagnostics)
}

func (o BgpPeeringIpEndpoint) primitive(ctx context.Context, diags *diag.Diagnostics) *apstra.ConnectivityTemplatePrimitive {
if !utils.HasValue(o.Id) {
o.Id = utils.NewUuidStringVal(diags)
if diags.HasError() {
return nil
}
}

result := apstra.ConnectivityTemplatePrimitive{
Id: (*apstra.ObjectId)(o.Id.ValueStringPointer()),
// Label: // set by caller
Attributes: o.attributes(ctx, diags),
// Subpolicies: // set below
Expand All @@ -194,6 +211,9 @@ func BgpPeeringIpEndpointSubpolicies(ctx context.Context, bgpPeeringIpEndpointMa
i := 0
for k, v := range bgpPeeringIpEndpoints {
subpolicies[i] = v.primitive(ctx, diags)
if diags.HasError() {
return nil
}
subpolicies[i].Label = k
i++
}
Expand Down Expand Up @@ -224,7 +244,7 @@ func newBgpPeeringIpEndpoint(_ context.Context, in *apstra.ConnectivityTemplateP
}

func BgpPeeringIpEndpointPrimitivesFromSubpolicies(ctx context.Context, subpolicies []*apstra.ConnectivityTemplatePrimitive, diags *diag.Diagnostics) types.Map {
result := make(map[string]BgpPeeringIpEndpoint, len(subpolicies))
result := make(map[string]BgpPeeringIpEndpoint)

for i, subpolicy := range subpolicies {
if subpolicy == nil {
Expand All @@ -242,6 +262,7 @@ func BgpPeeringIpEndpointPrimitivesFromSubpolicies(ctx context.Context, subpolic
}

newPrimitive := newBgpPeeringIpEndpoint(ctx, p, diags)
newPrimitive.Id = types.StringPointerValue((*string)(subpolicy.Id))
newPrimitive.RoutingPolicies = RoutingPolicyPrimitivesFromSubpolicies(ctx, subpolicy.Subpolicies, diags)
result[subpolicy.Label] = newPrimitive
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,22 @@ import (
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
resourceSchema "github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
)

type CustomStaticRoute struct {
Id types.String `tfsdk:"id"`
RoutingZoneId types.String `tfsdk:"routing_zone_id"`
Network customtypes.IPv46Prefix `tfsdk:"network"`
NextHop customtypes.IPv46Address `tfsdk:"next_hop"`
}

func (o CustomStaticRoute) AttrTypes() map[string]attr.Type {
return map[string]attr.Type{
"id": types.StringType,
"routing_zone_id": types.StringType,
"network": customtypes.IPv46PrefixType{},
"next_hop": customtypes.IPv46AddressType{},
Expand All @@ -36,6 +40,11 @@ func (o CustomStaticRoute) AttrTypes() map[string]attr.Type {

func (o CustomStaticRoute) ResourceAttributes() map[string]resourceSchema.Attribute {
return map[string]resourceSchema.Attribute{
"id": resourceSchema.StringAttribute{
MarkdownDescription: "Unique identifier for this CT Primitive element",
Computed: true,
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
},
"routing_zone_id": resourceSchema.StringAttribute{
MarkdownDescription: "Routing Zone ID where this route should be installed",
Required: true,
Expand Down Expand Up @@ -72,15 +81,22 @@ func (o CustomStaticRoute) attributes(_ context.Context, _ *diag.Diagnostics) *a
nextHop := net.ParseIP(o.NextHop.ValueString())

return &apstra.ConnectivityTemplatePrimitiveAttributesAttachCustomStaticRoute{
Label: o.NextHop.ValueString(), // todo is this necessary?
Network: network,
NextHop: nextHop,
SecurityZone: (*apstra.ObjectId)(o.RoutingZoneId.ValueStringPointer()),
}
}

func (o CustomStaticRoute) primitive(ctx context.Context, diags *diag.Diagnostics) *apstra.ConnectivityTemplatePrimitive {
if !utils.HasValue(o.Id) {
o.Id = utils.NewUuidStringVal(diags)
if diags.HasError() {
return nil
}
}

return &apstra.ConnectivityTemplatePrimitive{
Id: (*apstra.ObjectId)(o.Id.ValueStringPointer()),
// Label: // set by caller
Attributes: o.attributes(ctx, diags),
}
Expand All @@ -97,6 +113,9 @@ func CustomStaticRouteSubpolicies(ctx context.Context, customStaticRouteMap type
i := 0
for k, v := range customStaticRoutes {
subpolicies[i] = v.primitive(ctx, diags)
if diags.HasError() {
return nil
}
subpolicies[i].Label = k
i++
}
Expand All @@ -114,7 +133,7 @@ func newCustomStaticRoute(_ context.Context, in *apstra.ConnectivityTemplatePrim
}

func CustomStaticRoutePrimitivesFromSubpolicies(ctx context.Context, subpolicies []*apstra.ConnectivityTemplatePrimitive, diags *diag.Diagnostics) types.Map {
result := make(map[string]CustomStaticRoute, len(subpolicies))
result := make(map[string]CustomStaticRoute)

for i, subpolicy := range subpolicies {
if subpolicy == nil {
Expand All @@ -132,6 +151,7 @@ func CustomStaticRoutePrimitivesFromSubpolicies(ctx context.Context, subpolicies
}

newPrimitive := newCustomStaticRoute(ctx, p, diags)
newPrimitive.Id = types.StringPointerValue((*string)(subpolicy.Id))
result[subpolicy.Label] = newPrimitive
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ import (
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
resourceSchema "github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
)

type DynamicBgpPeering struct {
Id types.String `tfsdk:"id"`
Ttl types.Int64 `tfsdk:"ttl"`
BfdEnabled types.Bool `tfsdk:"bfd_enabled"`
Password types.String `tfsdk:"password"`
Expand All @@ -40,6 +43,7 @@ type DynamicBgpPeering struct {

func (o DynamicBgpPeering) AttrTypes() map[string]attr.Type {
return map[string]attr.Type{
"id": types.StringType,
"ttl": types.Int64Type,
"bfd_enabled": types.BoolType,
"password": types.StringType,
Expand All @@ -56,6 +60,11 @@ func (o DynamicBgpPeering) AttrTypes() map[string]attr.Type {

func (o DynamicBgpPeering) ResourceAttributes() map[string]resourceSchema.Attribute {
return map[string]resourceSchema.Attribute{
"id": resourceSchema.StringAttribute{
MarkdownDescription: "Unique identifier for this CT Primitive element",
Computed: true,
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
},
"ttl": resourceSchema.Int64Attribute{
MarkdownDescription: "BGP Time To Live. Omit to use device defaults.",
Optional: true,
Expand Down Expand Up @@ -183,7 +192,15 @@ func (o DynamicBgpPeering) attributes(_ context.Context, _ *diag.Diagnostics) *a
}

func (o DynamicBgpPeering) primitive(ctx context.Context, diags *diag.Diagnostics) *apstra.ConnectivityTemplatePrimitive {
if !utils.HasValue(o.Id) {
o.Id = utils.NewUuidStringVal(diags)
if diags.HasError() {
return nil
}
}

result := apstra.ConnectivityTemplatePrimitive{
Id: (*apstra.ObjectId)(o.Id.ValueStringPointer()),
// Label: // set by caller
Attributes: o.attributes(ctx, diags),
// Subpolicies: // set below
Expand All @@ -205,6 +222,9 @@ func DynamicBgpPeeringSubpolicies(ctx context.Context, dynamicBgpPeeringMap type
i := 0
for k, v := range dynamicBgpPeerings {
subpolicies[i] = v.primitive(ctx, diags)
if diags.HasError() {
return nil
}
subpolicies[i].Label = k
i++
}
Expand Down Expand Up @@ -236,7 +256,7 @@ func newDynamicBgpPeering(_ context.Context, in *apstra.ConnectivityTemplatePrim
}

func DynamicBgpPeeringPrimitivesFromSubpolicies(ctx context.Context, subpolicies []*apstra.ConnectivityTemplatePrimitive, diags *diag.Diagnostics) types.Map {
result := make(map[string]DynamicBgpPeering, len(subpolicies))
result := make(map[string]DynamicBgpPeering)

for i, subpolicy := range subpolicies {
if subpolicy == nil {
Expand All @@ -254,6 +274,7 @@ func DynamicBgpPeeringPrimitivesFromSubpolicies(ctx context.Context, subpolicies
}

newPrimitive := newDynamicBgpPeering(ctx, p, diags)
newPrimitive.Id = types.StringPointerValue((*string)(subpolicy.Id))
newPrimitive.RoutingPolicies = RoutingPolicyPrimitivesFromSubpolicies(ctx, subpolicy.Subpolicies, diags)
result[subpolicy.Label] = newPrimitive
}
Expand Down
Loading

0 comments on commit 4e95790

Please sign in to comment.