Skip to content

Commit

Permalink
feat: add credential to item datasource
Browse files Browse the repository at this point in the history
Issue GH-52
  • Loading branch information
SMillerDev committed Jun 11, 2024
1 parent 9f487d5 commit ee5e921
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 40 deletions.
1 change: 1 addition & 0 deletions docs/data-sources/item.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ data "onepassword_item" "example" {
### Read-Only

- `category` (String) The category of the item. One of ["login" "password" "database" "secure_note"]
- `credential` (String, Sensitive) API credential for this item.
- `database` (String) (Only applies to the database category) The name of the database.
- `hostname` (String) (Only applies to the database category) The address where the database can be found
- `id` (String) The Terraform resource identifier for this item in the format `vaults/<vault_id>/items/<item_id>`.
Expand Down
1 change: 1 addition & 0 deletions docs/resources/item.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ resource "onepassword_item" "demo_db" {

### Read-Only

- `credential` (String, Sensitive) API credential for this item.
- `id` (String) The Terraform resource identifier for this item in the format `vaults/<vault_id>/items/<item_id>`.
- `uuid` (String) The UUID of the item. Item identifiers are unique within a specific vault.

Expand Down
19 changes: 10 additions & 9 deletions internal/provider/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ import (
const (
terraformItemIDDescription = "The Terraform resource identifier for this item in the format `vaults/<vault_id>/items/<item_id>`."

itemUUIDDescription = "The UUID of the item. Item identifiers are unique within a specific vault."
vaultUUIDDescription = "The UUID of the vault the item is in."
categoryDescription = "The category of the item."
itemTitleDescription = "The title of the item."
urlDescription = "The primary URL for the item."
tagsDescription = "An array of strings of the tags assigned to the item."
usernameDescription = "Username for this item."
passwordDescription = "Password for this item."
noteValueDescription = "Secure Note value."
itemUUIDDescription = "The UUID of the item. Item identifiers are unique within a specific vault."
vaultUUIDDescription = "The UUID of the vault the item is in."
categoryDescription = "The category of the item."
itemTitleDescription = "The title of the item."
urlDescription = "The primary URL for the item."
tagsDescription = "An array of strings of the tags assigned to the item."
usernameDescription = "Username for this item."
passwordDescription = "Password for this item."
credentialDescription = "API credential for this item."
noteValueDescription = "Secure Note value."

dbHostnameDescription = "(Only applies to the database category) The address where the database can be found"
dbDatabaseDescription = "(Only applies to the database category) The name of the database."
Expand Down
40 changes: 25 additions & 15 deletions internal/provider/onepassword_item_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,22 @@ type OnePasswordItemDataSource struct {

// OnePasswordItemDataSourceModel describes the data source data model.
type OnePasswordItemDataSourceModel struct {
ID types.String `tfsdk:"id"`
Vault types.String `tfsdk:"vault"`
UUID types.String `tfsdk:"uuid"`
Title types.String `tfsdk:"title"`
Category types.String `tfsdk:"category"`
URL types.String `tfsdk:"url"`
Hostname types.String `tfsdk:"hostname"`
Database types.String `tfsdk:"database"`
Port types.String `tfsdk:"port"`
Type types.String `tfsdk:"type"`
Tags types.List `tfsdk:"tags"`
Username types.String `tfsdk:"username"`
Password types.String `tfsdk:"password"`
NoteValue types.String `tfsdk:"note_value"`
Section []OnePasswordItemSectionModel `tfsdk:"section"`
ID types.String `tfsdk:"id"`
Vault types.String `tfsdk:"vault"`
UUID types.String `tfsdk:"uuid"`
Title types.String `tfsdk:"title"`
Category types.String `tfsdk:"category"`
URL types.String `tfsdk:"url"`
Hostname types.String `tfsdk:"hostname"`
Database types.String `tfsdk:"database"`
Port types.String `tfsdk:"port"`
Type types.String `tfsdk:"type"`
Tags types.List `tfsdk:"tags"`
Username types.String `tfsdk:"username"`
Password types.String `tfsdk:"password"`
NoteValue types.String `tfsdk:"note_value"`
Credential types.String `tfsdk:"credential"`
Section []OnePasswordItemSectionModel `tfsdk:"section"`
}

type OnePasswordItemSectionModel struct {
Expand Down Expand Up @@ -135,6 +136,11 @@ func (d *OnePasswordItemDataSource) Schema(ctx context.Context, req datasource.S
Computed: true,
Sensitive: true,
},
"credential": schema.StringAttribute{
MarkdownDescription: credentialDescription,
Computed: true,
Sensitive: true,
},
"note_value": schema.StringAttribute{
MarkdownDescription: noteValueDescription,
Computed: true,
Expand Down Expand Up @@ -295,6 +301,10 @@ func (d *OnePasswordItemDataSource) Read(ctx context.Context, req datasource.Rea
data.Type = types.StringValue(f.Value)
}
}

if f.ID == "credential" && item.Category == "API_CREDENTIAL" {
data.Credential = types.StringValue(f.Value)
}
}
}

Expand Down
42 changes: 26 additions & 16 deletions internal/provider/onepassword_item_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,23 @@ type OnePasswordItemResource struct {

// OnePasswordItemResourceModel describes the resource data model.
type OnePasswordItemResourceModel struct {
ID types.String `tfsdk:"id"`
UUID types.String `tfsdk:"uuid"`
Vault types.String `tfsdk:"vault"`
Category types.String `tfsdk:"category"`
Title types.String `tfsdk:"title"`
URL types.String `tfsdk:"url"`
Hostname types.String `tfsdk:"hostname"`
Database types.String `tfsdk:"database"`
Port types.String `tfsdk:"port"`
Type types.String `tfsdk:"type"`
Tags types.List `tfsdk:"tags"`
Username types.String `tfsdk:"username"`
Password types.String `tfsdk:"password"`
NoteValue types.String `tfsdk:"note_value"`
Section []OnePasswordItemResourceSectionModel `tfsdk:"section"`
Recipe []PasswordRecipeModel `tfsdk:"password_recipe"`
ID types.String `tfsdk:"id"`
UUID types.String `tfsdk:"uuid"`
Vault types.String `tfsdk:"vault"`
Category types.String `tfsdk:"category"`
Title types.String `tfsdk:"title"`
URL types.String `tfsdk:"url"`
Hostname types.String `tfsdk:"hostname"`
Database types.String `tfsdk:"database"`
Port types.String `tfsdk:"port"`
Type types.String `tfsdk:"type"`
Tags types.List `tfsdk:"tags"`
Username types.String `tfsdk:"username"`
Password types.String `tfsdk:"password"`
NoteValue types.String `tfsdk:"note_value"`
Credential types.String `tfsdk:"credential"`
Section []OnePasswordItemResourceSectionModel `tfsdk:"section"`
Recipe []PasswordRecipeModel `tfsdk:"password_recipe"`
}

type PasswordRecipeModel struct {
Expand Down Expand Up @@ -217,6 +218,11 @@ func (r *OnePasswordItemResource) Schema(ctx context.Context, req resource.Schem
ValueModifier(),
},
},
"credential": schema.StringAttribute{
MarkdownDescription: credentialDescription,
Computed: true,
Sensitive: true,
},
"note_value": schema.StringAttribute{
MarkdownDescription: noteValueDescription,
Optional: true,
Expand Down Expand Up @@ -600,6 +606,10 @@ func itemToData(ctx context.Context, item *op.Item, data *OnePasswordItemResourc
data.Type = setStringValue(f.Value)
}
}

if f.ID == "credential" && item.Category == "API_CREDENTIAL" {
data.Credential = types.StringValue(f.Value)
}
}
}

Expand Down

0 comments on commit ee5e921

Please sign in to comment.