Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OCM-6448 | feat: Create CLI tool for creating AWS resources #44

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ ocm-common
.envrc
.env
cover.out

rosa-helper
22 changes: 19 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,31 @@
export GO111MODULE=on
export GOPROXY=https://proxy.golang.org

# Constants
GOPATH := $(shell go env GOPATH)

# Version
revision:=$(shell git rev-parse --short HEAD)
build_time:=$(shell date +%D@%T)
version_stamp:=$(revision)-$(build_time)
import_path:=github.com/openshift-online/ocm-support-cli
ldflags:=-X $(import_path)/pkg/info.VersionStamp=$(version_stamp)

# Disable CGO so that we always generate static binaries:
export CGO_ENABLED=0

# Unset GOFLAG for CI and ensure we've got nothing accidently set
unexport GOFLAGS

.PHONY: build
build:
go build
# Builds the CLI tool located in /cmd/
.PHONY: build-cli
build-cli: clean
go build -o rosa-helper -ldflags="$(ldflags)" ./cmd/rosa-helper || exit 1

# Installs the CLI tool located in /cmd/ to your bin folder for easy execution
.PHONY: install-cli
install-cli: clean
go build -o $(GOPATH)/bin/rosa-helper -ldflags="$(ldflags)" ./cmd/rosa-helper || exit 1

.PHONY: test
test:
Expand Down
23 changes: 23 additions & 0 deletions cmd/rosa-helper/create/cmd.go
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)
}
130 changes: 130 additions & 0 deletions cmd/rosa-helper/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-helper/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, ","))
}
85 changes: 85 additions & 0 deletions cmd/rosa-helper/create/subnets/cmd.go
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)
}
}
}
Loading
Loading