paultyng/terraform-provider-unifi

api.err.InvalidNetworkId while creating user resource

Tommi2Day opened this issue · 5 comments

creating a user resource from mac address results in a missing/wrong network_id
I am using UDMPRO 1.10.0 with controler 6.2.26 and unifi provider 0.29.0

resource "unifi_user" "flatcar" {
  mac  = lower(proxmox_vm_qemu.flatcar.network[0].macaddr)
  name = var.flatcar.name
  note = "flatcar Test"
  fixed_ip   = var.flatcar.ip
}
-----------------------------------------------------: timestamp=2021-08-30T13:20:09.117+0200
2021-08-30T13:20:09.119+0200 [INFO]  provider.terraform-provider-unifi_v0.29.0: 2021/08/30 13:20:09 [DEBUG] Unifi API Request Details:
---[ REQUEST ]---------------------------------------
PUT /proxy/network/api/s/default/rest/user/612cac275e1bf36442c4e0a2 HTTP/1.1
.....
{
 "_id": "612cac275e1bf36442c4e0a2",
 "site_id": "575f0cf9a8ec0c8dc0b255c9",
 "fixed_ip": "192.168.170.20",
 "mac": "f6:b0:ea:1f:13:41",
 "name": "flatcar-test",
 "network_id": "",
 "note": "flatcar Test",
 "use_fixedip": true,
 "usergroup_id": ""
}
-----------------------------------------------------: timestamp=2021-08-30T13:20:09.119+0200
2021-08-30T13:20:09.202+0200 [INFO]  provider.terraform-provider-unifi_v0.29.0: 2021/08/30 13:20:09 [DEBUG] Unifi API Response Details:
---[ RESPONSE ]--------------------------------------
.....
{
 "meta": {
  "rc": "error",
  "network_id": "",
  "msg": "api.err.InvalidNetworkId"
 },
 "data": []
}
-----------------------------------------------------: timestamp=2021-08-30T13:20:09.202+0200

│ Error: api.err.InvalidNetworkId (400 Bad Request) for PUT https://unifi/proxy/network/api/s/default/rest/user/612cac275e1bf36442c4e0a2
│
│   with unifi_user.flatcar,
│   on flatcar.tf line 48, in resource "unifi_user" "flatcar":
│   48: resource "unifi_user" "flatcar" {
│
╵

Maybe its better to rely on the network name instead of network_id or provide a network data source to get the proper id from name

I should create a datasource that lets you look up the ID via name for a network.

For this though, the validation isn't very helpful (which is also something to fix), but I believe if you specify fixed_ip you must specify a network ID as well.

I think something like this may be ideal? What are your thoughts?

data "unifi_network" "network" {
  name = "my network"
}

resource "unifi_user" "flatcar" {
  mac  = lower(proxmox_vm_qemu.flatcar.network[0].macaddr)
  name = var.flatcar.name
  note = "flatcar Test"
  fixed_ip   = var.flatcar.ip
  network_id = data.unifi_network.network.id
}

yes, looks good.

In the meantime I had a similar idea and started to try to contribute this myself along with a unifi_user data source with more or less only copy/paste schema and functions from related resource code, but you know better how to integrate

from the network perspective it would be nice to lookup alternative on both, name or id to retrieve network definitions like vlan etc, which can be usefull in vm definitions

THX

@Tommi2Day the unifi_network data source should go out in v0.30.0, I should write up issues for other data sources as well.

Or at least write an issue for other querying fields.

THX, with 0.30.1 the user resource with fixed_ip has been created.