diff --git a/internal/bitwarden/bwcli/filter.go b/internal/bitwarden/bwcli/filter.go deleted file mode 100644 index 8145c24..0000000 --- a/internal/bitwarden/bwcli/filter.go +++ /dev/null @@ -1,17 +0,0 @@ -package bwcli - -import "github.com/maxlaverse/terraform-provider-bitwarden/internal/bitwarden/models" - -func FilterObjectsByType(objs []models.Object, itemType models.ItemType) []models.Object { - if itemType == 0 { - return objs - } - - filtered := make([]models.Object, 0, len(objs)) - for _, obj := range objs { - if obj.Type == itemType { - filtered = append(filtered, obj) - } - } - return filtered -} diff --git a/internal/bitwarden/bwcli/password_manager_test.go b/internal/bitwarden/bwcli/password_manager_test.go index 33714b6..52f18fa 100644 --- a/internal/bitwarden/bwcli/password_manager_test.go +++ b/internal/bitwarden/bwcli/password_manager_test.go @@ -65,7 +65,7 @@ func TestGetItem(t *testing.T) { } } -func TestGetOrgCollection(t *testing.T) { +func TestGetOrganizationCollection(t *testing.T) { removeMocks, commandsExecuted := test_command.MockCommands(t, map[string]string{ "get org-collection object-id --organizationid org-id": `{}`, }) diff --git a/internal/bitwarden/embedded/password_manager_webapi.go b/internal/bitwarden/embedded/password_manager_webapi.go index 4bd4d12..041bcb7 100644 --- a/internal/bitwarden/embedded/password_manager_webapi.go +++ b/internal/bitwarden/embedded/password_manager_webapi.go @@ -237,7 +237,7 @@ func (v *webAPIVault) CreateObject(ctx context.Context, obj models.Object) (*mod return nil, fmt.Errorf("error encrypting collection for creation: %w", err) } - resEncCollection, err := v.client.CreateOrgCollection(ctx, obj.OrganizationID, *encObj) + resEncCollection, err := v.client.CreateOrganizationCollection(ctx, obj.OrganizationID, *encObj) if err != nil { return nil, fmt.Errorf("error creating collection: %w", err) } @@ -408,7 +408,7 @@ func (v *webAPIVault) DeleteObject(ctx context.Context, obj models.Object) error if obj.Object == models.ObjectTypeFolder { err = v.client.DeleteFolder(ctx, obj.ID) } else if obj.Object == models.ObjectTypeOrgCollection { - err = v.client.DeleteOrgCollection(ctx, obj.OrganizationID, obj.ID) + err = v.client.DeleteOrganizationCollection(ctx, obj.OrganizationID, obj.ID) } else { err = v.client.DeleteObject(ctx, obj.ID) } @@ -455,7 +455,7 @@ func (v *webAPIVault) EditObject(ctx context.Context, obj models.Object) (*model return nil, fmt.Errorf("error encrypting collection for creation: %w", err) } - resCollection, err := v.client.EditOrgCollection(ctx, obj.OrganizationID, obj.ID, *encObj) + resCollection, err := v.client.EditOrganizationCollection(ctx, obj.OrganizationID, obj.ID, *encObj) if err != nil { return nil, fmt.Errorf("error editing collection: %w", err) } diff --git a/internal/bitwarden/webapi/client.go b/internal/bitwarden/webapi/client.go index 186a853..14ecb8b 100644 --- a/internal/bitwarden/webapi/client.go +++ b/internal/bitwarden/webapi/client.go @@ -33,18 +33,18 @@ type Client interface { CreateObjectAttachment(ctx context.Context, itemId string, data []byte, req AttachmentRequestData) (*CreateObjectAttachmentResponse, error) CreateObjectAttachmentData(ctx context.Context, itemId, attachmentId string, data []byte) error CreateOrganization(ctx context.Context, req CreateOrganizationRequest) (*CreateOrganizationResponse, error) - CreateOrgCollection(ctx context.Context, orgId string, req OrganizationCreationRequest) (*Collection, error) + CreateOrganizationCollection(ctx context.Context, orgId string, req OrganizationCreationRequest) (*Collection, error) CreateProject(ctx context.Context, project models.Project) (*models.Project, error) CreateSecret(ctx context.Context, secret models.Secret) (*Secret, error) DeleteFolder(ctx context.Context, objID string) error DeleteObject(ctx context.Context, objID string) error DeleteObjectAttachment(ctx context.Context, itemId, attachmentId string) error - DeleteOrgCollection(ctx context.Context, orgID, collectionID string) error + DeleteOrganizationCollection(ctx context.Context, orgID, collectionID string) error DeleteProject(ctx context.Context, projectId string) error DeleteSecret(ctx context.Context, secretId string) error EditFolder(ctx context.Context, obj Folder) (*Folder, error) EditObject(context.Context, models.Object) (*models.Object, error) - EditOrgCollection(ctx context.Context, orgId, objId string, obj OrganizationCreationRequest) (*Collection, error) + EditOrganizationCollection(ctx context.Context, orgId, objId string, obj OrganizationCreationRequest) (*Collection, error) EditProject(context.Context, models.Project) (*models.Project, error) EditSecret(ctx context.Context, secret models.Secret) (*Secret, error) GetAPIKey(ctx context.Context, username, password string, kdfConfig models.KdfConfiguration) (*ApiKey, error) @@ -179,7 +179,7 @@ func (c *client) CreateOrganization(ctx context.Context, req CreateOrganizationR return doRequest[CreateOrganizationResponse](ctx, c.httpClient, httpReq) } -func (c *client) CreateOrgCollection(ctx context.Context, orgId string, req OrganizationCreationRequest) (*Collection, error) { +func (c *client) CreateOrganizationCollection(ctx context.Context, orgId string, req OrganizationCreationRequest) (*Collection, error) { httpReq, err := c.prepareRequest(ctx, "POST", fmt.Sprintf("%s/api/organizations/%s/collections", c.serverURL, orgId), req) if err != nil { return nil, fmt.Errorf("error preparing organization collection creation request: %w", err) @@ -245,7 +245,7 @@ func (c *client) DeleteObjectAttachment(ctx context.Context, itemId, attachmentI return err } -func (c *client) DeleteOrgCollection(ctx context.Context, orgID, collectionID string) error { +func (c *client) DeleteOrganizationCollection(ctx context.Context, orgID, collectionID string) error { httpReq, err := c.prepareRequest(ctx, "DELETE", fmt.Sprintf("%s/api/organizations/%s/collections/%s", c.serverURL, orgID, collectionID), nil) if err != nil { return fmt.Errorf("error preparing organization collection deletion request: %w", err) @@ -303,7 +303,7 @@ func (c *client) EditObject(ctx context.Context, obj models.Object) (*models.Obj return doRequest[models.Object](ctx, c.httpClient, req) } -func (c *client) EditOrgCollection(ctx context.Context, orgId, objId string, obj OrganizationCreationRequest) (*Collection, error) { +func (c *client) EditOrganizationCollection(ctx context.Context, orgId, objId string, obj OrganizationCreationRequest) (*Collection, error) { req, err := c.prepareRequest(ctx, "PUT", fmt.Sprintf("%s/api/organizations/%s/collections/%s", c.serverURL, orgId, objId), obj) if err != nil { return nil, fmt.Errorf("error preparing collection edition request: %w", err) diff --git a/internal/provider/data_source_item_secure_note.go b/internal/provider/data_source_item_secure_note.go index f7d9ea5..e3aa6e2 100644 --- a/internal/provider/data_source_item_secure_note.go +++ b/internal/provider/data_source_item_secure_note.go @@ -7,11 +7,9 @@ import ( ) func dataSourceItemSecureNote() *schema.Resource { - dataSourceItemSecureNoteSchema := schema_definition.BaseSchema(schema_definition.DataSource) - return &schema.Resource{ Description: "Use this data source to get information on an existing secure note item.", ReadContext: withPasswordManager(opItemRead(models.ItemTypeSecureNote)), - Schema: dataSourceItemSecureNoteSchema, + Schema: schema_definition.BaseSchema(schema_definition.DataSource), } } diff --git a/internal/provider/data_source_project.go b/internal/provider/data_source_project.go index 68b1501..4454338 100644 --- a/internal/provider/data_source_project.go +++ b/internal/provider/data_source_project.go @@ -6,11 +6,9 @@ import ( ) func dataSourceProject() *schema.Resource { - dataSourceProjectSchema := schema_definition.ProjectSchema(schema_definition.DataSource) - return &schema.Resource{ Description: "Use this data source to get information on an existing project.", ReadContext: withSecretsManager(opProjectRead), - Schema: dataSourceProjectSchema, + Schema: schema_definition.ProjectSchema(schema_definition.DataSource), } } diff --git a/internal/provider/data_source_secret.go b/internal/provider/data_source_secret.go index 3803f18..e4db937 100644 --- a/internal/provider/data_source_secret.go +++ b/internal/provider/data_source_secret.go @@ -6,11 +6,9 @@ import ( ) func dataSourceSecret() *schema.Resource { - dataSourceSecretSchema := schema_definition.SecretSchema(schema_definition.DataSource) - return &schema.Resource{ Description: "Use this data source to get information on an existing secret.", ReadContext: withSecretsManager(opSecretRead), - Schema: dataSourceSecretSchema, + Schema: schema_definition.SecretSchema(schema_definition.DataSource), } } diff --git a/internal/provider/operation_attachment.go b/internal/provider/operation_attachment.go index ca797f6..45a510a 100644 --- a/internal/provider/operation_attachment.go +++ b/internal/provider/operation_attachment.go @@ -51,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(transformation.AttachmentDataFromStruct(d, attachmentsAdded[0])) + return diag.FromErr(transformation.AttachmentObjectToSchema(d, attachmentsAdded[0])) } func opAttachmentDelete(ctx context.Context, d *schema.ResourceData, bwClient bitwarden.PasswordManager) diag.Diagnostics { @@ -99,7 +99,7 @@ func opAttachmentReadIgnoreMissing(ctx context.Context, d *schema.ResourceData, for _, attachment := range obj.Attachments { if attachment.ID == d.Id() { - return diag.FromErr(transformation.AttachmentDataFromStruct(d, attachment)) + return diag.FromErr(transformation.AttachmentObjectToSchema(d, attachment)) } } diff --git a/internal/provider/operation_folder.go b/internal/provider/operation_folder.go index dc07fb6..3fe431a 100644 --- a/internal/provider/operation_folder.go +++ b/internal/provider/operation_folder.go @@ -17,7 +17,7 @@ func opFolderCreate(ctx context.Context, d *schema.ResourceData, bwClient bitwar return diag.FromErr(err) } - return diag.FromErr(applyOperation(ctx, d, bwClient.CreateObject, transformation.ObjectStructFromData, transformation.ObjectDataFromStruct)) + return diag.FromErr(applyOperation(ctx, d, bwClient.CreateObject, transformation.BaseSchemaToObject, transformation.BaseObjectToSchema)) } func opFolderDelete(ctx context.Context, d *schema.ResourceData, bwClient bitwarden.PasswordManager) diag.Diagnostics { @@ -25,7 +25,7 @@ func opFolderDelete(ctx context.Context, d *schema.ResourceData, bwClient bitwar if err != nil { return diag.FromErr(err) } - return diag.FromErr(applyOperation(ctx, d, withNilReturn(bwClient.DeleteObject), transformation.ObjectStructFromData, transformation.ObjectDataFromStruct)) + return diag.FromErr(applyOperation(ctx, d, withNilReturn(bwClient.DeleteObject), transformation.BaseSchemaToObject, transformation.BaseObjectToSchema)) } func opFolderImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { @@ -44,10 +44,10 @@ func opFolderRead(ctx context.Context, d *schema.ResourceData, bwClient bitwarde return diag.FromErr(err) } if _, idProvided := d.GetOk(schema_definition.AttributeID); !idProvided { - return diag.FromErr(searchOperation(ctx, d, bwClient.ListObjects, transformation.ObjectDataFromStruct)) + return diag.FromErr(searchOperation(ctx, d, bwClient.ListObjects, transformation.BaseObjectToSchema)) } - return diag.FromErr(applyOperation(ctx, d, bwClient.GetObject, transformation.ObjectStructFromData, transformation.ObjectDataFromStruct)) + return diag.FromErr(applyOperation(ctx, d, bwClient.GetObject, transformation.BaseSchemaToObject, transformation.BaseObjectToSchema)) } func opFolderReadIgnoreMissing(ctx context.Context, d *schema.ResourceData, bwClient bitwarden.PasswordManager) diag.Diagnostics { @@ -55,7 +55,7 @@ func opFolderReadIgnoreMissing(ctx context.Context, d *schema.ResourceData, bwCl if err != nil { return diag.FromErr(err) } - return ignoreMissing(ctx, d, applyOperation(ctx, d, bwClient.GetObject, transformation.ObjectStructFromData, transformation.ObjectDataFromStruct)) + return ignoreMissing(ctx, d, applyOperation(ctx, d, bwClient.GetObject, transformation.BaseSchemaToObject, transformation.BaseObjectToSchema)) } func opFolderUpdate(ctx context.Context, d *schema.ResourceData, bwClient bitwarden.PasswordManager) diag.Diagnostics { @@ -63,5 +63,5 @@ func opFolderUpdate(ctx context.Context, d *schema.ResourceData, bwClient bitwar if err != nil { return diag.FromErr(err) } - return diag.FromErr(applyOperation(ctx, d, bwClient.EditObject, transformation.ObjectStructFromData, transformation.ObjectDataFromStruct)) + return diag.FromErr(applyOperation(ctx, d, bwClient.EditObject, transformation.BaseSchemaToObject, transformation.BaseObjectToSchema)) } diff --git a/internal/provider/operation_item.go b/internal/provider/operation_item.go index d547ffe..03fd9ae 100644 --- a/internal/provider/operation_item.go +++ b/internal/provider/operation_item.go @@ -23,7 +23,7 @@ func opItemCreate(attrType models.ItemType) passwordManagerOperation { return diag.FromErr(err) } - return diag.FromErr(applyOperation(ctx, d, bwClient.CreateObject, transformation.ObjectStructFromData, transformation.ObjectDataFromStruct)) + return diag.FromErr(applyOperation(ctx, d, bwClient.CreateObject, transformation.BaseSchemaToObject, transformation.BaseObjectToSchema)) } } @@ -32,7 +32,7 @@ func opItemDelete(ctx context.Context, d *schema.ResourceData, bwClient bitwarde if err != nil { return diag.FromErr(err) } - return diag.FromErr(applyOperation(ctx, d, withNilReturn(bwClient.DeleteObject), transformation.ObjectStructFromData, transformation.ObjectDataFromStruct)) + return diag.FromErr(applyOperation(ctx, d, withNilReturn(bwClient.DeleteObject), transformation.BaseSchemaToObject, transformation.BaseObjectToSchema)) } func opItemImport(attrType models.ItemType) schema.StateContextFunc { @@ -68,9 +68,9 @@ func opItemRead(attrType models.ItemType) passwordManagerOperation { return diag.FromErr(err) } if _, idProvided := d.GetOk(schema_definition.AttributeID); !idProvided { - return diag.FromErr(searchOperation(ctx, d, bwClient.ListObjects, transformation.ObjectDataFromStruct)) + return diag.FromErr(searchOperation(ctx, d, bwClient.ListObjects, transformation.BaseObjectToSchema)) } - return diag.FromErr(applyOperation(ctx, d, bwClient.GetObject, transformation.ObjectStructFromData, transformation.ObjectDataFromStruct)) + return diag.FromErr(applyOperation(ctx, d, bwClient.GetObject, transformation.BaseSchemaToObject, transformation.BaseObjectToSchema)) } } @@ -79,7 +79,7 @@ func opItemReadIgnoreMissing(ctx context.Context, d *schema.ResourceData, bwClie if err != nil { return diag.FromErr(err) } - return ignoreMissing(ctx, d, applyOperation(ctx, d, bwClient.GetObject, transformation.ObjectStructFromData, transformation.ObjectDataFromStruct)) + return ignoreMissing(ctx, d, applyOperation(ctx, d, bwClient.GetObject, transformation.BaseSchemaToObject, transformation.BaseObjectToSchema)) } func opItemUpdate(ctx context.Context, d *schema.ResourceData, bwClient bitwarden.PasswordManager) diag.Diagnostics { @@ -87,5 +87,5 @@ func opItemUpdate(ctx context.Context, d *schema.ResourceData, bwClient bitwarde if err != nil { return diag.FromErr(err) } - return diag.FromErr(applyOperation(ctx, d, bwClient.EditObject, transformation.ObjectStructFromData, transformation.ObjectDataFromStruct)) + return diag.FromErr(applyOperation(ctx, d, bwClient.EditObject, transformation.BaseSchemaToObject, transformation.BaseObjectToSchema)) } diff --git a/internal/provider/operation_org_collection.go b/internal/provider/operation_org_collection.go index 0d85902..b3c8c9f 100644 --- a/internal/provider/operation_org_collection.go +++ b/internal/provider/operation_org_collection.go @@ -18,7 +18,7 @@ func opOrganizationCollectionCreate(ctx context.Context, d *schema.ResourceData, if err != nil { return diag.FromErr(err) } - return diag.FromErr(applyOperation(ctx, d, bwClient.CreateObject, transformation.ObjectStructFromData, transformation.ObjectDataFromStruct)) + return diag.FromErr(applyOperation(ctx, d, bwClient.CreateObject, transformation.BaseSchemaToObject, transformation.BaseObjectToSchema)) } func opOrganizationCollectionDelete(ctx context.Context, d *schema.ResourceData, bwClient bitwarden.PasswordManager) diag.Diagnostics { @@ -26,7 +26,7 @@ func opOrganizationCollectionDelete(ctx context.Context, d *schema.ResourceData, if err != nil { return diag.FromErr(err) } - return diag.FromErr(applyOperation(ctx, d, withNilReturn(bwClient.DeleteObject), transformation.ObjectStructFromData, transformation.ObjectDataFromStruct)) + return diag.FromErr(applyOperation(ctx, d, withNilReturn(bwClient.DeleteObject), transformation.BaseSchemaToObject, transformation.BaseObjectToSchema)) } func opOrganizationCollectionImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { @@ -51,10 +51,10 @@ func opOrganizationCollectionRead(ctx context.Context, d *schema.ResourceData, b return diag.FromErr(err) } if _, idProvided := d.GetOk(schema_definition.AttributeID); !idProvided { - return diag.FromErr(searchOperation(ctx, d, bwClient.ListObjects, transformation.ObjectDataFromStruct)) + return diag.FromErr(searchOperation(ctx, d, bwClient.ListObjects, transformation.BaseObjectToSchema)) } - return diag.FromErr(applyOperation(ctx, d, bwClient.GetObject, transformation.ObjectStructFromData, transformation.ObjectDataFromStruct)) + return diag.FromErr(applyOperation(ctx, d, bwClient.GetObject, transformation.BaseSchemaToObject, transformation.BaseObjectToSchema)) } func opOrganizationCollectionReadIgnoreMissing(ctx context.Context, d *schema.ResourceData, bwClient bitwarden.PasswordManager) diag.Diagnostics { @@ -62,7 +62,7 @@ func opOrganizationCollectionReadIgnoreMissing(ctx context.Context, d *schema.Re if err != nil { return diag.FromErr(err) } - return ignoreMissing(ctx, d, applyOperation(ctx, d, bwClient.GetObject, transformation.ObjectStructFromData, transformation.ObjectDataFromStruct)) + return ignoreMissing(ctx, d, applyOperation(ctx, d, bwClient.GetObject, transformation.BaseSchemaToObject, transformation.BaseObjectToSchema)) } func opOrganizationCollectionUpdate(ctx context.Context, d *schema.ResourceData, bwClient bitwarden.PasswordManager) diag.Diagnostics { @@ -70,5 +70,5 @@ func opOrganizationCollectionUpdate(ctx context.Context, d *schema.ResourceData, if err != nil { return diag.FromErr(err) } - return diag.FromErr(applyOperation(ctx, d, bwClient.EditObject, transformation.ObjectStructFromData, transformation.ObjectDataFromStruct)) + return diag.FromErr(applyOperation(ctx, d, bwClient.EditObject, transformation.BaseSchemaToObject, transformation.BaseObjectToSchema)) } diff --git a/internal/provider/operation_organization.go b/internal/provider/operation_organization.go index da4ac32..3536967 100644 --- a/internal/provider/operation_organization.go +++ b/internal/provider/operation_organization.go @@ -20,8 +20,8 @@ func opOrganizationRead(ctx context.Context, d *schema.ResourceData, bwClient bi } if _, idProvided := d.GetOk(schema_definition.AttributeID); !idProvided { - return diag.FromErr(searchOperation(ctx, d, bwClient.ListObjects, transformation.ObjectDataFromStruct)) + return diag.FromErr(searchOperation(ctx, d, bwClient.ListObjects, transformation.BaseObjectToSchema)) } - return diag.FromErr(applyOperation(ctx, d, bwClient.GetObject, transformation.ObjectStructFromData, transformation.ObjectDataFromStruct)) + return diag.FromErr(applyOperation(ctx, d, bwClient.GetObject, transformation.BaseSchemaToObject, transformation.BaseObjectToSchema)) } diff --git a/internal/provider/operation_project.go b/internal/provider/operation_project.go index 0ed6789..b7922ea 100644 --- a/internal/provider/operation_project.go +++ b/internal/provider/operation_project.go @@ -11,11 +11,11 @@ import ( ) func opProjectCreate(ctx context.Context, d *schema.ResourceData, bwsClient bitwarden.SecretsManager) diag.Diagnostics { - return diag.FromErr(applyOperation(ctx, d, bwsClient.CreateProject, transformation.ProjectStructFromData, transformation.ProjectDataFromStruct)) + return diag.FromErr(applyOperation(ctx, d, bwsClient.CreateProject, transformation.ProjectSchemaToObject, transformation.ProjectObjectToSchema)) } func opProjectDelete(ctx context.Context, d *schema.ResourceData, bwsClient bitwarden.SecretsManager) diag.Diagnostics { - return diag.FromErr(applyOperation(ctx, d, withNilReturn(bwsClient.DeleteProject), transformation.ProjectStructFromData, transformation.ProjectDataFromStruct)) + return diag.FromErr(applyOperation(ctx, d, withNilReturn(bwsClient.DeleteProject), transformation.ProjectSchemaToObject, transformation.ProjectObjectToSchema)) } func opProjectImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { @@ -25,13 +25,13 @@ func opProjectImport(ctx context.Context, d *schema.ResourceData, meta interface func opProjectRead(ctx context.Context, d *schema.ResourceData, bwsClient bitwarden.SecretsManager) diag.Diagnostics { d.SetId(d.Get(schema_definition.AttributeID).(string)) - return diag.FromErr(applyOperation(ctx, d, bwsClient.GetProject, transformation.ProjectStructFromData, transformation.ProjectDataFromStruct)) + return diag.FromErr(applyOperation(ctx, d, bwsClient.GetProject, transformation.ProjectSchemaToObject, transformation.ProjectObjectToSchema)) } func opProjectReadIgnoreMissing(ctx context.Context, d *schema.ResourceData, bwsClient bitwarden.SecretsManager) diag.Diagnostics { - return ignoreMissing(ctx, d, applyOperation(ctx, d, bwsClient.GetProject, transformation.ProjectStructFromData, transformation.ProjectDataFromStruct)) + return ignoreMissing(ctx, d, applyOperation(ctx, d, bwsClient.GetProject, transformation.ProjectSchemaToObject, transformation.ProjectObjectToSchema)) } func opProjectUpdate(ctx context.Context, d *schema.ResourceData, bwsClient bitwarden.SecretsManager) diag.Diagnostics { - return diag.FromErr(applyOperation(ctx, d, bwsClient.EditProject, transformation.ProjectStructFromData, transformation.ProjectDataFromStruct)) + return diag.FromErr(applyOperation(ctx, d, bwsClient.EditProject, transformation.ProjectSchemaToObject, transformation.ProjectObjectToSchema)) } diff --git a/internal/provider/operation_secret.go b/internal/provider/operation_secret.go index 7fdf6f6..61e627e 100644 --- a/internal/provider/operation_secret.go +++ b/internal/provider/operation_secret.go @@ -12,11 +12,11 @@ import ( ) func opSecretCreate(ctx context.Context, d *schema.ResourceData, bwsClient bitwarden.SecretsManager) diag.Diagnostics { - return diag.FromErr(applyOperation(ctx, d, bwsClient.CreateSecret, transformation.SecretStructFromData, transformation.SecretDataFromStruct)) + return diag.FromErr(applyOperation(ctx, d, bwsClient.CreateSecret, transformation.SecretSchemaToObject, transformation.SecretObjectToSchema)) } func opSecretDelete(ctx context.Context, d *schema.ResourceData, bwsClient bitwarden.SecretsManager) diag.Diagnostics { - return diag.FromErr(applyOperation(ctx, d, withNilReturn(bwsClient.DeleteSecret), transformation.SecretStructFromData, transformation.SecretDataFromStruct)) + return diag.FromErr(applyOperation(ctx, d, withNilReturn(bwsClient.DeleteSecret), transformation.SecretSchemaToObject, transformation.SecretObjectToSchema)) } func opSecretImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { @@ -30,15 +30,15 @@ func opSecretRead(ctx context.Context, d *schema.ResourceData, bwsClient bitward return diag.FromErr(secretSearch(ctx, d, bwsClient)) } - return diag.FromErr(applyOperation(ctx, d, bwsClient.GetSecret, transformation.SecretStructFromData, transformation.SecretDataFromStruct)) + return diag.FromErr(applyOperation(ctx, d, bwsClient.GetSecret, transformation.SecretSchemaToObject, transformation.SecretObjectToSchema)) } func opSecretReadIgnoreMissing(ctx context.Context, d *schema.ResourceData, bwsClient bitwarden.SecretsManager) diag.Diagnostics { - return ignoreMissing(ctx, d, applyOperation(ctx, d, bwsClient.GetSecret, transformation.SecretStructFromData, transformation.SecretDataFromStruct)) + return ignoreMissing(ctx, d, applyOperation(ctx, d, bwsClient.GetSecret, transformation.SecretSchemaToObject, transformation.SecretObjectToSchema)) } func opSecretUpdate(ctx context.Context, d *schema.ResourceData, bwsClient bitwarden.SecretsManager) diag.Diagnostics { - return diag.FromErr(applyOperation(ctx, d, bwsClient.EditSecret, transformation.SecretStructFromData, transformation.SecretDataFromStruct)) + return diag.FromErr(applyOperation(ctx, d, bwsClient.EditSecret, transformation.SecretSchemaToObject, transformation.SecretObjectToSchema)) } func secretSearch(ctx context.Context, d *schema.ResourceData, bwsClient bitwarden.SecretsManager) error { @@ -52,5 +52,5 @@ func secretSearch(ctx context.Context, d *schema.ResourceData, bwsClient bitward return err } - return transformation.SecretDataFromStruct(ctx, d, secret) + return transformation.SecretObjectToSchema(ctx, d, secret) } diff --git a/internal/provider/resource_item_secure_note.go b/internal/provider/resource_item_secure_note.go index 0f70e02..8c91334 100644 --- a/internal/provider/resource_item_secure_note.go +++ b/internal/provider/resource_item_secure_note.go @@ -7,8 +7,6 @@ import ( ) func resourceItemSecureNote() *schema.Resource { - dataSourceItemSecureNoteSchema := schema_definition.BaseSchema(schema_definition.Resource) - return &schema.Resource{ Description: "Manages a secure note item.", CreateContext: withPasswordManager(opItemCreate(models.ItemTypeSecureNote)), @@ -16,6 +14,6 @@ func resourceItemSecureNote() *schema.Resource { UpdateContext: withPasswordManager(opItemUpdate), DeleteContext: withPasswordManager(opItemDelete), Importer: resourceImporter(opItemImport(models.ItemTypeSecureNote)), - Schema: dataSourceItemSecureNoteSchema, + Schema: schema_definition.BaseSchema(schema_definition.Resource), } } diff --git a/internal/provider/resource_project.go b/internal/provider/resource_project.go index abdd355..b3e5cee 100644 --- a/internal/provider/resource_project.go +++ b/internal/provider/resource_project.go @@ -6,15 +6,13 @@ import ( ) func resourceProject() *schema.Resource { - resourceProjectSchema := schema_definition.ProjectSchema(schema_definition.Resource) - return &schema.Resource{ Description: "Manages a Project.", CreateContext: withSecretsManager(opProjectCreate), ReadContext: withSecretsManager(opProjectReadIgnoreMissing), UpdateContext: withSecretsManager(opProjectUpdate), DeleteContext: withSecretsManager(opProjectDelete), - Schema: resourceProjectSchema, + Schema: schema_definition.ProjectSchema(schema_definition.Resource), Importer: resourceImporter(opProjectImport), } } diff --git a/internal/provider/resource_secret.go b/internal/provider/resource_secret.go index c87d3d4..273d785 100644 --- a/internal/provider/resource_secret.go +++ b/internal/provider/resource_secret.go @@ -6,15 +6,13 @@ import ( ) func resourceSecret() *schema.Resource { - resourceSecretSchema := schema_definition.SecretSchema(schema_definition.Resource) - return &schema.Resource{ Description: "Manages a secret.", CreateContext: withSecretsManager(opSecretCreate), ReadContext: withSecretsManager(opSecretReadIgnoreMissing), UpdateContext: withSecretsManager(opSecretUpdate), DeleteContext: withSecretsManager(opSecretDelete), - Schema: resourceSecretSchema, + Schema: schema_definition.SecretSchema(schema_definition.Resource), Importer: resourceImporter(opSecretImport), } } diff --git a/internal/schema_definition/schema.go b/internal/schema_definition/base.go similarity index 100% rename from internal/schema_definition/schema.go rename to internal/schema_definition/base.go diff --git a/internal/schema_definition/schema_test.go b/internal/schema_definition/base_test.go similarity index 100% rename from internal/schema_definition/schema_test.go rename to internal/schema_definition/base_test.go diff --git a/internal/schema_definition/schema_folder.go b/internal/schema_definition/folder.go similarity index 100% rename from internal/schema_definition/schema_folder.go rename to internal/schema_definition/folder.go diff --git a/internal/schema_definition/schema_login.go b/internal/schema_definition/item_login.go similarity index 100% rename from internal/schema_definition/schema_login.go rename to internal/schema_definition/item_login.go diff --git a/internal/schema_definition/schema_org_collection.go b/internal/schema_definition/org_collection.go similarity index 100% rename from internal/schema_definition/schema_org_collection.go rename to internal/schema_definition/org_collection.go diff --git a/internal/schema_definition/schema_organization.go b/internal/schema_definition/organization.go similarity index 100% rename from internal/schema_definition/schema_organization.go rename to internal/schema_definition/organization.go diff --git a/internal/schema_definition/schema_project.go b/internal/schema_definition/project.go similarity index 100% rename from internal/schema_definition/schema_project.go rename to internal/schema_definition/project.go diff --git a/internal/schema_definition/schema_attributes.go b/internal/schema_definition/schema_attributes.go index ac26462..0108eb7 100644 --- a/internal/schema_definition/schema_attributes.go +++ b/internal/schema_definition/schema_attributes.go @@ -49,7 +49,7 @@ const ( AttributeProjectID = "project_id" AttributeValue = "value" - // Data-source and Resource field schema_definition.Descriptions + // Data-source and Resource field descriptions DescriptionAttachments = "List of item attachments." DescriptionCollectionIDs = "Identifier of the collections the item belongs to." DescriptionCreationDate = "Date the item was created." @@ -106,7 +106,7 @@ const ( AttributeExperimental = "experimental" AttributeExperimentalEmbeddedClient = "embedded_client" - // Provider field schema_definition.Descriptions + // Provider field descriptions DescriptionAccessToken = "Machine Account Access Token (env: `BWS_ACCESS_TOKEN`))." DescriptionClientSecret = "Client Secret (env: `BW_CLIENTSECRET`). Do not commit this information in Git unless you know what you're doing. Prefer using a Terraform `variable {}` in order to inject this value from the environment." DescriptionClientID = "Client ID (env: `BW_CLIENTID`)" diff --git a/internal/schema_definition/schema_secret.go b/internal/schema_definition/secret.go similarity index 100% rename from internal/schema_definition/schema_secret.go rename to internal/schema_definition/secret.go diff --git a/internal/transformation/transformation_attachment.go b/internal/transformation/attachment.go similarity index 92% rename from internal/transformation/transformation_attachment.go rename to internal/transformation/attachment.go index aab5aaa..257d7a8 100644 --- a/internal/transformation/transformation_attachment.go +++ b/internal/transformation/attachment.go @@ -6,7 +6,7 @@ import ( "github.com/maxlaverse/terraform-provider-bitwarden/internal/schema_definition" ) -func AttachmentDataFromStruct(d *schema.ResourceData, attachment models.Attachment) error { +func AttachmentObjectToSchema(d *schema.ResourceData, attachment models.Attachment) error { d.SetId(attachment.ID) err := d.Set(schema_definition.AttributeAttachmentFileName, attachment.FileName) diff --git a/internal/transformation/list_options_from_data.go b/internal/transformation/list_options_from_data.go new file mode 100644 index 0000000..e59259b --- /dev/null +++ b/internal/transformation/list_options_from_data.go @@ -0,0 +1,38 @@ +package transformation + +import ( + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/maxlaverse/terraform-provider-bitwarden/internal/bitwarden" + "github.com/maxlaverse/terraform-provider-bitwarden/internal/schema_definition" +) + +func ListOptionsFromData(d *schema.ResourceData) []bitwarden.ListObjectsOption { + filters := []bitwarden.ListObjectsOption{} + + filterMap := map[string]bitwarden.ListObjectsOptionGenerator{ + schema_definition.AttributeFilterSearch: bitwarden.WithSearch, + schema_definition.AttributeFilterCollectionId: bitwarden.WithCollectionID, + schema_definition.AttributeOrganizationID: bitwarden.WithOrganizationID, + schema_definition.AttributeFilterFolderID: bitwarden.WithFolderID, + schema_definition.AttributeFilterOrganizationID: bitwarden.WithOrganizationID, + schema_definition.AttributeFilterURL: bitwarden.WithUrl, + } + + for attribute, optionFunc := range filterMap { + v, ok := d.GetOk(attribute) + if !ok { + continue + } + + if v, ok := v.(string); ok && len(v) > 0 { + filters = append(filters, optionFunc(v)) + } + } + + itemType, ok := d.GetOk(schema_definition.AttributeType) + if ok { + filters = append(filters, bitwarden.WithItemType(itemType.(int))) + } + + return filters +} diff --git a/internal/transformation/transformation_object.go b/internal/transformation/object.go similarity index 85% rename from internal/transformation/transformation_object.go rename to internal/transformation/object.go index f10a016..845ac2c 100644 --- a/internal/transformation/transformation_object.go +++ b/internal/transformation/object.go @@ -5,12 +5,11 @@ import ( "github.com/hashicorp/terraform-plugin-log/tflog" "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 ObjectDataFromStruct(ctx context.Context, d *schema.ResourceData, obj *models.Object) error { +func BaseObjectToSchema(ctx context.Context, d *schema.ResourceData, obj *models.Object) error { if obj == nil { // Object has been deleted return nil @@ -72,7 +71,7 @@ func ObjectDataFromStruct(ctx context.Context, d *schema.ResourceData, obj *mode return err } - err = d.Set(schema_definition.AttributeField, objectFieldDataFromStruct(obj)) + err = d.Set(schema_definition.AttributeField, objectFieldObjectToSchema(obj)) if err != nil { return err } @@ -129,7 +128,7 @@ func ObjectDataFromStruct(ctx context.Context, d *schema.ResourceData, obj *mode return nil } -func ObjectStructFromData(ctx context.Context, d *schema.ResourceData) models.Object { +func BaseSchemaToObject(ctx context.Context, d *schema.ResourceData) models.Object { var obj models.Object obj.ID = d.Id() @@ -181,11 +180,11 @@ func ObjectStructFromData(ctx context.Context, d *schema.ResourceData) models.Ob } if vList, ok := d.Get(schema_definition.AttributeAttachments).([]interface{}); ok { - obj.Attachments = objectAttachmentStructFromData(vList) + obj.Attachments = objectAttachmentSchemaToObject(vList) } if v, ok := d.Get(schema_definition.AttributeField).([]interface{}); ok { - obj.Fields = objectFieldStructFromData(v) + obj.Fields = objectFieldSchemaToObject(v) } if obj.Type == models.ItemTypeLogin { @@ -207,7 +206,7 @@ func ObjectStructFromData(ctx context.Context, d *schema.ResourceData) models.Ob return obj } -func objectFieldDataFromStruct(obj *models.Object) []interface{} { +func objectFieldObjectToSchema(obj *models.Object) []interface{} { fields := make([]interface{}, len(obj.Fields)) for k, f := range obj.Fields { field := map[string]interface{}{ @@ -227,7 +226,7 @@ func objectFieldDataFromStruct(obj *models.Object) []interface{} { return fields } -func objectAttachmentStructFromData(vList []interface{}) []models.Attachment { +func objectAttachmentSchemaToObject(vList []interface{}) []models.Attachment { attachments := make([]models.Attachment, len(vList)) for k, v := range vList { vc := v.(map[string]interface{}) @@ -256,7 +255,7 @@ func objectAttachmentsFromStruct(objAttachments []models.Attachment) []interface return attachments } -func objectFieldStructFromData(vList []interface{}) []models.Field { +func objectFieldSchemaToObject(vList []interface{}) []models.Field { fields := make([]models.Field, len(vList)) for k, v := range vList { vc := v.(map[string]interface{}) @@ -366,34 +365,3 @@ const ( URIMatchRegExp URIMatchStr = "regexp" URIMatchNever URIMatchStr = "never" ) - -func ListOptionsFromData(d *schema.ResourceData) []bitwarden.ListObjectsOption { - filters := []bitwarden.ListObjectsOption{} - - filterMap := map[string]bitwarden.ListObjectsOptionGenerator{ - schema_definition.AttributeFilterSearch: bitwarden.WithSearch, - schema_definition.AttributeFilterCollectionId: bitwarden.WithCollectionID, - schema_definition.AttributeOrganizationID: bitwarden.WithOrganizationID, - schema_definition.AttributeFilterFolderID: bitwarden.WithFolderID, - schema_definition.AttributeFilterOrganizationID: bitwarden.WithOrganizationID, - schema_definition.AttributeFilterURL: bitwarden.WithUrl, - } - - for attribute, optionFunc := range filterMap { - v, ok := d.GetOk(attribute) - if !ok { - continue - } - - if v, ok := v.(string); ok && len(v) > 0 { - filters = append(filters, optionFunc(v)) - } - } - - itemType, ok := d.GetOk(schema_definition.AttributeType) - if ok { - filters = append(filters, bitwarden.WithItemType(itemType.(int))) - } - - return filters -} diff --git a/internal/transformation/transformation_project.go b/internal/transformation/project.go similarity index 87% rename from internal/transformation/transformation_project.go rename to internal/transformation/project.go index 7f587dc..1029ae4 100644 --- a/internal/transformation/transformation_project.go +++ b/internal/transformation/project.go @@ -8,7 +8,7 @@ import ( "github.com/maxlaverse/terraform-provider-bitwarden/internal/schema_definition" ) -func ProjectStructFromData(_ context.Context, d *schema.ResourceData) models.Project { +func ProjectSchemaToObject(_ context.Context, d *schema.ResourceData) models.Project { var project models.Project project.ID = d.Id() @@ -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 ProjectObjectToSchema(_ context.Context, d *schema.ResourceData, project *models.Project) error { if project == nil { // Project has been deleted return nil diff --git a/internal/transformation/transformation_secret.go b/internal/transformation/secret.go similarity index 91% rename from internal/transformation/transformation_secret.go rename to internal/transformation/secret.go index 04e773f..2903be7 100644 --- a/internal/transformation/transformation_secret.go +++ b/internal/transformation/secret.go @@ -8,7 +8,7 @@ import ( "github.com/maxlaverse/terraform-provider-bitwarden/internal/schema_definition" ) -func SecretStructFromData(_ context.Context, d *schema.ResourceData) models.Secret { +func SecretSchemaToObject(_ context.Context, d *schema.ResourceData) models.Secret { var secret models.Secret secret.ID = d.Id() @@ -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 SecretObjectToSchema(_ context.Context, d *schema.ResourceData, secret *models.Secret) error { if secret == nil { // Secret has been deleted return nil