Skip to content

Commit

Permalink
Update code examples
Browse files Browse the repository at this point in the history
  • Loading branch information
brikis98 committed Jul 14, 2019
1 parent d6e8e02 commit 8370372
Show file tree
Hide file tree
Showing 18 changed files with 353 additions and 530 deletions.
39 changes: 10 additions & 29 deletions code/ruby/07-testing-terraform-code/web-server-full-di.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,39 +22,20 @@ def initialize(web_service)
def handle(path)
case path
when "/"
self.hello
[200, 'text/plain', 'Hello, World']
when "/api"
self.api
[201, 'application/json', '{"foo":"bar"}']
when "/web-service"
self.web_service
else
self.not_found
end
end

def hello
[200, 'text/plain', 'Hello, World']
end

def api
[201, 'application/json', '{"foo":"bar"}']
end

# New endpoint that calls a web service
=begin
def web_service
uri = URI("http://www.example.org")
response = Net::HTTP.get_response(uri)
[response.code.to_i, response['Content-Type'], response.body]
end
uri = URI("http://www.example.org")
response = Net::HTTP.get_response(uri)
[response.code.to_i, response['Content-Type'], response.body]
=end

def web_service
@web_service.proxy
end

def not_found
[404, 'text/plain', 'Not Found']
@web_service.proxy
else
[404, 'text/plain', 'Not Found']
end
end
end

Expand Down
18 changes: 3 additions & 15 deletions code/ruby/07-testing-terraform-code/web-server-full.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,13 @@ class Handlers
def handle(path)
case path
when "/"
self.hello
[200, 'text/plain', 'Hello, World']
when "/api"
self.api
[201, 'application/json', '{"foo":"bar"}']
else
self.not_found
[404, 'text/plain', 'Not Found']
end
end

def hello
[200, 'text/plain', 'Hello, World']
end

def api
[201, 'application/json', '{"foo":"bar"}']
end

def not_found
[404, 'text/plain', 'Not Found']
end
end

# This will only run if this script was called directly from the CLI, but
Expand Down
24 changes: 4 additions & 20 deletions code/ruby/08-terraform-team/web-server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,15 @@ def initialize(web_service)
def handle(path)
case path
when "/"
self.hello
[200, 'text/plain', 'Hello, World']
when "/api"
self.api
[201, 'application/json', '{"foo":"bar"}']
when "/web-service"
self.web_service
@web_service.proxy
else
self.not_found
[404, 'text/plain', 'Not Found']
end
end

def hello
[200, 'text/plain', 'Hello, World']
end

def api
[201, 'application/json', '{"foo":"bar"}']
end

def web_service
@web_service.proxy
end

def not_found
[404, 'text/plain', 'Not Found']
end
end

class WebService
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ resource "aws_launch_configuration" "example" {
echo "Hello, World" > index.html
nohup busybox httpd -f -p ${var.server_port} &
EOF

# Required when using a launch configuration with an auto scaling group.
# https://www.terraform.io/docs/providers/aws/r/launch_configuration.html
lifecycle {
create_before_destroy = true
}
}

resource "aws_autoscaling_group" "example" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ resource "aws_launch_configuration" "example" {
instance_type = "t2.micro"
security_groups = [aws_security_group.instance.id]
user_data = data.template_file.user_data.rendered

# Required when using a launch configuration with an auto scaling group.
# https://www.terraform.io/docs/providers/aws/r/launch_configuration.html
lifecycle {
create_before_destroy = true
}
}

data "template_file" "user_data" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ resource "aws_launch_configuration" "example" {
instance_type = var.instance_type
security_groups = [aws_security_group.instance.id]
user_data = data.template_file.user_data.rendered

# Required when using a launch configuration with an auto scaling group.
# https://www.terraform.io/docs/providers/aws/r/launch_configuration.html
lifecycle {
create_before_destroy = true
}
}

data "template_file" "user_data" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ resource "aws_launch_configuration" "example" {
? data.template_file.user_data[0].rendered
: data.template_file.user_data_new[0].rendered
)

# Required when using a launch configuration with an auto scaling group.
# https://www.terraform.io/docs/providers/aws/r/launch_configuration.html
lifecycle {
create_before_destroy = true
}
}

data "template_file" "user_data" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ resource "aws_launch_configuration" "example" {
security_groups = [aws_security_group.instance.id]

user_data = data.template_file.user_data.rendered

# Required when using a launch configuration with an auto scaling group.
# https://www.terraform.io/docs/providers/aws/r/launch_configuration.html
lifecycle {
create_before_destroy = true
}
}

data "template_file" "user_data" {
Expand All @@ -22,8 +28,8 @@ data "template_file" "user_data" {
}

resource "aws_autoscaling_group" "example" {
# Explicitly depend on the launch configuration's name so each time it's replaced,
# this ASG is also replaced
# Explicitly depend on the launch configuration's name so each time it's
# replaced, this ASG is also replaced
name = "${var.cluster_name}-${aws_launch_configuration.example.name}"

launch_configuration = aws_launch_configuration.example.name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ resource "aws_launch_configuration" "example" {
instance_type = var.instance_type
security_groups = [aws_security_group.instance.id]
user_data = var.user_data

# Required when using a launch configuration with an auto scaling group.
# https://www.terraform.io/docs/providers/aws/r/launch_configuration.html
lifecycle {
create_before_destroy = true
}
}

resource "aws_autoscaling_group" "example" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
variable "mysql_config" {
description = "The config for the MySQL DB"

type = object({
type = object({
address = string
port = number
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ resource "aws_launch_configuration" "example" {
instance_type = var.instance_type
security_groups = [aws_security_group.instance.id]
user_data = var.user_data

# Required when using a launch configuration with an auto scaling group.
# https://www.terraform.io/docs/providers/aws/r/launch_configuration.html
lifecycle {
create_before_destroy = true
}
}

resource "aws_autoscaling_group" "example" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ func TestHelloWorldAppExample(t *testing.T) {
t.Parallel()

opts := &terraform.Options{
// You should update this relative path to point at your alb
// example directory!
// You should update this relative path to point at your
// hello-world-app example directory!
TerraformDir: "../examples/hello-world-app/standalone",

Vars: map[string]interface{}{
Expand Down
Loading

0 comments on commit 8370372

Please sign in to comment.