Skip to content

Commit

Permalink
remove infra generation output option (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
bwagner5 authored Apr 4, 2022
1 parent 44f699a commit a52d98e
Show file tree
Hide file tree
Showing 6 changed files with 0 additions and 227 deletions.
11 changes: 0 additions & 11 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ const (
awsConfigFile = "~/.aws/config"
spotPricingDaysBack = 30

// cfnJSON is an output type
cfnJSON = "cfn-json"
// cfnYAML is an output type
cfnYAML = "cfn-yaml"
terraformHCL = "terraform-hcl"
tableOutput = "table"
tableWideOutput = "table-wide"
oneLine = "one-line"
Expand Down Expand Up @@ -384,12 +379,6 @@ func getOutputFn(outputFlag *string, currentFn selector.InstanceTypesOutputFn) s
outputFn := selector.InstanceTypesOutputFn(currentFn)
if outputFlag != nil {
switch *outputFlag {
case cfnJSON:
return selector.InstanceTypesOutputFn(outputs.CloudFormationSpotMixedInstancesPolicyJSONOutput)
case cfnYAML:
return selector.InstanceTypesOutputFn(outputs.CloudFormationSpotMixedInstancesPolicyYAMLOutput)
case terraformHCL:
return selector.InstanceTypesOutputFn(outputs.TerraformSpotMixedInstancesPolicyHCLOutput)
case tableWideOutput:
return selector.InstanceTypesOutputFn(outputs.TableOutputWide)
case tableOutput:
Expand Down
3 changes: 0 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ go 1.17
require (
github.com/aws/aws-sdk-go v1.43.31
github.com/blang/semver/v4 v4.0.0
github.com/ghodss/yaml v1.0.0
github.com/hashicorp/hcl v1.0.0
github.com/imdario/mergo v0.3.11
github.com/mitchellh/go-homedir v1.1.0
github.com/patrickmn/go-cache v2.1.0+incompatible
Expand All @@ -21,5 +19,4 @@ require (
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/smartystreets/goconvey v1.6.4 // indirect
go.uber.org/atomic v1.4.0 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
)
5 changes: 0 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
Expand All @@ -45,7 +44,6 @@ github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoA
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/imdario/mergo v0.3.11 h1:3tnifQM4i+fbajXKBHXWEH+KvNHqojZ778UH75j3bGA=
github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
Expand All @@ -63,10 +61,8 @@ github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvW
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
Expand Down Expand Up @@ -159,7 +155,6 @@ google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZi
google.golang.org/grpc v1.21.0/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/ini.v1 v1.57.0 h1:9unxIsFcTt4I55uWluz+UmL95q4kdJ0buvQ1ZIqVQww=
gopkg.in/ini.v1 v1.57.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
Expand Down
104 changes: 0 additions & 104 deletions pkg/selector/outputs/outputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"text/tabwriter"

"github.com/aws/amazon-ec2-instance-selector/v2/pkg/instancetypes"
"github.com/ghodss/yaml"
)

// SimpleInstanceTypeOutput is an OutputFn which outputs a slice of instance type names
Expand All @@ -49,109 +48,6 @@ func VerboseInstanceTypeOutput(instanceTypeInfoSlice []*instancetypes.Details) [
return []string{string(output)}
}

// TerraformSpotMixedInstancesPolicyHCLOutput is an OutputFn which returns an ASG MixedInstancePolicy in Terraform HCL syntax
func TerraformSpotMixedInstancesPolicyHCLOutput(instanceTypeInfoSlice []*instancetypes.Details) []string {
instanceTypeOverrides := instanceTypeInfoToOverrides(instanceTypeInfoSlice)
overridesString := ""
for _, override := range instanceTypeOverrides {
overridesString = overridesString + fmt.Sprintf(`
override {
instance_type = "%s"
}
`, override.InstanceType)
}
asgResource := fmt.Sprintf(`resource "aws_autoscaling_group" "AutoScalingGroupMIG" {
vpc_zone_identifier = [
"REPLACE_WITH_SUBNET_ID"
]
name = "AutoScalingGroupMIG"
max_size = 0
min_size = 0
desired_capacity = 0
mixed_instances_policy {
instances_distribution {
on_demand_base_capacity = 0
on_demand_percentage_above_base_capacity = 0
spot_allocation_strategy = "capacity-optimized"
}
launch_template {
launch_template_specification {
launch_template_id = "REPLACE_WITH_LAUNCH_TEMPLATE_ID"
version = "$$Latest"
}
%s
}
}
}
provider "aws" {
region = "us-east-1"
}
`, overridesString)

return []string{asgResource}
}

// CloudFormationSpotMixedInstancesPolicyYAMLOutput is an OutputFn which returns an ASG MixedInstancePolicy in CloudFormation YAML syntax
func CloudFormationSpotMixedInstancesPolicyYAMLOutput(instanceTypeInfoSlice []*instancetypes.Details) []string {
instanceTypeOverrides := instanceTypeInfoToOverrides(instanceTypeInfoSlice)
cfnMig := getCfnMIGResources(instanceTypeOverrides)
cfnMigYAML, err := yaml.Marshal(cfnMig)
if err != nil {
log.Printf("Unable to create CloudFormation YAML: %v\n", err)
}
return []string{string(cfnMigYAML)}
}

// CloudFormationSpotMixedInstancesPolicyJSONOutput is an OutputFn which returns an MixedInstancePolicy in CloudFormation JSON syntax
func CloudFormationSpotMixedInstancesPolicyJSONOutput(instanceTypeInfoSlice []*instancetypes.Details) []string {
instanceTypeOverrides := instanceTypeInfoToOverrides(instanceTypeInfoSlice)
cfnMig := getCfnMIGResources(instanceTypeOverrides)
cfnJSONMig, err := json.MarshalIndent(cfnMig, "", " ")
if err != nil {
log.Printf("Unable to create CloudFormation JSON: %v\n", err)
return []string{}
}
return []string{string(cfnJSONMig)}
}

func getCfnMIGResources(instanceTypeOverrides []InstanceTypeOverride) Resources {
resources := map[string]AutoScalingGroup{}
resources["AutoScalingGroupMIG"] = AutoScalingGroup{
Type: typeASG,
Properties: AutoScalingGroupProperties{
AutoScalingGroupName: "REPLACE_WITH_NAME",
VPCZoneIdentifier: []string{"replace-with-subnet-ids"},
MixedInstancesPolicy: MixedInstancesPolicy{
InstancesDistribution: InstancesDistribution{
OnDemandBaseCapacity: 0,
OnDemandPercentageAboveBaseCapacity: 0,
SpotAllocationStrategy: capacityOptimized,
},
LaunchTemplate: LaunchTemplate{
LaunchTemplateSpecification: LaunchTemplateSpecification{
LaunchTemplateID: "REPLACE_WITH_LAUNCH_TEMPLATE_ID",
Version: "REPLACE_WITH_VERSION",
},
Overrides: instanceTypeOverrides,
},
},
},
}
return Resources{Resources: resources}
}

func instanceTypeInfoToOverrides(instanceTypeInfoSlice []*instancetypes.Details) []InstanceTypeOverride {
instanceTypeOverrides := []InstanceTypeOverride{}
for _, instanceTypeInfo := range instanceTypeInfoSlice {
instanceTypeOverrides = append(instanceTypeOverrides, InstanceTypeOverride{InstanceType: *instanceTypeInfo.InstanceType})
}
return instanceTypeOverrides
}

// TableOutputShort is an OutputFn which returns a CLI table for easy reading
func TableOutputShort(instanceTypeInfoSlice []*instancetypes.Details) []string {
if len(instanceTypeInfoSlice) == 0 {
Expand Down
29 changes: 0 additions & 29 deletions pkg/selector/outputs/outputs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import (
"github.com/aws/amazon-ec2-instance-selector/v2/pkg/selector/outputs"
h "github.com/aws/amazon-ec2-instance-selector/v2/pkg/test"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/ghodss/yaml"
"github.com/hashicorp/hcl"
)

const (
Expand Down Expand Up @@ -77,33 +75,6 @@ func TestVerboseInstanceTypeOutput(t *testing.T) {
h.Assert(t, len(instanceTypeOut) == 0, "Should return 0 instance types when passed nil")
}

func TestTerraformSpotMixedInstancesPolicyHCLOutput(t *testing.T) {
instanceTypes := getInstanceTypes(t, "t3_micro.json")
instanceTypeOut := outputs.TerraformSpotMixedInstancesPolicyHCLOutput(instanceTypes)
outputStr := strings.Join(instanceTypeOut, "")
_, err := hcl.ParseString(outputStr)
h.Ok(t, err)
h.Assert(t, strings.Contains(outputStr, `instance_type = "t3.micro"`), "HCL should include a t3.micro instanceType override")
}

func TestCloudFormationSpotMixedInstancesPolicyYAMLOutput(t *testing.T) {
instanceTypes := getInstanceTypes(t, "t3_micro.json")
instanceTypeOut := outputs.CloudFormationSpotMixedInstancesPolicyYAMLOutput(instanceTypes)
outputStr := strings.Join(instanceTypeOut, "")
_, err := yaml.YAMLToJSON([]byte(outputStr))
h.Ok(t, err)
h.Assert(t, strings.Contains(outputStr, `InstanceType: t3.micro`), "CFN Yaml should include a t3.micro InstanceType override")
}

func TestCloudFormationSpotMixedInstancesPolicyJSONOutput(t *testing.T) {
instanceTypes := getInstanceTypes(t, "t3_micro.json")
instanceTypeOut := outputs.CloudFormationSpotMixedInstancesPolicyJSONOutput(instanceTypes)
outputStr := strings.Join(instanceTypeOut, "")
_, err := yaml.JSONToYAML([]byte(outputStr))
h.Ok(t, err)
h.Assert(t, strings.Contains(outputStr, `"InstanceType": "t3.micro"`), "CFN JSON should include a t3.micro InstanceType override")
}

func TestTableOutputShort(t *testing.T) {
instanceTypes := getInstanceTypes(t, "t3_micro.json")
instanceTypeOut := outputs.TableOutputShort(instanceTypes)
Expand Down
75 changes: 0 additions & 75 deletions pkg/selector/outputs/types.go

This file was deleted.

0 comments on commit a52d98e

Please sign in to comment.