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 filter by name in datasource of project #237

Merged
merged 5 commits into from
Jan 22, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 4 additions & 2 deletions client/v3/v3_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ type Service interface {
CreateProject(request *Project) (*Project, error)
GetProject(projectUUID string) (*Project, error)
ListProject(getEntitiesRequest *DSMetadata) (*ProjectListResponse, error)
ListAllProject() (*ProjectListResponse, error)
ListAllProject(filter string) (*ProjectListResponse, error)
UpdateProject(uuid string, body *Project) (*Project, error)
DeleteProject(uuid string) error
CreateAccessControlPolicy(request *AccessControlPolicy) (*AccessControlPolicy, error)
Expand Down Expand Up @@ -1243,10 +1243,11 @@ func (op Operations) ListProject(getEntitiesRequest *DSMetadata) (*ProjectListRe
* Note: Entities that have not been created successfully are not listed.
* @return *ProjectListResponse
*/
func (op Operations) ListAllProject() (*ProjectListResponse, error) {
func (op Operations) ListAllProject(filter string) (*ProjectListResponse, error) {
entities := make([]*Project, 0)

resp, err := op.ListProject(&DSMetadata{
Filter: &filter,
Kind: utils.StringPtr("project"),
Length: utils.Int64Ptr(itemsPerPage),
})
Expand All @@ -1261,6 +1262,7 @@ func (op Operations) ListAllProject() (*ProjectListResponse, error) {
if totalEntities > itemsPerPage {
for hasNext(&remaining) {
resp, err = op.ListProject(&DSMetadata{
Filter: &filter,
Kind: utils.StringPtr("project"),
Length: utils.Int64Ptr(itemsPerPage),
Offset: utils.Int64Ptr(offset),
Expand Down
59 changes: 54 additions & 5 deletions nutanix/data_source_nutanix_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package nutanix
import (
"fmt"

v3 "github.com/terraform-providers/terraform-provider-nutanix/client/v3"

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

Expand All @@ -11,8 +13,14 @@ func dataSourceNutanixProject() *schema.Resource {
Read: dataSourceNutanixProjectRead,
Schema: map[string]*schema.Schema{
"project_id": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"project_name"},
},
"project_name": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"project_id"},
},
"name": {
Type: schema.TypeString,
Expand Down Expand Up @@ -285,9 +293,23 @@ func dataSourceNutanixProject() *schema.Resource {
func dataSourceNutanixProjectRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*Client).API

projectID := d.Get("project_id").(string)
id, iok := d.GetOk("project_id")
name, nOk := d.GetOk("project_name")

if !iok && !nOk {
return fmt.Errorf("please provide `project_id` or `project_name`")
}

var err error
var project *v3.Project

if iok {
project, err = conn.V3.GetProject(id.(string))
}
if nOk {
project, err = findProjectByName(conn, name.(string))
}

project, err := conn.V3.GetProject(projectID)
if err != nil {
return err
}
Expand Down Expand Up @@ -347,7 +369,34 @@ func dataSourceNutanixProjectRead(d *schema.ResourceData, meta interface{}) erro
return fmt.Errorf("error setting `api_version` for Project(%s): %s", d.Id(), err)
}

d.SetId(projectID)
d.SetId(id.(string))

return nil
}

func findProjectByName(conn *v3.Client, name string) (*v3.Project, error) {
filter := fmt.Sprintf("name==%s", name)
resp, err := conn.V3.ListAllProject(filter)
if err != nil {
return nil, err
}

entities := resp.Entities

found := make([]*v3.Project, 0)
for _, v := range entities {
if v.Spec.Name == name {
found = append(found, v)
}
}

if len(found) > 1 {
return nil, fmt.Errorf("your query returned more than one result. Please use project_id argument instead")
}

if len(found) == 0 {
return nil, fmt.Errorf("project with the given name, not found")
}

return found[0], nil
}
128 changes: 124 additions & 4 deletions nutanix/data_source_nutanix_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/spf13/cast"
)

func TestAccNutanixProjectDataSource_basic(t *testing.T) {
func TestAccNutanixProjectDataSourceByID_basic(t *testing.T) {
resourceName := "nutanix_project.project_test"

subnetName := acctest.RandomWithPrefix("test-subnateName")
Expand All @@ -33,7 +33,7 @@ func TestAccNutanixProjectDataSource_basic(t *testing.T) {
CheckDestroy: testAccCheckNutanixProjectDestroy,
Steps: []resource.TestStep{
{
Config: testAccProjectDataSourceConfig(subnetName, name, description, categoryName, categoryVal, limit, rsType),
Config: testAccProjectDataSourceByIDConfig(subnetName, name, description, categoryName, categoryVal, limit, rsType),
Check: resource.ComposeTestCheckFunc(
testAccCheckNutanixProjectExists(&resourceName),
resource.TestCheckResourceAttr(resourceName, "name", name),
Expand All @@ -47,7 +47,7 @@ func TestAccNutanixProjectDataSource_basic(t *testing.T) {
),
},
{
Config: testAccProjectDataSourceConfig(
Config: testAccProjectDataSourceByIDConfig(
subnetName, updateName, updateDescription, updateCategoryName, updateCategoryVal, updateLimit, updateRSType),
Check: resource.ComposeTestCheckFunc(
testAccCheckNutanixProjectExists(&resourceName),
Expand All @@ -65,7 +65,63 @@ func TestAccNutanixProjectDataSource_basic(t *testing.T) {
})
}

func testAccProjectDataSourceConfig(subnetName, name, description, categoryName, categoryVal, limit, rsType string) string {
func TestAccNutanixProjectDataSourceByName_basic(t *testing.T) {
resourceName := "nutanix_project.project_test"

subnetName := acctest.RandomWithPrefix("test-subnateName")
name := acctest.RandomWithPrefix("test-project-name-dou")
description := acctest.RandomWithPrefix("test-project-desc-dou")
categoryName := "Environment"
categoryVal := "Staging"
limit := cast.ToString(acctest.RandIntRange(2, 4))
rsType := "STORAGE"

updateName := acctest.RandomWithPrefix("test-project-name-dou")
updateDescription := acctest.RandomWithPrefix("test-project-desc-dou")
updateCategoryName := "Environment"
updateCategoryVal := "Production"
updateLimit := cast.ToString(acctest.RandIntRange(4, 8))
updateRSType := "MEMORY"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckNutanixProjectDestroy,
Steps: []resource.TestStep{
{
Config: testAccProjectDataSourceByNameConfig(subnetName, name, description, categoryName, categoryVal, limit, rsType),
Check: resource.ComposeTestCheckFunc(
testAccCheckNutanixProjectExists(&resourceName),
resource.TestCheckResourceAttr(resourceName, "name", name),
resource.TestCheckResourceAttr(resourceName, "description", description),
resource.TestCheckResourceAttr(resourceName, "resource_domain.#", "1"),
resource.TestCheckResourceAttr(resourceName, "resource_domain.0.resources.#", "1"),
resource.TestCheckResourceAttr(resourceName, "resource_domain.0.resources.0.limit", limit),
resource.TestCheckResourceAttr(resourceName, "resource_domain.0.resources.0.resource_type", rsType),
resource.TestCheckResourceAttr(resourceName, "categories.#", "1"),
resource.TestCheckResourceAttr(resourceName, "api_version", "3.1"),
),
},
{
Config: testAccProjectDataSourceByNameConfig(
subnetName, updateName, updateDescription, updateCategoryName, updateCategoryVal, updateLimit, updateRSType),
Check: resource.ComposeTestCheckFunc(
testAccCheckNutanixProjectExists(&resourceName),
resource.TestCheckResourceAttr(resourceName, "name", updateName),
resource.TestCheckResourceAttr(resourceName, "description", updateDescription),
resource.TestCheckResourceAttr(resourceName, "resource_domain.#", "1"),
resource.TestCheckResourceAttr(resourceName, "resource_domain.0.resources.#", "1"),
resource.TestCheckResourceAttr(resourceName, "resource_domain.0.resources.0.limit", updateLimit),
resource.TestCheckResourceAttr(resourceName, "resource_domain.0.resources.0.resource_type", updateRSType),
resource.TestCheckResourceAttr(resourceName, "categories.#", "1"),
resource.TestCheckResourceAttr(resourceName, "api_version", "3.1"),
),
},
},
})
}

func testAccProjectDataSourceByIDConfig(subnetName, name, description, categoryName, categoryVal, limit, rsType string) string {
return fmt.Sprintf(`
data "nutanix_clusters" "clusters" {}

Expand Down Expand Up @@ -128,3 +184,67 @@ func testAccProjectDataSourceConfig(subnetName, name, description, categoryName,
}
`, subnetName, name, description, categoryName, categoryVal, limit, rsType)
}

func testAccProjectDataSourceByNameConfig(subnetName, name, description, categoryName, categoryVal, limit, rsType string) string {
return fmt.Sprintf(`
data "nutanix_clusters" "clusters" {}

locals {
cluster1 = [
for cluster in data.nutanix_clusters.clusters.entities :
cluster.metadata.uuid if cluster.service_list[0] != "PRISM_CENTRAL"
][0]
}

resource "nutanix_subnet" "subnet" {
cluster_uuid = local.cluster1
name = "%s"
description = "Description of my unit test VLAN"
vlan_id = 31
subnet_type = "VLAN"
subnet_ip = "10.250.140.0"
default_gateway_ip = "10.250.140.1"
prefix_length = 24

dhcp_options = {
boot_file_name = "bootfile"
domain_name = "nutanix"
tftp_server_name = "10.250.140.200"
}

dhcp_domain_name_server_list = ["8.8.8.8", "4.2.2.2"]
dhcp_domain_search_list = ["terraform.nutanix.com", "terraform.unit.test.com"]
}

resource "nutanix_project" "project_test" {
name = "%s"
description = "%s"

categories {
name = "%s"
value = "%s"
}

resource_domain {
resources {
limit = %s
resource_type = "%s"
}
}

default_subnet_reference {
uuid = nutanix_subnet.subnet.metadata.uuid
}

subnet_reference_list {
uuid = nutanix_subnet.subnet.metadata.uuid
}

api_version = "3.1"
}

data "nutanix_project" "test" {
project_name = nutanix_project.project_test.name
}
`, subnetName, name, description, categoryName, categoryVal, limit, rsType)
}
8 changes: 7 additions & 1 deletion nutanix/data_source_nutanix_projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,14 @@ func dataSourceNutanixProjects() *schema.Resource {
func dataSourceNutanixProjectsRead(d *schema.ResourceData, meta interface{}) error {
// Get client connection
conn := meta.(*Client).API
req := &v3.DSMetadata{}

resp, err := conn.V3.ListAllProject()
metadata, filtersOk := d.GetOk("metadata")
if filtersOk {
req = buildDataSourceListMetadata(metadata.(*schema.Set))
}

resp, err := conn.V3.ListProject(req)
if err != nil {
return err
}
Expand Down