Skip to content

Commit

Permalink
Merge pull request #9 from hunterkepley/ocm-6448
Browse files Browse the repository at this point in the history
OCM-6448 | feat: Create CLI tool for creating AWS resources
  • Loading branch information
ciaranRoche authored Apr 26, 2024
2 parents 5c65f88 + fbc93c9 commit 8e2a339
Show file tree
Hide file tree
Showing 9 changed files with 565 additions and 3 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.idea
/rosa-support
/rosa-support
rosa-support

23 changes: 23 additions & 0 deletions cmd/rosa-support/create/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package create

import (
"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"
"github.com/openshift-online/rosa-support/cmd/rosa-support/create/vpc"
"github.com/spf13/cobra"
)

var Cmd = &cobra.Command{
Use: "create",
Aliases: []string{"add"},
Short: "Create a resource from stdin",
Long: "Create a resource from stdin",
}

func init() {
Cmd.AddCommand(vpc.Cmd)
Cmd.AddCommand(sg.Cmd)
Cmd.AddCommand(subnets.Cmd)
Cmd.AddCommand(proxy.Cmd)
}
130 changes: 130 additions & 0 deletions cmd/rosa-support/create/proxy/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package proxy

import (
"fmt"
"os"

logger "github.com/openshift-online/ocm-common/pkg/log"
vpcClient "github.com/openshift-online/ocm-common/pkg/test/vpc_client"

"github.com/spf13/cobra"
)

var args struct {
region string
vpcID string
zone string
imageID string
privateKeyPath string
keyPairName string
caFilePath string
}

var Cmd = &cobra.Command{
Use: "proxy",
Short: "Create proxy",
Long: "Create proxy.",
Example: ` # Create a proxy
rosa-helper create proxy --region us-east-2 --vpc-id <vpc id>`,
Run: run,
}

func init() {
flags := Cmd.Flags()
flags.SortFlags = false
flags.StringVarP(
&args.region,
"region",
"",
"",
"Vpc region",
)
flags.StringVarP(
&args.vpcID,
"vpc-id",
"",
"",
"Creates a pair of subnets",
)
flags.StringVarP(
&args.zone,
"zone",
"",
"",
"Creates a proxy in the indicated zone",
)
flags.StringVarP(
&args.imageID,
"image-id",
"",
"",
"Creates a proxy with the image ID given",
)

flags.StringVarP(
&args.caFilePath,
"ca-file",
"",
"",
"Creates a proxy and stores the ca file",
)

flags.StringVarP(
&args.keyPairName,
"keypair-name",
"",
"",
"Stores key pair in the given path",
)

err := Cmd.MarkFlagRequired("vpc-id")
if err != nil {
logger.LogError(err.Error())
os.Exit(1)
}
err = Cmd.MarkFlagRequired("region")
if err != nil {
logger.LogError(err.Error())
os.Exit(1)
}
err = Cmd.MarkFlagRequired("zone")
if err != nil {
logger.LogError(err.Error())
os.Exit(1)
}
err = Cmd.MarkFlagRequired("ca-file")
if err != nil {
logger.LogError(err.Error())
os.Exit(1)
}
err = Cmd.MarkFlagRequired("keypair-name")
if err != nil {
logger.LogError(err.Error())
os.Exit(1)
}
}

func run(cmd *cobra.Command, _ []string) {
vpc, err := vpcClient.GenerateVPCByID(args.vpcID, args.region)
if err != nil {
panic(err)
}
_, ip, ca, err := vpc.LaunchProxyInstance(args.imageID, args.zone, args.keyPairName)
if err != nil {
panic(err)
}
httpProxy := fmt.Sprintf("http://%s:8080", ip)
httpsProxy := fmt.Sprintf("https://%s:8080", ip)
file, err := os.OpenFile(args.caFilePath, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
panic(err)
}

_, err = file.WriteString(ca)
if err != nil {
panic(err)
}
logger.LogInfo("HTTP PROXY: %s", httpProxy)
logger.LogInfo("HTTPs PROXY: %s", httpsProxy)
logger.LogInfo("CA FILE PATH: %s", args.caFilePath)
}
104 changes: 104 additions & 0 deletions cmd/rosa-support/create/sg/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package sg

import (
"fmt"
"os"
"strings"

logger "github.com/openshift-online/ocm-common/pkg/log"
vpcClient "github.com/openshift-online/ocm-common/pkg/test/vpc_client"

"github.com/spf13/cobra"
)

var args struct {
region string
count int
vpcID string
tags string
namePrefix 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 <vpc id>`,

Run: run,
}

func init() {
flags := Cmd.Flags()
flags.SortFlags = false
flags.StringVarP(
&args.region,
"region",
"",
"",
"Region of the security groups",
)
flags.StringVarP(
&args.namePrefix,
"name-prefix",
"",
"",
"Name prefix of the security groups, they will be named with <prefix>-0,<prefix>-1",
)

flags.IntVarP(
&args.count,
"count",
"",
0,
"Additional number of security groups to be created for the vpc",
)
flags.StringVarP(
&args.vpcID,
"vpc-id",
"",
"",
"Vpc ID for the VPC created for the additional security groups",
)
err := Cmd.MarkFlagRequired("vpc-id")
if err != nil {
logger.LogError(err.Error())
os.Exit(1)
}
err = Cmd.MarkFlagRequired("region")
if err != nil {
logger.LogError(err.Error())
os.Exit(1)
}
}
func run(cmd *cobra.Command, _ []string) {
vpc, err := vpcClient.GenerateVPCByID(args.vpcID, args.region)
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)
if err != nil {
panic(err)
}
groupID := *sg.GroupId
cidrPortsMap := map[string]int32{
vpc.CIDRValue: 8080,
"0.0.0.0/0": 22,
}
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, ","))
}
104 changes: 104 additions & 0 deletions cmd/rosa-support/create/subnets/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package sg

import (
"fmt"
"os"
"strings"

logger "github.com/openshift-online/ocm-common/pkg/log"
vpcClient "github.com/openshift-online/ocm-common/pkg/test/vpc_client"

"github.com/spf13/cobra"
)

var args struct {
region string
count int
vpcID string
tags string
namePrefix 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 <vpc id>`,

Run: run,
}

func init() {
flags := Cmd.Flags()
flags.SortFlags = false
flags.StringVarP(
&args.region,
"region",
"",
"",
"Region of the security groups",
)
flags.StringVarP(
&args.namePrefix,
"name-prefix",
"",
"",
"Name prefix of the security groups, they will be named with <prefix>-0,<prefix>-1",
)

flags.IntVarP(
&args.count,
"count",
"",
0,
"Additional number of security groups to be created for the vpc",
)
flags.StringVarP(
&args.vpcID,
"vpc-id",
"",
"",
"Vpc ID for the VPC created for the additional security groups",
)
err := Cmd.MarkFlagRequired("vpc-id")
if err != nil {
logger.LogError(err.Error())
os.Exit(1)
}
err = Cmd.MarkFlagRequired("region")
if err != nil {
logger.LogError(err.Error())
os.Exit(1)
}
}
func run(cmd *cobra.Command, _ []string) {
vpc, err := vpcClient.GenerateVPCByID(args.vpcID, args.region)
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)
if err != nil {
panic(err)
}
groupID := *sg.GroupId
cidrPortsMap := map[string]int32{
vpc.CIDRValue: 8080,
"0.0.0.0/0": 22,
}
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, ","))
}
Loading

0 comments on commit 8e2a339

Please sign in to comment.