Skip to content

Commit 535727c

Browse files
committed
add network port and security group association
1 parent e84f99b commit 535727c

File tree

5 files changed

+80
-15
lines changed

5 files changed

+80
-15
lines changed

README.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
1-
# My new created Terraform module
1+
# Terraform module for Openstack Instance
22

3-
Introduce your module briefly.
3+
This module create an Openstack instance
44

55
## Usage
66

7-
Provide the sample code to use your module.
7+
```
8+
resource "openstack_compute_keypair_v2" "keypair" {
9+
name = "my-keypair"
10+
}
11+
module "compute" {
12+
source = "shepherdcloud/instance/openstack"
13+
instance_name = "BLUE"
14+
instance_count = 2
15+
image_name = "cirros"
16+
flavor_name = "m1.tiny"
17+
keypair = "${openstack_compute_keypair_v2.keypair.name}"
18+
network_name = "my-network"
19+
security_group_names = ["default"]
20+
}
21+
```
822

923
## Scenarios
1024

examples/simple/main.tf

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1+
2+
3+
resource "openstack_compute_keypair_v2" "keypair" {
4+
name = "my-keypair"
5+
}
16
module "compute" {
2-
source = "../../"
3-
compute_name = "BLUE"
4-
instance_count = 2
5-
image_name = "cirros"
6-
flavor_name = "m1.tiny"
7-
keypair = "shepherd"
7+
source = "../../"
8+
instance_name = "BLUE"
9+
instance_count = 2
10+
image_name = "cirros"
11+
flavor_name = "m1.tiny"
12+
keypair = "${openstack_compute_keypair_v2.keypair.name}"
13+
network_name = "my-network"
14+
security_group_names = ["default"]
815
}

main.tf

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,43 @@ data "openstack_images_image_v2" "this" {
77
most_recent = true
88
}
99

10+
data "openstack_networking_network_v2" "this" {
11+
name = "${var.network_name}"
12+
}
13+
14+
data "openstack_networking_secgroup_v2" "this" {
15+
count = "${length(var.security_group_names)}"
16+
17+
name = "${var.security_group_names[count.index]}"
18+
}
19+
1020
resource "openstack_compute_instance_v2" "this" {
1121
count = "${var.instance_count}"
1222

13-
name = "${var.compute_name}-${count.index}"
14-
image_name = "${data.openstack_images_image_v2.this.name}"
15-
flavor_id = "${data.openstack_compute_flavor_v2.this.id}"
16-
key_pair = "${var.keypair}"
23+
name = "${var.instance_name}-${count.index}"
24+
image_name = "${data.openstack_images_image_v2.this.name}"
25+
flavor_id = "${data.openstack_compute_flavor_v2.this.id}"
26+
key_pair = "${var.keypair}"
27+
28+
network {
29+
port = "${openstack_networking_port_v2.this.*.id[count.index]}"
30+
}
31+
}
32+
33+
resource "openstack_networking_port_v2" "this" {
34+
count = "${var.instance_count}"
35+
36+
name = "${var.network_name}-port-${count.index}"
37+
network_id = "${data.openstack_networking_network_v2.this.id}"
38+
admin_state_up = "true"
39+
security_group_ids = ["${data.openstack_networking_secgroup_v2.this.*.id}"]
1740
}
41+
42+
# resource "openstack_compute_interface_attach_v2" "this" {
43+
# count = "${var.instance_count}"
44+
45+
46+
# instance_id = "${openstack_compute_instance_v2.this.*.id[count.index]}"
47+
# port_id = "${openstack_networking_port_v2.this.*.id[count.index]}"
48+
# }
49+

outputs.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ output "ids" {
44
}
55

66
output "names" {
7-
description = "List of IDs of instances"
7+
description = "List of instances name"
88
value = "${openstack_compute_instance_v2.this.*.name}"
99
}

variables.tf

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
variable "compute_name" {
1+
variable "instance_name" {
22
type = "string"
33
description = "The name (prefix) of the compute instance to create."
44
}
@@ -23,3 +23,15 @@ variable "keypair" {
2323
type = "string"
2424
description = "The name of the keypair to use"
2525
}
26+
27+
variable "network_name" {
28+
type = "string"
29+
default = ""
30+
description = "The name of the network to attach instance to"
31+
}
32+
33+
variable "security_group_names" {
34+
type = "list"
35+
default = ["default"]
36+
description = "The name of the network to attach instance to"
37+
}

0 commit comments

Comments
 (0)