diff --git a/cmd/main.go b/cmd/main.go index e0c9ca2..29cc902 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -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" @@ -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: diff --git a/go.mod b/go.mod index a045201..30da4e9 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 ) diff --git a/go.sum b/go.sum index 7f0c4e5..53c0f70 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= @@ -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= @@ -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= diff --git a/pkg/selector/outputs/outputs.go b/pkg/selector/outputs/outputs.go index 5e6d422..318cf8e 100644 --- a/pkg/selector/outputs/outputs.go +++ b/pkg/selector/outputs/outputs.go @@ -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 @@ -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 { diff --git a/pkg/selector/outputs/outputs_test.go b/pkg/selector/outputs/outputs_test.go index 4bb7ddf..6f484fd 100644 --- a/pkg/selector/outputs/outputs_test.go +++ b/pkg/selector/outputs/outputs_test.go @@ -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 ( @@ -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) diff --git a/pkg/selector/outputs/types.go b/pkg/selector/outputs/types.go deleted file mode 100644 index 06d975c..0000000 --- a/pkg/selector/outputs/types.go +++ /dev/null @@ -1,75 +0,0 @@ -// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"). You may -// not use this file except in compliance with the License. A copy of the -// License is located at -// -// http://aws.amazon.com/apache2.0/ -// -// or in the "license" file accompanying this file. This file is distributed -// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either -// express or implied. See the License for the specific language governing -// permissions and limitations under the License. - -package outputs - -const ( - capacityOptimized = "capacity-optimized" - typeASG = "AWS::AutoScaling::AutoScalingGroup" -) - -// Resources is a struct to represent json for a cloudformation Resources definition block. -type Resources struct { - Resources map[string]AutoScalingGroup `json:"Resources"` -} - -// AutoScalingGroup is a struct to represent json for a cloudformation ASG definition -type AutoScalingGroup struct { - Type string `json:"Type"` - Properties AutoScalingGroupProperties `json:"Properties"` -} - -// AutoScalingGroupProperties is a struct to represent json for a cloudformation ASG Properties definition -type AutoScalingGroupProperties struct { - AutoScalingGroupName string `json:"AutoScalingGroupName"` - MinSize int `json:"MinSize,string"` - MaxSize int `json:"MaxSize,string"` - DesiredCapacity int `json:"DesiredCapacity,string"` - VPCZoneIdentifier []string `json:"VPCZoneIdentifier"` - MixedInstancesPolicy MixedInstancesPolicy `json:"MixedInstancesPolicy"` -} - -// MixedInstancesPolicy is a struct to represent json for a cloudformation ASG MixedInstancesPolicy definition -type MixedInstancesPolicy struct { - InstancesDistribution InstancesDistribution `json:"InstancesDistribution"` - LaunchTemplate LaunchTemplate `json:"LaunchTemplate"` -} - -// InstancesDistribution is a struct to represent json for a cloudformation ASG MixedInstancesPolicy InstancesDistribution definition -type InstancesDistribution struct { - OnDemandAllocationStrategy string `json:"OnDemandAllocationStrategy,omitempty"` - OnDemandBaseCapacity int `json:"OnDemandBaseCapacity"` - OnDemandPercentageAboveBaseCapacity int `json:"OnDemandPercentageAboveBaseCapacity"` - SpotAllocationStrategy string `json:"SpotAllocationStrategy,omitempty"` - SpotInstancePools int `json:"SpotInstancePools,omitempty"` - SpotMaxPrice string `json:"SpotMaxPrice,omitempty"` -} - -// LaunchTemplate is a struct to represent json for a cloudformation LaunchTemplate definition -type LaunchTemplate struct { - LaunchTemplateSpecification LaunchTemplateSpecification `json:"LaunchTemplateSpecification"` - Overrides []InstanceTypeOverride `json:"Overrides"` -} - -// LaunchTemplateSpecification is a struct to represent json for a cloudformation LaunchTemplate LaunchTemplateSpecification definition -type LaunchTemplateSpecification struct { - LaunchTemplateID string `json:"LaunchTemplateId,omitempty"` - LaunchTemplateName string `json:"LaunchTemplateName,omitempty"` - Version string `json:"Version"` -} - -// InstanceTypeOverride is a struct to represent json for a cloudformation LaunchTemplate LaunchTemplateSpecification InstanceTypeOverrides definition -type InstanceTypeOverride struct { - InstanceType string `json:"InstanceType"` - WeightedCapacity int `json:"WeightedCapacity,omitempty"` -}