From 6eed88311a433c6e497a0be1c52922fd6180e792 Mon Sep 17 00:00:00 2001 From: hkepley Date: Wed, 1 May 2024 15:50:15 -0400 Subject: [PATCH] OCM-7805 | fix: Replace subnets command with correct command; improvements --- cmd/rosa-support/create/proxy/cmd.go | 34 +++++------ cmd/rosa-support/create/sg/cmd.go | 2 +- cmd/rosa-support/create/subnets/cmd.go | 82 ++++++++++---------------- cmd/rosa-support/create/vpc/cmd.go | 2 +- 4 files changed, 51 insertions(+), 69 deletions(-) diff --git a/cmd/rosa-support/create/proxy/cmd.go b/cmd/rosa-support/create/proxy/cmd.go index d5f946f..12cef32 100644 --- a/cmd/rosa-support/create/proxy/cmd.go +++ b/cmd/rosa-support/create/proxy/cmd.go @@ -11,13 +11,13 @@ import ( ) var args struct { - region string - vpcID string - zone string - imageID string - privateKeyPath string - keyPairName string - caFilePath string + region string + vpcID string + availabilityZone string + imageID string + privateKeyPath string + keyPairName string + caFilePath string } var Cmd = &cobra.Command{ @@ -25,7 +25,7 @@ var Cmd = &cobra.Command{ Short: "Create proxy", Long: "Create proxy.", Example: ` # Create a proxy - rosa-helper create proxy --region us-east-2 --vpc-id `, + rosa-support create proxy --region us-east-2 --vpc-id --availability-zone --ca-file --keypair-name `, Run: run, } @@ -37,21 +37,21 @@ func init() { "region", "", "", - "Vpc region", + "Vpc region (required)", ) flags.StringVarP( &args.vpcID, "vpc-id", "", "", - "Creates a pair of subnets", + "Creates a pair of subnets (required)", ) flags.StringVarP( - &args.zone, - "zone", + &args.availabilityZone, + "availability-zone", "", "", - "Creates a proxy in the indicated zone", + "Creates a proxy in the indicated AZ (required)", ) flags.StringVarP( &args.imageID, @@ -66,7 +66,7 @@ func init() { "ca-file", "", "", - "Creates a proxy and stores the ca file", + "Creates a proxy and stores the ca file (required)", ) flags.StringVarP( @@ -74,7 +74,7 @@ func init() { "keypair-name", "", "", - "Stores key pair in the given path", + "Stores key pair in the given path (required)", ) err := Cmd.MarkFlagRequired("vpc-id") @@ -87,7 +87,7 @@ func init() { logger.LogError(err.Error()) os.Exit(1) } - err = Cmd.MarkFlagRequired("zone") + err = Cmd.MarkFlagRequired("availability-zone") if err != nil { logger.LogError(err.Error()) os.Exit(1) @@ -109,7 +109,7 @@ func run(cmd *cobra.Command, _ []string) { if err != nil { panic(err) } - _, ip, ca, err := vpc.LaunchProxyInstance(args.imageID, args.zone, args.keyPairName) + _, ip, ca, err := vpc.LaunchProxyInstance(args.imageID, args.availabilityZone, args.keyPairName) if err != nil { panic(err) } diff --git a/cmd/rosa-support/create/sg/cmd.go b/cmd/rosa-support/create/sg/cmd.go index efe008e..a8e60b6 100644 --- a/cmd/rosa-support/create/sg/cmd.go +++ b/cmd/rosa-support/create/sg/cmd.go @@ -24,7 +24,7 @@ var Cmd = &cobra.Command{ Short: "Create security-groups", Long: "Create security-groups.", Example: `# Create a number of security groups" - rosa-helper create security-groups --name-prefix=mysg --region us-east-2 --vpc-id `, + rosa-support create security-groups --name-prefix=mysg --region us-east-2 --vpc-id `, Run: run, } diff --git a/cmd/rosa-support/create/subnets/cmd.go b/cmd/rosa-support/create/subnets/cmd.go index efe008e..b734dd4 100644 --- a/cmd/rosa-support/create/subnets/cmd.go +++ b/cmd/rosa-support/create/subnets/cmd.go @@ -1,7 +1,6 @@ -package sg +package subnets import ( - "fmt" "os" "strings" @@ -12,19 +11,18 @@ import ( ) var args struct { - region string - count int - vpcID string - tags string - namePrefix string + region string + availabilityZones string + vpcID string + tags string } var Cmd = &cobra.Command{ - Use: "security-groups", - Short: "Create security-groups", - Long: "Create security-groups.", - Example: `# Create a number of security groups" - rosa-helper create security-groups --name-prefix=mysg --region us-east-2 --vpc-id `, + Use: "subnets", + Short: "Create subnets", + Long: "Create subnets.", + Example: ` # Create a pair of subnets with prefix 'mysubnet-' in region 'us-east-2' + rosa-support create subnets --name-prefix=mysubnet --region us-east-2 --vpc-id --availability-zones `, Run: run, } @@ -37,36 +35,33 @@ func init() { "region", "", "", - "Region of the security groups", + "Vpc region", ) + err := Cmd.MarkFlagRequired("region") + if err != nil { + logger.LogError(err.Error()) + os.Exit(1) + } flags.StringVarP( - &args.namePrefix, - "name-prefix", + &args.availabilityZones, + "availability-zones", "", "", - "Name prefix of the security groups, they will be named with -0,-1", - ) - - flags.IntVarP( - &args.count, - "count", - "", - 0, - "Additional number of security groups to be created for the vpc", + "Availability zones to create subnets in", ) + err = Cmd.MarkFlagRequired("availability-zones") + if err != nil { + logger.LogError(err.Error()) + os.Exit(1) + } flags.StringVarP( &args.vpcID, "vpc-id", "", "", - "Vpc ID for the VPC created for the additional security groups", + "ID of vpc to be created", ) - err := Cmd.MarkFlagRequired("vpc-id") - if err != nil { - logger.LogError(err.Error()) - os.Exit(1) - } - err = Cmd.MarkFlagRequired("region") + err = Cmd.MarkFlagRequired("vpc-id") if err != nil { logger.LogError(err.Error()) os.Exit(1) @@ -77,28 +72,15 @@ func run(cmd *cobra.Command, _ []string) { if err != nil { panic(err) } - preparedSGs := []string{} - sgDescription := "This security group is created for OCM testing" - protocol := "tcp" - for i := 0; i < args.count; i++ { - sgName := fmt.Sprintf("%s-%d", args.namePrefix, i) - sg, err := vpc.AWSClient.CreateSecurityGroup(vpc.VpcID, sgName, sgDescription) + availabilityZones := strings.Split(args.availabilityZones, ",") + for _, availabilityZone := range availabilityZones { + subnetMap, err := vpc.PreparePairSubnetByZone(availabilityZone) if err != nil { panic(err) } - groupID := *sg.GroupId - cidrPortsMap := map[string]int32{ - vpc.CIDRValue: 8080, - "0.0.0.0/0": 22, + for subnetType, subnet := range subnetMap { + logger.LogInfo("AVAILABILITY ZONE %s %s SUBNET: %s", availabilityZone, strings.ToUpper(subnetType), + subnet.ID) } - for cidr, port := range cidrPortsMap { - _, err = vpc.AWSClient.AuthorizeSecurityGroupIngress(groupID, cidr, protocol, port, port) - if err != nil { - panic(err) - } - } - - preparedSGs = append(preparedSGs, groupID) } - logger.LogInfo("ADDITIONAL SECURITY GROUPS: %s", strings.Join(preparedSGs, ",")) } diff --git a/cmd/rosa-support/create/vpc/cmd.go b/cmd/rosa-support/create/vpc/cmd.go index 3bb67bf..0092f30 100644 --- a/cmd/rosa-support/create/vpc/cmd.go +++ b/cmd/rosa-support/create/vpc/cmd.go @@ -22,7 +22,7 @@ var Cmd = &cobra.Command{ Short: "Create vpc", Long: "Create vpc.", Example: ` # Create a vpc named "myvpc" - rosa-helper create vpc --name=myvpc --region us-east-2`, + rosa-support create vpc --name=myvpc --region us-east-2`, Run: run, }