Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a function for subnetting by requested rules #1419

Merged

Conversation

yunkon-kim
Copy link
Member

  • Add models for subnetting request including CIDR block and rules
  • Add SubnettingBy() and related functions
  • Add GetName() which is missing member method
  • (Minor) Relocate or rename some existing functions

Related to #1412

This PR will add a user-friendly function for subnetting a CIDR block by request rules.
I think it's helpful to make a draft of a global muti-cloud network configuration, like the "global base network - VPCs - subnets" style.
Note - By adding a rule, more than 3 depths are possible.

Command

./netutil -c "10.0.0.0/16" -s 16 -n 500

Example (line 198-218)

In this example, a CIDR Block is divided into multiple subnets by a rule {Type: "minSubnets", Value: 16}.
Each subnet is divided again into multiple subnets by a rule {Type: "minHosts", Value: 500}.

	fmt.Println("\nSubnetting a CIDR block by requests")
	request := netutil.SubnettingRequest{
		CIDRBlock: cidrBlock,
		SubnettingRules: []netutil.SubnettingRule{
			{Type: "minSubnets", Value: minSubnets},
			{Type: "minHosts", Value: hostsPerSubnet},
		},
	}

	// Subnetting by requests
	networkConfig, err := netutil.SubnettingBy(request)
	if err != nil {
		fmt.Println("Error subnetting network:", err)
		return
	}

	pretty, err = json.MarshalIndent(networkConfig, "", "   ")
	if err != nil {
		fmt.Printf("marshaling error: %s\n", err)
	}
	fmt.Printf("[Subnetting result]\n%s\n", string(pretty))

[Subnetting result]

{
   "cidrBlock": "10.0.0.0/16",
   "subnets": [
      {
         "cidrBlock": "10.0.0.0/20",
         "subnets": [
            {
               "cidrBlock": "10.0.0.0/23"
            },
            {
               "cidrBlock": "10.0.2.0/23"
            },
            {
               "cidrBlock": "10.0.4.0/23"
            },
            {
               "cidrBlock": "10.0.6.0/23"
            },
            {
               "cidrBlock": "10.0.8.0/23"
            },
            {
               "cidrBlock": "10.0.10.0/23"
            },
            {
               "cidrBlock": "10.0.12.0/23"
            },
            {
               "cidrBlock": "10.0.14.0/23"
            }
         ]
      },
      {
         "cidrBlock": "10.0.16.0/20",
         "subnets": [
            {
               "cidrBlock": "10.0.16.0/23"
            },
            {
               "cidrBlock": "10.0.18.0/23"
            },
            {
               "cidrBlock": "10.0.20.0/23"
            },
            {
               "cidrBlock": "10.0.22.0/23"
            },
            {
               "cidrBlock": "10.0.24.0/23"
            },
            {
               "cidrBlock": "10.0.26.0/23"
            },
            {
               "cidrBlock": "10.0.28.0/23"
            },
            {
               "cidrBlock": "10.0.30.0/23"
            }
         ]
      },
      ...
   ]
}

This is the same result as #1417 (except name field).

* Add models for subnetting request including CIDR block and rules
* Add SubnettingBy() and related functions
* Add GetName() which is missing member method
@yunkon-kim
Copy link
Member Author

Additionally, considering this PR, it seems to provide the basic functions of netutil.

Resolve #1412

@seokho-son
Copy link
Member

/approve

Thanks!! :)

@github-actions github-actions bot added the approved This PR is approved and will be merged soon. label Jan 11, 2024
@cb-github-robot cb-github-robot merged commit 8ca744c into cloud-barista:main Jan 11, 2024
3 checks passed
@yunkon-kim yunkon-kim deleted the yunkon-kim-patch-240111 branch January 12, 2024 00:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved This PR is approved and will be merged soon.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants