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

Added singular and plural datasources for service groups with test cases #363

Merged
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
8 changes: 4 additions & 4 deletions client/v3/v3_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ type Service interface {
UpdateRecoveryPlan(uuid string, body *RecoveryPlanInput) (*RecoveryPlanResponse, error)
DeleteRecoveryPlan(uuid string) (*DeleteResponse, error)
GetServiceGroup(uuid string) (*ServiceGroupResponse, error)
ListServiceGroups(getEntitiesRequest *DSMetadata) (*ServiceGroupListResponse, error)
listServiceGroups(getEntitiesRequest *DSMetadata) (*ServiceGroupListResponse, error)
ListAllServiceGroups(filter string) (*ServiceGroupListResponse, error)
CreateServiceGroup(request *ServiceGroupInput) (*Reference, error)
UpdateServiceGroup(uuid string, body *ServiceGroupInput) error
Expand Down Expand Up @@ -2262,7 +2262,7 @@ func (op Operations) DeleteServiceGroup(uuid string) error {
func (op Operations) ListAllServiceGroups(filter string) (*ServiceGroupListResponse, error) {
entities := make([]*ServiceGroupListEntry, 0)

resp, err := op.ListServiceGroups(&DSMetadata{
resp, err := op.listServiceGroups(&DSMetadata{
Filter: &filter,
Kind: utils.StringPtr("service_group"),
Length: utils.Int64Ptr(itemsPerPage),
Expand All @@ -2277,7 +2277,7 @@ func (op Operations) ListAllServiceGroups(filter string) (*ServiceGroupListRespo

if totalEntities > itemsPerPage {
for hasNext(&remaining) {
resp, err = op.ListServiceGroups(&DSMetadata{
resp, err = op.listServiceGroups(&DSMetadata{
Filter: &filter,
Kind: utils.StringPtr("service_group"),
Length: utils.Int64Ptr(itemsPerPage),
Expand All @@ -2300,7 +2300,7 @@ func (op Operations) ListAllServiceGroups(filter string) (*ServiceGroupListRespo
return resp, nil
}

func (op Operations) ListServiceGroups(getEntitiesRequest *DSMetadata) (*ServiceGroupListResponse, error) {
func (op Operations) listServiceGroups(getEntitiesRequest *DSMetadata) (*ServiceGroupListResponse, error) {
ctx := context.TODO()
path := "/service_groups/list"

Expand Down
2 changes: 1 addition & 1 deletion client/v3/v3_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2539,7 +2539,7 @@ type ServiceListEntry struct {
type ServiceGroupListEntry struct {
UUID *string `json:"uuid,omitempty"`
ServiceGroup *ServiceGroupInput `json:"service_group,omitempty"`
AssociatedPoliciesList []*Reference `json:"associated_policies_list,omitempty"`
AssociatedPoliciesList []*ReferenceValues `json:"associated_policies_list,omitempty"`
}

type ServiceGroupInput struct {
Expand Down
130 changes: 130 additions & 0 deletions nutanix/data_source_nutanix_service_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package nutanix

import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/terraform-providers/terraform-provider-nutanix/utils"
)

// dataSourceNutanixServiceGroup returns schema for datasource nutanix service group
// /v3/service_groups/{uuid}
// https://www.nutanix.dev/api_references/prism-central-v3/#/b3A6MjU1ODc2OTk-get-a-existing-service-group
func dataSourceNutanixServiceGroup() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceNutanixServiceGroupRead,
Schema: map[string]*schema.Schema{
"uuid": {
Type: schema.TypeString,
Required: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"service_list": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"protocol": {
Type: schema.TypeString,
Computed: true,
},
"icmp_type_code_list": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"code": {
Type: schema.TypeString,
Computed: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"tcp_port_range_list": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"end_port": {
Type: schema.TypeInt,
Computed: true,
},
"start_port": {
Type: schema.TypeInt,
Computed: true,
},
},
},
},
"udp_port_range_list": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"end_port": {
Type: schema.TypeInt,
Computed: true,
},
"start_port": {
Type: schema.TypeInt,
Computed: true,
},
},
},
},
},
},
},
"is_system_defined": {
Type: schema.TypeBool,
Description: "specifying whether it is a system defined service group",
Computed: true,
},
},
}
}

func dataSourceNutanixServiceGroupRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*Client).API

if uuid, uuidOk := d.GetOk("uuid"); uuidOk {
svGroup, reqErr := conn.V3.GetServiceGroup(uuid.(string))

if reqErr != nil {
return diag.Errorf("error reading user with error %s", reqErr)
}

if err := d.Set("name", utils.StringValue(svGroup.ServiceGroup.Name)); err != nil {
return diag.FromErr(err)
}

if err := d.Set("description", utils.StringValue(svGroup.ServiceGroup.Description)); err != nil {
return diag.FromErr(err)
}

if err := d.Set("is_system_defined", utils.BoolValue(svGroup.ServiceGroup.SystemDefined)); err != nil {
return diag.FromErr(err)
}

if err := d.Set("service_list", flattenServiceEntry(svGroup.ServiceGroup)); err != nil {
return diag.FromErr(err)
}

d.SetId(uuid.(string))
} else {
return diag.Errorf("please provide `uuid`")
}
return nil
}
55 changes: 55 additions & 0 deletions nutanix/data_source_nutanix_service_group_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package nutanix

import (
"fmt"
"testing"

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

func TestAccNutanixServiceGroupDataSource_basic(t *testing.T) {
name := acctest.RandomWithPrefix("nutanix_service_gr")
description := "this is nutanix service group"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccServiceGroupDataSourceConfig(name, description),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.nutanix_service_group.service_group", "name", name),
resource.TestCheckResourceAttr("data.nutanix_service_group.service_group", "description", description),
resource.TestCheckResourceAttr("data.nutanix_service_group.service_group", "service_list.#", "1"),
resource.TestCheckResourceAttr("data.nutanix_service_group.service_group", "service_list.0.protocol", "TCP"),
),
},
},
})
}

func testAccServiceGroupDataSourceConfig(name, description string) string {
return fmt.Sprintf(`
resource "nutanix_service_group" "test" {
name = "%[1]s"
description = "%[2]s"

service_list {
protocol = "TCP"
tcp_port_range_list {
start_port = 22
end_port = 22
}

tcp_port_range_list {
start_port = 2222
end_port = 2222
}
}
}
data "nutanix_service_group" "service_group" {
uuid = "${nutanix_service_group.test.id}"
}

`, name, description)
}
Loading