Skip to content

Commit

Permalink
tags filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhishekism9450 committed Mar 23, 2023
1 parent 44b3b3c commit f3c1f22
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 17 deletions.
24 changes: 18 additions & 6 deletions client/era/era_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ type Service interface {
UpdateTimeMachineCluster(ctx context.Context, tmsID string, clsID string, body *TmsClusterIntentInput) (*TmsClusterResponse, error)
DeleteTimeMachineCluster(ctx context.Context, tmsID string, clsID string, body *DeleteTmsClusterInput) (*ProvisionDatabaseResponse, error)
CreateTags(ctx context.Context, body *CreateTagsInput) (*TagsIntentResponse, error)
ReadTags(ctx context.Context, id string) (*GetTagsResponse, error)
ReadTags(ctx context.Context, id string, name string) (*GetTagsResponse, error)
UpdateTags(ctx context.Context, body *GetTagsResponse, id string) (*GetTagsResponse, error)
DeleteTags(ctx context.Context, id string) (*string, error)
ListTags(ctx context.Context) (*ListTagsResponse, error)
ListTags(ctx context.Context, entityType string) (*ListTagsResponse, error)
CreateNetwork(ctx context.Context, body *NetworkIntentInput) (*NetworkIntentResponse, error)
GetNetwork(ctx context.Context, id string, name string) (*NetworkIntentResponse, error)
UpdateNetwork(ctx context.Context, body *NetworkIntentInput, id string) (*NetworkIntentResponse, error)
Expand Down Expand Up @@ -760,8 +760,16 @@ func (sc ServiceClient) CreateTimeMachineCluster(ctx context.Context, tmsID stri
return res, sc.c.Do(ctx, httpReq, res)
}

func (sc ServiceClient) ReadTags(ctx context.Context, id string) (*GetTagsResponse, error) {
httpReq, err := sc.c.NewRequest(ctx, http.MethodGet, fmt.Sprintf("/tags?id=%s", id), nil)
func (sc ServiceClient) ReadTags(ctx context.Context, id, name string) (*GetTagsResponse, error) {
path := "/tags"
if id != "" {
path += fmt.Sprintf("?id=%s", id)
}
if name != "" {
path += fmt.Sprintf("?name=%s", name)
}

httpReq, err := sc.c.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -899,8 +907,12 @@ func (sc ServiceClient) DeleteNetwork(ctx context.Context, id string) (*string,
return res, sc.c.Do(ctx, httpReq, res)
}

func (sc ServiceClient) ListTags(ctx context.Context) (*ListTagsResponse, error) {
httpReq, err := sc.c.NewRequest(ctx, http.MethodGet, "/tags", nil)
func (sc ServiceClient) ListTags(ctx context.Context, entityType string) (*ListTagsResponse, error) {
path := "/tags"
if entityType != "" {
path += fmt.Sprintf("?entityType=%s", entityType)
}
httpReq, err := sc.c.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, err
}
Expand Down
24 changes: 17 additions & 7 deletions nutanix/data_source_nutanix_ndb_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ func dataSourceNutanixNDBTag() *schema.Resource {
ReadContext: dataSourceNutanixNDBTagRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Optional: true,
Computed: true,
ConflictsWith: []string{"name"},
},
"name": {
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
Optional: true,
Computed: true,
ConflictsWith: []string{"id"},
},
"description": {
Type: schema.TypeString,
Expand Down Expand Up @@ -58,8 +62,14 @@ func dataSourceNutanixNDBTag() *schema.Resource {
func dataSourceNutanixNDBTagRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*Client).Era

tagID := d.Get("id")
resp, err := conn.Service.ReadTags(ctx, tagID.(string))
tagID, iok := d.GetOk("id")
tagName, nok := d.GetOk("name")

if !iok && !nok {
return diag.Errorf("please provide one of id or name attributes")
}

resp, err := conn.Service.ReadTags(ctx, tagID.(string), tagName.(string))
if err != nil {
return diag.FromErr(err)
}
Expand Down Expand Up @@ -100,6 +110,6 @@ func dataSourceNutanixNDBTagRead(ctx context.Context, d *schema.ResourceData, me
return diag.FromErr(err)
}

d.SetId(tagID.(string))
d.SetId(*resp.ID)
return nil
}
36 changes: 36 additions & 0 deletions nutanix/data_source_nutanix_ndb_tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,25 @@ func TestAccEraTagDataSource_basic(t *testing.T) {
})
}

func TestAccEraTagDataSource_ByName(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccEraPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccEraTagDataSourceConfigByName(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.nutanix_ndb_tags.tags", "tags.#"),
resource.TestCheckResourceAttrSet("data.nutanix_ndb_tags.tags", "tags.0.name"),
resource.TestCheckResourceAttrSet("data.nutanix_ndb_tags.tags", "tags.0.status"),
resource.TestCheckResourceAttr("data.nutanix_ndb_tag.tag", "values", "0"),
resource.TestCheckResourceAttr("data.nutanix_ndb_tag.tag", "status", "ENABLED"),
),
},
},
})
}

func testAccEraTagDataSourceConfig() string {
return `
resource "nutanix_ndb_tag" "acctest-managed" {
Expand All @@ -41,3 +60,20 @@ func testAccEraTagDataSourceConfig() string {
}
`
}

func testAccEraTagDataSourceConfigByName() string {
return `
resource "nutanix_ndb_tag" "acctest-managed" {
name= "test-tag-name"
description = "test tag description"
entity_type = "DATABASE"
required = false
}
data "nutanix_ndb_tags" "tags"{ }
data "nutanix_ndb_tag" "tag"{
name = data.nutanix_ndb_tags.tags.tags.0.name
}
`
}
14 changes: 13 additions & 1 deletion nutanix/data_source_nutanix_ndb_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@ import (
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
era "github.com/terraform-providers/terraform-provider-nutanix/client/era"
)

func dataSourceNutanixNDBTags() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceNutanixNDBTagsRead,
Schema: map[string]*schema.Schema{
"entity_type": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"DATABASE", "TIME_MACHINE",
"CLONE", "DATABASE_SERVER"}, false),
},
"tags": {
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -68,7 +75,12 @@ func dataSourceNutanixNDBTags() *schema.Resource {
func dataSourceNutanixNDBTagsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*Client).Era

resp, err := conn.Service.ListTags(ctx)
entityType := ""
if entity, eok := d.GetOk("entity_type"); eok {
entityType = entity.(string)
}

resp, err := conn.Service.ListTags(ctx, entityType)
if err != nil {
return diag.FromErr(err)
}
Expand Down
38 changes: 38 additions & 0 deletions nutanix/data_source_nutanix_ndb_tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,29 @@ func TestAccEraTagsDataSource_basic(t *testing.T) {
})
}

func TestAccEraTagsDataSource_WithEntityType(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccEraPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccEraTagsDataSourceConfigWithEntityType(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.nutanix_ndb_tags.tags", "tags.#"),
resource.TestCheckResourceAttrSet("data.nutanix_ndb_tags.tags", "tags.0.name"),
resource.TestCheckResourceAttrSet("data.nutanix_ndb_tags.tags", "tags.0.status"),
resource.TestCheckResourceAttrSet("data.nutanix_ndb_tags.tags", "tags.0.values"),
resource.TestCheckResourceAttrSet("data.nutanix_ndb_tags.tags", "tags.0.status"),
resource.TestCheckResourceAttrSet("data.nutanix_ndb_tags.tags", "tags.0.required"),
resource.TestCheckResourceAttrSet("data.nutanix_ndb_tags.tags", "tags.0.status"),
resource.TestCheckResourceAttrSet("data.nutanix_ndb_tags.tags", "tags.0.required"),
resource.TestCheckResourceAttr("data.nutanix_ndb_tags.tags", "tags.0.entity_type", "DATABASE"),
),
},
},
})
}

func testAccEraTagsDataSourceConfig() string {
return `
resource "nutanix_ndb_tag" "acctest-managed" {
Expand All @@ -40,3 +63,18 @@ func testAccEraTagsDataSourceConfig() string {
data "nutanix_ndb_tags" "tags"{ }
`
}

func testAccEraTagsDataSourceConfigWithEntityType() string {
return `
resource "nutanix_ndb_tag" "acctest-managed" {
name= "test-tag"
description = "test tag description"
entity_type = "DATABASE"
required = false
}
data "nutanix_ndb_tags" "tags"{
entity_type= "DATABASE"
}
`
}
6 changes: 3 additions & 3 deletions nutanix/resource_nutanix_ndb_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func resourceNutanixNDBTagsCreate(ctx context.Context, d *schema.ResourceData, m

uniqueID := ""
// fetch all the tags
tagsListResp, er := conn.Service.ListTags(ctx)
tagsListResp, er := conn.Service.ListTags(ctx, entityType)
if er != nil {
return diag.FromErr(er)
}
Expand All @@ -117,7 +117,7 @@ func resourceNutanixNDBTagsCreate(ctx context.Context, d *schema.ResourceData, m
func resourceNutanixNDBTagsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*Client).Era

resp, err := conn.Service.ReadTags(ctx, d.Id())
resp, err := conn.Service.ReadTags(ctx, d.Id(), "")

if err != nil {
return diag.FromErr(err)
Expand Down Expand Up @@ -164,7 +164,7 @@ func resourceNutanixNDBTagsUpdate(ctx context.Context, d *schema.ResourceData, m

// read the tag

resp, err := conn.Service.ReadTags(ctx, d.Id())
resp, err := conn.Service.ReadTags(ctx, d.Id(), "")
if err != nil {
return diag.FromErr(err)
}
Expand Down

0 comments on commit f3c1f22

Please sign in to comment.