Skip to content

Commit

Permalink
regroup operations and transformations
Browse files Browse the repository at this point in the history
  • Loading branch information
maxlaverse committed Dec 17, 2024
1 parent 25838d3 commit 5276986
Show file tree
Hide file tree
Showing 29 changed files with 586 additions and 503 deletions.
42 changes: 0 additions & 42 deletions internal/provider/data_source.go

This file was deleted.

2 changes: 1 addition & 1 deletion internal/provider/data_source_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
func dataSourceAttachment() *schema.Resource {
return &schema.Resource{
Description: "Use this data source to get the content on an existing item's attachment.",
ReadContext: withPasswordManager(resourceReadDataSourceAttachment),
ReadContext: withPasswordManager(opAttachmentRead),
Schema: map[string]*schema.Schema{
schema_definition.AttributeID: {
Description: schema_definition.DescriptionIdentifier,
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/data_source_folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
func dataSourceFolder() *schema.Resource {
return &schema.Resource{
Description: "Use this data source to get information on an existing folder.",
ReadContext: withPasswordManager(resourceReadDataSourceObject(models.ObjectTypeFolder)),
ReadContext: withPasswordManager(opObjectRead(models.ObjectTypeFolder)),
Schema: schema_definition.FolderSchema(schema_definition.DataSource),
}
}
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 @@ -14,7 +14,7 @@ func dataSourceItemLogin() *schema.Resource {

return &schema.Resource{
Description: "Use this data source to get information on an existing login item.",
ReadContext: withPasswordManager(resourceReadDataSourceItem(models.ObjectTypeItem, models.ItemTypeLogin)),
ReadContext: withPasswordManager(opItemRead(models.ObjectTypeItem, models.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 @@ -11,7 +11,7 @@ func dataSourceItemSecureNote() *schema.Resource {

return &schema.Resource{
Description: "Use this data source to get information on an existing secure note item.",
ReadContext: withPasswordManager(resourceReadDataSourceItem(models.ObjectTypeItem, models.ItemTypeSecureNote)),
ReadContext: withPasswordManager(opItemRead(models.ObjectTypeItem, models.ItemTypeSecureNote)),
Schema: dataSourceItemSecureNoteSchema,
}
}
2 changes: 1 addition & 1 deletion internal/provider/data_source_org_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
func dataSourceOrgCollection() *schema.Resource {
return &schema.Resource{
Description: "Use this data source to get information on an existing organization collection.",
ReadContext: withPasswordManager(resourceReadDataSourceObject(models.ObjectTypeOrgCollection)),
ReadContext: withPasswordManager(opObjectRead(models.ObjectTypeOrgCollection)),
Schema: schema_definition.OrgCollectionSchema(schema_definition.DataSource),
}
}
2 changes: 1 addition & 1 deletion internal/provider/data_source_organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
func dataSourceOrganization() *schema.Resource {
return &schema.Resource{
Description: "Use this data source to get information on an existing organization.",
ReadContext: withPasswordManager(resourceReadDataSourceObject(models.ObjectTypeOrganization)),
ReadContext: withPasswordManager(opObjectRead(models.ObjectTypeOrganization)),
Schema: schema_definition.OrganizationSchema(),
}
}
2 changes: 1 addition & 1 deletion internal/provider/data_source_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func dataSourceProject() *schema.Resource {

return &schema.Resource{
Description: "Use this data source to get information on an existing project.",
ReadContext: withSecretsManager(resourceReadDataSourceProject),
ReadContext: withSecretsManager(opProjectRead),
Schema: dataSourceProjectSchema,
}
}
2 changes: 1 addition & 1 deletion internal/provider/data_source_secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func dataSourceSecret() *schema.Resource {

return &schema.Resource{
Description: "Use this data source to get information on an existing secret.",
ReadContext: withSecretsManager(resourceReadDataSourceSecret),
ReadContext: withSecretsManager(opSecretRead),
Schema: dataSourceSecretSchema,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@ import (
"crypto/sha1"
"encoding/hex"
"errors"
"fmt"
"io"
"os"
"strings"

"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/maxlaverse/terraform-provider-bitwarden/internal/bitwarden"
"github.com/maxlaverse/terraform-provider-bitwarden/internal/bitwarden/models"
"github.com/maxlaverse/terraform-provider-bitwarden/internal/schema_definition"
)

func resourceCreateAttachment(ctx context.Context, d *schema.ResourceData, bwClient bitwarden.PasswordManager) diag.Diagnostics {
func opAttachmentCreate(ctx context.Context, d *schema.ResourceData, bwClient bitwarden.PasswordManager) diag.Diagnostics {
itemId := d.Get(schema_definition.AttributeAttachmentItemID).(string)

existingAttachments, err := listExistingAttachments(ctx, bwClient, itemId)
Expand Down Expand Up @@ -50,74 +53,59 @@ func resourceCreateAttachment(ctx context.Context, d *schema.ResourceData, bwCli
return diag.FromErr(attachmentDataFromStruct(d, attachmentsAdded[0]))
}

func resourceReadAttachment(ctx context.Context, d *schema.ResourceData, bwClient bitwarden.PasswordManager) diag.Diagnostics {
func opAttachmentDelete(ctx context.Context, d *schema.ResourceData, bwClient bitwarden.PasswordManager) diag.Diagnostics {
itemId := d.Get(schema_definition.AttributeAttachmentItemID).(string)

obj, err := bwClient.GetObject(ctx, models.Object{ID: itemId, Object: models.ObjectTypeItem})
if err != nil {
// If the item is not found, we can't simply consider the attachment as
// deleted, because we won't have an item to attach it to.
// This means we don't need a special handling for NotFound errors and
// should just return whatever we get.
return diag.FromErr(err)
}
return diag.FromErr(bwClient.DeleteAttachment(ctx, itemId, d.Id()))
}

for _, attachment := range obj.Attachments {
if attachment.ID == d.Id() {
return diag.FromErr(attachmentDataFromStruct(d, attachment))
}
func opAttachmentImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
split := strings.Split(d.Id(), "/")
if len(split) != 2 {
return nil, fmt.Errorf("invalid ID specified, should be in the format <item_id>/<attachment_id>: '%s'", d.Id())
}

// If the item exists but the attachment is not found, we consider the
// attachment as deleted.
d.SetId("")
return diag.Diagnostics{}
d.SetId(split[0])
d.Set(schema_definition.AttributeAttachmentItemID, split[1])
return []*schema.ResourceData{d}, nil
}

func resourceDeleteAttachment(ctx context.Context, d *schema.ResourceData, bwClient bitwarden.PasswordManager) diag.Diagnostics {
func opAttachmentRead(ctx context.Context, d *schema.ResourceData, bwClient bitwarden.PasswordManager) diag.Diagnostics {
itemId := d.Get(schema_definition.AttributeAttachmentItemID).(string)

return diag.FromErr(bwClient.DeleteAttachment(ctx, itemId, d.Id()))
}

func attachmentDataFromStruct(d *schema.ResourceData, attachment models.Attachment) error {
d.SetId(attachment.ID)

err := d.Set(schema_definition.AttributeAttachmentFileName, attachment.FileName)
if err != nil {
return err
}
attachmentId := d.Get(schema_definition.AttributeID).(string)

err = d.Set(schema_definition.AttributeAttachmentSize, attachment.Size)
if err != nil {
return err
}
err = d.Set(schema_definition.AttributeAttachmentSizeName, attachment.SizeName)
content, err := bwClient.GetAttachment(ctx, itemId, attachmentId)
if err != nil {
return err
return diag.FromErr(err)
}

err = d.Set(schema_definition.AttributeAttachmentURL, attachment.Url)
if err != nil {
return err
}
d.SetId(attachmentId)

return nil
return diag.FromErr(d.Set(schema_definition.AttributeAttachmentContent, string(content)))
}

func resourceReadDataSourceAttachment(ctx context.Context, d *schema.ResourceData, bwClient bitwarden.PasswordManager) diag.Diagnostics {
func opAttachmentReadIgnoreMissing(ctx context.Context, d *schema.ResourceData, bwClient bitwarden.PasswordManager) diag.Diagnostics {
itemId := d.Get(schema_definition.AttributeAttachmentItemID).(string)

attachmentId := d.Get(schema_definition.AttributeID).(string)

content, err := bwClient.GetAttachment(ctx, itemId, attachmentId)
obj, err := bwClient.GetObject(ctx, models.Object{ID: itemId, Object: models.ObjectTypeItem})
if err != nil {
// If the item is not found, we can't simply consider the attachment as
// deleted, because we won't have an item to attach it to.
// This means we don't need a special handling for NotFound errors and
// should just return whatever we get.
return diag.FromErr(err)
}

d.SetId(attachmentId)
for _, attachment := range obj.Attachments {
if attachment.ID == d.Id() {
return diag.FromErr(attachmentDataFromStruct(d, attachment))
}
}

return diag.FromErr(d.Set(schema_definition.AttributeAttachmentContent, string(content)))
// If the item exists but the attachment is not found, we consider the
// attachment as deleted.
d.SetId("")
return diag.Diagnostics{}
}

func listExistingAttachments(ctx context.Context, client bitwarden.PasswordManager, itemId string) ([]models.Attachment, error) {
Expand Down Expand Up @@ -176,3 +164,21 @@ func contentSha1Sum(content string) (string, error) {

return hex.EncodeToString(outputChecksum[:]), nil
}

func contentHash(val interface{}) string {
hash, _ := contentSha1Sum(val.(string))
return hash
}

func fileHashComputable(val interface{}, _ cty.Path) diag.Diagnostics {
_, err := fileSha1Sum(val.(string))
if err != nil {
return diag.FromErr(fmt.Errorf("unable to compute hash of file: %w", err))
}
return diag.Diagnostics{}
}

func fileHash(val interface{}) string {
hash, _ := fileSha1Sum(val.(string))
return hash
}
29 changes: 29 additions & 0 deletions internal/provider/operation_folder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package provider

import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/maxlaverse/terraform-provider-bitwarden/internal/bitwarden"
"github.com/maxlaverse/terraform-provider-bitwarden/internal/bitwarden/models"
"github.com/maxlaverse/terraform-provider-bitwarden/internal/schema_definition"
)

func opFolderCreate(ctx context.Context, d *schema.ResourceData, bwClient bitwarden.PasswordManager) diag.Diagnostics {
err := d.Set(schema_definition.AttributeObject, models.ObjectTypeFolder)
if err != nil {
return diag.FromErr(err)
}

return objectCreate(ctx, d, bwClient)
}

func opFolderImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
d.SetId(d.Id())
err := d.Set(schema_definition.AttributeObject, models.ObjectTypeFolder)
if err != nil {
return nil, err
}
return []*schema.ResourceData{d}, nil
}
9 changes: 9 additions & 0 deletions internal/provider/operation_generic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package provider

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

func resourceImporter(stateContext schema.StateContextFunc) *schema.ResourceImporter {
return &schema.ResourceImporter{
StateContext: stateContext,
}
}
21 changes: 21 additions & 0 deletions internal/provider/operation_item.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package provider

import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/maxlaverse/terraform-provider-bitwarden/internal/bitwarden"
"github.com/maxlaverse/terraform-provider-bitwarden/internal/bitwarden/models"
"github.com/maxlaverse/terraform-provider-bitwarden/internal/schema_definition"
)

func opItemRead(attrObject models.ObjectType, attrType models.ItemType) passwordManagerOperation {
return func(ctx context.Context, d *schema.ResourceData, bwClient bitwarden.PasswordManager) diag.Diagnostics {
err := d.Set(schema_definition.AttributeType, attrType)
if err != nil {
return diag.FromErr(err)
}
return opObjectRead(attrObject)(ctx, d, bwClient)
}
}
Loading

0 comments on commit 5276986

Please sign in to comment.