Skip to content

Commit

Permalink
Feat/v4 static routes on v4 networks (#14)
Browse files Browse the repository at this point in the history
* datasource for pbrs

* lint fixes. go error (gomnd, gosimple, golint)

* go checks, magic numbers(gomnd)

* fix config testcase as base client will differ in sdks

* datasourc for route tables

* resource for static route

* tests and docs for pbrs

* docs for route table

* docs for static route

* lint fixes

* remove other services

* change module name from v4 to v2

* change package name to networkingv2

* change package name to networkingv2

* remove other services

---------

Co-authored-by: Abhishek <[email protected]>
  • Loading branch information
Haroon-Dweikat-Ntx and abhimutant authored Sep 6, 2024
1 parent 4ff3f14 commit 1a966d9
Show file tree
Hide file tree
Showing 8 changed files with 993 additions and 0 deletions.
3 changes: 3 additions & 0 deletions nutanix/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ func Provider() *schema.Provider {
"nutanix_vpcs_v2": networkingv2.DataSourceNutanixVPCsv2(),
"nutanix_floating_ip_v2": networkingv2.DatasourceNutanixFloatingIPV2(),
"nutanix_floating_ips_v2": networkingv2.DatasourceNutanixFloatingIPsV2(),
"nutanix_route_table_v2": networkingv2.DatasourceNutanixRouteTableV2(),
"nutanix_route_tables_v2": networkingv2.DatasourceNutanixRouteTablesV2(),
"nutanix_pbr_v2": networkingv2.DatasourceNutanixPbrV2(),
"nutanix_pbrs_v2": networkingv2.DatasourceNutanixPbrsV2(),
},
Expand Down Expand Up @@ -281,6 +283,7 @@ func Provider() *schema.Provider {
"nutanix_subnet_v2": networkingv2.ResourceNutanixSubnetV2(),
"nutanix_floating_ip_v2": networkingv2.ResourceNutanixFloatingIPv2(),
"nutanix_vpc_v2": networkingv2.ResourceNutanixVPCsV2(),
"nutanix_route_table_v2": networkingv2.ResourceNutanixRouteTablesV2(),
"nutanix_pbr_v2": networkingv2.ResourceNutanixPbrsV2(),
},
ConfigureContextFunc: providerConfigure,
Expand Down
3 changes: 3 additions & 0 deletions nutanix/sdks/v4/networking/networking.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
)

type Client struct {
RoutesTable *api.RouteTablesApi
APIClientInstance *network.ApiClient
RoutingPolicy *api.RoutingPoliciesApi
SubnetAPIInstance *api.SubnetsApi
VpcAPIInstance *api.VpcsApi
Expand All @@ -30,6 +32,7 @@ func NewNetworkingClient(credentials client.Credentials) (*Client, error) {
}

f := &Client{
RoutesTable: api.NewRouteTablesApi(baseClient),
RoutingPolicy: api.NewRoutingPoliciesApi(baseClient),
SubnetAPIInstance: api.NewSubnetsApi(baseClient),
VpcAPIInstance: api.NewVpcsApi(baseClient),
Expand Down
247 changes: 247 additions & 0 deletions nutanix/services/v2/networkingv2/data_source_nutanix_route_table_v2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
package networkingv2

import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
import1 "github.com/nutanix/ntnx-api-golang-clients/networking-go-client/v4/models/networking/v4/config"
conns "github.com/terraform-providers/terraform-provider-nutanix/nutanix"
"github.com/terraform-providers/terraform-provider-nutanix/utils"
)

func DatasourceNutanixRouteTableV2() *schema.Resource {
return &schema.Resource{
ReadContext: DatasourceNutanixRouteTableV2Read,
Schema: map[string]*schema.Schema{
"ext_id": {
Type: schema.TypeString,
Required: 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(),
},
},
"vpc_reference": {
Type: schema.TypeString,
Computed: true,
},
"external_routing_domain_reference": {
Type: schema.TypeString,
Computed: true,
},
"static_routes": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: DatasourceRoutesSchemaV4(),
},
},
"dynamic_routes": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: DatasourceRoutesSchemaV4(),
},
},
"local_routes": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: DatasourceRoutesSchemaV4(),
},
},
},
}
}

func DatasourceNutanixRouteTableV2Read(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.Client).NetworkingAPI

extID := d.Get("ext_id")
resp, err := conn.RoutesTable.GetRouteTableById(utils.StringPtr(extID.(string)))
if err != nil {
return diag.Errorf("error while fetching route table : %v", err)
}

getResp := resp.Data.GetValue().(import1.RouteTable)

if err := d.Set("links", flattenLinks(getResp.Links)); err != nil {
return diag.FromErr(err)
}
if err := d.Set("tenant_id", getResp.TenantId); err != nil {
return diag.FromErr(err)
}
if err := d.Set("metadata", flattenMetadata(getResp.Metadata)); err != nil {
return diag.FromErr(err)
}

if err := d.Set("vpc_reference", getResp.VpcReference); err != nil {
return diag.FromErr(err)
}
if err := d.Set("external_routing_domain_reference", getResp.ExternalRoutingDomainReference); err != nil {
return diag.FromErr(err)
}
if err := d.Set("static_routes", flattenRoute(getResp.StaticRoutes)); err != nil {
return diag.FromErr(err)
}
if err := d.Set("dynamic_routes", flattenRoute(getResp.DynamicRoutes)); err != nil {
return diag.FromErr(err)
}
if err := d.Set("local_routes", flattenRoute(getResp.LocalRoutes)); err != nil {
return diag.FromErr(err)
}

d.SetId(*getResp.ExtId)
return nil
}

func flattenRoute(pr []import1.Route) []interface{} {
if len(pr) > 0 {
routes := make([]interface{}, len(pr))

for k, v := range pr {
route := make(map[string]interface{})

route["is_active"] = v.IsActive
route["priority"] = v.Priority
route["destination"] = flattenIPSubnet(v.Destination)
route["next_hop_type"] = flattenNexthopType(v.NexthopType)
if v.NexthopReference != nil {
route["next_hop_reference"] = v.NexthopReference
}
if v.NexthopIpAddress != nil {
route["next_hop_ip_address"] = flattenNodeIPAddress(v.NexthopIpAddress)
}
if v.NexthopName != nil {
route["next_hop_name"] = v.NexthopName
}
if v.Source != nil {
route["source"] = v.Source
}

routes[k] = route
}
return routes
}
return nil
}

func DatasourceRoutesSchemaV4() map[string]*schema.Schema {
return map[string]*schema.Schema{
"is_active": {
Type: schema.TypeBool,
Computed: true,
},
"priority": {
Type: schema.TypeInt,
Computed: true,
},
"destination": {
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,
},
},
},
},
},
},
},
"next_hop_type": {
Type: schema.TypeString,
Computed: true,
},
"next_hop_reference": {
Type: schema.TypeString,
Computed: true,
},
"next_hop_ip_address": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ipv4": SchemaForValuePrefixLength(),
"ipv6": SchemaForValuePrefixLength(),
},
},
},
"next_hop_name": {
Type: schema.TypeString,
Computed: true,
},
"source": {
Type: schema.TypeString,
Computed: true,
},
}
}

func flattenNexthopType(pr *import1.NexthopType) string {
if pr != nil {
const two, three, four, five, six = 2, 3, 4, 5, 6

if *pr == import1.NexthopType(two) {
return "IP_ADDRESS"
}
if *pr == import1.NexthopType(three) {
return "DIRECT_CONNECT_VIF"
}
if *pr == import1.NexthopType(four) {
return "INTERNAL_SUBNET"
}
if *pr == import1.NexthopType(five) {
return "EXTERNAL_SUBNET"
}
if *pr == import1.NexthopType(six) {
return "VPN_CONNECTION"
}
}
return "UNKNOWN"
}
Loading

0 comments on commit 1a966d9

Please sign in to comment.