Skip to content

Commit

Permalink
Add workaround for missing UUID parameter for data source
Browse files Browse the repository at this point in the history
  • Loading branch information
jan committed Dec 27, 2021
1 parent f04ba96 commit e5bbc88
Showing 1 changed file with 47 additions and 3 deletions.
50 changes: 47 additions & 3 deletions nutanix/data_source_address_group.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
package nutanix

import "github.com/hashicorp/terraform-plugin-sdk/helper/schema"
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"strings"
)

func dataSourceNutanixAddressGroup() *schema.Resource {
return &schema.Resource{
Read: resourceNutanixAddressGroupRead,
Read: dataSourceNutanixAddressGroupRead,
Schema: map[string]*schema.Schema{
"name": {
"uuid": {
Type: schema.TypeString,
Required: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -38,3 +46,39 @@ func dataSourceNutanixAddressGroup() *schema.Resource {
}

}

func dataSourceNutanixAddressGroupRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*Client).API

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

if reqErr != nil {
if strings.Contains(fmt.Sprint(reqErr), "ENTITY_NOT_FOUND") {
d.SetId("")
}
return fmt.Errorf("error reading user with error %s", reqErr)
}

if name, nameOk := d.GetOk("name"); nameOk {
d.Set("name", name.(string))
}

if desc, descOk := d.GetOk("description"); descOk {
d.Set("description", desc.(string))
}

if str, strOk := d.GetOk("address_group_string"); strOk {
d.Set("address_group_string", str.(string))
}

if err := d.Set("ip_address_block_list", flattenAddressEntry(group.AddressGroup.BlockList)); err != nil {
return err
}

d.SetId(uuid.(string))
} else {
return fmt.Errorf("please provide `uuid`")
}
return nil
}

0 comments on commit e5bbc88

Please sign in to comment.