Skip to content
This repository has been archived by the owner on Sep 9, 2022. It is now read-only.

Add support for batch job queue #473

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ $ terraforming
Commands:
terraforming alb # ALB
terraforming asg # AutoScaling Group
terraforming bjq # Batch Job Queue
terraforming cwa # CloudWatch Alarm
terraforming dbpg # Database Parameter Group
terraforming dbsg # Database Security Group
Expand Down
2 changes: 2 additions & 0 deletions lib/terraforming.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "aws-sdk-autoscaling"
require "aws-sdk-batch"
require "aws-sdk-cloudwatch"
require "aws-sdk-dynamodb"
require "aws-sdk-ec2"
Expand Down Expand Up @@ -26,6 +27,7 @@
require "terraforming/cli"
require "terraforming/resource/alb"
require "terraforming/resource/auto_scaling_group"
require "terraforming/resource/batch_job_queue"
require "terraforming/resource/cloud_watch_alarm"
require "terraforming/resource/db_parameter_group"
require "terraforming/resource/db_security_group"
Expand Down
5 changes: 5 additions & 0 deletions lib/terraforming/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ def asg
execute(Terraforming::Resource::AutoScalingGroup, options)
end

desc "bjq", "Batch Job Queue"
def bjq
execute(Terraforming::Resource::BatchJobQueue, options)
end

desc "cwa", "CloudWatch Alarm"
def cwa
execute(Terraforming::Resource::CloudWatchAlarm, options)
Expand Down
74 changes: 74 additions & 0 deletions lib/terraforming/resource/batch_job_queue.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
module Terraforming
module Resource
class BatchJobQueue
include Terraforming::Util

def self.tf(client: Aws::Batch::Client.new)
self.new(client).tf
end

def self.tfstate(client: Aws::Batch::Client.new)
self.new(client).tfstate
end

def initialize(client)
@client = client
end

def tf
apply_template(@client, "tf/batch_job_queue")
end

def tfstate
queues.inject({}) do |resources, queue|
attributes = {
"arn" => queue.job_queue_arn,
"id" => queue.job_queue_arn,
"name" => queue.job_queue_name,
"priority" => queue.priority.to_s,
"state" => queue.state,
}

attributes.merge!(compute_environments_attributes_of(queue))

resources["aws_batch_job_queue.#{module_name_of(queue)}"] = {
"type" => "aws_batch_job_queue",
"primary" => {
"id" => queue.job_queue_arn,
"attributes" => attributes
}
}
resources
end
end

private

def queues
@client.describe_job_queues.map(&:job_queues).flatten
end

def module_name_of(queue)
normalize_module_name(queue.job_queue_name)
end

def compute_environments_attributes_of(queue)
attributes = { "compute_environments.#" => queue.compute_environment_order.length.to_s }

queue.compute_environment_order.each do |compute_environment|
attributes.merge!(compute_environment_attributes_of(compute_environment))
end

attributes
end

def compute_environment_attributes_of(compute_environment)
attributes = {
"compute_environments.#{compute_environment.order.to_s}" => compute_environment.compute_environment
}

attributes
end
end
end
end
12 changes: 12 additions & 0 deletions lib/terraforming/template/tf/batch_job_queue.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<% queues.each do |queue| -%>
resource "aws_batch_job_queue" "<%= normalize_module_name(queue.job_queue_name) %>" {
name = "<%= queue.job_queue_name %>"
priority = "<%= queue.priority %>"
state = "<%= queue.state %>"
compute_environments = [
<% queue.compute_environment_order.each do |compute_environment| -%>
"<%= compute_environment.compute_environment %>",
<% end -%>
]
}
<%- end -%>
7 changes: 7 additions & 0 deletions spec/lib/terraforming/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ module Terraforming
it_behaves_like "CLI examples"
end

describe "bjq" do
let(:klass) { Terraforming::Resource::BatchJobQueue }
let(:command) { :bjq }

it_behaves_like "CLI examples"
end

describe "cwa" do
let(:klass) { Terraforming::Resource::CloudWatchAlarm }
let(:command) { :cwa }
Expand Down
99 changes: 99 additions & 0 deletions spec/lib/terraforming/resource/batch_job_queue_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
require "spec_helper"

module Terraforming
module Resource
describe BatchJobQueue do
let(:client) do
Aws::Batch::Client.new(stub_responses: true)
end

let(:queues) do
[
{
compute_environment_order: [
{
compute_environment: "arn:aws:batch:us-east-1:012345678910:compute-environment/C4OnDemand",
order: 1,
},
],
job_queue_arn: "arn:aws:batch:us-east-1:012345678910:job-queue/HighPriority",
job_queue_name: "HighPriority",
priority: 1,
state: "ENABLED",
status: "VALID",
status_reason: "JobQueue Healthy",
},
{
compute_environment_order: [
{
compute_environment: "M4Spot",
order: 1,
},
],
job_queue_name: "LowPriority",
priority: 10,
state: "DISABLE",
}
]
end

before do
client.stub_responses(:describe_job_queues, job_queues: queues)
end

describe ".tf" do
it "should generate tf" do
expect(described_class.tf(client: client)).to eq <<-EOS
resource "aws_batch_job_queue" "HighPriority" {
name = "HighPriority"
priority = 1
state = "ENABLED"
compute_environments = ["arn:aws:batch:us-east-1:012345678910:compute-environment/C4OnDemand"]
}

resource "aws_batch_job_queue" "LowPriority" {
name = "LowPriority"
priority = 10
state = "DISABLE"
compute_environments = ["M4Spot"]
}

EOS
end
end

describe ".tfstate" do
it "should generate tfstate" do
expect(described_class.tfstate(client: client)).to eq({
"aws_batch_job_queue.HighPriority" => {
"type" => "aws_batch_job_queue",
"primary" => {
"id" => "HighPriority",
"attributes" => {
"name" => "HighPriority",
"priority" => "1",
"state" => "ENABLED",
"compute_environments.order" => "1",
"compute_environments.compute_environment" => "arn:aws:batch:us-east-1:012345678910:compute-environment/C4OnDemand"
}
}
},
"aws_batch_job_queue.LowPriority" => {
"type" => "aws_batch_job_queue",
"primary" => {
"id" => "LowPriority",
"attributes" => {
"name" => "LowPriority",
"priority" => "10",
"state" => "DISABLE",
"compute_environments.order" => "1",
"compute_environments.compute_environment" => "M4Spot"
}
}
}
})
end
end
end
end
end
1 change: 1 addition & 0 deletions terraforming.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"]

spec.add_dependency "aws-sdk-autoscaling", "~> 1"
spec.add_dependency "aws-sdk-batch", "~> 1"
spec.add_dependency "aws-sdk-cloudwatch", "~> 1"
spec.add_dependency "aws-sdk-dynamodb", "~> 1.18"
spec.add_dependency "aws-sdk-ec2", "~> 1"
Expand Down