From dd1407e1329e1d80dcbd73939199609729d82f62 Mon Sep 17 00:00:00 2001 From: Kobi Samoray Date: Fri, 17 Nov 2023 09:26:39 +0200 Subject: [PATCH] Implement ODS runbook report data source Signed-off-by: Kobi Samoray --- ...xt_policy_ods_runbook_invocation_report.go | 103 ++++++++++++++++++ ...licy_ods_runbook_invocation_report_test.go | 42 +++++++ nsxt/provider.go | 1 + ...ds_runbook_invocation_report.html.markdown | 37 +++++++ 4 files changed, 183 insertions(+) create mode 100644 nsxt/data_source_nsxt_policy_ods_runbook_invocation_report.go create mode 100644 nsxt/data_source_nsxt_policy_ods_runbook_invocation_report_test.go create mode 100644 website/docs/d/policy_ods_runbook_invocation_report.html.markdown diff --git a/nsxt/data_source_nsxt_policy_ods_runbook_invocation_report.go b/nsxt/data_source_nsxt_policy_ods_runbook_invocation_report.go new file mode 100644 index 000000000..ae2aea2f9 --- /dev/null +++ b/nsxt/data_source_nsxt_policy_ods_runbook_invocation_report.go @@ -0,0 +1,103 @@ +/* Copyright © 2023 VMware, Inc. All Rights Reserved. + SPDX-License-Identifier: MPL-2.0 */ + +package nsxt + +import ( + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra/sha/runbook_invocations" +) + +func dataSourceNsxtPolicyODSRunbookInvocationReport() *schema.Resource { + return &schema.Resource{ + Read: dataSourceNsxtPolicyODSPRunbookInvocationReportRead, + + Schema: map[string]*schema.Schema{ + "invocation_id": { + Type: schema.TypeString, + Required: true, + Description: "UUID of runbook invocation", + }, + "target_node": { + Type: schema.TypeString, + Computed: true, + Optional: true, + Description: "Identifier of an appliance node or transport node", + }, + "error_detail": { + Type: schema.TypeString, + Computed: true, + Optional: true, + Description: "The report error detail", + }, + "invalid_reason": { + Type: schema.TypeString, + Computed: true, + Optional: true, + Description: "Invalid report reason", + }, + "recommendation_code": { + Type: schema.TypeInt, + Computed: true, + Optional: true, + Description: "Online Diagnostic System recommendation code", + }, + "recommendation_message": { + Type: schema.TypeString, + Computed: true, + Optional: true, + Description: "Online Diagnostic System recommendation message", + }, + "result_code": { + Type: schema.TypeInt, + Computed: true, + Optional: true, + Description: "Online Diagnostic System result code", + }, + "result_message": { + Type: schema.TypeString, + Computed: true, + Optional: true, + Description: "Online Diagnostic System result message", + }, + "request_status": { + Type: schema.TypeString, + Computed: true, + Optional: true, + Description: "Request status of a runbook invocation", + }, + "operation_state": { + Type: schema.TypeString, + Computed: true, + Optional: true, + Description: "Operation state of a runbook invocation on the target node", + }, + }, + } +} + +func dataSourceNsxtPolicyODSPRunbookInvocationReportRead(d *schema.ResourceData, m interface{}) error { + connector := getPolicyConnector(m) + invocationID := d.Get("invocation_id").(string) + client := runbook_invocations.NewReportClient(connector) + + obj, err := client.Get(invocationID) + if err != nil { + return handleDataSourceReadError(d, "OdsRunbookInvocationReport", invocationID, err) + } + + d.SetId(invocationID) + d.Set("target_node", obj.TargetNode) + d.Set("error_detail", obj.ErrorDetail) + d.Set("invalid_reason", obj.InvalidReason) + d.Set("recommendation_code", obj.RecommendationCode) + d.Set("recommendation_message", obj.RecommendationMessage) + d.Set("result_code", obj.ResultCode) + d.Set("result_message", obj.ResultMessage) + if obj.Status != nil { + d.Set("request_status", obj.Status.RequestStatus) + d.Set("operation_state", obj.Status.OperationState) + } + + return nil +} diff --git a/nsxt/data_source_nsxt_policy_ods_runbook_invocation_report_test.go b/nsxt/data_source_nsxt_policy_ods_runbook_invocation_report_test.go new file mode 100644 index 000000000..2478dc02b --- /dev/null +++ b/nsxt/data_source_nsxt_policy_ods_runbook_invocation_report_test.go @@ -0,0 +1,42 @@ +/* Copyright © 2023 VMware, Inc. All Rights Reserved. + SPDX-License-Identifier: MPL-2.0 */ + +package nsxt + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccResourceNsxtPolicyODSRunbookInvocationReport_basic(t *testing.T) { + name := getAccTestResourceName() + testResourceName := "data.nsxt_policy_ods_runbook_invocation_report.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { + testAccOnlyLocalManager(t) + testAccPreCheck(t) + testAccNSXVersion(t, "4.2.0") + testAccEnvDefined(t, "NSXT_TEST_HOST_TRANSPORT_NODE") + }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccNsxtPolicyODSRunbookInvocationReportReadTemplate(name), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet(testResourceName, "target_node"), + resource.TestCheckResourceAttrSet(testResourceName, "request_status"), + resource.TestCheckResourceAttrSet(testResourceName, "operation_state"), + ), + }, + }, + }) +} + +func testAccNsxtPolicyODSRunbookInvocationReportReadTemplate(name string) string { + return testAccNsxtPolicyODSRunbookInvocationCreateTemplate(name, "ControllerConn", "") + ` +data "nsxt_policy_ods_runbook_invocation_report" "test" { + invocation_id = nsxt_policy_ods_runbook_invocation.test.id +}` +} diff --git a/nsxt/provider.go b/nsxt/provider.go index c76926cdd..ab7f965f2 100644 --- a/nsxt/provider.go +++ b/nsxt/provider.go @@ -323,6 +323,7 @@ func Provider() *schema.Provider { "nsxt_upgrade_prepare_ready": dataSourceNsxtUpgradePrepareReady(), "nsxt_policy_vtep_ha_host_switch_profile": dataSourceNsxtVtepHAHostSwitchProfile(), "nsxt_policy_ods_pre_defined_runbook": dataSourceNsxtPolicyODSPreDefinedRunbook(), + "nsxt_policy_ods_runbook_invocation_report": dataSourceNsxtPolicyODSRunbookInvocationReport(), }, ResourcesMap: map[string]*schema.Resource{ diff --git a/website/docs/d/policy_ods_runbook_invocation_report.html.markdown b/website/docs/d/policy_ods_runbook_invocation_report.html.markdown new file mode 100644 index 000000000..20c100a8b --- /dev/null +++ b/website/docs/d/policy_ods_runbook_invocation_report.html.markdown @@ -0,0 +1,37 @@ +--- +subcategory: "ODS Runbook" +layout: "nsxt" +page_title: "NSXT: nsxt_policy_ods_runbook_invocation_report" +description: Policy ODS runbook invocation report data source. +--- + +# nsxt_policy_ods_runbook_invocation_report + +This data source provides information about policy ODS runbook invocation report on NSX. +This data source is applicable to NSX Policy Manager. + +## Example Usage + +```hcl +data "nsxt_policy_ods_runbook_invocation_report" "test" { + invocation_id = nsxt_policy_ods_runbook_invocation.test.id +} +``` + +## Argument Reference + +* `invocation_id` - (Required) UUID of runbook invocation. + +## Attributes Reference + +In addition to arguments listed above, the following attributes are exported: + +* `target_node` - Identifier of an appliance node or transport node. +* `error_detail` - The report error detail. +* `invalid_reason` - Invalid report reason. +* `recommendation_code` - Online Diagnostic System recommendation code. +* `recommendation_message` - Online Diagnostic System recommendation message. +* `result_code` - Online Diagnostic System result code. +* `result_message` - Online Diagnostic System result message. +* `request_status` - Request status of a runbook invocation. +* `operation_state` - Operation state of a runbook invocation on the target node.