/// <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(); NetworkDomain networkDomain; switch (ParameterSetName) { case "From network domain": { networkDomain = NetworkDomain; break; } case "By Id": { networkDomain = await client.GetNetworkDomain(Id, cancellationToken); if (networkDomain == null) { WriteError( Errors.ResourceNotFoundById <NetworkDomain>(Id) ); return; } break; } default: { ThrowTerminatingError( Errors.UnrecognizedParameterSet(this) ); return; } } if (!ShouldProcess(target: $"network domain '{networkDomain.Id}' ('{networkDomain.Name}') in '{networkDomain.DatacenterId}'", action: "Destroy")) { return; } ApiResponseV2 apiResponse = await client.DeleteNetworkDomain(networkDomain.Id, cancellationToken); if (!apiResponse.IsSuccess()) { WriteError( Errors.CloudControlApi(client, apiResponse) ); } else { WriteObject(apiResponse); } }
/// <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; } } if (!ShouldProcess(target: $"VLAN '{vlan.Id}' ('{vlan.Name}') in '{vlan.NetworkDomain.Name}'", action: "Edit")) { return; } ApiResponseV2 editResponse = await client.EditVlan(vlan, cancellationToken); if (!editResponse.IsSuccess()) { WriteError( Errors.CloudControlApi(client, editResponse) ); return; } Vlan updatedVlan = await client.GetVlan(vlan.Id, cancellationToken); if (updatedVlan == null) { WriteError( Errors.ResourceNotFoundById <Vlan>(vlan.Id) ); return; } WriteObject(updatedVlan); }
/// <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); }