yandex-cloud / terraform-provider-yandex

Terraform Yandex provider
https://www.terraform.io/docs/providers/yandex/
Mozilla Public License 2.0
208 stars 114 forks source link

Datasource for yandex_vpc_subnet and yandex_compute_image do not work by resource name #59

Closed asterix201 closed 4 years ago

asterix201 commented 4 years ago

It is not possible to get data about a resource by resource name - I'm getting an error.

variable "fmg_image_name" {
  description = "FortiManager image id"
  default     = "fmg"
}
variable "folder_id" {
  description = "infra folder id"
  default     = "b1gpv67mve1f5snnl7ut"
}

data "yandex_compute_image" "fmg" {
  folder_id = var.folder_id
  name = var.fmg_image_name
}
data "yandex_vpc_subnet" "fmg" {
  folder_id = var.folder_id
  name = "secure-network-subnet-2"
}
output "image_id" {
  value = data.yandex_compute_image.fmg.id
}
output "subnet_id" {
  value = data.yandex_vpc_subnet.fmg.id
}

output: 
Error: failed to resolve data source image by name: image with name "fmg" not found

  on main.tf line 507, in data "yandex_compute_image" "fmg":
 507: data "yandex_compute_image" "fmg" {

Error: failed to resolve data source subnet by name: subnet with name "secure-network-subnet-2" not found

  on main.tf line 511, in data "yandex_vpc_subnet" "fmg":
 511: data "yandex_vpc_subnet" "fmg" {

But all these resources are exist

yc compute image list --folder-name=infra
+----------------------+------+--------+-------------+--------+
|          ID          | NAME | FAMILY | PRODUCT IDS | STATUS |
+----------------------+------+--------+-------------+--------+
| fd8nn6em7rfj5arksfsa | fmg  |        |             | READY  |
+----------------------+------+--------+-------------+--------+

yc vpc subnet list --folder-name=infra
+----------------------+-------------------------+----------------------+----------------+---------------+-----------------+
|          ID          |          NAME           |      NETWORK ID      | ROUTE TABLE ID |     ZONE      |      RANGE      |
+----------------------+-------------------------+----------------------+----------------+---------------+-----------------+
| b0clt3qn0ellalh7n0l0 | secure-network-subnet-2 | enp0suumfsb1bgn56cif |                | ru-central1-c | [10.100.2.0/24] |
| e2li2lh70ociip68clll | secure-network-subnet-1 | enp0suumfsb1bgn56cif |                | ru-central1-b | [10.100.1.0/24] |
| e9b5g56kr4et5b10hhu9 | secure-network-subnet-0 | enp0suumfsb1bgn56cif |                | ru-central1-a | [10.100.0.0/24] |
+----------------------+-------------------------+----------------------+----------------+---------------+-----------------+
GennadySpb commented 4 years ago

Hi @asterix201 !

I try reproduce and could not get result as you describe.

$ cat test.tf
// vars definitions
variable "fmg_image_name" {
  description = "FortiManager image id"
  default     = "fedora-31-1582631432"
}
variable "folder_id" {
  description = "infra folder id"
  default     = "standard-images"
}

// data sources
data "yandex_compute_image" "fmg" {
  folder_id = var.folder_id
  name      = var.fmg_image_name
}

// outputs
output "image_id" {
  value = data.yandex_compute_image.fmg.id
}

$ YC_TOKEN=$YC_TOKEN terraform apply
data.yandex_compute_image.fmg: Refreshing state...

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

image_id = fd8t9okqs036vlfi4ivi

If I assign intensionaly incorrect folder_id value then get error as you show

$ YC_TOKEN=$YC_TOKEN terraform apply -var folder_id=fsdsdfsdfdsdsfsd
data.yandex_compute_image.fmg: Refreshing state...

Error: failed to resolve data source image by name: image with name "fedora-31-1582631432" not found

  on test.tf line 12, in data "yandex_compute_image" "fmg":
  12: data "yandex_compute_image" "fmg" {

May be in some place inside tf code default value of variable folder_id redefined or in wrapper script / command-line arg?

asterix201 commented 4 years ago

May be in some place inside tf code default value of variable folder_id redefined or in wrapper script / command-line arg?

Reviewed code more closely. Yes, you are right. There was setting another folder_id in tfvars file and I've missed this. After update to the correct value, it works. Thank you a lot for the help! :)