-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ae97944
commit f1c796b
Showing
14 changed files
with
320 additions
and
10 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
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,109 @@ | ||
compute_resource: | ||
aws_autoscaling_group: | ||
paths: | ||
- cbf::all_select("type"; "aws_autoscaling_group") | ||
type: resource | ||
variables: | ||
properties: | ||
provider_region: | ||
- paths: | ||
- '.configuration' | ||
property: "region" | ||
launch_configuration: | ||
- paths: | ||
- '.configuration.root_module.resources[] | select(.address == "${this.address}") | .expressions.launch_configuration?.references[]? | select(endswith(".id") or endswith(".name")) | gsub("\\.(id|name)$"; "")' | ||
- '.configuration.root_module.resources[] | select(.address == "${this.address}") | .expressions.launch_template[]?.id?.references[]? | select(endswith(".id") or endswith(".name")) | gsub("\\.(id|name)$"; "")' | ||
reference: | ||
paths: | ||
- cbf::all_select("address"; "${key}") | ||
- cbf::all_select("address"; ("${key}" | split(".")[0:2] | join("."))) | .resources[] | select(.name", ("${key}" | split(".")[2])) | ||
- .prior_state.values.root_module.resources[] | select(.address == "${key}") | ||
return_path: true | ||
ami: | ||
- paths: | ||
- '${launch_configuration}.values.image_id' | ||
reference: | ||
paths: | ||
- cbf::all_select("type"; "aws_ami") | select(.values.image_id == "${key}") | ||
- .prior_state.values.root_module.resources[] | select(.type == "aws_ami") | select(.values.image_id == "${key}") | ||
return_path: true | ||
properties: | ||
name: | ||
- paths: ".name" | ||
address: | ||
- paths: ".address" | ||
type: | ||
- paths: ".type" | ||
vCPUs: | ||
- paths: "${launch_configuration}.values.instance_type" | ||
reference: | ||
json_file: aws_instances | ||
property: ".VCPU" | ||
memory: | ||
- paths: "${launch_configuration}.values.instance_type" | ||
unit: mb | ||
reference: | ||
json_file: aws_instances | ||
property: ".MemoryMb" | ||
zone: | ||
zone: | ||
- paths: ".values.availability_zone" | ||
region: | ||
- paths: ".values.availability_zone" | ||
regex: | ||
pattern: '^(.+-\d+)[a-z]+' | ||
group: 1 | ||
- paths: ".configuration.provider_config.aws.expressions.region" | ||
replication_factor: | ||
- default: 1 | ||
count: | ||
- paths: | ||
- '.values | if has("max_size") then (.min_size // 1) + ${config.provider.aws.avg_autoscaler_size_percent} * (.max_size - (.min_size? // 1)) else null end' | ||
storage: | ||
- type: list | ||
item: | ||
- paths: '${ami}.values.block_device_mappings[].ebs | select(length > 0)' | ||
properties: | ||
size: | ||
- paths: ".volume_size" | ||
default: 8 | ||
unit: gb | ||
type: | ||
- paths: ".volume_type" | ||
default: standard | ||
reference: | ||
general: disk_types | ||
- paths: | ||
- '${launch_configuration}.values.ebs_block_device[] | select(length > 0)' | ||
- '${launch_configuration}.values.block_device_mappings[] | select(length > 0) | select(.virtual_name == null or (.virtual_name | startswith("ephemeral") | not)) | .ebs' | ||
properties: | ||
size: | ||
- paths: ".volume_size" | ||
unit: gb | ||
- paths: ".snapshot_id" | ||
reference: | ||
paths: .prior_state.values.root_module.resources[] | select(.values.id == "${key}") | .values | ||
property: ".volume_size" | ||
- default: 8 | ||
unit: gb | ||
type: | ||
- paths: ".volume_type" | ||
default: standard | ||
reference: | ||
general: disk_types | ||
- paths: | ||
- '${launch_configuration}.values.ephemeral_block_device[] | select(length > 0)' | ||
- '${launch_configuration}.values.block_device_mappings[] | select(length > 0) | select(.virtual_name != null and (.virtual_name | startswith("ephemeral")))' | ||
properties: | ||
size: | ||
- paths: '${launch_configuration}.values.instance_type' | ||
unit: gb | ||
reference: | ||
json_file: aws_instances | ||
property: ".InstanceStorage.SizePerDiskGB" | ||
type: | ||
- paths: '${launch_configuration}.values.instance_type' | ||
default: standard | ||
reference: | ||
json_file: aws_instances | ||
property: ".InstanceStorage.Type" |
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
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,80 @@ | ||
package plan_test | ||
|
||
import ( | ||
"path" | ||
"testing" | ||
|
||
"github.com/carboniferio/carbonifer/internal/plan" | ||
"github.com/carboniferio/carbonifer/internal/providers" | ||
"github.com/carboniferio/carbonifer/internal/resources" | ||
"github.com/carboniferio/carbonifer/internal/terraform" | ||
"github.com/carboniferio/carbonifer/internal/testutils" | ||
"github.com/shopspring/decimal" | ||
"github.com/spf13/viper" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGetResource_AWSASG(t *testing.T) { | ||
|
||
testutils.SkipWithCreds(t) | ||
|
||
// reset | ||
terraform.ResetTerraformExec() | ||
|
||
wd := path.Join(testutils.RootDir, "test/terraform/aws_asg") | ||
viper.Set("workdir", wd) | ||
|
||
wantResources := map[string]resources.Resource{ | ||
"aws_autoscaling_group.asg_with_launchconfig": resources.ComputeResource{ | ||
Identification: &resources.ResourceIdentification{ | ||
Name: "asg_with_launchconfig", | ||
Address: "aws_autoscaling_group.asg_with_launchconfig", | ||
ResourceType: "aws_autoscaling_group", | ||
Provider: providers.AWS, | ||
Region: "eu-west-3", | ||
Count: 6, | ||
ReplicationFactor: 1, | ||
}, | ||
Specs: &resources.ComputeResourceSpecs{ | ||
VCPUs: int32(4), | ||
MemoryMb: int32(16384), | ||
|
||
HddStorage: decimal.Zero, | ||
SsdStorage: decimal.NewFromInt(180), | ||
}, | ||
}, | ||
"aws_autoscaling_group.asg_launch_template": resources.ComputeResource{ | ||
Identification: &resources.ResourceIdentification{ | ||
Name: "asg_launch_template", | ||
Address: "aws_autoscaling_group.asg_launch_template", | ||
ResourceType: "aws_autoscaling_group", | ||
Provider: providers.AWS, | ||
Region: "eu-west-3", | ||
Count: 6, | ||
ReplicationFactor: 1, | ||
}, | ||
Specs: &resources.ComputeResourceSpecs{ | ||
VCPUs: int32(4), | ||
MemoryMb: int32(16384), | ||
|
||
HddStorage: decimal.NewFromInt(300), | ||
SsdStorage: decimal.NewFromInt(30 + 150), | ||
}, | ||
}, | ||
} | ||
tfPlan, err := terraform.TerraformPlan() | ||
assert.NoError(t, err) | ||
gotResources, err := plan.GetResources(tfPlan) | ||
assert.NoError(t, err) | ||
for _, got := range gotResources { | ||
if got.GetIdentification().ResourceType == "aws_launch_configuration" { | ||
// This should not exists, it should be ignored | ||
assert.Fail(t, "aws_launch_configuration should be ignored") | ||
} else if got.GetIdentification().ResourceType == "aws_autoscaling_group" { | ||
assert.Equal(t, wantResources[got.GetAddress()], got) | ||
} else { | ||
// Anything else should be unsupported | ||
assert.IsType(t, resources.UnsupportedResource{}, got) | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -15,5 +15,6 @@ provider: | |
aws: | ||
avg_cpu_use: 0.5 | ||
avg_gpu_use: 0.5 | ||
avg_autoscaler_size_percent: 0.5 | ||
log: | ||
level : "warn" |
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,33 @@ | ||
resource "aws_launch_template" "my_launch_template" { | ||
name_prefix = "my_launch_template" | ||
image_id = "${data.aws_ami.ubuntu.id}" | ||
instance_type = "m5d.xlarge" | ||
|
||
block_device_mappings { | ||
device_name = "/dev/sda1" | ||
|
||
ebs { | ||
volume_size = 300 | ||
volume_type = "standard" | ||
delete_on_termination = true | ||
} | ||
} | ||
|
||
block_device_mappings { | ||
device_name = "/dev/sdb" | ||
virtual_name = "ephemeral0" | ||
} | ||
|
||
} | ||
|
||
resource "aws_autoscaling_group" "asg_launch_template" { | ||
availability_zones = ["eu-west-3a"] | ||
desired_capacity = 4 | ||
max_size = 2 | ||
min_size = 10 | ||
|
||
launch_template { | ||
id = aws_launch_template.my_launch_template.id | ||
version = "$Latest" | ||
} | ||
} |
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,21 @@ | ||
|
||
resource "aws_autoscaling_group" "asg_with_launchconfig" { | ||
name = "asg_with_launchconfig" | ||
max_size = 10 | ||
min_size = 2 | ||
desired_capacity = 4 | ||
vpc_zone_identifier = [aws_subnet.subnet_1.id, aws_subnet.subnet_2.id] | ||
launch_configuration = aws_launch_configuration.asg_launch_config.name | ||
|
||
} | ||
|
||
resource "aws_launch_configuration" "asg_launch_config" { | ||
name = "asg_launch_config" | ||
image_id = data.aws_ami.ubuntu.id | ||
instance_type = "m5d.xlarge" | ||
|
||
ephemeral_block_device { | ||
device_name = "/dev/sdk" | ||
virtual_name = "ephemeral0" | ||
} | ||
} |
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,9 @@ | ||
data "aws_ami" "ubuntu" { | ||
most_recent = true | ||
|
||
filter { | ||
name = "block-device-mapping.volume-size" | ||
values = [ "30" ] | ||
} | ||
} | ||
|
Oops, something went wrong.