Skip to content

Commit

Permalink
Merge branch 'main' into 829-implement-freeform-group-generators
Browse files Browse the repository at this point in the history
  • Loading branch information
bwJuniper authored Aug 30, 2024
2 parents 3436b2e + 71c996c commit 05a9a4b
Show file tree
Hide file tree
Showing 20 changed files with 211 additions and 29 deletions.
12 changes: 12 additions & 0 deletions apstra/data_source_freeform_config_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,18 @@ func (o *dataSourceFreeformConfigTemplate) Read(ctx context.Context, req datasou
return
}

// Read the system assignments
assignments, err := bp.GetConfigTemplateAssignments(ctx, api.Id)
if err != nil {
resp.Diagnostics.AddError("error reading ConfigTemplate System Assignments", err.Error())
return
}

config.AssignedTo = utils.SetValueOrNull(ctx, types.StringType, assignments, &resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
}

// Set state
resp.Diagnostics.Append(resp.State.Set(ctx, &config)...)
}
Expand Down
28 changes: 28 additions & 0 deletions apstra/freeform/config_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type ConfigTemplate struct {
Name types.String `tfsdk:"name"`
Text types.String `tfsdk:"text"`
Tags types.Set `tfsdk:"tags"`
AssignedTo types.Set `tfsdk:"assigned_to"`
}

func (o ConfigTemplate) DataSourceAttributes() map[string]dataSourceSchema.Attribute {
Expand Down Expand Up @@ -61,6 +62,11 @@ func (o ConfigTemplate) DataSourceAttributes() map[string]dataSourceSchema.Attri
ElementType: types.StringType,
Computed: true,
},
"assigned_to": dataSourceSchema.SetAttribute{
MarkdownDescription: "Set of System IDs to which the ConfigTemplate is assigned",
ElementType: types.StringType,
Computed: true,
},
}
}

Expand Down Expand Up @@ -99,6 +105,15 @@ func (o ConfigTemplate) ResourceAttributes() map[string]resourceSchema.Attribute
setvalidator.ValueStringsAre(stringvalidator.LengthAtLeast(1)),
},
},
"assigned_to": resourceSchema.SetAttribute{
MarkdownDescription: "Set of System IDs to which the ConfigTemplate is assigned",
ElementType: types.StringType,
Optional: true,
Validators: []validator.Set{
setvalidator.SizeAtLeast(1),
setvalidator.ValueStringsAre(stringvalidator.LengthAtLeast(1)),
},
},
}
}

Expand All @@ -121,3 +136,16 @@ func (o *ConfigTemplate) LoadApiData(ctx context.Context, in *apstra.ConfigTempl
o.Text = types.StringValue(in.Text)
o.Tags = utils.SetValueOrNull(ctx, types.StringType, in.Tags, diags) // safe to ignore diagnostic here
}

func (o ConfigTemplate) NeedsUpdate(state ConfigTemplate) bool {
switch {
case !o.Name.Equal(state.Name):
return true
case !o.Text.Equal(state.Text):
return true
case !o.Tags.Equal(state.Tags):
return true
}

return false
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (o resourceDataCenterConnectivityTemplateInterface) render(rType, rName str
o.blueprintId,
o.name,
stringOrNull(o.description),
stringSetOrNull(o.tags),
stringSliceOrNull(o.tags),
ipLinks,
routingZoneConstraints,
virtualNetworkMultiples,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (o resourceDataCenterConnectivityTemplateLoopback) render(rType, rName stri
o.blueprintId,
o.name,
stringOrNull(o.description),
stringSetOrNull(o.tags),
stringSliceOrNull(o.tags),
bgpPeeringIpEndoints,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ func (o resourceDataCenterConnectivityTemplatePrimitiveVirtualNetworkMultiple) r
fmt.Sprintf(resourceDataCenterConnectivityTemplatePrimitiveVirtualNetworkMultipleHCL,
o.name,
stringOrNull(o.untaggedVnId),
stringSetOrNull(o.taggedVnIds),
stringSliceOrNull(o.taggedVnIds),
),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (o resourceDataCenterConnectivityTemplateSvi) render(rType, rName string) s
o.blueprintId,
o.name,
stringOrNull(o.description),
stringSetOrNull(o.tags),
stringSliceOrNull(o.tags),
bgpPeeringIpEndoints,
dynamicBgpPeerings,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (o resourceDataCenterConnectivityTemplateSystem) render(rType, rName string
o.blueprintId,
o.name,
stringOrNull(o.description),
stringSetOrNull(o.tags),
stringSliceOrNull(o.tags),
customStaticRoutes,
)
}
Expand Down
2 changes: 1 addition & 1 deletion apstra/resource_datacenter_device_allocation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func TestResourceDatacenterDeviceAllocation(t *testing.T) {
cidrOrNull(in.loopbackIpv4),
cidrOrNull(in.loopbackIpv6),
stringOrNull(in.deployMode),
stringSetOrNull(in.tags),
stringSliceOrNull(in.tags),
)
}

Expand Down
6 changes: 3 additions & 3 deletions apstra/resource_datacenter_routing_zone_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ func (o testRoutingZone) render(bpId apstra.ObjectId, rType, rName string) strin

intPtrOrNull(o.vlan),
intPtrOrNull(o.vni),
stringSetOrNull(o.dhcpServers),
stringSliceOrNull(o.dhcpServers),
stringOrNull(o.routingPolicy),
stringSetOrNull(o.importRTs),
stringSetOrNull(o.exportRTs),
stringSliceOrNull(o.importRTs),
stringSliceOrNull(o.exportRTs),
stringOrNull(o.irbMode),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (o resourceAllocGroup) render(rType, rName string) string {
o.blueprintId,
o.name,
utils.StringersToFriendlyString(o.groupType),
stringSetOrNull(o.poolIds),
stringSliceOrNull(o.poolIds),
)
}

Expand Down
78 changes: 70 additions & 8 deletions apstra/resource_freeform_config_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,32 @@ func (o *resourceFreeformConfigTemplate) Create(ctx context.Context, req resourc
return
}

// record the id and provisionally set the state
plan.Id = types.StringValue(id.String())
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}

// set the CT system assignments, if any
if !plan.AssignedTo.IsNull() {
var assignments []apstra.ObjectId
resp.Diagnostics.Append(plan.AssignedTo.ElementsAs(ctx, &assignments, false)...)
if resp.Diagnostics.HasError() {
return
}

updateRequest := make(map[apstra.ObjectId]*apstra.ObjectId, len(assignments))
for _, assignment := range assignments {
updateRequest[assignment] = &id
}

err = bp.UpdateConfigTemplateAssignments(ctx, updateRequest)
if err != nil {
resp.Diagnostics.AddError("error updating ConfigTemplate system Assignments", err.Error())
return
}
}

// set state
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
Expand Down Expand Up @@ -117,6 +142,18 @@ func (o *resourceFreeformConfigTemplate) Read(ctx context.Context, req resource.
return
}

// Read the system assignments
assignments, err := bp.GetConfigTemplateAssignments(ctx, api.Id)
if err != nil {
resp.Diagnostics.AddError("error reading ConfigTemplate System Assignments", err.Error())
return
}

state.AssignedTo = utils.SetValueOrNull(ctx, types.StringType, assignments, &resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
}

// Set state
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
}
Expand All @@ -129,6 +166,13 @@ func (o *resourceFreeformConfigTemplate) Update(ctx context.Context, req resourc
return
}

// Get state values
var state freeform.ConfigTemplate
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}

// get a client for the Freeform reference design
bp, err := o.getBpClientFunc(ctx, plan.BlueprintId.ValueString())
if err != nil {
Expand All @@ -149,17 +193,35 @@ func (o *resourceFreeformConfigTemplate) Update(ctx context.Context, req resourc
return
}

request := plan.Request(ctx, &resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
if plan.NeedsUpdate(state) {
request := plan.Request(ctx, &resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
}

// Update Config Template
err = bp.UpdateConfigTemplate(ctx, apstra.ObjectId(plan.Id.ValueString()), request)
if err != nil {
resp.Diagnostics.AddError("error updating Config Template", err.Error())
return
}
}

// Update Config Template
err = bp.UpdateConfigTemplate(ctx, apstra.ObjectId(plan.Id.ValueString()), request)
if err != nil {
resp.Diagnostics.AddError("error updating Config Template", err.Error())
return
// update the assignments if necessary
if !plan.AssignedTo.Equal(state.AssignedTo) {
var planAssignments []apstra.ObjectId
resp.Diagnostics.Append(plan.AssignedTo.ElementsAs(ctx, &planAssignments, false)...)
if resp.Diagnostics.HasError() {
return
}

err = bp.UpdateConfigTemplateAssignmentsByTemplate(ctx, apstra.ObjectId(plan.Id.ValueString()), planAssignments)
if err != nil {
resp.Diagnostics.AddError("error updating Resource Assignments", err.Error())
return
}
}

// set state
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
}
Expand Down
24 changes: 20 additions & 4 deletions apstra/resource_freeform_config_template_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package tfapstra_test
import (
"context"
"fmt"
"github.com/Juniper/apstra-go-sdk/apstra"
"math/rand"
"strconv"
"testing"
Expand All @@ -23,6 +24,7 @@ resource %q %q {
name = %q
text = %q
tags = %s
assigned_to = %s
}
`
)
Expand All @@ -32,6 +34,7 @@ type resourceFreeformConfigTemplate struct {
name string
text string
tags []string
assignedTo []apstra.ObjectId
}

func (o resourceFreeformConfigTemplate) render(rType, rName string) string {
Expand All @@ -40,7 +43,8 @@ func (o resourceFreeformConfigTemplate) render(rType, rName string) string {
o.blueprintId,
o.name,
o.text,
stringSetOrNull(o.tags),
stringSliceOrNull(o.tags),
stringSliceOrNull(o.assignedTo),
)
}

Expand All @@ -60,6 +64,15 @@ func (o resourceFreeformConfigTemplate) testChecks(t testing.TB, rType, rName st
}
}

if len(o.assignedTo) > 0 {
result.append(t, "TestCheckResourceAttr", "assigned_to.#", strconv.Itoa(len(o.assignedTo)))
for _, assignment := range o.assignedTo {
result.append(t, "TestCheckTypeSetElemAttr", "assigned_to.*", string(assignment))
}
} else {
result.append(t, "TestCheckNoResourceAttr", "assigned_to")
}

return result
}

Expand All @@ -69,7 +82,7 @@ func TestResourceFreeformConfigTemplate(t *testing.T) {
apiVersion := version.Must(version.NewVersion(client.ApiVersion()))

// create a blueprint
bp := testutils.FfBlueprintA(t, ctx)
bp, intSysIds, _ := testutils.FfBlueprintB(t, ctx, 3, 0)

type testStep struct {
config resourceFreeformConfigTemplate
Expand All @@ -80,7 +93,7 @@ func TestResourceFreeformConfigTemplate(t *testing.T) {
}

testCases := map[string]testCase{
"start_with_no_tags": {
"start_minimal": {
steps: []testStep{
{
config: resourceFreeformConfigTemplate{
Expand All @@ -95,6 +108,7 @@ func TestResourceFreeformConfigTemplate(t *testing.T) {
name: acctest.RandString(6) + ".jinja",
text: acctest.RandString(6),
tags: randomStrings(rand.Intn(10)+2, 6),
assignedTo: intSysIds,
},
},
{
Expand All @@ -106,14 +120,15 @@ func TestResourceFreeformConfigTemplate(t *testing.T) {
},
},
},
"start_with_tags": {
"start_maximal": {
steps: []testStep{
{
config: resourceFreeformConfigTemplate{
blueprintId: bp.Id().String(),
name: acctest.RandString(6) + ".jinja",
text: acctest.RandString(6),
tags: randomStrings(rand.Intn(10)+2, 6),
assignedTo: intSysIds,
},
},
{
Expand All @@ -129,6 +144,7 @@ func TestResourceFreeformConfigTemplate(t *testing.T) {
name: acctest.RandString(6) + ".jinja",
text: acctest.RandString(6),
tags: randomStrings(rand.Intn(10)+2, 6),
assignedTo: intSysIds,
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion apstra/resource_freeform_link_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (o resourceFreeformLink) render(rType, rName string) string {
rType, rName,
o.blueprintId,
o.name,
stringSetOrNull(o.tags),
stringSliceOrNull(o.tags),
o.endpoints[0].SystemId,
stringPtrOrNull(o.endpoints[0].Interface.Data.IfName),
intPtrOrNull(o.endpoints[0].Interface.Data.TransformationId),
Expand Down
2 changes: 1 addition & 1 deletion apstra/resource_freeform_resource_integraion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (o resourceFreeformResource) render(rType, rName string) string {
ipNetOrNull(o.ipv4Value),
ipNetOrNull(o.ipv6Value),
stringOrNull(o.allocatedFrom.String()),
stringSetOrNull(o.assignedTo),
stringSliceOrNull(o.assignedTo),
)
}

Expand Down
2 changes: 1 addition & 1 deletion apstra/resource_freeform_system_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (o resourceFreeformSystem) render(rType, rName string) string {
o.hostname,
o.systemType,
stringOrNull(o.deployMode),
stringSetOrNull(o.tags),
stringSliceOrNull(o.tags),
)
}

Expand Down
Loading

0 comments on commit 05a9a4b

Please sign in to comment.