|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/Devolutions/go-dvls" |
| 5 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 6 | + "github.com/hashicorp/terraform-plugin-framework/types/basetypes" |
| 7 | +) |
| 8 | + |
| 9 | +func newEntryCredentialSSHKeyFromResourceModel(data *EntryCredentialSSHKeyResourceModel) dvls.Entry { |
| 10 | + var tags []string |
| 11 | + |
| 12 | + for _, v := range data.Tags { |
| 13 | + tags = append(tags, v.ValueString()) |
| 14 | + } |
| 15 | + |
| 16 | + entryCredentialSSHKey := dvls.Entry{ |
| 17 | + Id: data.Id.ValueString(), |
| 18 | + VaultId: data.VaultId.ValueString(), |
| 19 | + Name: data.Name.ValueString(), |
| 20 | + Path: data.Folder.ValueString(), |
| 21 | + Type: dvls.EntryCredentialType, |
| 22 | + SubType: dvls.EntryCredentialSubTypePrivateKey, |
| 23 | + Description: data.Description.ValueString(), |
| 24 | + Tags: tags, |
| 25 | + Data: dvls.EntryCredentialPrivateKeyData{ |
| 26 | + OverridePassword: data.Password.ValueString(), |
| 27 | + Passphrase: data.Passphrase.ValueString(), |
| 28 | + PrivateKey: data.PrivateKeyData.ValueString(), |
| 29 | + PublicKey: data.PublicKey.ValueString(), |
| 30 | + }, |
| 31 | + } |
| 32 | + |
| 33 | + return entryCredentialSSHKey |
| 34 | +} |
| 35 | + |
| 36 | +func setEntryCredentialSSHKeyResourceModel(entryCredentialSSHKey dvls.Entry, data *EntryCredentialSSHKeyResourceModel) { |
| 37 | + var model EntryCredentialSSHKeyResourceModel |
| 38 | + |
| 39 | + // VALIDATE IF THE ENTRY IS THE CORRECT SUBTYPE |
| 40 | + |
| 41 | + model.Id = basetypes.NewStringValue(entryCredentialSSHKey.Id) |
| 42 | + model.VaultId = basetypes.NewStringValue(entryCredentialSSHKey.VaultId) |
| 43 | + model.Name = basetypes.NewStringValue(entryCredentialSSHKey.Name) |
| 44 | + |
| 45 | + if entryCredentialSSHKey.Path != "" { |
| 46 | + model.Folder = basetypes.NewStringValue(entryCredentialSSHKey.Path) |
| 47 | + } |
| 48 | + |
| 49 | + if entryCredentialSSHKey.Description != "" { |
| 50 | + model.Description = basetypes.NewStringValue(entryCredentialSSHKey.Description) |
| 51 | + } |
| 52 | + |
| 53 | + if entryCredentialSSHKey.Tags != nil { |
| 54 | + var tagsBase []types.String |
| 55 | + |
| 56 | + for _, v := range entryCredentialSSHKey.Tags { |
| 57 | + tagsBase = append(tagsBase, basetypes.NewStringValue(v)) |
| 58 | + } |
| 59 | + |
| 60 | + model.Tags = tagsBase |
| 61 | + } |
| 62 | + |
| 63 | + if entryCredentialSSHKey.Data != nil { |
| 64 | + data, ok := entryCredentialSSHKey.GetCredentialPrivateKeyData() |
| 65 | + if ok { |
| 66 | + if data.OverridePassword != "" { |
| 67 | + model.Password = basetypes.NewStringValue(data.OverridePassword) |
| 68 | + } |
| 69 | + |
| 70 | + if data.Passphrase != "" { |
| 71 | + model.Passphrase = basetypes.NewStringValue(data.Passphrase) |
| 72 | + } |
| 73 | + |
| 74 | + if data.PrivateKey != "" { |
| 75 | + model.PrivateKeyData = basetypes.NewStringValue(data.PrivateKey) |
| 76 | + } |
| 77 | + |
| 78 | + if data.PublicKey != "" { |
| 79 | + model.PublicKey = basetypes.NewStringValue(data.PublicKey) |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + *data = model |
| 85 | +} |
| 86 | + |
| 87 | +func setEntryCredentialSSHKeyDataModel(entryCredentialSSHKey dvls.Entry, data *EntryCredentialSSHKeyDataSourceModel) { |
| 88 | + var model EntryCredentialSSHKeyDataSourceModel |
| 89 | + |
| 90 | + model.Id = basetypes.NewStringValue(entryCredentialSSHKey.Id) |
| 91 | + model.VaultId = basetypes.NewStringValue(entryCredentialSSHKey.VaultId) |
| 92 | + model.Name = basetypes.NewStringValue(entryCredentialSSHKey.Name) |
| 93 | + |
| 94 | + if entryCredentialSSHKey.Path != "" { |
| 95 | + model.Folder = basetypes.NewStringValue(entryCredentialSSHKey.Path) |
| 96 | + } |
| 97 | + |
| 98 | + if entryCredentialSSHKey.Description != "" { |
| 99 | + model.Description = basetypes.NewStringValue(entryCredentialSSHKey.Description) |
| 100 | + } |
| 101 | + |
| 102 | + if entryCredentialSSHKey.Tags != nil { |
| 103 | + var tagsBase []types.String |
| 104 | + |
| 105 | + for _, v := range entryCredentialSSHKey.Tags { |
| 106 | + tagsBase = append(tagsBase, basetypes.NewStringValue(v)) |
| 107 | + } |
| 108 | + |
| 109 | + model.Tags = tagsBase |
| 110 | + } |
| 111 | + |
| 112 | + if entryCredentialSSHKey.Data != nil { |
| 113 | + data, ok := entryCredentialSSHKey.GetCredentialPrivateKeyData() |
| 114 | + if ok { |
| 115 | + if data.OverridePassword != "" { |
| 116 | + model.Password = basetypes.NewStringValue(data.OverridePassword) |
| 117 | + } |
| 118 | + |
| 119 | + if data.Passphrase != "" { |
| 120 | + model.Passphrase = basetypes.NewStringValue(data.Passphrase) |
| 121 | + } |
| 122 | + |
| 123 | + if data.PrivateKey != "" { |
| 124 | + model.PrivateKeyData = basetypes.NewStringValue(data.PrivateKey) |
| 125 | + } |
| 126 | + |
| 127 | + if data.PublicKey != "" { |
| 128 | + model.PublicKey = basetypes.NewStringValue(data.PublicKey) |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + *data = model |
| 134 | +} |
0 commit comments