Skip to content

Commit

Permalink
Merge pull request #31 from Edge-Center/feat/CLOUDDEV-343-network-det…
Browse files Browse the repository at this point in the history
…ails

feat(CLOUDDEV-343): shared networks with details
  • Loading branch information
anaxaim authored Nov 17, 2023
2 parents 8d42e4a + 294620f commit c5f7841
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 32 deletions.
27 changes: 27 additions & 0 deletions docs/data-sources/network.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ output "view" {
- `project_name` (String) The name of the project. Either 'project_id' or 'project_name' must be specified.
- `region_id` (Number) The uuid of the region. Either 'region_id' or 'region_name' must be specified.
- `region_name` (String) The name of the region. Either 'region_id' or 'region_name' must be specified.
- `shared_with_subnets` (Boolean) Get shared networks with details of subnets.

### Read-Only

Expand All @@ -59,6 +60,7 @@ output "view" {
- `metadata_read_only` (List of Object) A list of read-only metadata items, e.g. tags. (see [below for nested schema](#nestedatt--metadata_read_only))
- `mtu` (Number) Maximum Transmission Unit (MTU) for the network. It determines the maximum packet size that can be transmitted without fragmentation.
- `shared` (Boolean)
- `subnets` (Block List) A list of read-only metadata items, e.g. tags. (see [below for nested schema](#nestedblock--subnets))
- `type` (String) 'vlan' or 'vxlan' network type is allowed. Default value is 'vxlan'

<a id="nestedatt--metadata_read_only"></a>
Expand All @@ -71,3 +73,28 @@ Read-Only:
- `value` (String)


<a id="nestedblock--subnets"></a>
### Nested Schema for `subnets`

Read-Only:

- `available_ips` (Number) The number of available IPs in the subnet.
- `cidr` (String) Represents the IP address range of the subnet.
- `dns_nameservers` (List of String) List of DNS name servers for the subnet.
- `enable_dhcp` (Boolean) Enable DHCP for this subnet. If true, DHCP will be used to assign IP addresses to instances within this subnet.
- `gateway_ip` (String) The IP address of the gateway for this subnet.
- `has_router` (Boolean) Indicates whether the subnet has a router attached to it.
- `host_routes` (List of Object) List of additional routes to be added to instances that are part of this subnet. (see [below for nested schema](#nestedatt--subnets--host_routes))
- `id` (String) The ID of the subnet.
- `name` (String) The name of the subnet.
- `total_ips` (Number) The total number of IPs in the subnet.

<a id="nestedatt--subnets--host_routes"></a>
### Nested Schema for `subnets.host_routes`

Read-Only:

- `destination` (String)
- `nexthop` (String)


133 changes: 115 additions & 18 deletions edgecenter/data_source_edgecenter_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (

"github.com/Edge-Center/edgecentercloud-go/edgecenter/network/v1/availablenetworks"
"github.com/Edge-Center/edgecentercloud-go/edgecenter/network/v1/networks"
"github.com/Edge-Center/edgecentercloud-go/edgecenter/subnet/v1/subnets"
"github.com/Edge-Center/edgecentercloud-go/edgecenter/utils/metadata"
)

func dataSourceNetwork() *schema.Resource {
Expand Down Expand Up @@ -45,6 +47,11 @@ func dataSourceNetwork() *schema.Resource {
Required: true,
Description: "The name of the network.",
},
"shared_with_subnets": {
Type: schema.TypeBool,
Optional: true,
Description: "Get shared networks with details of subnets.",
},
"mtu": {
Type: schema.TypeInt,
Computed: true,
Expand All @@ -63,6 +70,82 @@ func dataSourceNetwork() *schema.Resource {
Type: schema.TypeBool,
Computed: true,
},
"subnets": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Description: `A list of read-only metadata items, e.g. tags.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The ID of the subnet.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The name of the subnet.",
},
"available_ips": {
Type: schema.TypeInt,
Computed: true,
Description: "The number of available IPs in the subnet.",
},
"total_ips": {
Type: schema.TypeInt,
Computed: true,
Description: "The total number of IPs in the subnet.",
},
"enable_dhcp": {
Type: schema.TypeBool,
Computed: true,
Description: "Enable DHCP for this subnet. If true, DHCP will be used to assign IP addresses to instances within this subnet.",
},
"has_router": {
Type: schema.TypeBool,
Computed: true,
Description: "Indicates whether the subnet has a router attached to it.",
},
"cidr": {
Type: schema.TypeString,
Computed: true,
Description: "Represents the IP address range of the subnet.",
},
"dns_nameservers": {
Type: schema.TypeList,
Computed: true,
Description: "List of DNS name servers for the subnet.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"host_routes": {
Type: schema.TypeList,
Computed: true,
Description: "List of additional routes to be added to instances that are part of this subnet.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"destination": {
Type: schema.TypeString,
Computed: true,
},
"nexthop": {
Type: schema.TypeString,
Computed: true,
Description: "IPv4 address to forward traffic to if it's destination IP matches 'destination' CIDR",
},
},
},
},
"gateway_ip": {
Type: schema.TypeString,
Computed: true,
Description: "The IP address of the gateway for this subnet.",
},
},
},
},
"metadata_k": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -131,31 +214,38 @@ func dataSourceNetworkRead(_ context.Context, d *schema.ResourceData, m interfac
metaOpts.MetadataKV = typedMetadataKV
}

nets, err := networks.ListAll(client, *metaOpts)
if err != nil {
return diag.FromErr(err)
}
var (
withDetails = d.Get("shared_with_subnets").(bool)
rawNetwork map[string]interface{}
subs []subnets.Subnet
meta []metadata.Metadata
)

// todo refactor, also refactor inner func
var rawNetwork map[string]interface{}
network, found := findNetworkByName(name, nets)
if !found {
// trying to find among shared networks
nets, err := availablenetworks.ListAll(clientShared, nil)
if !withDetails {
nets, err := networks.ListAll(client, *metaOpts)
if err != nil {
return diag.FromErr(err)
}
network, found := findSharedNetworkByName(name, nets)
network, found := findNetworkByName(name, nets)
if !found {
return diag.Errorf("network with name %s not found", name)
return diag.Errorf("network with name %s not found. you can try to set 'shared_with_subnets' parameter", name)
}

meta = network.Metadata
rawNetwork, err = StructToMap(network)
if err != nil {
return diag.FromErr(err)
}
} else {
rawNetwork, err = StructToMap(network)
nets, err := availablenetworks.ListAll(clientShared, nil)
if err != nil {
return diag.FromErr(err)
}
sharedNetwork, found := findSharedNetworkByName(name, nets)
if !found {
return diag.Errorf("shared network with name %s not found", name)
}
subs = sharedNetwork.Subnets
rawNetwork, err = StructToMap(sharedNetwork)
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -169,10 +259,17 @@ func dataSourceNetworkRead(_ context.Context, d *schema.ResourceData, m interfac
d.Set("project_id", rawNetwork["project_id"])
d.Set("external", rawNetwork["external"])
d.Set("shared", rawNetwork["shared"])

metadataReadOnly := PrepareMetadataReadonly(network.Metadata)
if err := d.Set("metadata_read_only", metadataReadOnly); err != nil {
return diag.FromErr(err)
if withDetails {
if len(subs) > 0 {
if err := d.Set("subnets", prepareSubnets(subs)); err != nil {
return diag.FromErr(err)
}
}
} else {
metadataReadOnly := PrepareMetadataReadonly(meta)
if err := d.Set("metadata_read_only", metadataReadOnly); err != nil {
return diag.FromErr(err)
}
}

log.Println("[DEBUG] Finish Network reading")
Expand Down
16 changes: 2 additions & 14 deletions edgecenter/data_source_edgecenter_subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,20 +189,8 @@ func dataSourceSubnetRead(_ context.Context, d *schema.ResourceData, m interface
return diag.FromErr(err)
}

dns := make([]string, len(subnet.DNSNameservers))
for i, ns := range subnet.DNSNameservers {
dns[i] = ns.String()
}
d.Set("dns_nameservers", dns)

hrs := make([]map[string]string, len(subnet.HostRoutes))
for i, hr := range subnet.HostRoutes {
hR := map[string]string{"destination": "", "nexthop": ""}
hR["destination"] = hr.Destination.String()
hR["nexthop"] = hr.NextHop.String()
hrs[i] = hR
}
d.Set("host_routes", hrs)
d.Set("dns_nameservers", dnsNameserversToStringList(subnet.DNSNameservers))
d.Set("host_routes", hostRoutesToListOfMaps(subnet.HostRoutes))
d.Set("region_id", subnet.RegionID)
d.Set("project_id", subnet.ProjectID)
d.Set("gateway_ip", subnet.GatewayIP.String())
Expand Down
43 changes: 43 additions & 0 deletions edgecenter/utils_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package edgecenter

import (
"encoding/json"
"net"

"github.com/Edge-Center/edgecentercloud-go/edgecenter/network/v1/availablenetworks"
"github.com/Edge-Center/edgecentercloud-go/edgecenter/network/v1/networks"
"github.com/Edge-Center/edgecentercloud-go/edgecenter/subnet/v1/subnets"
)

// findNetworkByName searches for a network with the given name among the given networks.
Expand Down Expand Up @@ -45,3 +47,44 @@ func StructToMap(obj interface{}) (map[string]interface{}, error) {

return newMap, nil
}

func dnsNameserversToStringList(dnsNameservers []net.IP) []string {
dns := make([]string, len(dnsNameservers))
for i, ns := range dnsNameservers {
dns[i] = ns.String()
}

return dns
}

func hostRoutesToListOfMaps(hostRoutes []subnets.HostRoute) []map[string]string {
hrs := make([]map[string]string, len(hostRoutes))
for i, hr := range hostRoutes {
hR := map[string]string{"destination": "", "nexthop": ""}
hR["destination"] = hr.Destination.String()
hR["nexthop"] = hr.NextHop.String()
hrs[i] = hR
}

return hrs
}

func prepareSubnets(subs []subnets.Subnet) []map[string]interface{} {
subnetList := make([]map[string]interface{}, 0, len(subs))
for _, s := range subs {
subnetList = append(subnetList, map[string]interface{}{
"id": s.ID,
"name": s.Name,
"enable_dhcp": s.EnableDHCP,
"cidr": s.CIDR.String(),
"available_ips": s.AvailableIps,
"total_ips": s.TotalIps,
"has_router": s.HasRouter,
"dns_nameservers": dnsNameserversToStringList(s.DNSNameservers),
"host_routes": hostRoutesToListOfMaps(s.HostRoutes),
"gateway_ip": s.GatewayIP.String(),
})
}

return subnetList
}

0 comments on commit c5f7841

Please sign in to comment.