From 535727cd3e17b7c1732efe4a4d28440997caee67 Mon Sep 17 00:00:00 2001 From: Rachid Date: Sun, 24 Mar 2019 19:09:11 +0100 Subject: [PATCH] add network port and security group association --- README.md | 20 +++++++++++++++++--- examples/simple/main.tf | 19 +++++++++++++------ main.tf | 40 ++++++++++++++++++++++++++++++++++++---- outputs.tf | 2 +- variables.tf | 14 +++++++++++++- 5 files changed, 80 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 18a8d9b..d969bb0 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/examples/simple/main.tf b/examples/simple/main.tf index 14be1f1..83fef3c 100644 --- a/examples/simple/main.tf +++ b/examples/simple/main.tf @@ -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"] } diff --git a/main.tf b/main.tf index 6982348..1701422 100644 --- a/main.tf +++ b/main.tf @@ -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]}" +# } + diff --git a/outputs.tf b/outputs.tf index 60c4c1e..f7537b9 100644 --- a/outputs.tf +++ b/outputs.tf @@ -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}" } \ No newline at end of file diff --git a/variables.tf b/variables.tf index 966f7f7..6218967 100644 --- a/variables.tf +++ b/variables.tf @@ -1,4 +1,4 @@ -variable "compute_name" { +variable "instance_name" { type = "string" description = "The name (prefix) of the compute instance to create." } @@ -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" +}