Skip to content

Commit

Permalink
Merge pull request #496 from Juniper/rename-ct-assignemnt-to-cts-assi…
Browse files Browse the repository at this point in the history
…gnment

Deprecate `apstra_datacenter_connectivity_template_assignment`; Introduce `apstra_datacenter_connectivity_template_assignments` and `apstra__datacenter_connectivity_templates_assignment` resources
  • Loading branch information
chrismarget-j authored Dec 29, 2023
2 parents a4d7e10 + e614a53 commit c51bccf
Show file tree
Hide file tree
Showing 13 changed files with 692 additions and 15 deletions.
90 changes: 90 additions & 0 deletions apstra/blueprint/connectivity_template_assignments.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package blueprint

import (
"context"
"github.com/Juniper/apstra-go-sdk/apstra"
"github.com/hashicorp/terraform-plugin-framework-validators/setvalidator"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/diag"
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 ConnectivityTemplateAssignments struct {
BlueprintId types.String `tfsdk:"blueprint_id"`
ConnectivityTemplateId types.String `tfsdk:"connectivity_template_id"`
ApplicationPointIds types.Set `tfsdk:"application_point_ids"`
}

func (o ConnectivityTemplateAssignments) ResourceAttributes() map[string]resourceSchema.Attribute {
return map[string]resourceSchema.Attribute{
"blueprint_id": resourceSchema.StringAttribute{
MarkdownDescription: "Apstra Blueprint ID.",
Required: true,
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()},
Validators: []validator.String{stringvalidator.LengthAtLeast(1)},
},
"connectivity_template_id": resourceSchema.StringAttribute{
MarkdownDescription: "Connectivity Template ID which should be applied to the Application Points.",
Required: true,
Validators: []validator.String{stringvalidator.LengthAtLeast(1)},
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()},
},
"application_point_ids": resourceSchema.SetAttribute{
MarkdownDescription: "Set of Apstra node IDs of the Interfaces or Systems where the Connectivity " +
"Template should be applied.",
Required: true,
ElementType: types.StringType,
Validators: []validator.Set{
setvalidator.SizeAtLeast(1),
setvalidator.ValueStringsAre(stringvalidator.LengthAtLeast(1)),
},
},
}
}

func (o *ConnectivityTemplateAssignments) Request(ctx context.Context, state *ConnectivityTemplateAssignments, diags *diag.Diagnostics) map[apstra.ObjectId]map[apstra.ObjectId]bool {
var desired, current []apstra.ObjectId // Application Point IDs

diags.Append(o.ApplicationPointIds.ElementsAs(ctx, &desired, false)...)
if diags.HasError() {
return nil
}
desiredMap := make(map[apstra.ObjectId]bool, len(desired))
for _, apId := range desired {
desiredMap[apId] = true
}

if state != nil {
diags.Append(state.ApplicationPointIds.ElementsAs(ctx, &current, false)...)
if diags.HasError() {
return nil
}
}
currentMap := make(map[apstra.ObjectId]bool, len(current))
for _, apId := range current {
currentMap[apId] = true
}

result := make(map[apstra.ObjectId]map[apstra.ObjectId]bool)
ctId := apstra.ObjectId(o.ConnectivityTemplateId.ValueString())

for _, ApplicationPointId := range desired {
if _, ok := currentMap[ApplicationPointId]; !ok {
// desired Application Point not found in currentMap -- need to add
result[ApplicationPointId] = map[apstra.ObjectId]bool{ctId: true} // causes CT to be added
}
}

for _, ApplicationPointId := range current {
if _, ok := desiredMap[ApplicationPointId]; !ok {
// current Application Point not found in desiredMap -- need to remove
result[ApplicationPointId] = map[apstra.ObjectId]bool{ctId: false} // causes CT to be added
}
}

return result
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import (
"github.com/hashicorp/terraform-plugin-framework/types"
)

type ConnectivityTemplateAssignment struct {
type ConnectivityTemplatesAssignment struct {
BlueprintId types.String `tfsdk:"blueprint_id"`
ConnectivityTemplateIds types.Set `tfsdk:"connectivity_template_ids"`
ApplicationPointId types.String `tfsdk:"application_point_id"`
}

func (o ConnectivityTemplateAssignment) ResourceAttributes() map[string]resourceSchema.Attribute {
func (o ConnectivityTemplatesAssignment) ResourceAttributes() map[string]resourceSchema.Attribute {
return map[string]resourceSchema.Attribute{
"blueprint_id": resourceSchema.StringAttribute{
MarkdownDescription: "Apstra Blueprint ID.",
Expand All @@ -29,7 +29,7 @@ func (o ConnectivityTemplateAssignment) ResourceAttributes() map[string]resource
Validators: []validator.String{stringvalidator.LengthAtLeast(1)},
},
"application_point_id": resourceSchema.StringAttribute{
MarkdownDescription: "Apstra node ID of the Interface or System where the Connectivity Template " +
MarkdownDescription: "Apstra node ID of the Interface or System where the Connectivity Templates " +
"should be applied.",
Required: true,
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()},
Expand All @@ -47,7 +47,7 @@ func (o ConnectivityTemplateAssignment) ResourceAttributes() map[string]resource
}
}

func (o *ConnectivityTemplateAssignment) AddDelRequest(ctx context.Context, state *ConnectivityTemplateAssignment, diags *diag.Diagnostics) ([]apstra.ObjectId, []apstra.ObjectId) {
func (o *ConnectivityTemplatesAssignment) AddDelRequest(ctx context.Context, state *ConnectivityTemplatesAssignment, diags *diag.Diagnostics) ([]apstra.ObjectId, []apstra.ObjectId) {
var planIds, stateIds []apstra.ObjectId

if o != nil { // o will be nil in Delete()
Expand Down
2 changes: 2 additions & 0 deletions apstra/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,8 @@ func (p *Provider) Resources(_ context.Context) []func() resource.Resource {
func() resource.Resource { return &resourceDatacenterConfiglet{} },
func() resource.Resource { return &resourceDatacenterConnectivityTemplate{} },
func() resource.Resource { return &resourceDatacenterConnectivityTemplateAssignment{} },
func() resource.Resource { return &resourceDatacenterConnectivityTemplateAssignments{} },
func() resource.Resource { return &resourceDatacenterConnectivityTemplatesAssignment{} },
func() resource.Resource { return &resourceDatacenterExternalGateway{} },
func() resource.Resource { return &resourceDatacenterGenericSystem{} },
func() resource.Resource { return &resourceDatacenterPropertySet{} },
Expand Down
18 changes: 11 additions & 7 deletions apstra/resource_datacenter_connectivity_template_assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,21 @@ func (o *resourceDatacenterConnectivityTemplateAssignment) Configure(ctx context
}

func (o *resourceDatacenterConnectivityTemplateAssignment) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
deprecationMessage := "This resource has been deprecated and will be removed in a future release. Please migrate " +
"your configuration to use the `apstra_datacenter_connectivity_templates_assignment` resource."
resp.Schema = schema.Schema{
MarkdownDescription: docCategoryDatacenter + "This resource assigns one or more Connectivity Templates to an " +
DeprecationMessage: deprecationMessage,
MarkdownDescription: docCategoryDatacenter + "**Deprecation Warning**\n\n" + deprecationMessage +
"\n\nThis resource assigns one or more Connectivity Templates to an " +
"Application Point. Application Points are graph nodes including interfaces at the " +
"fabric edge, and switches within the fabric.",
Attributes: blueprint.ConnectivityTemplateAssignment{}.ResourceAttributes(),
Attributes: blueprint.ConnectivityTemplatesAssignment{}.ResourceAttributes(),
}
}

func (o *resourceDatacenterConnectivityTemplateAssignment) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
// Retrieve values from plan.
var plan blueprint.ConnectivityTemplateAssignment
var plan blueprint.ConnectivityTemplatesAssignment
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
Expand Down Expand Up @@ -76,7 +80,7 @@ func (o *resourceDatacenterConnectivityTemplateAssignment) Create(ctx context.Co

func (o *resourceDatacenterConnectivityTemplateAssignment) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
// Retrieve values from state
var state blueprint.ConnectivityTemplateAssignment
var state blueprint.ConnectivityTemplatesAssignment
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
Expand Down Expand Up @@ -124,14 +128,14 @@ func (o *resourceDatacenterConnectivityTemplateAssignment) Read(ctx context.Cont

func (o *resourceDatacenterConnectivityTemplateAssignment) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
// Retrieve values from plan
var plan blueprint.ConnectivityTemplateAssignment
var plan blueprint.ConnectivityTemplatesAssignment
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}

// Retrieve values from state
var state blueprint.ConnectivityTemplateAssignment
var state blueprint.ConnectivityTemplatesAssignment
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
Expand Down Expand Up @@ -178,7 +182,7 @@ func (o *resourceDatacenterConnectivityTemplateAssignment) Update(ctx context.Co

func (o *resourceDatacenterConnectivityTemplateAssignment) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
// Retrieve values from state
var state blueprint.ConnectivityTemplateAssignment
var state blueprint.ConnectivityTemplatesAssignment
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
Expand Down
Loading

0 comments on commit c51bccf

Please sign in to comment.