Skip to content

Commit

Permalink
changed outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
mericozkayagan committed Feb 26, 2023
1 parent 539a127 commit 42e9f66
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 27 deletions.
5 changes: 1 addition & 4 deletions cmd/reboot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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()
},
}

Expand Down
5 changes: 1 addition & 4 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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")
},
}

Expand Down
6 changes: 1 addition & 5 deletions cmd/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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")
},
}

Expand Down
6 changes: 3 additions & 3 deletions src/ec2/filter_by_tag/filter_by_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
{
Expand All @@ -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
}
8 changes: 6 additions & 2 deletions src/ec2/reboot_instance/reboot_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@ 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,
}))

// 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{
Expand All @@ -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)
Expand Down
15 changes: 6 additions & 9 deletions src/ec2/start_stop_instance/start_stop_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 42e9f66

Please sign in to comment.