-
Notifications
You must be signed in to change notification settings - Fork 48
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
Allow Section Fields LookUp by Title #117
Comments
I started playing with the provider last night and wanted behaviour like this. I've created a simple wrapper in my project to re-shape the object that we get from the Some caveats:
If there are duplicates, Terraform will fail due to a non-unique key.
variable "title" {
type = string
}
variable "vault_uuid" {
type = string
}
terraform {
required_version = ">= 1.0.0"
required_providers {
onepassword = {
source = "1Password/onepassword"
}
}
}
data "onepassword_item" "main" {
vault = var.vault_uuid
title = var.title
}
locals {
fields = { for f in data.onepassword_item.main.section[0].field : f.label => {
id = f.id
purpose = f.purpose
type = f.type
value = f.value
}
}
sections = { for s in data.onepassword_item.main.section : s.label => {
id = s.id
fields = { for f in s.field : f.label => {
id = f.id
purpose = f.purpose
type = f.type
value = f.value
} }
} if s.label != "" }
}
output "category" {
value = data.onepassword_item.main.category
}
output "database" {
value = data.onepassword_item.main.database
}
output "fields" {
value = local.fields
}
output "hostname" {
value = data.onepassword_item.main.hostname
}
output "id" {
value = data.onepassword_item.main.id
}
output "note_value" {
value = data.onepassword_item.main.note_value
sensitive = true
}
output "password" {
value = data.onepassword_item.main.password
sensitive = true
}
output "port" {
value = data.onepassword_item.main.port
}
output "sections" {
value = local.sections
}
output "tags" {
value = data.onepassword_item.main.tags
}
output "title" {
value = data.onepassword_item.main.title
}
output "type" {
value = data.onepassword_item.main.type
}
output "url" {
value = data.onepassword_item.main.url
}
output "username" {
value = data.onepassword_item.main.username
}
output "uuid" {
value = data.onepassword_item.main.uuid
}
output "vault" {
value = data.onepassword_item.main.vault
} Consume the module like this: data "onepassword_vault" "vault" {
name = "My Vault"
}
module "test_item" {
source = "./module"
vault_uuid = data.onepassword_vault.vault.uuid
title = "Test"
}
output "value_from_text_field_in_section" {
value = module.test_item.sections["Section One"].fields["text"].value
} |
The module is way prettier than what I did. Given a onepassword_item named determined_ci_github (hence the
So I find the section with the desired label, then in that section I find the field with the given ID (since it has a name with weird chars). Then afterwards, I can reference This approach becomes a real mess when you want multiple fields from one item. The module is likely much better for that. |
A bit simpler workaround, but still, having to boilerplate this all around the code is not clean. (using separate vault id verification here) data onepassword_vault ops {
name = var.ops_vault
}
data onepassword_item service {
vault = split("/", data.onepassword_vault.ops.id)[1]
title = "service"
}
locals {
service = {for k1, v1 in data.onepassword_item.service.section : v1.label => {for k2, v2 in v1.field : v2.label => v2.value}}
} |
Thank you @kitforbes!!! Great solution. |
Summary
Allow Section Fields LookUp by Title when using
data "onepassword_item"
resource.Use cases
As a programmer, I would like to have a way to lookup a Section Field by Title.
Given the following secret:
I would like to be able to use the title (in this case
DO_ACCESS_TOKEN
) of the field.Proposed solution
Introduce
fields
(plural), or break change (🤷🏻) in order to look up the fields by title.Otherwise, document how I suppose to do this because my skills are limited and I can not find a simple way to do it.
I am not sure what I am doing wrong here.
Is there a workaround to accomplish this today?
I think that, you loop thru the fields, match in the field name, get the value back into a
local
and then use it! 😭References & Prior Work
The text was updated successfully, but these errors were encountered: