-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enable widget and dashboard creation
- Loading branch information
1 parent
75f64bc
commit 570f704
Showing
22 changed files
with
1,012 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package tfapstra | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
testutils "github.com/Juniper/terraform-provider-apstra/apstra/test_utils" | ||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"testing" | ||
) | ||
|
||
const ( | ||
resourceIbaProbeHCL = ` | ||
resource "apstra_iba_probe" "p_device_health" { | ||
blueprint_id = "%s" | ||
predefined_probe_id = "device_health" | ||
probe_config = jsonencode( | ||
{ | ||
"max_cpu_utilization": 80, | ||
"max_memory_utilization": 80, | ||
"max_disk_utilization": 80, | ||
"duration": 660, | ||
"threshold_duration": 360, | ||
"history_duration": 604800 | ||
} | ||
) | ||
} | ||
` | ||
) | ||
|
||
func TestAccResourceProbe(t *testing.T) { | ||
ctx := context.Background() | ||
bpClient, bpDelete, err := testutils.MakeOrFindBlueprint(ctx, "1bb57", testutils.BlueprintA) | ||
if err != nil { | ||
t.Fatal(errors.Join(err, bpDelete(ctx))) | ||
} | ||
defer func() { | ||
err = bpDelete(ctx) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
}() | ||
|
||
resource.Test(t, resource.TestCase{ | ||
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
// Create and Read testing | ||
{ | ||
Config: insecureProviderConfigHCL + fmt.Sprintf(resourceIbaProbeHCL, bpClient.Id()), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
// Verify ID has any value set | ||
resource.TestCheckResourceAttrSet("apstra_iba_probe.p_device_health", "id"), resource.TestCheckResourceAttr("apstra_iba_probe.p_device_health", "name", "Device System Health")), | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
package tfapstra | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/Juniper/apstra-go-sdk/apstra" | ||
"github.com/Juniper/terraform-provider-apstra/apstra/iba" | ||
"github.com/Juniper/terraform-provider-apstra/apstra/utils" | ||
"github.com/hashicorp/terraform-plugin-framework/resource" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
var _ resource.ResourceWithConfigure = &resourceIbaWidget{} | ||
|
||
type resourceIbaWidget struct { | ||
client *apstra.Client | ||
} | ||
|
||
func (o *resourceIbaWidget) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { | ||
resp.TypeName = req.ProviderTypeName + "_iba_widget" | ||
} | ||
|
||
func (o *resourceIbaWidget) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { | ||
o.client = ResourceGetClient(ctx, req, resp) | ||
} | ||
|
||
func (o *resourceIbaWidget) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
MarkdownDescription: "This resource creates a IBA Widget.", | ||
Attributes: iba.IbaWidget{}.ResourceAttributes(), | ||
} | ||
} | ||
|
||
func (o *resourceIbaWidget) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { | ||
// Retrieve values from plan | ||
var plan iba.IbaWidget | ||
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
// create a blueprint client | ||
bpClient, err := o.client.NewTwoStageL3ClosClient(ctx, apstra.ObjectId(plan.BlueprintId.ValueString())) | ||
if err != nil { | ||
resp.Diagnostics.AddError("failed to create blueprint client", err.Error()) | ||
return | ||
} | ||
|
||
// Convert the plan into an API Request | ||
probeReq := plan.Request(ctx, &resp.Diagnostics) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
id, err := bpClient.CreateIbaWidget(ctx, probeReq) | ||
if err != nil { | ||
resp.Diagnostics.AddError("failed to create Iba Probe", err.Error()) | ||
return | ||
} | ||
plan.Id = types.StringValue(id.String()) | ||
|
||
// Set state | ||
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...) | ||
} | ||
|
||
func (o *resourceIbaWidget) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { | ||
var state iba.IbaWidget | ||
resp.Diagnostics.Append(req.State.Get(ctx, &state)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
bpClient, err := o.client.NewTwoStageL3ClosClient(ctx, apstra.ObjectId(state.BlueprintId.ValueString())) | ||
if err != nil { | ||
if utils.IsApstra404(err) { | ||
resp.State.RemoveResource(ctx) | ||
return | ||
} | ||
resp.Diagnostics.AddError("failed to create blueprint client", err.Error()) | ||
return | ||
} | ||
|
||
api, err := bpClient.GetIbaWidget(ctx, apstra.ObjectId(state.Id.ValueString())) | ||
if err != nil { | ||
if utils.IsApstra404(err) { | ||
resp.State.RemoveResource(ctx) | ||
return | ||
} | ||
resp.Diagnostics.AddError("Failed to Read IBA Dashboard", err.Error()) | ||
return | ||
} | ||
|
||
// create new state object | ||
state.LoadApiData(ctx, api, &resp.Diagnostics) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
// set state | ||
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...) | ||
} | ||
|
||
// Update resource | ||
func (o *resourceIbaWidget) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { | ||
// Get plan values | ||
var plan iba.IbaWidget | ||
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
widgetReq := plan.Request(ctx, &resp.Diagnostics) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
bpClient, err := o.client.NewTwoStageL3ClosClient(ctx, apstra.ObjectId(plan.BlueprintId.ValueString())) | ||
if err != nil { | ||
resp.Diagnostics.AddError("failed to create blueprint client", err.Error()) | ||
return | ||
} | ||
|
||
// Update IBA Widget | ||
err = bpClient.UpdateIbaWidget(ctx, apstra.ObjectId(plan.Id.ValueString()), widgetReq) | ||
if err != nil { | ||
resp.Diagnostics.AddError("plan", fmt.Sprintf("%q", plan)) | ||
resp.Diagnostics.AddError("error updating IBA Dashboard plan", err.Error()) | ||
return | ||
} | ||
|
||
api, err := bpClient.GetIbaWidget(ctx, apstra.ObjectId(plan.Id.ValueString())) | ||
if err != nil { | ||
resp.Diagnostics.AddError("Failed to Read IBA Dashboard", err.Error()) | ||
return | ||
} | ||
plan.LoadApiData(ctx, api, &resp.Diagnostics) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
// Set state | ||
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...) | ||
} | ||
|
||
// Delete resource | ||
func (o *resourceIbaWidget) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { | ||
var state iba.IbaWidget | ||
resp.Diagnostics.Append(req.State.Get(ctx, &state)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
bpClient, err := o.client.NewTwoStageL3ClosClient(ctx, apstra.ObjectId(state.BlueprintId.ValueString())) | ||
if err != nil { | ||
if utils.IsApstra404(err) { | ||
return // 404 is okay | ||
} | ||
resp.Diagnostics.AddError("failed to create blueprint client", err.Error()) | ||
return | ||
} | ||
|
||
// Delete IBA Probe by calling API | ||
err = bpClient.DeleteIbaWidget(ctx, apstra.ObjectId(state.Id.ValueString())) | ||
if err != nil { | ||
if utils.IsApstra404(err) { | ||
return // 404 is okay | ||
} | ||
resp.Diagnostics.AddError("error deleting IBA Probe", err.Error()) | ||
return | ||
} | ||
} |
Oops, something went wrong.