Skip to content

Commit

Permalink
code review, add group_id attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismarget-j committed Aug 30, 2024
1 parent a48b9e5 commit 7ba6971
Show file tree
Hide file tree
Showing 18 changed files with 460 additions and 326 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type dataSourceFreeformGroupGenerator struct {
}

func (o *dataSourceFreeformGroupGenerator) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_freeform_group_generator"
resp.TypeName = req.ProviderTypeName + "_freeform_resource_group_generator"
}

func (o *dataSourceFreeformGroupGenerator) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
Expand Down Expand Up @@ -87,7 +87,7 @@ func (o *dataSourceFreeformGroupGenerator) Read(ctx context.Context, req datasou
}

config.Id = types.StringValue(api.Id.String())
config.LoadApiData(ctx, api.Data)
config.LoadApiData(ctx, api.Data, &resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
}
Expand Down
1 change: 0 additions & 1 deletion apstra/freeform/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ func (o *Link) LoadApiData(ctx context.Context, in *apstra.FreeformLinkData, dia
}

o.Speed = types.StringValue(string(in.Speed))
o.Type = types.StringValue(in.Type.String())
o.Name = types.StringValue(in.Label)
o.Endpoints = newFreeformEndpointMap(ctx, in.Endpoints, diags) // safe to ignore diagnostic here
o.AggregateLinkId = types.StringPointerValue((*string)(in.AggregateLinkId))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,30 @@ package freeform

import (
"context"
"regexp"

"github.com/Juniper/apstra-go-sdk/apstra"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
dataSourceSchema "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"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"
"regexp"
)

type GroupGenerator struct {
BlueprintId types.String `tfsdk:"blueprint_id"`
Id types.String `tfsdk:"id"`
BlueprintId types.String `tfsdk:"blueprint_id"`
GroupId types.String `tfsdk:"group_id"`
Name types.String `tfsdk:"name"`
Scope types.String `tfsdk:"scope"`
}

func (o GroupGenerator) 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 Group lives.",
Required: true,
Validators: []validator.String{stringvalidator.LengthAtLeast(1)},
},
"id": dataSourceSchema.StringAttribute{
MarkdownDescription: "Populate this field to look up the Freeform Group Generator by ID. Required when `name` is omitted.",
Optional: true,
Expand All @@ -41,6 +38,16 @@ func (o GroupGenerator) DataSourceAttributes() map[string]dataSourceSchema.Attri
}...),
},
},
"blueprint_id": dataSourceSchema.StringAttribute{
MarkdownDescription: "Apstra Blueprint ID. Used to identify " +
"the Blueprint where the Group lives.",
Required: true,
Validators: []validator.String{stringvalidator.LengthAtLeast(1)},
},
"group_id": dataSourceSchema.StringAttribute{
MarkdownDescription: "Resource Group the Group Generator belongs to.",
Computed: true,
},
"name": dataSourceSchema.StringAttribute{
MarkdownDescription: "Populate this field to look up Group Generator by Name. Required when `id` is omitted.",
Optional: true,
Expand All @@ -57,16 +64,21 @@ func (o GroupGenerator) DataSourceAttributes() map[string]dataSourceSchema.Attri

func (o GroupGenerator) ResourceAttributes() map[string]resourceSchema.Attribute {
return map[string]resourceSchema.Attribute{
"id": resourceSchema.StringAttribute{
MarkdownDescription: "ID of the Group Generator within the Freeform Blueprint.",
Computed: true,
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
},
"blueprint_id": resourceSchema.StringAttribute{
MarkdownDescription: "Apstra Blueprint ID.",
Required: true,
Validators: []validator.String{stringvalidator.LengthAtLeast(1)},
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()},
},
"id": resourceSchema.StringAttribute{
MarkdownDescription: "ID of the Group Generator within the Freeform Blueprint.",
Computed: true,
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
"group_id": resourceSchema.StringAttribute{
MarkdownDescription: "Resource Group the Group Generator belongs to. Omit to create at the `root` level.",
Optional: true,
Validators: []validator.String{stringvalidator.LengthAtLeast(1)},
},
"name": resourceSchema.StringAttribute{
MarkdownDescription: "Freeform Group Generator name as shown in the Web UI.",
Expand All @@ -79,22 +91,24 @@ func (o GroupGenerator) ResourceAttributes() map[string]resourceSchema.Attribute
},
},
"scope": resourceSchema.StringAttribute{
MarkdownDescription: "Scope is a graph query which selects target nodes for which Groups should be generated.\n" +
"Example: `node('system', name='target', label=aeq('*prod*'))`",
MarkdownDescription: "Scope is a graph query which selects target nodes for which Group Generators should " +
"be generated.\nExample: `node('system', name='target', label=aeq('*prod*'))`",
Required: true,
Validators: []validator.String{stringvalidator.LengthAtLeast(1)},
},
}
}

func (o *GroupGenerator) Request(_ context.Context) *apstra.FreeformGroupGeneratorData {
func (o *GroupGenerator) Request(_ context.Context, _ *diag.Diagnostics) *apstra.FreeformGroupGeneratorData {
return &apstra.FreeformGroupGeneratorData{
Label: o.Name.ValueString(),
Scope: o.Scope.ValueString(),
ParentId: (*apstra.ObjectId)(o.GroupId.ValueStringPointer()),
Label: o.Name.ValueString(),
Scope: o.Scope.ValueString(),
}
}

func (o *GroupGenerator) LoadApiData(_ context.Context, in *apstra.FreeformGroupGeneratorData) {
func (o *GroupGenerator) LoadApiData(_ context.Context, in *apstra.FreeformGroupGeneratorData, _ *diag.Diagnostics) {
o.GroupId = types.StringPointerValue((*string)(in.ParentId))
o.Name = types.StringValue(in.Label)
o.Scope = types.StringValue(in.Scope)
}
125 changes: 0 additions & 125 deletions apstra/resource_freeform_group_generator_integration_test.go

This file was deleted.

1 change: 0 additions & 1 deletion apstra/resource_freeform_link_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ func (o resourceFreeformLink) testChecks(t testing.TB, rType, rName string, ipAl

// required and computed attributes can always be checked
result.append(t, "TestCheckResourceAttrSet", "id")
result.append(t, "TestCheckResourceAttr", "type", apstra.FFLinkTypeEthernet.String())
result.append(t, "TestCheckNoResourceAttr", "aggregate_link_id")
result.append(t, "TestCheckResourceAttr", "blueprint_id", o.blueprintId)
result.append(t, "TestCheckResourceAttr", "name", o.name)
Expand Down
2 changes: 1 addition & 1 deletion apstra/resource_freeform_resource_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func (o *resourceFreeformResourceGenerator) Delete(ctx context.Context, req reso
return
}

// Delete Config Template by calling API
// Delete Resource Generator by calling API
err = bp.DeleteResourceGenerator(ctx, apstra.ObjectId(state.Id.ValueString()))
if err != nil {
if utils.IsApstra404(err) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type resourceFreeformGroupGenerator struct {
}

func (o *resourceFreeformGroupGenerator) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_freeform_group_generator"
resp.TypeName = req.ProviderTypeName + "_freeform_resource_group_generator"
}

func (o *resourceFreeformGroupGenerator) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
Expand Down Expand Up @@ -67,7 +67,7 @@ func (o *resourceFreeformGroupGenerator) Create(ctx context.Context, req resourc
}

// Convert the plan into an API Request
request := plan.Request(ctx)
request := plan.Request(ctx, &resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
}
Expand All @@ -79,9 +79,8 @@ func (o *resourceFreeformGroupGenerator) Create(ctx context.Context, req resourc
return
}

plan.Id = types.StringValue(id.String())

// set state
plan.Id = types.StringValue(id.String())
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
}

Expand Down Expand Up @@ -113,7 +112,7 @@ func (o *resourceFreeformGroupGenerator) Read(ctx context.Context, req resource.
return
}

state.LoadApiData(ctx, api.Data)
state.LoadApiData(ctx, api.Data, &resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
}
Expand Down Expand Up @@ -151,7 +150,7 @@ func (o *resourceFreeformGroupGenerator) Update(ctx context.Context, req resourc
}

// Convert the plan into an API Request
request := plan.Request(ctx)
request := plan.Request(ctx, &resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
}
Expand Down Expand Up @@ -193,7 +192,7 @@ func (o *resourceFreeformGroupGenerator) Delete(ctx context.Context, req resourc
return
}

// Delete Config Template by calling API
// Delete Resource Generator by calling API
err = bp.DeleteGroupGenerator(ctx, apstra.ObjectId(state.Id.ValueString()))
if err != nil {
if utils.IsApstra404(err) {
Expand Down
Loading

0 comments on commit 7ba6971

Please sign in to comment.