Skip to content

Commit

Permalink
add support for data folder
Browse files Browse the repository at this point in the history
  • Loading branch information
maxlaverse committed Apr 20, 2024
1 parent aa13e7c commit 8e6e5a3
Show file tree
Hide file tree
Showing 12 changed files with 217 additions and 87 deletions.
27 changes: 27 additions & 0 deletions docs/data-sources/folder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "bitwarden_folder Data Source - terraform-provider-bitwarden"
subcategory: ""
description: |-
Use this data source to get information on an existing Folder.
---

# bitwarden_folder (Data Source)

Use this data source to get information on an existing Folder.



<!-- schema generated by tfplugindocs -->
## Schema

### Optional

- `filter_collection_id` (String) Filter search results by collection ID
- `filter_organization_id` (String) Filter search results by organization ID
- `id` (String) Identifier.
- `search` (String) Search items matching the search string. Can be combined with filters to narrow down the search.

### Read-Only

- `name` (String) Name.
5 changes: 4 additions & 1 deletion docs/resources/folder.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ resource "bitwarden_folder" "cloud_credentials" {

- `name` (String) Name.

### Read-Only
### Optional

- `id` (String) Identifier.

### Read-Only


## Import

Import is supported using the following syntax:
Expand Down
13 changes: 12 additions & 1 deletion internal/provider/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/maxlaverse/terraform-provider-bitwarden/internal/bitwarden/bw"
)

func readDataSource(attrObject bw.ObjectType, attrType bw.ItemType) schema.ReadContextFunc {
func readDataSourceItem(attrObject bw.ObjectType, attrType bw.ItemType) schema.ReadContextFunc {
return func(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
d.SetId(d.Get(attributeID).(string))
err := d.Set(attributeObject, attrObject)
Expand All @@ -22,3 +22,14 @@ func readDataSource(attrObject bw.ObjectType, attrType bw.ItemType) schema.ReadC
return objectRead(ctx, d, meta)
}
}

func readDataSourceFolder() schema.ReadContextFunc {
return func(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
d.SetId(d.Get(attributeID).(string))
err := d.Set(attributeObject, bw.ObjectTypeFolder)
if err != nil {
return diag.FromErr(err)
}
return objectRead(ctx, d, meta)
}
}
13 changes: 13 additions & 0 deletions internal/provider/data_source_folder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package provider

import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceFolder() *schema.Resource {
return &schema.Resource{
Description: "Use this data source to get information on an existing Folder.",
ReadContext: readDataSourceFolder(),
Schema: folderSchema(DataSource),
}
}
36 changes: 36 additions & 0 deletions internal/provider/data_source_folder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package provider

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccDataSourceFolderAttributes(t *testing.T) {
ensureVaultwardenConfigured(t)

resourceName := "bitwarden_folder.foo"

resource.UnitTest(t, resource.TestCase{
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: tfConfigProvider() + tfConfigResourceFolder(),
},
{
Config: tfConfigProvider() + tfConfigResourceFolder() + tfConfigDataFolder(),
Check: checkObject(resourceName),
},
},
})
}

func tfConfigDataFolder() string {
return `
data "bitwarden_folder" "foo_data" {
provider = bitwarden
search = "folder-bar"
}
`
}
2 changes: 1 addition & 1 deletion internal/provider/data_source_item_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func dataSourceItemLogin() *schema.Resource {

return &schema.Resource{
Description: "Use this data source to get information on an existing Login.",
ReadContext: readDataSource(bw.ObjectTypeItem, bw.ItemTypeLogin),
ReadContext: readDataSourceItem(bw.ObjectTypeItem, bw.ItemTypeLogin),
Schema: dataSourceItemLoginSchema,
}
}
2 changes: 1 addition & 1 deletion internal/provider/data_source_item_secure_note.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func dataSourceItemSecureNote() *schema.Resource {

return &schema.Resource{
Description: "Use this data source to get information on an existing Secure Note.",
ReadContext: readDataSource(bw.ObjectTypeItem, bw.ItemTypeSecureNote),
ReadContext: readDataSourceItem(bw.ObjectTypeItem, bw.ItemTypeSecureNote),
Schema: dataSourceItemSecureNoteSchema,
}
}
3 changes: 2 additions & 1 deletion internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,13 @@ func New(version string) func() *schema.Provider {
},
DataSourcesMap: map[string]*schema.Resource{
"bitwarden_attachment": dataSourceAttachment(),
"bitwarden_folder": dataSourceFolder(),
"bitwarden_item_login": dataSourceItemLogin(),
"bitwarden_item_secure_note": dataSourceItemSecureNote(),
},
ResourcesMap: map[string]*schema.Resource{
"bitwarden_folder": resourceFolder(),
"bitwarden_attachment": resourceAttachment(),
"bitwarden_folder": resourceFolder(),
"bitwarden_item_login": resourceItemLogin(),
"bitwarden_item_secure_note": resourceItemSecureNote(),
},
Expand Down
18 changes: 1 addition & 17 deletions internal/provider/resource_folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,7 @@ func resourceFolder() *schema.Resource {
DeleteContext: objectDelete,
Importer: importFolderResource(),

Schema: map[string]*schema.Schema{
attributeID: {
Description: descriptionIdentifier,
Type: schema.TypeString,
Computed: true,
},
attributeName: {
Description: descriptionName,
Type: schema.TypeString,
Required: true,
},
attributeObject: {
Description: descriptionInternal,
Type: schema.TypeString,
Computed: true,
},
},
Schema: folderSchema(Resource),
}
}

Expand Down
65 changes: 0 additions & 65 deletions internal/provider/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package provider

import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

type schemaTypeEnum int
Expand All @@ -12,49 +11,6 @@ const (
Resource schemaTypeEnum = 1
)

func loginSchema(schemaType schemaTypeEnum) map[string]*schema.Schema {
base := map[string]*schema.Schema{
attributeLoginPassword: {
Description: descriptionLoginPassword,
Type: schema.TypeString,
Computed: schemaType == DataSource,
Optional: schemaType == Resource,
Sensitive: true,
},
attributeLoginUsername: {
Description: descriptionLoginUsername,
Type: schema.TypeString,
Computed: schemaType == DataSource,
Optional: schemaType == Resource,
Sensitive: true,
},
attributeLoginTotp: {
Description: descriptionLoginTotp,
Type: schema.TypeString,
Computed: schemaType == DataSource,
Optional: schemaType == Resource,
Sensitive: true,
},
attributeLoginURIs: {
Description: descriptionLoginUri,
Type: schema.TypeList,
Elem: uriElem(),
Computed: schemaType == DataSource,
Optional: schemaType == Resource,
Sensitive: false,
},
}

if schemaType == DataSource {
base[attributeFilterURL] = &schema.Schema{
Description: descriptionFilterURL,
Type: schema.TypeString,
Optional: true,
}
}
return base
}

func baseSchema(schemaType schemaTypeEnum) map[string]*schema.Schema {

base := map[string]*schema.Schema{
Expand Down Expand Up @@ -220,27 +176,6 @@ func baseSchema(schemaType schemaTypeEnum) map[string]*schema.Schema {
return base
}

func uriElem() *schema.Resource {
validMatchStr := []string{"default", "base_domain", "host", "start_with", "exact", "regexp", "never"}

return &schema.Resource{
Schema: map[string]*schema.Schema{
attributeLoginURIsMatch: {
Description: descriptionLoginUriMatch,
Type: schema.TypeString,
Default: validMatchStr[0],
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice(validMatchStr, false)),
Optional: true,
},
attributeLoginURIsValue: {
Description: descriptionLoginUriValue,
Type: schema.TypeString,
Required: true,
},
},
}
}

func attachmentSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
attributeID: {
Expand Down
50 changes: 50 additions & 0 deletions internal/provider/schema_folder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package provider

import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func folderSchema(schemaType schemaTypeEnum) map[string]*schema.Schema {
base := map[string]*schema.Schema{
attributeID: {
Description: descriptionIdentifier,
Type: schema.TypeString,
Computed: schemaType == Resource,
Optional: true,
},
attributeName: {
Description: descriptionName,
Type: schema.TypeString,
Computed: schemaType == DataSource,
Required: schemaType == Resource,
},
attributeObject: {
Description: descriptionInternal,
Type: schema.TypeString,
Computed: true,
},
}

if schemaType == DataSource {
base[attributeFilterCollectionId] = &schema.Schema{
Description: descriptionFilterCollectionID,
Type: schema.TypeString,
Optional: true,
}

base[attributeFilterOrganizationID] = &schema.Schema{
Description: descriptionFilterOrganizationID,
Type: schema.TypeString,
Optional: true,
}

base[attributeFilterSearch] = &schema.Schema{
Description: descriptionFilterSearch,
Type: schema.TypeString,
Optional: true,
AtLeastOneOf: []string{attributeFilterSearch, attributeID},
}
}

return base
}
70 changes: 70 additions & 0 deletions internal/provider/schema_login.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package provider

import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func loginSchema(schemaType schemaTypeEnum) map[string]*schema.Schema {
base := map[string]*schema.Schema{
attributeLoginPassword: {
Description: descriptionLoginPassword,
Type: schema.TypeString,
Computed: schemaType == DataSource,
Optional: schemaType == Resource,
Sensitive: true,
},
attributeLoginUsername: {
Description: descriptionLoginUsername,
Type: schema.TypeString,
Computed: schemaType == DataSource,
Optional: schemaType == Resource,
Sensitive: true,
},
attributeLoginTotp: {
Description: descriptionLoginTotp,
Type: schema.TypeString,
Computed: schemaType == DataSource,
Optional: schemaType == Resource,
Sensitive: true,
},
attributeLoginURIs: {
Description: descriptionLoginUri,
Type: schema.TypeList,
Elem: uriElem(),
Computed: schemaType == DataSource,
Optional: schemaType == Resource,
Sensitive: false,
},
}

if schemaType == DataSource {
base[attributeFilterURL] = &schema.Schema{
Description: descriptionFilterURL,
Type: schema.TypeString,
Optional: true,
}
}
return base
}

func uriElem() *schema.Resource {
validMatchStr := []string{"default", "base_domain", "host", "start_with", "exact", "regexp", "never"}

return &schema.Resource{
Schema: map[string]*schema.Schema{
attributeLoginURIsMatch: {
Description: descriptionLoginUriMatch,
Type: schema.TypeString,
Default: validMatchStr[0],
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice(validMatchStr, false)),
Optional: true,
},
attributeLoginURIsValue: {
Description: descriptionLoginUriValue,
Type: schema.TypeString,
Required: true,
},
},
}
}

0 comments on commit 8e6e5a3

Please sign in to comment.