Skip to content

Commit

Permalink
update state existence detection in batch id plan modifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismarget-j committed Dec 18, 2024
1 parent 3b6f746 commit b530f48
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -361,15 +361,28 @@ func (o bgpPeeringGenericSystemBatchIdPlanModifier) MarkdownDescription(ctx cont
}

func (o bgpPeeringGenericSystemBatchIdPlanModifier) PlanModifyString(ctx context.Context, req planmodifier.StringRequest, resp *planmodifier.StringResponse) {
var plan BgpPeeringGenericSystem
var plan, state BgpPeeringGenericSystem

// unpacking the parent object's plan should always work
resp.Diagnostics.Append(req.Plan.GetAttribute(ctx, req.Path.ParentPath(), &plan)...)
if resp.Diagnostics.HasError() {
return
}

// do we have any children?
// attempting to unpack the parent object's state indicates whether state *exists*
d := req.State.GetAttribute(ctx, req.Path.ParentPath(), &state)
stateDoesNotExist := d.HasError()

// do we have zero children?
if len(plan.RoutingPolicies.Elements()) == 0 {
resp.PlanValue = types.StringNull() // with no children the batch id should be null
return
}

// are we a new object?
if stateDoesNotExist {
resp.PlanValue = types.StringUnknown() // we are a new object. the batch id is not knowable
return
}

// we're not new, and we have children. use the old value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,21 +326,28 @@ func (o bgpPeeringIpEndpointBatchIdPlanModifier) MarkdownDescription(ctx context
}

func (o bgpPeeringIpEndpointBatchIdPlanModifier) PlanModifyString(ctx context.Context, req planmodifier.StringRequest, resp *planmodifier.StringResponse) {
var plan BgpPeeringIpEndpoint
var plan, state BgpPeeringIpEndpoint

// unpacking the parent object's plan should always work
resp.Diagnostics.Append(req.Plan.GetAttribute(ctx, req.Path.ParentPath(), &plan)...)
if resp.Diagnostics.HasError() {
return
}

// do we have any children?
// attempting to unpack the parent object's state indicates whether state *exists*
d := req.State.GetAttribute(ctx, req.Path.ParentPath(), &state)
stateDoesNotExist := d.HasError()

// do we have zero children?
if len(plan.RoutingPolicies.Elements()) == 0 {
resp.PlanValue = types.StringNull() // with no children the batch id should be null
return
}

// are we a new object?
if plan.Id.IsUnknown() {
if stateDoesNotExist {
resp.PlanValue = types.StringUnknown() // we are a new object. the batch id is not knowable
return
}

// we're not new, and we have children. use the old value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,21 +337,28 @@ func (o dynamicBgpPeeringBatchIdPlanModifier) MarkdownDescription(ctx context.Co
}

func (o dynamicBgpPeeringBatchIdPlanModifier) PlanModifyString(ctx context.Context, req planmodifier.StringRequest, resp *planmodifier.StringResponse) {
var plan DynamicBgpPeering
var plan, state DynamicBgpPeering

// unpacking the parent object's plan should always work
resp.Diagnostics.Append(req.Plan.GetAttribute(ctx, req.Path.ParentPath(), &plan)...)
if resp.Diagnostics.HasError() {
return
}

// do we have any children?
// attempting to unpack the parent object's state indicates whether state *exists*
d := req.State.GetAttribute(ctx, req.Path.ParentPath(), &state)
stateDoesNotExist := d.HasError()

// do we have zero children?
if len(plan.RoutingPolicies.Elements()) == 0 {
resp.PlanValue = types.StringNull() // with no children the batch id should be null
return
}

// are we a new object?
if plan.Id.IsUnknown() {
if stateDoesNotExist {
resp.PlanValue = types.StringUnknown() // we are a new object. the batch id is not knowable
return
}

// we're not new, and we have children. use the old value
Expand Down
13 changes: 10 additions & 3 deletions apstra/blueprint/connectivity_templates/primitives/ip_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,13 +335,19 @@ func (o ipLinkBatchIdPlanModifier) MarkdownDescription(ctx context.Context) stri
}

func (o ipLinkBatchIdPlanModifier) PlanModifyString(ctx context.Context, req planmodifier.StringRequest, resp *planmodifier.StringResponse) {
var plan IpLink
var plan, state IpLink

// unpacking the parent object's plan should always work
resp.Diagnostics.Append(req.Plan.GetAttribute(ctx, req.Path.ParentPath(), &plan)...)
if resp.Diagnostics.HasError() {
return
}

// do we have any children?
// attempting to unpack the parent object's state indicates whether state *exists*
d := req.State.GetAttribute(ctx, req.Path.ParentPath(), &state)
stateDoesNotExist := d.HasError()

// do we have zero children?
if len(plan.BgpPeeringGenericSystems.Elements())+
len(plan.BgpPeeringIpEndpoints.Elements())+
len(plan.DynamicBgpPeerings.Elements())+
Expand All @@ -351,8 +357,9 @@ func (o ipLinkBatchIdPlanModifier) PlanModifyString(ctx context.Context, req pla
}

// are we a new object?
if plan.Id.IsUnknown() {
if stateDoesNotExist {
resp.PlanValue = types.StringUnknown() // we are a new object. the batch id is not knowable
return
}

// we're not new, and we have children. use the old value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,22 +208,29 @@ func (o virtualNetworkSingleBatchIdPlanModifier) MarkdownDescription(ctx context
}

func (o virtualNetworkSingleBatchIdPlanModifier) PlanModifyString(ctx context.Context, req planmodifier.StringRequest, resp *planmodifier.StringResponse) {
var plan VirtualNetworkSingle
var plan, state VirtualNetworkSingle

// unpacking the parent object's plan should always work
resp.Diagnostics.Append(req.Plan.GetAttribute(ctx, req.Path.ParentPath(), &plan)...)
if resp.Diagnostics.HasError() {
return
}

// do we have any children?
// attempting to unpack the parent object's state indicates whether state *exists*
d := req.State.GetAttribute(ctx, req.Path.ParentPath(), &state)
stateDoesNotExist := d.HasError()

// do we have zero children?
if len(plan.BgpPeeringGenericSystems.Elements())+
len(plan.StaticRoutes.Elements()) == 0 {
resp.PlanValue = types.StringNull() // with no children the batch id should be null
return
}

// are we a new object?
if plan.Id.IsUnknown() {
if stateDoesNotExist {
resp.PlanValue = types.StringUnknown() // we are a new object. the batch id is not knowable
return
}

// we're not new, and we have children. use the old value
Expand Down

0 comments on commit b530f48

Please sign in to comment.