-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add network_profile data source (#92)
Rename network_profile arguments related to isolation to match with the IaaS api arguments Signed-off-by: Deepak Mettem <[email protected]>
- Loading branch information
Showing
7 changed files
with
326 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.