From e76af40efe7b33db9a2990a252ffc5bb2b2209d7 Mon Sep 17 00:00:00 2001 From: zhewang Date: Fri, 1 Nov 2024 14:16:39 +0800 Subject: [PATCH] OCM-11855 | test: Support bastion proxy --- cmd/rosa-support/create/bastion/cmd.go | 95 ++++++++++++++++++++++++++ cmd/rosa-support/create/cmd.go | 2 + go.mod | 2 +- go.sum | 4 +- 4 files changed, 100 insertions(+), 3 deletions(-) create mode 100644 cmd/rosa-support/create/bastion/cmd.go diff --git a/cmd/rosa-support/create/bastion/cmd.go b/cmd/rosa-support/create/bastion/cmd.go new file mode 100644 index 0000000..ab18c19 --- /dev/null +++ b/cmd/rosa-support/create/bastion/cmd.go @@ -0,0 +1,95 @@ +package bastion + +import ( + "fmt" + logger "github.com/openshift-online/ocm-common/pkg/log" + vpcClient "github.com/openshift-online/ocm-common/pkg/test/vpc_client" + "github.com/spf13/cobra" + "os" +) + +var args struct { + region string + vpcID string + availabilityZone string + cidr string +} + +var Cmd = &cobra.Command{ + Use: "bastion", + Short: "Create bastion proxy", + Long: "Create bastion proxy.", + Example: ` # Create a bastion proxy in region 'us-east-2' + rosa-support create bastion --region us-east-2 --availability-zone us-east-2a --vpc-id `, + + Run: run, +} + +func init() { + flags := Cmd.Flags() + flags.SortFlags = false + flags.StringVarP( + &args.region, + "region", + "", + "", + "Vpc region (required)", + ) + err := Cmd.MarkFlagRequired("region") + if err != nil { + logger.LogError(err.Error()) + os.Exit(1) + } + flags.StringVarP( + &args.vpcID, + "vpc-id", + "", + "", + "ID of vpc to be used to create bastion proxy (required)", + ) + err = Cmd.MarkFlagRequired("vpc-id") + if err != nil { + logger.LogError(err.Error()) + os.Exit(1) + } + flags.StringVarP( + &args.availabilityZone, + "availability-zone", + "", + "", + "Availability zone to create public subnet of specified vpc (required)", + ) + err = Cmd.MarkFlagRequired("availability-zone") + if err != nil { + logger.LogError(err.Error()) + os.Exit(1) + } + flags.StringVarP( + &args.cidr, + "cidr-block", + "", + "", + "Only IP address within CIDR block can access other resources through bastion "+ + "proxy(not required, default is 0.0.0.0/0)", + ) +} + +func run(cmd *cobra.Command, _ []string) { + vpc, err := vpcClient.GenerateVPCByID(args.vpcID, args.region) + if err != nil { + panic(err) + } + instance, err := vpc.PrepareBastionProxy(args.availabilityZone, args.cidr) + if err != nil { + panic(err) + } + + inst := *instance + publicIp := *inst.PublicIpAddress + httpProxy := fmt.Sprintf("http://%s:3128", publicIp) + httpsProxy := fmt.Sprintf("https://%s:3128", publicIp) + logger.LogInfo("Bastion instance ID: %s", *inst.InstanceId) + logger.LogInfo("Bastion HTTP PROXY: %s", httpProxy) + logger.LogInfo("Bastion HTTPs PROXY: %s", httpsProxy) + +} diff --git a/cmd/rosa-support/create/cmd.go b/cmd/rosa-support/create/cmd.go index ffedc66..fc76599 100644 --- a/cmd/rosa-support/create/cmd.go +++ b/cmd/rosa-support/create/cmd.go @@ -17,6 +17,7 @@ limitations under the License. package create import ( + "github.com/openshift-online/rosa-support/cmd/rosa-support/create/bastion" "github.com/openshift-online/rosa-support/cmd/rosa-support/create/proxy" "github.com/openshift-online/rosa-support/cmd/rosa-support/create/sg" subnets "github.com/openshift-online/rosa-support/cmd/rosa-support/create/subnets" @@ -36,4 +37,5 @@ func init() { Cmd.AddCommand(sg.Cmd) Cmd.AddCommand(subnets.Cmd) Cmd.AddCommand(proxy.Cmd) + Cmd.AddCommand(bastion.Cmd) } diff --git a/go.mod b/go.mod index cc9aa62..771a3e4 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/openshift-online/rosa-support go 1.21 require ( - github.com/openshift-online/ocm-common v0.0.12 + github.com/openshift-online/ocm-common v0.0.13 github.com/spf13/cobra v1.8.0 ) diff --git a/go.sum b/go.sum index f4d3377..06bc315 100644 --- a/go.sum +++ b/go.sum @@ -56,8 +56,8 @@ github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9Y github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8= github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= -github.com/openshift-online/ocm-common v0.0.12 h1:2oGZCXd8O/nZVlM0pvTtm3hDGbW/ncTtvSLLB+nuQf0= -github.com/openshift-online/ocm-common v0.0.12/go.mod h1:6MWje2NFNJ3IWpGs7BYj6DWagWXHyp8EnmYY7XFTtI4= +github.com/openshift-online/ocm-common v0.0.13 h1:FUn4PwuxOLsGbyJIC+izQQxYP3hA6dW5z/ep4HbTE4Q= +github.com/openshift-online/ocm-common v0.0.13/go.mod h1:6MWje2NFNJ3IWpGs7BYj6DWagWXHyp8EnmYY7XFTtI4= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=