Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Folder exists for new projects #219

Closed
pryorda opened this issue Oct 23, 2017 · 7 comments
Closed

Folder exists for new projects #219

pryorda opened this issue Oct 23, 2017 · 7 comments

Comments

@pryorda
Copy link

pryorda commented Oct 23, 2017

Hi there,

Looks like there is a problem creating folders that already exist.

Terraform Version

2017/10/23 11:54:25 [INFO] Terraform version: 0.10.7 5f9bf20ba6aaa174600109a95cc77f7788d4959b+CHANGES

vSphere Provider Version

v0.4.2

Affected Resource(s)

resource "vsphere_folder"

Terraform Configuration Files

tfvars file
environment = "qa"

# Application Network
vsphere_network_label = "dvPortGroup_qa"
vsphere_network_ipv4_gateway = "xxx"
vsphere_network_ipv4_addresses = "xxx"
# Storage Network
vsphere_secondary_network_label = "Storage Network"
vsphere_secondary_network_ipv4_prefix_length = 24
vsphere_secondary_network_ipv4_addresses = "xxx"

# VM Location
vsphere_cluster = "VM02 Cluster"
vsphere_folder_path = "QA/QAEX"
vsphere_datastore = "Nimble Datastores/qaex"

# VM Specs
vsphere_vcpu = "8"
vsphere_memory = "8192"
data_disk_size = "50"
# Providers
# VSphere
provider "vsphere" {
  user                 = "${var.vsphere_username}"
  password             = "${var.vsphere_password}"
  vsphere_server       = "${var.vsphere_server}"
  allow_unverified_ssl = "${var.vsphere_allow_unverified_ssl}"
}

data "vsphere_datacenter" "datacenter" {
  name = "${var.vsphere_datacenter}"
}

resource "vsphere_folder" "instance" {
  type           = "vm"
  path           = "${var.vsphere_folder_path}"
  datacenter_id  = "${data.vsphere_datacenter.datacenter.id}"
  count          = "${var.vsphere_network_ipv4_addresses != "" ? 1 : 0}"
}

# DNS for instances
data "aws_route53_zone" "private-domain" {
  name         = "${lower(var.provider)}.domain.com."
  private_zone = true
}

resource "aws_route53_record" "instance-dns" {
  zone_id = "${data.aws_route53_zone.private-domain.id}"
  type    = "A"
  ttl     = "900"
  records = ["${element(split(",", var.vsphere_network_ipv4_addresses), count.index)}"]
  name    = "${lower(data.null_data_source.hostname.outputs["name"])}${count.index + 1}.${lower(var.region)}"
  count   = "${var.vsphere_network_ipv4_addresses != "" ? length(split(",", "${var.vsphere_network_ipv4_addresses}")) : 0}"
}

# Instance Resource
resource "vsphere_virtual_machine" "instance" {
  count   = "${var.vsphere_network_ipv4_addresses != "" ? length(split(",", "${var.vsphere_network_ipv4_addresses}")) : 0}"
  name  = "${lower(data.null_data_source.hostname.outputs["name"])}${count.index + 1}.${lower(var.region)}"

  detach_unknown_disks_on_delete = true

  folder     = "${vsphere_folder.instance.path}"
  datacenter = "${var.vsphere_datacenter}"
  cluster    = "${var.vsphere_cluster}"
  domain     = "${lower(var.region)}.${lower(var.provider)}.domain.com"

  dns_servers  = ["${split(",",var.vsphere_network_domain_resolvers)}"]
  dns_suffixes = ["${lower(var.region)}.${lower(var.provider)}.domain.com","${split(",",var.vsphere_network_domain_search)}"]
  vcpu         = "${var.vsphere_vcpu}"
  memory       = "${var.vsphere_memory}"
  time_zone    = "${var.vsphere_cluster_timezone}"

  network_interface {
    label              = "${var.vsphere_network_label}"
    ipv4_address       = "${element(split(",", var.vsphere_network_ipv4_addresses), count.index)}"
    ipv4_prefix_length = "${var.vsphere_network_ipv4_prefix_length}"
    ipv4_gateway       = "${var.vsphere_network_ipv4_gateway}"
  }

  disk {
    datastore = "${var.vsphere_datastore}"
    template  = "${var.vsphere_template}"
    type      = "thin"
  }

  disk {
    datastore = "${var.vsphere_datastore}"
    vmdk      = "${lower(data.null_data_source.hostname.outputs["name"])}${count.index + 1}.${lower(var.region)}.data_disk.vmdk"
    keep_on_remove = true
  }

  lifecycle {
    ignore_changes = ["network_interface", "dns_suffixes"]
  }
}

resource "vsphere_virtual_disk" "data_disk" {
  count   = "${var.vsphere_network_ipv4_addresses != "" ? length(split(",", "${var.vsphere_network_ipv4_addresses}")) : 0}"
  size          = "${var.data_disk_size}"
  vmdk_path     = "${lower(data.null_data_source.hostname.outputs["name"])}${count.index + 1}.${lower(var.region)}.data_disk.vmdk"
  datacenter    = "${var.vsphere_datacenter}"
  datastore     = "${var.vsphere_datastore}"
  type          = "thin"
  adapter_type  = "lsiLogic"
  lifecycle {
    prevent_destroy = true
  }
}

output "instance_ids" {
  value = ["${vsphere_virtual_machine.instance.*.id}"]
}

output "instances_dns" {
  value = ["${vsphere_virtual_machine.instance.*.name}"]
}

output "instance_private_ips" {
  value = ["${vsphere_virtual_machine.instance.*.network_interface.0.ipv4_address}"]
}

Debug Output

2017/10/23 11:54:38 [TRACE] [walkApply] Entering eval tree: module.vsphere_linux.vsphere_folder.instance
2017/10/23 11:54:38 [TRACE] root.vsphere_linux: eval: *terraform.EvalSequence
2017/10/23 11:54:38 [TRACE] root.vsphere_linux: eval: *terraform.EvalInstanceInfo
2017/10/23 11:54:38 [TRACE] root.vsphere_linux: eval: *terraform.EvalReadDiff
2017/10/23 11:54:38 [TRACE] root.vsphere_linux: eval: *terraform.EvalIf
2017/10/23 11:54:38 [TRACE] root.vsphere_linux: eval: terraform.EvalNoop
2017/10/23 11:54:38 [TRACE] root.vsphere_linux: eval: *terraform.EvalIf
2017/10/23 11:54:38 [TRACE] root.vsphere_linux: eval: *terraform.EvalInterpolate
2017/10/23 11:54:38 [TRACE] root.vsphere_linux: eval: *terraform.EvalGetProvider
2017/10/23 11:54:38 [TRACE] root.vsphere_linux: eval: *terraform.EvalReadState
2017/10/23 11:54:38 [TRACE] root.vsphere_linux: eval: *terraform.EvalValidateResource
2017/10/23 11:54:38 [TRACE] root.vsphere_linux: eval: *terraform.EvalDiff
2017/10/23 11:54:38 [TRACE] root.vsphere_linux: eval: *terraform.EvalReadDiff
2017/10/23 11:54:38 [TRACE] root.vsphere_linux: eval: *terraform.EvalCompareDiff
2017/10/23 11:54:38 [TRACE] root.vsphere_linux: eval: *terraform.EvalGetProvider
2017/10/23 11:54:38 [TRACE] root.vsphere_linux: eval: *terraform.EvalReadState
2017/10/23 11:54:38 [TRACE] root.vsphere_linux: eval: *terraform.EvalApplyPre
2017/10/23 11:54:38 [TRACE] root.vsphere_linux: eval: *terraform.EvalApply
2017/10/23 11:54:38 [DEBUG] apply: vsphere_folder.instance: executing Apply
2017/10/23 11:54:38 [TRACE] root.vsphere_linux: eval: *terraform.EvalWriteState
2017/10/23 11:54:38 [TRACE] root.vsphere_linux: eval: *terraform.EvalApplyProvisioners
2017/10/23 11:54:38 [TRACE] root.vsphere_linux: eval: *terraform.EvalIf
2017/10/23 11:54:38 [TRACE] root.vsphere_linux: eval: *terraform.EvalWriteState
2017/10/23 11:54:38 [TRACE] root.vsphere_linux: eval: *terraform.EvalWriteDiff
2017/10/23 11:54:38 [TRACE] root.vsphere_linux: eval: *terraform.EvalApplyPost
2017/10/23 11:54:38 [ERROR] root.vsphere_linux: eval: *terraform.EvalApplyPost, err: 1 error(s) occurred:

* vsphere_folder.instance: error creating folder: ServerFaultCode: The name 'QAEX' already exists.
2017/10/23 11:54:38 [ERROR] root.vsphere_linux: eval: *terraform.EvalSequence, err: 1 error(s) occurred:

* vsphere_folder.instance: error creating folder: ServerFaultCode: The name 'QAEX' already exists.
2017/10/23 11:54:38 [TRACE] [walkApply] Exiting eval tree: module.vsphere_linux.vsphere_folder.instance
@pryorda
Copy link
Author

pryorda commented Oct 23, 2017

#38 Looks like the same issue.

@pryorda
Copy link
Author

pryorda commented Oct 23, 2017

@vancluever what made you remove all the find logic from the old create method?

@vancluever
Copy link
Contributor

vancluever commented Oct 23, 2017

@pryorda - sorry you have having trouble. Are you trying to create a folder resource for a folder that already exists?

If that's the case, what you would want to do instead is import the resource into the existing configuration. Have you tried that yet?

The implicit import behaviour that existed in this resource before was removed because it probably should not have existed in the first place - it never was Terraform's place to manage the lifecycle of resources that it did not create until explicit import became available, and I can only assume that it was there because import or data source functionality did not exist at the time it was created.

Keep in mind that importing the resource will subject it to full lifecycle management too, so if you destroy the resource, the folder will delete itself as well (something that I think might not have been happening under the old implementation as well, but I'm not 100% sure). If that's not acceptable for you, I would just forgo using the resource for now, as if the folder is created already and you don't want to manage it in Terraform, then the resource is not really bringing much value to you and you can just use the path directly in the vsphere_virtual_machine resource.

Thanks!

@pryorda
Copy link
Author

pryorda commented Oct 23, 2017

Are you working on a datasource for folders we can use?

@vancluever
Copy link
Contributor

Hey @pryorda, right now no resource takes an ID for a folder, so while I'm not necessarily ruling out that there won't be one in the future, right now there are no plans to create one. The main driver for that work would be if we decide to wholesale move the folder arguments in every resource over to take an ID, so that most of the heavy lifting of looking for and asserting a specific type of folder is done before attempting to effect a resource on it., but that's still TBD.

For now, we are just recommending that you use the path directly, either by hardcoding it or by putting it in a variable.

Thanks!

@pryorda
Copy link
Author

pryorda commented Oct 28, 2017

What would the import syntax be for a folder?

@vancluever
Copy link
Contributor

Hey @pryorda, you can refer to the documentation here for the import syntax.

Closing this one now a it's not really a bug but an (answered) question.

Thanks!

@ghost ghost locked and limited conversation to collaborators Apr 19, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants