Skip to content

Commit

Permalink
docs and tcs for vpc
Browse files Browse the repository at this point in the history
  • Loading branch information
abhimutant committed Jun 21, 2024
1 parent 29ec702 commit 4770a95
Show file tree
Hide file tree
Showing 13 changed files with 1,194 additions and 25 deletions.
1 change: 1 addition & 0 deletions nutanix/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ func Provider() *schema.Provider {
"nutanix_ndb_cluster": ndb.ResourceNutanixNDBCluster(),
"nutanix_subnet_v4": networking.ResourceNutanixSubnetv4(),
"nutanix_floating_ip_v4": networking.ResourceNutanixFloatingIPv4(),
"nutanix_vpc_v4": networking.ResourceNutanixVPCsV4(),
},
ConfigureContextFunc: providerConfigure,
}
Expand Down
4 changes: 4 additions & 0 deletions nutanix/services/v1/prism/data_source_nutanix_vpcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ func DataSourceNutanixVPCs() *schema.Resource {
},
},
},
"active_gateway_count": {
Type: schema.TypeInt,
Computed: true,
},
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,10 @@ func DataSourceVPCSchemaV4() map[string]*schema.Schema {
},
},
},
"active_gateway_count": {
Type: schema.TypeInt,
Computed: true,
},
},
},
},
Expand Down Expand Up @@ -1230,6 +1234,7 @@ func flattenExternalSubnets(pr []import1.ExternalSubnet) []map[string]interface{
sub["external_ips"] = flattenNtpServer(v.ExternalIps)
sub["gateway_nodes"] = v.GatewayNodes
sub["active_gateway_node"] = flattenActiveGatewayNode(v.ActiveGatewayNode)
sub["active_gateway_count"] = v.ActiveGatewayCount

extSubs[k] = sub
}
Expand Down
19 changes: 9 additions & 10 deletions nutanix/services/v2/networking/data_source_nutanix_vpc_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package networking

import (
"context"
"encoding/json"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -72,6 +71,10 @@ func DataSourceNutanixVPCv4() *schema.Resource {
},
},
},
"vpc_type": {
Type: schema.TypeString,
Computed: true,
},
"snat_ips": {
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -130,6 +133,10 @@ func DataSourceNutanixVPCv4() *schema.Resource {
},
},
},
"active_gateway_count": {
Type: schema.TypeInt,
Computed: true,
},
},
},
},
Expand Down Expand Up @@ -181,15 +188,7 @@ func dataSourceNutanixVPCv4Read(ctx context.Context, d *schema.ResourceData, met
extID := d.Get("ext_id")
resp, err := conn.VpcAPIInstance.GetVpcById(utils.StringPtr(extID.(string)))
if err != nil {
var errordata map[string]interface{}
e := json.Unmarshal([]byte(err.Error()), &errordata)
if e != nil {
return diag.FromErr(e)
}
data := errordata["data"].(map[string]interface{})
errorList := data["error"].([]interface{})
errorMessage := errorList[0].(map[string]interface{})
return diag.Errorf("error while fetching subnets : %v", errorMessage["message"])
return diag.Errorf("error while fetching vpc : %v", err)
}

getResp := resp.Data.GetValue().(import1.Vpc)
Expand Down
55 changes: 55 additions & 0 deletions nutanix/services/v2/networking/data_source_nutanix_vpc_v2_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package networking_test

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
acc "github.com/terraform-providers/terraform-provider-nutanix/nutanix/acctest"
)

const datasourceNamevpc = "data.nutanix_vpc_v4.test"

func TestAccNutanixVpcDataSourceV2_basic(t *testing.T) {
r := acctest.RandInt()
name := fmt.Sprintf("test-vpc-%d", r)
desc := "test vpc description"
resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Providers: acc.TestAccProviders,
Steps: []resource.TestStep{
{
Config: testAccVpcDataSourceConfig(name, desc),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(datasourceNamevpc, "name", name),
resource.TestCheckResourceAttr(datasourceNamevpc, "description", desc),
resource.TestCheckResourceAttrSet(datasourceNamevpc, "metadata.#"),
resource.TestCheckResourceAttrSet(datasourceNamevpc, "links.#"),
resource.TestCheckResourceAttrSet(datasourceNamevpc, "snat_ips.#"),
resource.TestCheckResourceAttrSet(datasourceNamevpc, "external_subnets.#"),
),
},
},
})
}

func testAccVpcDataSourceConfig(name, desc string) string {
return fmt.Sprintf(`
resource "nutanix_vpc_v4" "test" {
name = "%[1]s"
description = "%[2]s"
external_subnets{
subnet_reference = "bd319622-1a45-4075-811a-2b0399bf9a49"
}
}
data "nutanix_vpc_v4" "test" {
ext_id = nutanix_vpc_v4.test.ext_id
depends_on = [
resource.nutanix_vpc_v4.test
]
}
`, name, desc)
}
187 changes: 173 additions & 14 deletions nutanix/services/v2/networking/data_source_nutanix_vpcs_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package networking

import (
"context"
"encoding/json"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand Down Expand Up @@ -40,7 +39,172 @@ func DataSourceNutanixVPCsv4() *schema.Resource {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: DataSourceVPCSchemaV4(),
Schema: map[string]*schema.Schema{
"ext_id": {
Type: schema.TypeString,
Computed: true,
},
"tenant_id": {
Type: schema.TypeString,
Computed: true,
},
"links": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"href": {
Type: schema.TypeString,
Computed: true,
},
"rel": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"metadata": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: DatasourceMetadataSchemaV4(),
},
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"common_dhcp_options": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"domain_name_servers": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ipv4": SchemaForValuePrefixLength(),
"ipv6": SchemaForValuePrefixLength(),
},
},
},
},
},
},
"vpc_type": {
Type: schema.TypeString,
Computed: true,
},
"snat_ips": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ipv4": SchemaForValuePrefixLength(),
"ipv6": SchemaForValuePrefixLength(),
},
},
},
"external_subnets": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"subnet_reference": {
Type: schema.TypeString,
Computed: true,
},
"external_ips": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ipv4": SchemaForValuePrefixLength(),
"ipv6": SchemaForValuePrefixLength(),
},
},
},
"gateway_nodes": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"active_gateway_node": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"node_id": {
Type: schema.TypeString,
Computed: true,
},
"node_ip_address": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ipv4": SchemaForValuePrefixLength(),
"ipv6": SchemaForValuePrefixLength(),
},
},
},
},
},
},
"active_gateway_count": {
Type: schema.TypeInt,
Computed: true,
},
},
},
},
"external_routing_domain_reference": {
Type: schema.TypeString,
Computed: true,
},
"externally_routable_prefixes": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ipv4": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ip": SchemaForValuePrefixLength(),
"prefix_length": {
Type: schema.TypeInt,
Computed: true,
},
},
},
},
"ipv6": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ip": SchemaForValuePrefixLength(),
"prefix_length": {
Type: schema.TypeInt,
Computed: true,
},
},
},
},
},
},
},
},
},
},
},
Expand Down Expand Up @@ -81,20 +245,15 @@ func dataSourceNutanixVPCsv4Read(ctx context.Context, d *schema.ResourceData, me
}
resp, err := conn.VpcAPIInstance.ListVpcs(page, limit, filter, orderBy, selects)
if err != nil {
var errordata map[string]interface{}
e := json.Unmarshal([]byte(err.Error()), &errordata)
if e != nil {
return diag.FromErr(e)
}
data := errordata["data"].(map[string]interface{})
errorList := data["error"].([]interface{})
errorMessage := errorList[0].(map[string]interface{})
return diag.Errorf("error while fetching subnets : %v", errorMessage["message"])
return diag.Errorf("error while fetching vpcs : %v", err)
}
getResp := resp.Data.GetValue().([]import1.Vpc)
getResp := resp.Data

if err := d.Set("vpcs", flattenVPCsEntities(getResp)); err != nil {
return diag.FromErr(err)
if getResp != nil {
tmp := getResp.GetValue().([]import1.Vpc)
if err := d.Set("vpcs", flattenVPCsEntities(tmp)); err != nil {
return diag.FromErr(err)
}
}

d.SetId(resource.UniqueId())
Expand Down
Loading

0 comments on commit 4770a95

Please sign in to comment.