@@ -288,34 +288,6 @@ public async Task<string> CreateSecurityGroup(string groupName, string groupDesc
288288
289289 // snippet-end:[EC2.dotnetv4.CreateSecurityGroup]
290290
291- // snippet-start:[EC2.dotnetv4.CreateVPC]
292- /// <summary>
293- /// Create a new Amazon EC2 VPC.
294- /// </summary>
295- /// <param name="cidrBlock">The CIDR block for the new security group.</param>
296- /// <returns>The VPC Id of the new VPC.</returns>
297- public async Task < string ? > CreateVPC ( string cidrBlock )
298- {
299-
300- try
301- {
302- var response = await _amazonEC2 . CreateVpcAsync ( new CreateVpcRequest
303- {
304- CidrBlock = cidrBlock ,
305- } ) ;
306-
307- Vpc vpc = response . Vpc ;
308- Console . WriteLine ( $ "Created VPC with ID: { vpc . VpcId } .") ;
309- return vpc . VpcId ;
310- }
311- catch ( AmazonEC2Exception ex )
312- {
313- Console . WriteLine ( $ "Couldn't create VPC because: { ex . Message } ") ;
314- return null ;
315- }
316- }
317- // snippet-end:[EC2.dotnetv4.CreateVPC]
318-
319291 // snippet-start:[EC2.dotnetv4.DeleteKeyPair]
320292 /// <summary>
321293 /// Delete an Amazon EC2 key pair.
@@ -391,24 +363,6 @@ await _amazonEC2.DeleteSecurityGroupAsync(
391363 }
392364 // snippet-end:[EC2.dotnetv4.DeleteSecurityGroup]
393365
394- // snippet-start:[EC2.dotnetv4.DeleteVPC]
395- /// <summary>
396- /// Delete an Amazon EC2 VPC.
397- /// </summary>
398- /// <returns>A Boolean value indicating the success of the action.</returns>
399- public async Task < bool > DeleteVpc ( string vpcId )
400- {
401- var request = new DeleteVpcRequest
402- {
403- VpcId = vpcId ,
404- } ;
405-
406- var response = await _amazonEC2 . DeleteVpcAsync ( request ) ;
407-
408- return response . HttpStatusCode == System . Net . HttpStatusCode . OK ;
409- }
410- // snippet-end:[EC2.dotnetv4.DeleteVPC]
411-
412366 // snippet-start:[EC2.dotnetv4.DescribeImages]
413367 /// <summary>
414368 /// Get information about existing Amazon EC2 images.
@@ -428,20 +382,6 @@ public async Task<List<Image>> DescribeImages(List<string>? imageIds)
428382 return response . Images ;
429383 }
430384
431- /// <summary>
432- /// Display the information returned by DescribeImages.
433- /// </summary>
434- /// <param name="images">The list of image information to display.</param>
435- public void DisplayImageInfo ( List < Image > images )
436- {
437- images . ForEach ( image =>
438- {
439- Console . WriteLine ( $ "{ image . Name } Created on: { image . CreationDate } ") ;
440- } ) ;
441-
442- }
443- // snippet-end:[EC2.dotnetv4.DescribeImages]
444-
445385 // snippet-start:[EC2.dotnetv4.DescribeInstance]
446386 /// <summary>
447387 /// Get information about an Amazon EC2 instance.
@@ -471,62 +411,6 @@ public void DisplayInstanceInformation(Instance instance)
471411 }
472412 // snippet-end:[EC2.dotnetv4.DescribeInstance]
473413
474- // snippet-start:[EC2.dotnetv4.DescribeInstances]
475- /// <summary>
476- /// Get information about EC2 instances with a particular state.
477- /// </summary>
478- /// <param name="tagName">The name of the tag to filter on.</param>
479- /// <param name="tagValue">The value of the tag to look for.</param>
480- /// <returns>True if successful.</returns>
481- public async Task < bool > GetInstancesWithState ( string state )
482- {
483- try
484- {
485- // Filters the results of the instance list.
486- var filters = new List < Filter >
487- {
488- new Filter
489- {
490- Name = $ "instance-state-name",
491- Values = new List < string > { state , } ,
492- } ,
493- } ;
494- var request = new DescribeInstancesRequest { Filters = filters , } ;
495-
496- Console . WriteLine ( $ "\n Showing instances with state { state } ") ;
497- var paginator = _amazonEC2 . Paginators . DescribeInstances ( request ) ;
498-
499- await foreach ( var response in paginator . Responses )
500- {
501- foreach ( var reservation in response . Reservations )
502- {
503- foreach ( var instance in reservation . Instances )
504- {
505- Console . Write ( $ "Instance ID: { instance . InstanceId } ") ;
506- Console . WriteLine ( $ "\t Current State: { instance . State . Name } ") ;
507- }
508- }
509- }
510-
511- return true ;
512- }
513- catch ( AmazonEC2Exception ec2Exception )
514- {
515- if ( ec2Exception . ErrorCode == "InvalidParameterValue" )
516- {
517- _logger . LogError (
518- $ "Invalid parameter value for filtering instances.") ;
519- }
520-
521- return false ;
522- }
523- catch ( Exception ex )
524- {
525- Console . WriteLine ( $ "Couldn't list instances because: { ex . Message } ") ;
526- return false ;
527- }
528- }
529- // snippet-end:[EC2.dotnetv4.DescribeInstances]
530414
531415 // snippet-start:[EC2.dotnetv4.DescribeInstanceTypes]
532416 /// <summary>
@@ -585,6 +469,7 @@ public async Task<List<KeyPairInfo>> DescribeKeyPairs(string keyPairName)
585469 {
586470 try
587471 {
472+ var pairs = new List < KeyPairInfo > ( ) ;
588473 var request = new DescribeKeyPairsRequest ( ) ;
589474 if ( ! string . IsNullOrEmpty ( keyPairName ) )
590475 {
@@ -595,7 +480,12 @@ public async Task<List<KeyPairInfo>> DescribeKeyPairs(string keyPairName)
595480 }
596481
597482 var response = await _amazonEC2 . DescribeKeyPairsAsync ( request ) ;
598- return response . KeyPairs . ToList ( ) ;
483+ if ( response . KeyPairs != null )
484+ {
485+ pairs = response . KeyPairs . ToList ( ) ;
486+ }
487+ return pairs ;
488+
599489 }
600490 catch ( AmazonEC2Exception ec2Exception )
601491 {
@@ -674,36 +564,36 @@ public void DisplaySecurityGroupInfoAsync(SecurityGroup securityGroup)
674564 {
675565 Console . WriteLine ( $ "{ securityGroup . GroupName } ") ;
676566 Console . WriteLine ( "Ingress permissions:" ) ;
677- securityGroup . IpPermissions . ForEach ( permission =>
567+ securityGroup . IpPermissions ? . ForEach ( permission =>
678568 {
679569 Console . WriteLine ( $ "\t FromPort: { permission . FromPort } ") ;
680570 Console . WriteLine ( $ "\t IpProtocol: { permission . IpProtocol } ") ;
681571
682572 Console . Write ( $ "\t Ipv4Ranges: ") ;
683- permission . Ipv4Ranges . ForEach ( range => { Console . Write ( $ "{ range . CidrIp } ") ; } ) ;
573+ permission . Ipv4Ranges ? . ForEach ( range => { Console . Write ( $ "{ range . CidrIp } ") ; } ) ;
684574
685575 Console . WriteLine ( $ "\n \t Ipv6Ranges:") ;
686- permission . Ipv6Ranges . ForEach ( range => { Console . Write ( $ "{ range . CidrIpv6 } ") ; } ) ;
576+ permission . Ipv6Ranges ? . ForEach ( range => { Console . Write ( $ "{ range . CidrIpv6 } ") ; } ) ;
687577
688578 Console . Write ( $ "\n \t PrefixListIds: ") ;
689- permission . PrefixListIds . ForEach ( id => Console . Write ( $ "{ id . Id } ") ) ;
579+ permission . PrefixListIds ? . ForEach ( id => Console . Write ( $ "{ id . Id } ") ) ;
690580
691581 Console . WriteLine ( $ "\n \t To Port: { permission . ToPort } ") ;
692582 } ) ;
693583 Console . WriteLine ( "Egress permissions:" ) ;
694- securityGroup . IpPermissionsEgress . ForEach ( permission =>
584+ securityGroup . IpPermissionsEgress ? . ForEach ( permission =>
695585 {
696586 Console . WriteLine ( $ "\t FromPort: { permission . FromPort } ") ;
697587 Console . WriteLine ( $ "\t IpProtocol: { permission . IpProtocol } ") ;
698588
699589 Console . Write ( $ "\t Ipv4Ranges: ") ;
700- permission . Ipv4Ranges . ForEach ( range => { Console . Write ( $ "{ range . CidrIp } ") ; } ) ;
590+ permission . Ipv4Ranges ? . ForEach ( range => { Console . Write ( $ "{ range . CidrIp } ") ; } ) ;
701591
702592 Console . WriteLine ( $ "\n \t Ipv6Ranges:") ;
703- permission . Ipv6Ranges . ForEach ( range => { Console . Write ( $ "{ range . CidrIpv6 } ") ; } ) ;
593+ permission . Ipv6Ranges ? . ForEach ( range => { Console . Write ( $ "{ range . CidrIpv6 } ") ; } ) ;
704594
705595 Console . Write ( $ "\n \t PrefixListIds: ") ;
706- permission . PrefixListIds . ForEach ( id => Console . Write ( $ "{ id . Id } ") ) ;
596+ permission . PrefixListIds ? . ForEach ( id => Console . Write ( $ "{ id . Id } ") ) ;
707597
708598 Console . WriteLine ( $ "\n \t To Port: { permission . ToPort } ") ;
709599 } ) ;
@@ -744,60 +634,6 @@ public async Task<bool> DisassociateIp(string associationId)
744634 }
745635 // snippet-end:[EC2.dotnetv4.DisassociateAddress]
746636
747- // snippet-start:[EC2.dotnetv4.GetAMIList]
748- /// <summary>
749- /// Retrieve a list of available Amazon Linux images.
750- /// </summary>
751- /// <returns>A list of image information.</returns>
752- public async Task < List < Image > > GetEC2AmiList ( )
753- {
754- var filter = new Filter { Name = "architecture" , Values = new List < string > { "x86_64" } } ;
755- var filters = new List < Filter > { filter } ;
756- var response = await _amazonEC2 . DescribeImagesAsync ( new DescribeImagesRequest { Filters = filters } ) ;
757- return response . Images ;
758- }
759- // snippet-end:[EC2.dotnetv4.GetAMIList]
760-
761- // snippet-start:[EC2.dotnetv4.RebootInstances]
762- /// <summary>
763- /// Reboot a specific EC2 instance.
764- /// </summary>
765- /// <param name="ec2InstanceId">The instance Id of the instance that will be rebooted.</param>
766- /// <returns>Async Task.</returns>
767- public async Task < bool > RebootInstances ( string ec2InstanceId )
768- {
769- try
770- {
771- var request = new RebootInstancesRequest
772- {
773- InstanceIds = new List < string > { ec2InstanceId } ,
774- } ;
775-
776- await _amazonEC2 . RebootInstancesAsync ( request ) ;
777-
778- // Wait for the instance to be running.
779- Console . Write ( "Waiting for the instance to start." ) ;
780- await WaitForInstanceState ( ec2InstanceId , InstanceStateName . Running ) ;
781-
782- return true ;
783- }
784- catch ( AmazonEC2Exception ec2Exception )
785- {
786- if ( ec2Exception . ErrorCode == "InvalidInstanceId" )
787- {
788- _logger . LogError (
789- $ "InstanceId { ec2InstanceId } is invalid, unable to reboot. { ec2Exception . Message } ") ;
790- }
791- return false ;
792- }
793- catch ( Exception ex )
794- {
795- _logger . LogError (
796- $ "An error occurred while rebooting the instance { ec2InstanceId } .: { ex . Message } ") ;
797- return false ;
798- }
799- }
800- // snippet-end:[EC2.dotnetv4.RebootInstances]
801637
802638 // snippet-start:[EC2.dotnetv4.ReleaseAddress]
803639 /// <summary>
@@ -812,8 +648,8 @@ public async Task<bool> ReleaseAddress(string allocationId)
812648 {
813649 var request = new ReleaseAddressRequest { AllocationId = allocationId } ;
814650
815- var response = await _amazonEC2 . ReleaseAddressAsync ( request ) ;
816- return response . HttpStatusCode == HttpStatusCode . OK ;
651+ await _amazonEC2 . ReleaseAddressAsync ( request ) ;
652+ return true ;
817653 }
818654 catch ( AmazonEC2Exception ec2Exception )
819655 {
0 commit comments