Skip to content

Commit

Permalink
add network port and security group association
Browse files Browse the repository at this point in the history
  • Loading branch information
orachide committed Mar 24, 2019
1 parent e84f99b commit 535727c
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 15 deletions.
20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
# My new created Terraform module
# Terraform module for Openstack Instance

Introduce your module briefly.
This module create an Openstack instance

## Usage

Provide the sample code to use your module.
```
resource "openstack_compute_keypair_v2" "keypair" {
name = "my-keypair"
}
module "compute" {
source = "shepherdcloud/instance/openstack"
instance_name = "BLUE"
instance_count = 2
image_name = "cirros"
flavor_name = "m1.tiny"
keypair = "${openstack_compute_keypair_v2.keypair.name}"
network_name = "my-network"
security_group_names = ["default"]
}
```

## Scenarios

Expand Down
19 changes: 13 additions & 6 deletions examples/simple/main.tf
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@


resource "openstack_compute_keypair_v2" "keypair" {
name = "my-keypair"
}
module "compute" {
source = "../../"
compute_name = "BLUE"
instance_count = 2
image_name = "cirros"
flavor_name = "m1.tiny"
keypair = "shepherd"
source = "../../"
instance_name = "BLUE"
instance_count = 2
image_name = "cirros"
flavor_name = "m1.tiny"
keypair = "${openstack_compute_keypair_v2.keypair.name}"
network_name = "my-network"
security_group_names = ["default"]
}
40 changes: 36 additions & 4 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,43 @@ data "openstack_images_image_v2" "this" {
most_recent = true
}

data "openstack_networking_network_v2" "this" {
name = "${var.network_name}"
}

data "openstack_networking_secgroup_v2" "this" {
count = "${length(var.security_group_names)}"

name = "${var.security_group_names[count.index]}"
}

resource "openstack_compute_instance_v2" "this" {
count = "${var.instance_count}"

name = "${var.compute_name}-${count.index}"
image_name = "${data.openstack_images_image_v2.this.name}"
flavor_id = "${data.openstack_compute_flavor_v2.this.id}"
key_pair = "${var.keypair}"
name = "${var.instance_name}-${count.index}"
image_name = "${data.openstack_images_image_v2.this.name}"
flavor_id = "${data.openstack_compute_flavor_v2.this.id}"
key_pair = "${var.keypair}"

network {
port = "${openstack_networking_port_v2.this.*.id[count.index]}"
}
}

resource "openstack_networking_port_v2" "this" {
count = "${var.instance_count}"

name = "${var.network_name}-port-${count.index}"
network_id = "${data.openstack_networking_network_v2.this.id}"
admin_state_up = "true"
security_group_ids = ["${data.openstack_networking_secgroup_v2.this.*.id}"]
}

# resource "openstack_compute_interface_attach_v2" "this" {
# count = "${var.instance_count}"


# instance_id = "${openstack_compute_instance_v2.this.*.id[count.index]}"
# port_id = "${openstack_networking_port_v2.this.*.id[count.index]}"
# }

2 changes: 1 addition & 1 deletion outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ output "ids" {
}

output "names" {
description = "List of IDs of instances"
description = "List of instances name"
value = "${openstack_compute_instance_v2.this.*.name}"
}
14 changes: 13 additions & 1 deletion variables.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
variable "compute_name" {
variable "instance_name" {
type = "string"
description = "The name (prefix) of the compute instance to create."
}
Expand All @@ -23,3 +23,15 @@ variable "keypair" {
type = "string"
description = "The name of the keypair to use"
}

variable "network_name" {
type = "string"
default = ""
description = "The name of the network to attach instance to"
}

variable "security_group_names" {
type = "list"
default = ["default"]
description = "The name of the network to attach instance to"
}

0 comments on commit 535727c

Please sign in to comment.