-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OCM-6448 | feat: Create CLI tool for creating AWS resources
- Loading branch information
1 parent
765ad84
commit 43bb03a
Showing
10 changed files
with
506 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,4 @@ ocm-common | |
.envrc | ||
.env | ||
cover.out | ||
|
||
rosa-helper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package create | ||
|
||
import ( | ||
"github.com/openshift-online/ocm-common/cmd/rosa-helper/create/proxy" | ||
"github.com/openshift-online/ocm-common/cmd/rosa-helper/create/sg" | ||
"github.com/openshift-online/ocm-common/cmd/rosa-helper/create/subnets" | ||
"github.com/openshift-online/ocm-common/cmd/rosa-helper/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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, ",")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package subnets | ||
|
||
import ( | ||
"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 | ||
zones string | ||
vpcID string | ||
tags string | ||
} | ||
|
||
var Cmd = &cobra.Command{ | ||
Use: "subnets", | ||
Short: "Create subnets", | ||
Long: "Create subnets.", | ||
Example: ` # Create a pair of subnets with prefix 'mysubnet-' in region 'us-east-2' | ||
rosa-helper create subnets --name-prefix=mysubnet --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", | ||
) | ||
err := Cmd.MarkFlagRequired("region") | ||
if err != nil { | ||
logger.LogError(err.Error()) | ||
os.Exit(1) | ||
} | ||
flags.StringVarP( | ||
&args.zones, | ||
"zones", | ||
"", | ||
"", | ||
"Zones to create subnets in", | ||
) | ||
err = Cmd.MarkFlagRequired("zones") | ||
if err != nil { | ||
logger.LogError(err.Error()) | ||
os.Exit(1) | ||
} | ||
flags.StringVarP( | ||
&args.vpcID, | ||
"vpc-id", | ||
"", | ||
"", | ||
"ID of vpc to be created", | ||
) | ||
err = Cmd.MarkFlagRequired("vpc-id") | ||
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) | ||
} | ||
zones := strings.Split(args.zones, ",") | ||
for _, zone := range zones { | ||
subnetMap, err := vpc.PreparePairSubnetByZone(zone) | ||
if err != nil { | ||
panic(err) | ||
} | ||
for subnetType, subnet := range subnetMap { | ||
logger.LogInfo("ZONE %s %s SUBNET: %s", zone, strings.ToUpper(subnetType), subnet.ID) | ||
} | ||
} | ||
} |
Oops, something went wrong.