/// <summary> /// Calculate the IPv4 network start address, assuming the specified network prefix size. /// </summary> /// <param name="baseAddress"> /// The IP address acting as the network base address. /// </param> /// <param name="prefixSize"> /// The number of bits in the network prefix (1-31). /// </param> /// <returns> /// Receives the first IP address in the network. /// </returns> public static IPAddress CalculateIPv4NetworkStartAddress(this IPAddress baseAddress, int prefixSize) { IPAddress startAddress, endAddress; baseAddress.CalculateIPv4NetworkAddresses(prefixSize, out startAddress, out endAddress); return(startAddress); }
/// <summary> /// Asynchronously perform Cmdlet processing. /// </summary> /// <param name="cancellationToken"> /// A <see cref="CancellationToken"/> that can be used to cancel Cmdlet processing. /// </param> /// <returns> /// A <see cref="Task"/> representing the asynchronous operation. /// </returns> protected override async Task ProcessRecordAsync(CancellationToken cancellationToken) { CloudControlClient client = GetClient(); Vlan vlan; switch (ParameterSetName) { case "From VLAN": { vlan = VLAN; break; } case "By Id": { vlan = await client.GetVlan(Id, cancellationToken); if (vlan == null) { WriteError( Errors.ResourceNotFoundById <Vlan>(Id) ); return; } break; } default: { ThrowTerminatingError( Errors.UnrecognizedParameterSet(this) ); return; } } IPAddress baseAddress = IPAddress.Parse(vlan.PrivateIPv4Range.Address); IPAddress existingStartAddress, existingEndAddress; baseAddress.CalculateIPv4NetworkAddresses(vlan.PrivateIPv4Range.PrefixSize, out existingStartAddress, out existingEndAddress ); string existingNetwork = $"{baseAddress}/{vlan.PrivateIPv4Range.PrefixSize} ({existingStartAddress}-{existingEndAddress})"; IPAddress targetStartAddress, targetEndAddress; baseAddress.CalculateIPv4NetworkAddresses(IPv4PrefixSize, out targetStartAddress, out targetEndAddress ); string targetNetwork = $"{baseAddress}/{IPv4PrefixSize} ({targetStartAddress}-{targetEndAddress})"; if (IPv4PrefixSize >= vlan.PrivateIPv4Range.PrefixSize) { InvalidParameter(nameof(IPv4PrefixSize), $"Cannot expand VLAN network from {existingNetwork} to {targetNetwork}. To expand the VLAN's IPv4 network, reduce its IPv4 prefix size." ); return; } WriteVerbose( $"Expanding VLAN '{vlan.Name}' (in network domain '{vlan.NetworkDomain.Name}') from {existingNetwork} to {targetNetwork}." ); if (!ShouldProcess(target: $"'{vlan.Name}' ('{vlan.Id}') in '{vlan.NetworkDomain.Name}' (from {existingNetwork} to {targetNetwork}).", action: "Expand")) { return; } WriteVerbose("Initiating expansion of VLAN..."); ApiResponseV2 editResponse = await client.ExpandVlan(vlan.Id, IPv4PrefixSize, cancellationToken); if (!editResponse.IsSuccess()) { WriteError( Errors.CloudControlApi(client, editResponse) ); return; } WriteVerbose("VLAN expansion initiated."); Vlan updatedVlan = await client.GetVlan(vlan.Id, cancellationToken); if (updatedVlan == null) { WriteError( Errors.ResourceNotFoundById <Vlan>(vlan.Id) ); return; } WriteObject(updatedVlan); }