Skip to content

Commit

Permalink
Add address group data sources
Browse files Browse the repository at this point in the history
  • Loading branch information
jan committed Dec 26, 2021
1 parent d3a78d4 commit f04ba96
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 4 deletions.
2 changes: 1 addition & 1 deletion client/v3/v3_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2567,7 +2567,7 @@ type AddressGroupResponse struct {

type AddressGroupListEntry struct {
AddressGroup *AddressGroupInput `json:"address_group,omitempty"`
AssociatedPoliciesList []*Reference `json:"associated_policies_list,omitempty"`
AssociatedPoliciesList []*ReferenceValues `json:"associated_policies_list,omitempty"`
}

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

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

func dataSourceNutanixAddressGroup() *schema.Resource {
return &schema.Resource{
Read: resourceNutanixAddressGroupRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"ip_address_block_list": {
Type: schema.TypeList,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ip": {
Type: schema.TypeString,
Required: true,
},
"prefix_length": {
Type: schema.TypeInt,
Required: true,
},
},
},
},
"address_group_string": {
Type: schema.TypeString,
Computed: true,
},
},
}

}
162 changes: 162 additions & 0 deletions nutanix/data_source_nutanix_address_groups.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package nutanix

import (
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
v3 "github.com/terraform-providers/terraform-provider-nutanix/client/v3"
"github.com/terraform-providers/terraform-provider-nutanix/utils"
)

func dataSourceNutanixAddressGroups() *schema.Resource {
return &schema.Resource{
Read: dataSourceNutanixAddressGroupsRead,
Schema: map[string]*schema.Schema{
"metadata": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"filter": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"kind": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"sort_order": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"offset": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"length": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"sort_attribute": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
},
},
"entities": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"address_group": {
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"ip_address_block_list": {
Type: schema.TypeList,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ip": {
Type: schema.TypeString,
Required: true,
},
"prefix_length": {
Type: schema.TypeInt,
Required: true,
},
},
},
},
"address_group_string": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"associated_policies_list": {
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"kind": {
Type: schema.TypeString,
Computed: true,
},
"uuid": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
},
},
},
}
}

func dataSourceNutanixAddressGroupsRead(d *schema.ResourceData, meta interface{}) error {
// Get client connection
conn := meta.(*Client).API
req := &v3.DSMetadata{}

metadata, filtersOk := d.GetOk("metadata")
if filtersOk {
req = buildDataSourceListMetadata(metadata.(*schema.Set))
}

resp, err := conn.V3.ListAllAddressGroups(utils.StringValue(req.Filter))
if err != nil {
return err
}

if err := d.Set("entities", flattenAddressGroup(resp.Entities)); err != nil {
return err
}

d.SetId(resource.UniqueId())
return nil
}

func flattenAddressGroup(entries []*v3.AddressGroupListEntry) interface{} {
entities := make([]map[string]interface{}, len(entries))

for i, entry := range entries {

entities[i] = map[string]interface{}{
"address_group": map[string]interface{}{
"name": entry.AddressGroup.Name,
"description": entry.AddressGroup.Description,
"address_group_string": entry.AddressGroup.AddressGroupString,
"ip_address_block_list": flattenAddressEntry(entry.AddressGroup.BlockList),
},
"associated_policies_list": flattenReferenceList(entry.AssociatedPoliciesList),
}
}

return entities
}
2 changes: 2 additions & 0 deletions nutanix/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ func Provider() terraform.ResourceProvider {
"nutanix_protection_rules": dataSourceNutanixProtectionRules(),
"nutanix_recovery_plan": dataSourceNutanixRecoveryPlan(),
"nutanix_recovery_plans": dataSourceNutanixRecoveryPlans(),
"nutanix_address_groups": dataSourceNutanixAddressGroups(),
"nutanix_address_group": dataSourceNutanixAddressGroup(),
},
ResourcesMap: map[string]*schema.Resource{
"nutanix_virtual_machine": resourceNutanixVirtualMachine(),
Expand Down
6 changes: 3 additions & 3 deletions nutanix/resource_nutanix_address_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func resourceNutanixAddressGroupRead(d *schema.ResourceData, meta interface{}) e
return err
}

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

Expand All @@ -138,9 +138,9 @@ func resourceNutanixAddressGroupRead(d *schema.ResourceData, meta interface{}) e
return d.Set("description", utils.StringValue(resp.AddressGroup.Description))
}

func flattenAddressEntry(group *v3.AddressGroupInput) []map[string]interface{} {
func flattenAddressEntry(group []*v3.IPAddressBlock) []map[string]interface{} {
groupList := make([]map[string]interface{}, 0)
for _, v := range group.BlockList {
for _, v := range group {
groupItem := make(map[string]interface{})
groupItem["ip"] = v.IPAddress
groupItem["prefix_length"] = v.PrefixLength
Expand Down

0 comments on commit f04ba96

Please sign in to comment.