Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement ODS runbook report data source #1035

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions nsxt/data_source_nsxt_policy_ods_pre_defined_runbook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/* Copyright © 2023 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: MPL-2.0 */

package nsxt

import (
"fmt"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra/sha"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
)

func dataSourceNsxtPolicyODSPreDefinedRunbook() *schema.Resource {
return &schema.Resource{
Read: dataSourceNsxtPolicyODSPreDefinedRunbookRead,

Schema: map[string]*schema.Schema{
"id": getDataSourceIDSchema(),
"display_name": getDataSourceDisplayNameSchema(),
"description": getDataSourceDescriptionSchema(),
"path": getPathSchema(),
},
}
}

func dataSourceNsxtPolicyODSPreDefinedRunbookRead(d *schema.ResourceData, m interface{}) error {
connector := getPolicyConnector(m)
client := sha.NewPreDefinedRunbooksClient(connector)

objID := d.Get("id").(string)
objName := d.Get("display_name").(string)
var obj model.OdsPredefinedRunbook
if objID != "" {
// Get by id
objGet, err := client.Get(objID)
if err != nil {
return handleDataSourceReadError(d, "OdsPredefinedRunbook", objID, err)
}
obj = objGet
} else if objName == "" {
return fmt.Errorf("error obtaining OdsPredefinedRunbook ID or name during read")
} else {
// Get by full name/prefix
objList, err := client.List(nil, nil, nil, nil, nil, nil)
if err != nil {
return handleListError("OdsPredefinedRunbook", err)
}
// go over the list to find the correct one (prefer a perfect match. If not - prefix match)
var perfectMatch []model.OdsPredefinedRunbook
var prefixMatch []model.OdsPredefinedRunbook
for _, objInList := range objList.Results {
if strings.HasPrefix(*objInList.DisplayName, objName) {
prefixMatch = append(prefixMatch, objInList)
}
if *objInList.DisplayName == objName {
perfectMatch = append(perfectMatch, objInList)
}
}
if len(perfectMatch) > 0 {
if len(perfectMatch) > 1 {
return fmt.Errorf("found multiple OdsPredefinedRunbook with name '%s'", objName)
}
obj = perfectMatch[0]
} else if len(prefixMatch) > 0 {
if len(prefixMatch) > 1 {
return fmt.Errorf("found multiple OdsPredefinedRunbooks with name starting with '%s'", objName)
}
obj = prefixMatch[0]
} else {
return fmt.Errorf("OdsPredefinedRunbook with name '%s' was not found", objName)
}
}

d.SetId(*obj.Id)
d.Set("display_name", obj.DisplayName)
d.Set("description", obj.Description)
d.Set("path", obj.Path)

return nil
}
41 changes: 41 additions & 0 deletions nsxt/data_source_nsxt_policy_ods_pre_defined_runbook_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* Copyright © 2023 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: MPL-2.0 */

package nsxt

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccDataSourceNsxtPolicyODSPredefinedRunbook_basic(t *testing.T) {
name := "ControllerConn"
testResourceName := "data.nsxt_policy_ods_pre_defined_runbook.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccOnlyLocalManager(t)
testAccPreCheck(t)
testAccNSXVersion(t, "4.1.0")
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccNsxtPolicyODSPredefinedRunbookReadTemplate(name),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(testResourceName, "display_name", name),
resource.TestCheckResourceAttrSet(testResourceName, "path"),
),
},
},
})
}

func testAccNsxtPolicyODSPredefinedRunbookReadTemplate(name string) string {
return fmt.Sprintf(`
data "nsxt_policy_ods_pre_defined_runbook" "test" {
display_name = "%s"
}`, name)
}
103 changes: 103 additions & 0 deletions nsxt/data_source_nsxt_policy_ods_runbook_invocation_report.go
Original file line number Diff line number Diff line change
@@ -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
}
42 changes: 42 additions & 0 deletions nsxt/data_source_nsxt_policy_ods_runbook_invocation_report_test.go
Original file line number Diff line number Diff line change
@@ -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
}`
}
3 changes: 3 additions & 0 deletions nsxt/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,8 @@ func Provider() *schema.Provider {
"nsxt_policy_vtep_ha_host_switch_profile": dataSourceNsxtVtepHAHostSwitchProfile(),
"nsxt_policy_distributed_flood_protection_profile": dataSourceNsxtPolicyDistributedFloodProtectionProfile(),
"nsxt_policy_gateway_flood_protection_profile": dataSourceNsxtPolicyGatewayFloodProtectionProfile(),
"nsxt_policy_ods_pre_defined_runbook": dataSourceNsxtPolicyODSPreDefinedRunbook(),
"nsxt_policy_ods_runbook_invocation_report": dataSourceNsxtPolicyODSRunbookInvocationReport(),
},

ResourcesMap: map[string]*schema.Resource{
Expand Down Expand Up @@ -492,6 +494,7 @@ func Provider() *schema.Provider {
"nsxt_policy_gateway_flood_protection_profile_binding": resourceNsxtPolicyGatewayFloodProtectionProfileBinding(),
"nsxt_policy_compute_sub_cluster": resourceNsxtPolicyComputeSubCluster(),
"nsxt_policy_tier0_inter_vrf_routing": resourceNsxtPolicyTier0InterVRFRouting(),
"nsxt_policy_ods_runbook_invocation": resourceNsxtPolicyODSRunbookInvocation(),
},

ConfigureFunc: providerConfigure,
Expand Down
Loading
Loading