en1cc
en1cc

Reputation: 35

How to delete openstack_blockstorage_volume_v2 with this module

I wrote an module for the openstack provider to setup my instances. This worked well since I needed an extra volume. I dont need this on much machines, so it should be optional to add and remove this. Can anyone help me with my module? The way I wrote it, I'm unable to destroy it together with the instance.

instances.tf

locals {
  user_image_id   = element(concat(data.openstack_images_image_v2.user_image.*.id, [""]), 0)
  user_image_name = element(concat(data.openstack_images_image_v2.user_image.*.name, [""]), 0)
  metadata_default = {
    "managed-by-terraform" = true
  }
  tags_default = ["managed-by-terraform=true"]
}

data "cloudinit_config" "cloud_config_linux" {
  count         = var.instance_count
  gzip          = false
  base64_encode = false

  part {
    content_type = "text/cloud-config"
    content      = file("${path.module}/templates/cloud.cfg.tmpl")
  }
}


resource "openstack_compute_instance_v2" "instance" {
  count            = var.instance_count
  name             = "${var.instance_name}${format("%02d", count.index + 1)}"
  power_state      = var.power_state
  flavor_name      = var.flavor_name
  security_groups  = var.security_groups
  image_id         = var.image_id != null ? var.image_id : local.user_image_id != "" ? local.user_image_id : data.openstack_images_image_v2.default_image.id
  #user_data       = length(regexall("[Ww]indows", local.user_image_name)) > 0 ? data.cloudinit_config.cloud_config_windows.*.rendered[count.index] : data.cloudinit_config.cloud_config_linux.*.rendered[count.index]
  user_data        = data.cloudinit_config.cloud_config_linux.*.rendered[count.index]
  metadata         = merge(local.metadata_default, var.metadata)
  tags             = concat(local.tags_default, var.tags)
  key_pair         = var.key_pair

  network {
    name           = var.servergroup_name
    fixed_ip_v4    = var.fixed_ip_v4
  }
}

resource "openstack_blockstorage_volume_v2" "volume" {
  count       = var.create_volume ? 1 : 0
  name        = "${var.instance_name}_volume_${format("%02d", count.index + 1)}"
  size        = var.volume_size
  metadata    = merge(local.metadata_default, var.metadata)
}

resource "openstack_compute_volume_attach_v2" "attached" {
  count       = var.create_volume ? 1 : 0
  instance_id = openstack_compute_instance_v2.instance.*.id[count.index]
  volume_id   = openstack_blockstorage_volume_v2.volume.*.id[count.index]
}

resource "openstack_compute_floatingip_v2" "floatingip" {
  count = var.attach_floatingip ? var.instance_count : 0
  pool  = var.floatingip_pool
}

resource "openstack_compute_floatingip_associate_v2" "floatingip_associate" {
  count       = var.attach_floatingip ? var.instance_count : 0
  floating_ip = openstack_compute_floatingip_v2.floatingip.*.address[count.index]
  instance_id = openstack_compute_instance_v2.instance.*.id[count.index]
}

variables.tf

variable "instance_count" {
  description = "The amount of instances which should be spawned."
  type        = number
  default     = 1
}

variable "instance_name" {
  description = "The instance name. Only the last part, tentant and availability zone will be automatically prepended."
  type        = string
}

variable "power_state" {
  description = "Power state of the instances. 'active' or 'shutoff'."
  type        = string
  default     = "active"
}

variable "key_pair" {
  description = "The name of a key pair to put on the server. The key pair must already be created and associated with the tenant's account. Changing this creates a new server. Currently only used for debugging!"
  type        = string
  default     = null
}

variable "flavor_name" {
  description = "The OpenStack instance flavor name which should be used."
  type        = string
  default     = "m1c.tiny"
}

variable "image_id" {
  description = "The OpenStack image id which should be used. If none is given and image_name is empty, we automatically use our best practice image (currently Debian 11)."
  type        = string
  default     = null
}

variable "image_name" {
  description = "The OpenStack image name which should be used. If none is given and image_id is empty, we automatically use our best practice image (currently Debian 11)."
  type        = string
  default     = null
}

variable "metadata" {
  description = "Metadata for this isntance."
  type        = map(string)
  default     = {}
}

variable "tags" {
  description = "Tags for this instance."
  type        = list(string)
  default     = []
}

variable "security_groups" {
  description = "A list of security groups this instance should have attached."
  type        = list(string)
  default     = ["japasp_default"]
}

variable "servergroup_name" {
  description = "The name of the server group this instance belongs to."
  type        = string
}

variable "create_volume" {
  type        = bool
  default     = false
}

variable "volume_size" {
  type        = number
  default     = 0
}

variable "attach_floatingip" {
  description = "If the instance should get a floating IP attached. Be careful, if you use instance_count because every instance will get one attached."
  type        = bool
  default     = false
}

variable "floatingip_pool" {
  description = "The pool of floating IPs which should be used."
  type        = string
  default     = "ext-net"
}

variable "fixed_ip_v4" {
  description = "The static *internal* IP for this servers. Ensure you block it in the DHCP!"
  type        = string
  default     = null
}

How I call the module

module "prod_nextcloud" {
  instance_count    = 0
  source            = "gitlab.com/XXXXX/instance/openstack"
  version           = "0.6.5"
  instance_name     = "prod_nextcloud"
  flavor_name       = "m1.medium"
  servergroup_name  = "servergroup_cbk"
  security_groups   = ["japasp_default","http"]
  attach_floatingip = true
  fixed_ip_v4       = "172.21.11.111"
  create_volume     = true
  volume_size       = 350
  providers         = { openstack = openstack.cbk }
}


output "prod_nextcloud_cbk_public_ip" {
  description = "List with public IPs of the instances"
  value       = module.prod_nextcloud.public_ips
}

´terraform plan` output:

╷
│ Error: Invalid index
│ 
│   on cloudflare_japasp_systems.tf line 11, in module "cloudflare_A_prod_nextcloud":
│   11:   record_value = "${module.prod_nextcloud.public_ips[0]}"
│     ├────────────────
│     │ module.prod_nextcloud.public_ips is empty tuple
│ 
│ The given key does not identify an element in this collection value: the
│ collection has no elements.
╵
╷
│ Error: Invalid index
│ 
│   on .terraform/modules/prod_nextcloud/instances.tf line 50, in resource "openstack_compute_volume_attach_v2" "attached":
│   50:   instance_id = openstack_compute_instance_v2.instance.*.id[count.index]
│     ├────────────────
│     │ count.index is 0
│     │ openstack_compute_instance_v2.instance is empty tuple
│ 
│ The given key does not identify an element in this collection value: the
│ collection has no elements.
╵
Uploading artifacts for failed job
00:00
Uploading artifacts...
WARNING: /builds/japasp/terraform/environment/production/production.plan.json: no matching files. Ensure that the artifact path is relative to the working directory 
ERROR: No files to upload                          
Cleaning up project directory and file based variables
00:01
ERROR: Job failed: exit code 1

Upvotes: 0

Views: 257

Answers (0)

Related Questions