Skip to content

Commit

Permalink
Add network_profile data source (#92)
Browse files Browse the repository at this point in the history
Rename network_profile arguments related to isolation to match with the IaaS api arguments

Signed-off-by: Deepak Mettem <[email protected]>
  • Loading branch information
dmettem authored and markpeek committed Nov 1, 2019
1 parent 422300d commit 6b4cfc1
Show file tree
Hide file tree
Showing 7 changed files with 326 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ resource "vra_network_profile" "external_subnet_isolation" {
data.vra_fabric_network.subnet.id
]

isolation_type = "SUBNET"
isolation_network_domain_id = data.vra_network_domain.vpc.id
isolated_network_cidr_prefix = var.cidr_prefix
isolation_external_fabric_network_id = data.vra_fabric_network.external_subnet.id
isolation_type = "SUBNET"
isolated_network_domain_id = data.vra_network_domain.vpc.id
isolated_network_cidr_prefix = var.cidr_prefix
isolated_external_fabric_network_id = data.vra_fabric_network.external_subnet.id

tags {
key = "foo"
Expand Down
2 changes: 1 addition & 1 deletion examples/network_profile/isolation_with_subnet/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ resource "vra_network_profile" "subnet_isolation" {
]

isolation_type = "SUBNET"
isolation_network_domain_id = data.vra_network_domain.vpc.id
isolated_network_domain_id = data.vra_network_domain.vpc.id
isolated_network_cidr_prefix = var.cidr_prefix

tags {
Expand Down
201 changes: 201 additions & 0 deletions vra/data_source_network_profile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
package vra

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/vmware/vra-sdk-go/pkg/client/network_profile"
"github.com/vmware/vra-sdk-go/pkg/models"

"log"
"strings"
)

func dataSourceNetworkProfile() *schema.Resource {
return &schema.Resource{
Read: dataSourceNetworkProfileRead,

Schema: map[string]*schema.Schema{
"custom_properties": &schema.Schema{
Type: schema.TypeMap,
Computed: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"external_region_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"fabric_network_ids": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"filter": {
Type: schema.TypeString,
Optional: true,
},
"id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"isolated_network_cidr_prefix": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
},
"isolated_external_fabric_network_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"isolated_network_domain_cidr": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"isolated_network_domain_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"isolation_type": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"links": linksSchema(),
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"organization_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"owner": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"region_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"security_group_ids": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"tags": tagsSchema(),
"updated_at": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceNetworkProfileRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("Reading the vra_network_profile data source with filter %s", d.Get("filter"))
apiClient := meta.(*Client).apiClient

var networkProfile *models.NetworkProfile

id := d.Get("id").(string)
filter := d.Get("filter").(string)

if id == "" && filter == "" {
return fmt.Errorf("one of id or filter is required")
}

if id != "" {
getResp, err := apiClient.NetworkProfile.GetNetworkProfile(network_profile.NewGetNetworkProfileParams().WithID(id))

if err != nil {
return err
}
networkProfile = getResp.GetPayload()
} else {
getResp, err := apiClient.NetworkProfile.GetNetworkProfiles(network_profile.NewGetNetworkProfilesParams().WithDollarFilter(withString(filter)))
if err != nil {
return err
}

networkProfiles := *getResp.Payload
if len(networkProfiles.Content) > 1 {
return fmt.Errorf("vra_network_profile must filter to a network profile")
}
if len(networkProfiles.Content) == 0 {
return fmt.Errorf("vra_network_profile filter did not match any network profile")
}

networkProfile = networkProfiles.Content[0]
}

d.SetId(*networkProfile.ID)
d.Set("custom_properties", networkProfile.CustomProperties)
d.Set("description", networkProfile.Description)
d.Set("external_region_id", networkProfile.ExternalRegionID)
d.Set("isolation_type", networkProfile.IsolationType)
d.Set("isolated_network_domain_cidr", networkProfile.IsolationNetworkDomainCIDR)
d.Set("isolated_network_cidr_prefix", networkProfile.IsolatedNetworkCIDRPrefix)
d.Set("name", networkProfile.Name)
d.Set("organization_id", networkProfile.OrganizationID)
d.Set("owner", networkProfile.Owner)
d.Set("updated_at", networkProfile.UpdatedAt)

if err := d.Set("tags", flattenTags(networkProfile.Tags)); err != nil {
return fmt.Errorf("error setting network profile tags - error: %v", err)
}

if err := d.Set("links", flattenLinks(networkProfile.Links)); err != nil {
return fmt.Errorf("error setting network profile links - error: %#v", err)
}

if fabricNetworkLinks, ok := networkProfile.Links["fabric-networks"]; ok {
if fabricNetworkLinks.Hrefs != nil {
fabricNetworkIds := make([]string, 0, len(fabricNetworkLinks.Hrefs))

for _, link := range fabricNetworkLinks.Hrefs {
fabricNetworkIds = append(fabricNetworkIds, strings.TrimPrefix(link, "/iaas/api/fabric-networks/"))
}

d.Set("fabric_network_ids", fabricNetworkIds)
}
}

if extFabricNetworkLink, ok := networkProfile.Links["isolated-external-fabric-networks"]; ok {
if extFabricNetworkLink.Href != "" {
d.Set("isolated_external_fabric_network_id", strings.TrimPrefix(extFabricNetworkLink.Href, "/iaas/api/fabric-networks/"))
}
}

if networkDomainLink, ok := networkProfile.Links["network-domains"]; ok {
if networkDomainLink.Href != "" {
d.Set("isolated_network_domain_id", strings.TrimPrefix(networkDomainLink.Href, "/iaas/api/network-domains/"))
}
}

if securityGroupLinks, ok := networkProfile.Links["security-groups"]; ok {
if securityGroupLinks.Hrefs != nil {
securityGroupIds := make([]string, 0, len(securityGroupLinks.Hrefs))

for _, link := range securityGroupLinks.Hrefs {
securityGroupIds = append(securityGroupIds, strings.TrimPrefix(link, "/iaas/api/security-groups/"))
}

d.Set("security_group_ids", securityGroupIds)
}
}

if regionLink, ok := networkProfile.Links["region"]; ok {
if regionLink.Href != "" {
d.Set("region_id", strings.TrimPrefix(regionLink.Href, "/iaas/api/regions/"))
}
}

log.Printf("Finished reading the vra_network_profile data source with filter %s", d.Get("filter"))
return nil
}
87 changes: 87 additions & 0 deletions vra/data_source_network_profile_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package vra

import (
"fmt"

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

"regexp"
"testing"
)

func TestAccDataSourceVRANetworkProfile(t *testing.T) {
rInt := acctest.RandInt()
resourceName1 := "vra_network_profile.this"
dataSourceName1 := "data.vra_network_profile.this"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheckAWS(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckVRANetworkProfileDestroy,
Steps: []resource.TestStep{
{
Config: testAccDataSourceVRANetworkProfileNotFound(rInt),
ExpectError: regexp.MustCompile("vra_network_profile filter did not match any network profile"),
},
{
Config: testAccDataSourceVRANetworkProfileNameFilter(rInt),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(resourceName1, "id", dataSourceName1, "id"),
resource.TestCheckResourceAttrPair(resourceName1, "description", dataSourceName1, "description"),
resource.TestCheckResourceAttrPair(resourceName1, "name", dataSourceName1, "name"),
resource.TestCheckResourceAttrPair(resourceName1, "isolation_type", dataSourceName1, "isolation_type"),
resource.TestCheckResourceAttrPair(resourceName1, "region_id", dataSourceName1, "region_id"),
),
},
{
Config: testAccDataSourceVRANetworkProfileRegionIdFilter(rInt),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(resourceName1, "id", dataSourceName1, "id"),
resource.TestCheckResourceAttrPair(resourceName1, "description", dataSourceName1, "description"),
resource.TestCheckResourceAttrPair(resourceName1, "name", dataSourceName1, "name"),
resource.TestCheckResourceAttrPair(resourceName1, "isolation_type", dataSourceName1, "isolation_type"),
resource.TestCheckResourceAttrPair(resourceName1, "region_id", dataSourceName1, "region_id"),
),
},
{
Config: testAccDataSourceVRANetworkProfileById(rInt),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(resourceName1, "id", dataSourceName1, "id"),
resource.TestCheckResourceAttrPair(resourceName1, "description", dataSourceName1, "description"),
resource.TestCheckResourceAttrPair(resourceName1, "name", dataSourceName1, "name"),
resource.TestCheckResourceAttrPair(resourceName1, "isolation_type", dataSourceName1, "isolation_type"),
resource.TestCheckResourceAttrPair(resourceName1, "region_id", dataSourceName1, "region_id"),
),
},
},
})
}

func testAccDataSourceVRANetworkProfileNotFound(rInt int) string {
return testAccCheckVRANetworkProfileConfig(rInt) + fmt.Sprintf(`
data "vra_network_profile" "this" {
filter = "name eq 'foobar'"
}`)
}

func testAccDataSourceVRANetworkProfileNameFilter(rInt int) string {
return testAccCheckVRANetworkProfileConfig(rInt) + fmt.Sprintf(`
data "vra_network_profile" "this" {
filter = "name eq '${vra_network_profile.this.name}'"
}`)
}

func testAccDataSourceVRANetworkProfileRegionIdFilter(rInt int) string {
return testAccCheckVRANetworkProfileConfig(rInt) + fmt.Sprintf(`
data "vra_network_profile" "this" {
filter = "regionId eq '${data.vra_region.this.id}'"
}`)
}

func testAccDataSourceVRANetworkProfileById(rInt int) string {
return testAccCheckVRANetworkProfileConfig(rInt) + fmt.Sprintf(`
data "vra_network_profile" "this" {
id = vra_network_profile.this.id
}`)
}
1 change: 1 addition & 0 deletions vra/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func Provider() *schema.Provider {
"vra_image": dataSourceImage(),
"vra_network": dataSourceNetwork(),
"vra_network_domain": dataSourceNetworkDomain(),
"vra_network_profile": dataSourceNetworkProfile(),
"vra_project": dataSourceProject(),
"vra_region": dataSourceRegion(),
"vra_region_enumeration": dataSourceRegionEnumeration(),
Expand Down
20 changes: 10 additions & 10 deletions vra/resource_network_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ func resourceNetworkProfile() *schema.Resource {
Type: schema.TypeInt,
Optional: true,
},
"isolation_external_fabric_network_id": &schema.Schema{
"isolated_external_fabric_network_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"isolation_network_domain_cidr": &schema.Schema{
"isolated_network_domain_cidr": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"isolation_network_domain_id": &schema.Schema{
"isolated_network_domain_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
Expand Down Expand Up @@ -100,9 +100,9 @@ func resourceNetworkProfileCreate(d *schema.ResourceData, m interface{}) error {

networkProfileSpecification := models.NetworkProfileSpecification{
IsolationType: d.Get("isolation_type").(string),
IsolationNetworkDomainID: d.Get("isolation_network_domain_id").(string),
IsolationNetworkDomainCIDR: d.Get("isolation_network_domain_cidr").(string),
IsolationExternalFabricNetworkID: d.Get("isolation_external_fabric_network_id").(string),
IsolationNetworkDomainID: d.Get("isolated_network_domain_id").(string),
IsolationNetworkDomainCIDR: d.Get("isolated_network_domain_cidr").(string),
IsolationExternalFabricNetworkID: d.Get("isolated_external_fabric_network_id").(string),
IsolatedNetworkCIDRPrefix: int32(d.Get("isolated_network_cidr_prefix").(int)),
Name: &name,
RegionID: &regionID,
Expand Down Expand Up @@ -155,7 +155,7 @@ func resourceNetworkProfileRead(d *schema.ResourceData, m interface{}) error {
d.Set("description", networkProfile.Description)
d.Set("external_region_id", networkProfile.ExternalRegionID)
d.Set("isolation_type", networkProfile.IsolationType)
d.Set("isolation_network_domain_cidr", networkProfile.IsolationNetworkDomainCIDR)
d.Set("isolated_network_domain_cidr", networkProfile.IsolationNetworkDomainCIDR)
d.Set("isolated_network_cidr_prefix", networkProfile.IsolatedNetworkCIDRPrefix)
d.Set("name", networkProfile.Name)
d.Set("organization_id", networkProfile.OrganizationID)
Expand Down Expand Up @@ -183,9 +183,9 @@ func resourceNetworkProfileUpdate(d *schema.ResourceData, m interface{}) error {

networkProfileSpecification := models.NetworkProfileSpecification{
IsolationType: d.Get("isolation_type").(string),
IsolationNetworkDomainID: d.Get("isolation_network_domain_id").(string),
IsolationNetworkDomainCIDR: d.Get("isolation_network_domain_cidr").(string),
IsolationExternalFabricNetworkID: d.Get("isolation_external_fabric_network_id").(string),
IsolationNetworkDomainID: d.Get("isolated_network_domain_id").(string),
IsolationNetworkDomainCIDR: d.Get("isolated_network_domain_cidr").(string),
IsolationExternalFabricNetworkID: d.Get("isolated_external_fabric_network_id").(string),
IsolatedNetworkCIDRPrefix: int32(d.Get("isolated_network_cidr_prefix").(int)),
Name: &name,
RegionID: &regionID,
Expand Down
Loading

0 comments on commit 6b4cfc1

Please sign in to comment.