Skip to content

Commit

Permalink
address label -> name and other code review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rajagopalans committed Oct 4, 2023
1 parent 5f0c6f2 commit fb43639
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 20 deletions.
8 changes: 4 additions & 4 deletions apstra/blueprint/iba_widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
type IbaWidget struct {
BlueprintId types.String `tfsdk:"blueprint_id"`
Id types.String `tfsdk:"id"`
Label types.String `tfsdk:"label"`
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
}

Expand All @@ -33,11 +33,11 @@ func (o IbaWidget) DataSourceAttributes() map[string]dataSourceSchema.Attribute
stringvalidator.LengthAtLeast(1),
stringvalidator.ExactlyOneOf(path.Expressions{
path.MatchRelative(),
path.MatchRoot("label"),
path.MatchRoot("name"),
}...),
},
},
"label": dataSourceSchema.StringAttribute{
"name": dataSourceSchema.StringAttribute{
MarkdownDescription: "Populate this field to look up a IBA Widget by name. Required when `id` is omitted.",
Optional: true,
Computed: true,
Expand All @@ -54,6 +54,6 @@ func (o IbaWidget) DataSourceAttributes() map[string]dataSourceSchema.Attribute

func (o *IbaWidget) LoadApiData(ctx context.Context, in *apstra.IbaWidget, _ *diag.Diagnostics) {
o.Id = types.StringValue(in.Id.String())
o.Label = types.StringValue(in.Data.Label)
o.Name = types.StringValue(in.Data.Label)
o.Description = types.StringValue(in.Data.Description)
}
8 changes: 4 additions & 4 deletions apstra/data_source_datacenter_iba_widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ func (o *dataSourceDatacenterIbaWidget) Read(ctx context.Context, req datasource

var api *apstra.IbaWidget
switch {
case !config.Label.IsNull():
api, err = bpClient.GetIbaWidgetByLabel(ctx, config.Label.ValueString())
case !config.Name.IsNull():
api, err = bpClient.GetIbaWidgetByLabel(ctx, config.Name.ValueString())
if err != nil {
if utils.IsApstra404(err) {
resp.Diagnostics.AddAttributeError(
path.Root("name"),
"IBA widget not found",
fmt.Sprintf("IBA Widget with label %s not found", config.Label))
fmt.Sprintf("IBA Widget with name %s not found", config.Name))
return
}
resp.Diagnostics.AddAttributeError(
Expand All @@ -80,7 +80,7 @@ func (o *dataSourceDatacenterIbaWidget) Read(ctx context.Context, req datasource
return
}
resp.Diagnostics.AddAttributeError(
path.Root("name"), "Failed reading IBA Widgett", err.Error(),
path.Root("name"), "Failed reading IBA Widget", err.Error(),
)
return
}
Expand Down
6 changes: 3 additions & 3 deletions apstra/data_source_datacenter_iba_widget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const (
dataSourceDatacenterIbaWidgetTemplateByNameHCL = `
data "apstra_datacenter_iba_widget" "test" {
blueprint_id = "%s"
label = "%s"
name = "%s"
}
`

Expand Down Expand Up @@ -62,7 +62,7 @@ func TestAccDataSourceDatacenterIbaWidget(t *testing.T) {
string(bpClient.Id()), string(widgetIdA)),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.apstra_datacenter_iba_widget.test", "id", widgetIdA.String()),
resource.TestCheckResourceAttr("data.apstra_datacenter_iba_widget.test", "label",
resource.TestCheckResourceAttr("data.apstra_datacenter_iba_widget.test", "name",
widgetDataA.Label),
),
},
Expand All @@ -72,7 +72,7 @@ func TestAccDataSourceDatacenterIbaWidget(t *testing.T) {
string(bpClient.Id()), widgetDataA.Label),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.apstra_datacenter_iba_widget.test", "id", widgetIdA.String()),
resource.TestCheckResourceAttr("data.apstra_datacenter_iba_widget.test", "label",
resource.TestCheckResourceAttr("data.apstra_datacenter_iba_widget.test", "name",
widgetDataA.Label),
),
},
Expand Down
14 changes: 5 additions & 9 deletions apstra/data_source_datacenter_iba_widgets.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/Juniper/apstra-go-sdk/apstra"
"github.com/Juniper/terraform-provider-apstra/apstra/utils"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
Expand Down Expand Up @@ -72,16 +73,11 @@ func (o *dataSourceDatacenterIbaWidgets) Read(ctx context.Context, req datasourc
return
}

ids := make([]apstra.ObjectId, len(ws))
for i, j := range ws {
ids[i] = j.Id
}

idSet, diags := types.SetValueFrom(ctx, types.StringType, ids)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
ids := make([]attr.Value, len(ws))
for i, id := range ws {
ids[i] = types.StringValue(id.Id.String())
}
idSet := types.SetValueMust(types.StringType, ids)

// create new state object
state := struct {
Expand Down

0 comments on commit fb43639

Please sign in to comment.