forked from nutanix/terraform-provider-nutanix
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
56d3d0f
commit 7cea969
Showing
4 changed files
with
360 additions
and
0 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
248 changes: 248 additions & 0 deletions
248
nutanix/services/v2/networking/data_source_nutanix_route_table_v2.go
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,248 @@ | ||
package networking | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
|
||
"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 DatasourceNutanixRouteTableV4() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: DatasourceNutanixRouteTableV4Read, | ||
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 DatasourceNutanixRouteTableV4Read(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
conn := meta.(*conns.Client).NetworkingAPI | ||
|
||
extID := d.Get("ext_id") | ||
resp, err := conn.RoutesTable.GetRouteTable(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 route table : %v", errorMessage["message"]) | ||
} | ||
|
||
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) | ||
route["next_hop_reference"] = v.NexthopReference | ||
route["next_hop_ip_address"] = flattenNodeIPAddress(v.NexthopIpAddress) | ||
route["next_hop_name"] = v.NexthopName | ||
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" | ||
} |
108 changes: 108 additions & 0 deletions
108
nutanix/services/v2/networking/data_source_nutanix_route_tables_v2.go
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,108 @@ | ||
package networking | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"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 DatasourceNutanixRouteTablesV4() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: DatasourceNutanixRouteTablesV4Read, | ||
Schema: map[string]*schema.Schema{ | ||
"page": { | ||
Type: schema.TypeInt, | ||
Optional: true, | ||
}, | ||
"limit": { | ||
Type: schema.TypeInt, | ||
Optional: true, | ||
}, | ||
"filter": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"route_tables": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: DatasourceNutanixRouteTableV4(), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func DatasourceNutanixRouteTablesV4Read(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
conn := meta.(*conns.Client).NetworkingAPI | ||
|
||
// initialize query params | ||
var filter *string | ||
var page, limit *int | ||
|
||
if pagef, ok := d.GetOk("page"); ok { | ||
page = utils.IntPtr(pagef.(int)) | ||
} else { | ||
page = nil | ||
} | ||
if limitf, ok := d.GetOk("limit"); ok { | ||
limit = utils.IntPtr(limitf.(int)) | ||
} else { | ||
limit = nil | ||
} | ||
if filterf, ok := d.GetOk("filter"); ok { | ||
filter = utils.StringPtr(filterf.(string)) | ||
} else { | ||
filter = nil | ||
} | ||
|
||
resp, err := conn.RoutesTable.ListRouteTables(page, limit, filter) | ||
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 route tables : %v", errorMessage["message"]) | ||
} | ||
|
||
getResp := resp.Data.GetValue().([]import1.RouteTable) | ||
|
||
if err := d.Set("route_tables", flattenRouteTableEntities(getResp)); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
d.SetId(resource.UniqueId()) | ||
return nil | ||
} | ||
|
||
func flattenRouteTableEntities(pr []import1.RouteTable) []interface{} { | ||
if len(pr) > 0 { | ||
routes := make([]interface{}, len(pr)) | ||
|
||
for k, v := range pr { | ||
route := make(map[string]interface{}) | ||
|
||
route["ext_id"] = v.ExtId | ||
route["tenant_id"] = v.TenantId | ||
route["links"] = flattenLinks(v.Links) | ||
route["metadata"] = flattenMetadata(v.Metadata) | ||
route["vpc_reference"] = v.VpcReference | ||
route["external_routing_domain_reference"] = v.ExternalRoutingDomainReference | ||
route["static_routes"] = flattenRoute(v.StaticRoutes) | ||
route["dynamic_routes"] = flattenRoute(v.DynamicRoutes) | ||
route["local_routes"] = flattenRoute(v.LocalRoutes) | ||
|
||
routes[k] = route | ||
} | ||
return routes | ||
} | ||
return nil | ||
} |