Skip to content

Commit

Permalink
finish alloc_group integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bwJuniper committed Aug 1, 2024
1 parent fdd52f2 commit 2510db7
Show file tree
Hide file tree
Showing 17 changed files with 689 additions and 41 deletions.
68 changes: 46 additions & 22 deletions apstra/blueprint/freeform_alloc_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package blueprint
import (
"context"
"fmt"
"github.com/Juniper/terraform-provider-apstra/apstra/utils"
"github.com/hashicorp/terraform-plugin-framework-validators/setvalidator"
"regexp"
"strings"

"github.com/Juniper/apstra-go-sdk/apstra"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
Expand All @@ -22,19 +25,19 @@ type FreeformAllocGroup struct {
Id types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Type types.String `tfsdk:"type"`
PoolId types.String `tfsdk:"pool_id"`
PoolIds types.Set `tfsdk:"pool_ids"`
}

func (o FreeformAllocGroup) DataSourceAttributes() map[string]dataSourceSchema.Attribute {
return map[string]dataSourceSchema.Attribute{
"blueprint_id": dataSourceSchema.StringAttribute{
MarkdownDescription: "Apstra Blueprint ID. Used to identify " +
"the Blueprint where the System lives.",
"the Blueprint where the Allocation Group lives.",
Required: true,
Validators: []validator.String{stringvalidator.LengthAtLeast(1)},
},
"id": dataSourceSchema.StringAttribute{
MarkdownDescription: "Populate this field to look up the Freeform System by ID. Required when `name` is omitted.",
MarkdownDescription: "Populate this field to look up the Allocation Group by ID. Required when `name` is omitted.",
Optional: true,
Computed: true,
Validators: []validator.String{
Expand All @@ -46,18 +49,20 @@ func (o FreeformAllocGroup) DataSourceAttributes() map[string]dataSourceSchema.A
},
},
"name": dataSourceSchema.StringAttribute{
MarkdownDescription: "Populate this field to look up System by Name. Required when `id` is omitted.",
MarkdownDescription: "Populate this field to look up the Allocation Group by Name. Required when `id` is omitted.",
Optional: true,
Computed: true,
Validators: []validator.String{stringvalidator.LengthAtLeast(1)},
},
"type": dataSourceSchema.StringAttribute{
MarkdownDescription: "type of the Resource Pool, either Internal or External",
Computed: true,
MarkdownDescription: fmt.Sprintf("type of the Allocation Group, must be one of :\n - `" +
strings.Join(utils.AllResourcePoolTypes(), "`\n - `") + "`\n"),
Computed: true,
},
"pool_id": dataSourceSchema.StringAttribute{
"pool_ids": dataSourceSchema.SetAttribute{
MarkdownDescription: "Pool ID assigned to the allocation group",
Computed: true,
ElementType: types.StringType,
},
}
}
Expand All @@ -71,40 +76,59 @@ func (o FreeformAllocGroup) ResourceAttributes() map[string]resourceSchema.Attri
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()},
},
"id": resourceSchema.StringAttribute{
MarkdownDescription: "ID of the Freeform System.",
MarkdownDescription: "ID of the Freeform Allocation Group.",
Computed: true,
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
},
"name": resourceSchema.StringAttribute{
MarkdownDescription: "Freeform System name as shown in the Web UI.",
MarkdownDescription: "Freeform Allocation Group name as shown in the Web UI.",
Required: true,
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()},
Validators: []validator.String{
stringvalidator.RegexMatches(regexp.MustCompile("^[a-zA-Z0-9.-_]+$"), "name may consist only of the following characters : a-zA-Z0-9.-_")},
stringvalidator.RegexMatches(regexp.MustCompile("^[a-zA-Z0-9.-_]+$"),
"name may consist only of the following characters : a-zA-Z0-9.-_"),
},
},
"type": resourceSchema.StringAttribute{
MarkdownDescription: fmt.Sprintf("Type of the System. Must be one of `%s` or `%s`", apstra.SystemTypeInternal, apstra.SystemTypeExternal),
Required: true,
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()},
Validators: []validator.String{stringvalidator.OneOf(apstra.SystemTypeInternal.String(), apstra.SystemTypeExternal.String())},
MarkdownDescription: fmt.Sprintf("type of the Allocation Group, must be one of :\n - `" +
strings.Join(utils.AllResourcePoolTypes(), "`\n - `") + "`\n"),
Required: true,
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()},
Validators: []validator.String{stringvalidator.OneOf(utils.AllFFResourceTypes()...)},
},
"pool_id": resourceSchema.StringAttribute{
MarkdownDescription: "ID (usually serial number) of the Managed Device to associate with this System",
Optional: true,
Validators: []validator.String{stringvalidator.LengthAtLeast(1)},
"pool_ids": resourceSchema.SetAttribute{
MarkdownDescription: "ID of the Pool to associate with this Allocation Group",
ElementType: types.StringType,
Required: true,
Validators: []validator.Set{setvalidator.SizeAtLeast(1)},
},
}
}

func (o *FreeformAllocGroup) Request(ctx context.Context, diags *diag.Diagnostics) *apstra.FreeformAllocGroupData {
// unpack
var allocGroupType apstra.ResourcePoolType
err := utils.ApiStringerFromFriendlyString(&allocGroupType, o.Type.ValueString())
if err != nil {
diags.AddError(fmt.Sprintf("error parsing type %q", o.Type.ValueString()), err.Error())
}

var poolIds []apstra.ObjectId
diags.Append(o.PoolIds.ElementsAs(ctx, &poolIds, false)...)
if diags.HasError() {
return nil
}

return &apstra.FreeformAllocGroupData{
Name: "",
Type: apstra.ResourcePoolType{},
PoolIds: nil,
Name: o.Name.ValueString(),
Type: allocGroupType,
PoolIds: poolIds,
}
}

func (o *FreeformAllocGroup) LoadApiData(ctx context.Context, in *apstra.FreeformAllocGroupData, diags *diag.Diagnostics) {
// pack
o.Name = types.StringValue(in.Name)
o.Type = types.StringValue(in.Type.String())
o.Type = types.StringValue(utils.StringersToFriendlyString(in.Type))
o.PoolIds = utils.SetValueOrNull(ctx, types.StringType, in.PoolIds, diags)
}
4 changes: 2 additions & 2 deletions apstra/blueprint/freeform_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ func (o FreeformResource) ResourceAttributes() map[string]resourceSchema.Attribu
},
"type": resourceSchema.StringAttribute{
MarkdownDescription: "type of the Resource, must be one of :\n - `" +
strings.Join(utils.AllResourceTypes(), "`\n - `") + "`\n",
strings.Join(utils.AllFFResourceTypes(), "`\n - `") + "`\n",
Optional: true,
Validators: []validator.String{stringvalidator.OneOf(utils.AllResourceTypes()...)},
Validators: []validator.String{stringvalidator.OneOf(utils.AllFFResourceTypes()...)},
},
"integer_value": resourceSchema.Int64Attribute{
MarkdownDescription: fmt.Sprintf("Value used by integer type resources (`%s`, `%s`, `%s`, `%s`). "+
Expand Down
4 changes: 2 additions & 2 deletions apstra/data_source_freeform_alloc_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type dataSourceFreeformAllocGroup struct {
}

func (o *dataSourceFreeformAllocGroup) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_freeform_system"
resp.TypeName = req.ProviderTypeName + "_freeform_alloc_group"
}

func (o *dataSourceFreeformAllocGroup) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
Expand All @@ -32,7 +32,7 @@ func (o *dataSourceFreeformAllocGroup) Configure(ctx context.Context, req dataso

func (o *dataSourceFreeformAllocGroup) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: docCategoryFreeform + "This data source provides details of a specific Freeform System.\n\n" +
MarkdownDescription: docCategoryFreeform + "This data source provides details of a Freeform Allocation Group.\n\n" +
"At least one optional attribute is required.",
Attributes: blueprint.FreeformAllocGroup{}.DataSourceAttributes(),
}
Expand Down
1 change: 1 addition & 0 deletions apstra/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var (
ResourceFreeformPropertySet = resourceFreeformPropertySet{}
ResourceFreeformResourceGroup = resourceFreeformResourceGroup{}
ResourceFreeformResource = resourceFreeformResource{}
ResourceFreeformAllocGroup = resourceFreeformAllocGroup{}
ResourceFreeformSystem = resourceFreeformSystem{}
ResourceIntegerPool = resourceIntegerPool{}
ResourceIpv4Pool = resourceIpv4Pool{}
Expand Down
2 changes: 2 additions & 0 deletions apstra/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@ func (p *Provider) DataSources(_ context.Context) []func() datasource.DataSource
func() datasource.DataSource { return &dataSourceDatacenterVirtualNetwork{} },
func() datasource.DataSource { return &dataSourceDatacenterVirtualNetworks{} },
func() datasource.DataSource { return &dataSourceDeviceConfig{} },
func() datasource.DataSource { return &dataSourceFreeformAllocGroup{} },
func() datasource.DataSource { return &dataSourceFreeformConfigTemplate{} },
func() datasource.DataSource { return &dataSourceFreeformLink{} },
func() datasource.DataSource { return &dataSourceFreeformPropertySet{} },
Expand Down Expand Up @@ -610,6 +611,7 @@ func (p *Provider) Resources(_ context.Context) []func() resource.Resource {
func() resource.Resource { return &resourceDatacenterIpLinkAddressing{} },
func() resource.Resource { return &resourceDatacenterVirtualNetwork{} },
func() resource.Resource { return &resourceDeviceAllocation{} },
func() resource.Resource { return &resourceFreeformAllocGroup{} },
func() resource.Resource { return &resourceFreeformConfigTemplate{} },
func() resource.Resource { return &resourceFreeformLink{} },
func() resource.Resource { return &resourceFreeformPropertySet{} },
Expand Down
20 changes: 10 additions & 10 deletions apstra/resource_freeform_alloc_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type resourceFreeformAllocGroup struct {
}

func (o *resourceFreeformAllocGroup) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_freeform_system"
resp.TypeName = req.ProviderTypeName + "_freeform_alloc_group"
}

func (o *resourceFreeformAllocGroup) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
Expand All @@ -32,7 +32,7 @@ func (o *resourceFreeformAllocGroup) Configure(ctx context.Context, req resource

func (o *resourceFreeformAllocGroup) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: docCategoryFreeform + "This resource creates a System in a Freeform Blueprint.",
MarkdownDescription: docCategoryFreeform + "This resource creates an Allocation Group in a Freeform Blueprint.",
Attributes: blueprint.FreeformAllocGroup{}.ResourceAttributes(),
}
}
Expand Down Expand Up @@ -71,9 +71,9 @@ func (o *resourceFreeformAllocGroup) Create(ctx context.Context, req resource.Cr
return
}

id, err := bp.CreateSystem(ctx, request)
id, err := bp.CreateAllocGroup(ctx, request)
if err != nil {
resp.Diagnostics.AddError("error creating new System", err.Error())
resp.Diagnostics.AddError("error creating new Allocation Group", err.Error())
return
}

Expand Down Expand Up @@ -101,13 +101,13 @@ func (o *resourceFreeformAllocGroup) Read(ctx context.Context, req resource.Read
return
}

api, err := bp.GetSystem(ctx, apstra.ObjectId(state.Id.ValueString()))
api, err := bp.GetAllocGroup(ctx, apstra.ObjectId(state.Id.ValueString()))
if err != nil {
if utils.IsApstra404(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("Error retrieving Freeform System", err.Error())
resp.Diagnostics.AddError(fmt.Sprintf("Error retrieving Freeform Allocation Group %s", state.Id), err.Error())
return
}

Expand Down Expand Up @@ -154,9 +154,9 @@ func (o *resourceFreeformAllocGroup) Update(ctx context.Context, req resource.Up
}

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

Expand Down Expand Up @@ -191,12 +191,12 @@ func (o *resourceFreeformAllocGroup) Delete(ctx context.Context, req resource.De
}

// Delete Config Template by calling API
err = bp.DeleteSystem(ctx, apstra.ObjectId(state.Id.ValueString()))
err = bp.DeleteAllocGroup(ctx, apstra.ObjectId(state.Id.ValueString()))
if err != nil {
if utils.IsApstra404(err) {
return // 404 is okay
}
resp.Diagnostics.AddError("error deleting Freeform System", err.Error())
resp.Diagnostics.AddError("error deleting Freeform Allocation Group", err.Error())
return
}
}
Expand Down
Loading

0 comments on commit 2510db7

Please sign in to comment.