From 42e9f66fa949c240ba476444d92846b0bb257d68 Mon Sep 17 00:00:00 2001 From: Meric Ozkayagan Date: Sun, 26 Feb 2023 16:41:40 +0300 Subject: [PATCH] changed outputs --- cmd/reboot.go | 5 +---- cmd/start.go | 5 +---- cmd/stop.go | 6 +----- src/ec2/filter_by_tag/filter_by_tag.go | 6 +++--- src/ec2/reboot_instance/reboot_instance.go | 8 ++++++-- .../start_stop_instance/start_stop_instance.go | 15 ++++++--------- 6 files changed, 18 insertions(+), 27 deletions(-) diff --git a/cmd/reboot.go b/cmd/reboot.go index ab7db1d..1edae7a 100644 --- a/cmd/reboot.go +++ b/cmd/reboot.go @@ -3,7 +3,6 @@ package cmd import ( "fmt" - "github.com/mericozkayagan/minecraft/src/ec2/filter_by_tag" "github.com/mericozkayagan/minecraft/src/ec2/reboot_instance" "github.com/spf13/cobra" ) @@ -14,9 +13,7 @@ var rebootCmd = &cobra.Command{ Long: `Reboot the instance`, Run: func(cmd *cobra.Command, args []string) { fmt.Println("reboot called") - - instanceId, publicIp := filter_by_tag.FilterByTag() - reboot_instance.RebootInstance(instanceId, publicIp) + reboot_instance.RebootInstance() }, } diff --git a/cmd/start.go b/cmd/start.go index 7af13c1..6258005 100644 --- a/cmd/start.go +++ b/cmd/start.go @@ -3,7 +3,6 @@ package cmd import ( "fmt" - "github.com/mericozkayagan/minecraft/src/ec2/filter_by_tag" "github.com/mericozkayagan/minecraft/src/ec2/start_stop_instance" "github.com/spf13/cobra" ) @@ -14,9 +13,7 @@ var startCmd = &cobra.Command{ Long: `Start the instance`, Run: func(cmd *cobra.Command, args []string) { fmt.Println("start called") - - instanceId, publicIp := filter_by_tag.FilterByTag() - start_stop_instance.StartStopInstance("start", instanceId, publicIp) + start_stop_instance.StartStopInstance("start") }, } diff --git a/cmd/stop.go b/cmd/stop.go index 7dfb64c..0d6c4e8 100644 --- a/cmd/stop.go +++ b/cmd/stop.go @@ -3,7 +3,6 @@ package cmd import ( "fmt" - "github.com/mericozkayagan/minecraft/src/ec2/filter_by_tag" "github.com/mericozkayagan/minecraft/src/ec2/start_stop_instance" "github.com/spf13/cobra" ) @@ -14,10 +13,7 @@ var stopCmd = &cobra.Command{ Long: `Stop the instance`, Run: func(cmd *cobra.Command, args []string) { fmt.Println("stop called") - - fmt.Println() - instanceId, publicIp := filter_by_tag.FilterByTag() - start_stop_instance.StartStopInstance("stop", instanceId, publicIp) + start_stop_instance.StartStopInstance("stop") }, } diff --git a/src/ec2/filter_by_tag/filter_by_tag.go b/src/ec2/filter_by_tag/filter_by_tag.go index b28fd51..c44ba5a 100644 --- a/src/ec2/filter_by_tag/filter_by_tag.go +++ b/src/ec2/filter_by_tag/filter_by_tag.go @@ -10,13 +10,13 @@ import ( "github.com/aws/aws-sdk-go/service/ec2" ) -func FilterByTag() (instanceId, publicIp *string) { +func FilterByTag() (instanceId *string, publicIp *ec2.InstanceNetworkInterfaceAssociation) { sess := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, })) svc := ec2.New(sess) - fmt.Printf("listing instances with the tag: Minecraft") + fmt.Println("listing instances with the tag: Minecraft") params := &ec2.DescribeInstancesInput{ Filters: []*ec2.Filter{ { @@ -33,5 +33,5 @@ func FilterByTag() (instanceId, publicIp *string) { log.Fatal(err.Error()) } - return resp.Reservations[0].Instances[0].InstanceId, resp.Reservations[0].Instances[0].PublicIpAddress + return resp.Reservations[0].Instances[0].InstanceId, resp.Reservations[0].Instances[0].NetworkInterfaces[0].Association } diff --git a/src/ec2/reboot_instance/reboot_instance.go b/src/ec2/reboot_instance/reboot_instance.go index 0010a59..4e52865 100644 --- a/src/ec2/reboot_instance/reboot_instance.go +++ b/src/ec2/reboot_instance/reboot_instance.go @@ -7,9 +7,10 @@ import ( "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/ec2" + "github.com/mericozkayagan/minecraft/src/ec2/filter_by_tag" ) -func RebootInstance(instanceId, publicIp *string) { +func RebootInstance() { sess := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, })) @@ -17,6 +18,8 @@ func RebootInstance(instanceId, publicIp *string) { // Create new EC2 client svc := ec2.New(sess) + instanceId, _ := filter_by_tag.FilterByTag() + // We set DryRun to true to check to see if the instance exists and we have the // necessary permissions to monitor the instance. input := &ec2.RebootInstancesInput{ @@ -37,7 +40,8 @@ func RebootInstance(instanceId, publicIp *string) { if err != nil { fmt.Println("Error", err) } else { - fmt.Println("Successfully rebooted the instance with the IP: ", publicIp) + _, publicIp := filter_by_tag.FilterByTag() + fmt.Println("Successfully started the instance with the IP: ", publicIp) } } else { // This could be due to a lack of permissions fmt.Println("Error", err) diff --git a/src/ec2/start_stop_instance/start_stop_instance.go b/src/ec2/start_stop_instance/start_stop_instance.go index 3bc9f28..44d1b9c 100644 --- a/src/ec2/start_stop_instance/start_stop_instance.go +++ b/src/ec2/start_stop_instance/start_stop_instance.go @@ -8,21 +8,17 @@ import ( "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/ec2" + "github.com/mericozkayagan/minecraft/src/ec2/filter_by_tag" ) -func StartStopInstance(command string, instanceId, publicIp *string) { +func StartStopInstance(command string) { sess := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, })) // Create new EC2 client svc := ec2.New(sess) - result, err := svc.DescribeInstances(nil) - if err != nil { - fmt.Println("Error", err) - } else { - fmt.Println("Success", result) - } + instanceId, _ := filter_by_tag.FilterByTag() // Turn monitoring on if strings.TrimSpace(strings.ToLower(command)) == "start" { @@ -46,7 +42,8 @@ func StartStopInstance(command string, instanceId, publicIp *string) { if err != nil { fmt.Println("Error", err) } else { - fmt.Println("Successfully started the instance with the IP: ", publicIp) + _, publicIp := filter_by_tag.FilterByTag() + fmt.Println("Successfully started the instance with the config: ", publicIp) } } else { // This could be due to a lack of permissions fmt.Println("Error", err) @@ -66,7 +63,7 @@ func StartStopInstance(command string, instanceId, publicIp *string) { if err != nil { fmt.Println("Error", err) } else { - fmt.Println("Successfully stopped the instance with the IP: ", publicIp) + fmt.Println("Successfully stopped the instance.") } } else { fmt.Println("Error", err)