Skip to content

Commit

Permalink
move transformation in own package (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxlaverse authored Dec 18, 2024
1 parent f48a79a commit 20e95bc
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 23 deletions.
5 changes: 3 additions & 2 deletions internal/provider/operation_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"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"
"github.com/maxlaverse/terraform-provider-bitwarden/internal/transformation"
)

func opAttachmentCreate(ctx context.Context, d *schema.ResourceData, bwClient bitwarden.PasswordManager) diag.Diagnostics {
Expand Down Expand Up @@ -50,7 +51,7 @@ func opAttachmentCreate(ctx context.Context, d *schema.ResourceData, bwClient bi
return diag.FromErr(errors.New("BUG: at least one attachment removed"))
}

return diag.FromErr(attachmentDataFromStruct(d, attachmentsAdded[0]))
return diag.FromErr(transformation.AttachmentDataFromStruct(d, attachmentsAdded[0]))
}

func opAttachmentDelete(ctx context.Context, d *schema.ResourceData, bwClient bitwarden.PasswordManager) diag.Diagnostics {
Expand Down Expand Up @@ -98,7 +99,7 @@ func opAttachmentReadIgnoreMissing(ctx context.Context, d *schema.ResourceData,

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

Expand Down
9 changes: 5 additions & 4 deletions internal/provider/operation_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/maxlaverse/terraform-provider-bitwarden/internal/bitwarden/bwcli"
"github.com/maxlaverse/terraform-provider-bitwarden/internal/bitwarden/models"
"github.com/maxlaverse/terraform-provider-bitwarden/internal/schema_definition"
"github.com/maxlaverse/terraform-provider-bitwarden/internal/transformation"
)

type objectOperationFunc func(ctx context.Context, secret models.Object) (*models.Object, error)
Expand Down Expand Up @@ -124,7 +125,7 @@ func objectSearch(ctx context.Context, d *schema.ResourceData, bwClient bitwarde
return fmt.Errorf("BUG: object type not set in the resource data")
}

objs, err := bwClient.ListObjects(ctx, models.ObjectType(objType.(string)), listOptionsFromData(d)...)
objs, err := bwClient.ListObjects(ctx, models.ObjectType(objType.(string)), transformation.ListOptionsFromData(d)...)
if err != nil {
return err
}
Expand Down Expand Up @@ -159,14 +160,14 @@ func objectSearch(ctx context.Context, d *schema.ResourceData, bwClient bitwarde
return errors.New("object is soft deleted")
}

return objectDataFromStruct(ctx, d, &obj)
return transformation.ObjectDataFromStruct(ctx, d, &obj)
}

func objectOperation(ctx context.Context, d *schema.ResourceData, operation objectOperationFunc) error {
obj, err := operation(ctx, objectStructFromData(ctx, d))
obj, err := operation(ctx, transformation.ObjectStructFromData(ctx, d))
if err != nil {
return err
}

return objectDataFromStruct(ctx, d, obj)
return transformation.ObjectDataFromStruct(ctx, d, obj)
}
5 changes: 3 additions & 2 deletions internal/provider/operation_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"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"
"github.com/maxlaverse/terraform-provider-bitwarden/internal/transformation"
)

type projectOperationFunc func(ctx context.Context, secret models.Project) (*models.Project, error)
Expand Down Expand Up @@ -64,10 +65,10 @@ func opProjectUpdate(ctx context.Context, d *schema.ResourceData, bwsClient bitw
}

func projectOperation(ctx context.Context, d *schema.ResourceData, operation projectOperationFunc) error {
project, err := operation(ctx, projectStructFromData(ctx, d))
project, err := operation(ctx, transformation.ProjectStructFromData(ctx, d))
if err != nil {
return err
}

return projectDataFromStruct(ctx, d, project)
return transformation.ProjectDataFromStruct(ctx, d, project)
}
7 changes: 4 additions & 3 deletions internal/provider/operation_secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"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"
"github.com/maxlaverse/terraform-provider-bitwarden/internal/transformation"
)

type secretOperationFunc func(ctx context.Context, secret models.Secret) (*models.Secret, error)
Expand Down Expand Up @@ -79,14 +80,14 @@ func secretSearch(ctx context.Context, d *schema.ResourceData, bwsClient bitward
return err
}

return secretDataFromStruct(ctx, d, secret)
return transformation.SecretDataFromStruct(ctx, d, secret)
}

func secretOperation(ctx context.Context, d *schema.ResourceData, operation secretOperationFunc) error {
secret, err := operation(ctx, secretStructFromData(ctx, d))
secret, err := operation(ctx, transformation.SecretStructFromData(ctx, d))
if err != nil {
return err
}

return secretDataFromStruct(ctx, d, secret)
return transformation.SecretDataFromStruct(ctx, d, secret)
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package provider
package transformation

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

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

err := d.Set(schema_definition.AttributeAttachmentFileName, attachment.FileName)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package provider
package transformation

import (
"context"
Expand All @@ -10,7 +10,7 @@ import (
"github.com/maxlaverse/terraform-provider-bitwarden/internal/schema_definition"
)

func objectDataFromStruct(ctx context.Context, d *schema.ResourceData, obj *models.Object) error {
func ObjectDataFromStruct(ctx context.Context, d *schema.ResourceData, obj *models.Object) error {
if obj == nil {
// Object has been deleted
return nil
Expand Down Expand Up @@ -129,7 +129,7 @@ func objectDataFromStruct(ctx context.Context, d *schema.ResourceData, obj *mode
return nil
}

func objectStructFromData(ctx context.Context, d *schema.ResourceData) models.Object {
func ObjectStructFromData(ctx context.Context, d *schema.ResourceData) models.Object {
var obj models.Object

obj.ID = d.Id()
Expand Down Expand Up @@ -367,7 +367,7 @@ const (
URIMatchNever URIMatchStr = "never"
)

func listOptionsFromData(d *schema.ResourceData) []bitwarden.ListObjectsOption {
func ListOptionsFromData(d *schema.ResourceData) []bitwarden.ListObjectsOption {
filters := []bitwarden.ListObjectsOption{}

filterMap := map[string]bitwarden.ListObjectsOptionGenerator{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package provider
package transformation

import (
"context"
Expand All @@ -8,7 +8,7 @@ import (
"github.com/maxlaverse/terraform-provider-bitwarden/internal/schema_definition"
)

func projectStructFromData(_ context.Context, d *schema.ResourceData) models.Project {
func ProjectStructFromData(_ context.Context, d *schema.ResourceData) models.Project {
var project models.Project

project.ID = d.Id()
Expand All @@ -23,7 +23,7 @@ func projectStructFromData(_ context.Context, d *schema.ResourceData) models.Pro
return project
}

func projectDataFromStruct(_ context.Context, d *schema.ResourceData, project *models.Project) error {
func ProjectDataFromStruct(_ context.Context, d *schema.ResourceData, project *models.Project) error {
if project == nil {
// Project has been deleted
return nil
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package provider
package transformation

import (
"context"
Expand All @@ -8,7 +8,7 @@ import (
"github.com/maxlaverse/terraform-provider-bitwarden/internal/schema_definition"
)

func secretStructFromData(_ context.Context, d *schema.ResourceData) models.Secret {
func SecretStructFromData(_ context.Context, d *schema.ResourceData) models.Secret {
var secret models.Secret

secret.ID = d.Id()
Expand All @@ -35,7 +35,7 @@ func secretStructFromData(_ context.Context, d *schema.ResourceData) models.Secr
return secret
}

func secretDataFromStruct(_ context.Context, d *schema.ResourceData, secret *models.Secret) error {
func SecretDataFromStruct(_ context.Context, d *schema.ResourceData, secret *models.Secret) error {
if secret == nil {
// Secret has been deleted
return nil
Expand Down

0 comments on commit 20e95bc

Please sign in to comment.