|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Management.Automation; |
| 5 | +using System.Text; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using UiPath.PowerShell.Models; |
| 8 | +using UiPath.PowerShell.Util; |
| 9 | +using UiPath.Web.Client20204; |
| 10 | +using UiPath.Web.Client20204.Models; |
| 11 | + |
| 12 | +namespace UiPath.PowerShell.Cmdlets |
| 13 | +{ |
| 14 | + [Cmdlet(VerbsCommon.Add, Nouns.Bucket)] |
| 15 | + public class AddBucket : AuthenticatedCmdlet |
| 16 | + { |
| 17 | + private const string FileSystemSet = nameof(FileSystemSet); |
| 18 | + private const string AmazonSet = nameof(AmazonSet); |
| 19 | + private const string AzureSet = nameof(AzureSet); |
| 20 | + private const string MinioSet = nameof(MinioSet); |
| 21 | + private const string OrchestratorSet = nameof(OrchestratorSet); |
| 22 | + |
| 23 | + [Parameter(Mandatory = true, Position = 1, ParameterSetName = OrchestratorSet)] |
| 24 | + [Parameter(Mandatory = true, Position = 1, ParameterSetName = FileSystemSet)] |
| 25 | + [Parameter(Mandatory = true, Position = 1, ParameterSetName = AmazonSet)] |
| 26 | + [Parameter(Mandatory = true, Position = 1, ParameterSetName = AzureSet)] |
| 27 | + [Parameter(Mandatory = true, Position = 1, ParameterSetName = MinioSet)] |
| 28 | + public string Name { get; set; } |
| 29 | + |
| 30 | + [Parameter] |
| 31 | + public string Description { get; set; } |
| 32 | + |
| 33 | + [Parameter] |
| 34 | + public SwitchParameter ReadOnly { get; set; } |
| 35 | + |
| 36 | + [Parameter] |
| 37 | + public SwitchParameter AuditReadAccess { get; set; } |
| 38 | + |
| 39 | + [Parameter(Mandatory = true, ParameterSetName = FileSystemSet)] |
| 40 | + public SwitchParameter FileSystem { get; set; } |
| 41 | + |
| 42 | + [Parameter(Mandatory = true, ParameterSetName = FileSystemSet)] |
| 43 | + public string Path { get; set; } |
| 44 | + |
| 45 | + [Parameter(Mandatory = true, ParameterSetName = AmazonSet)] |
| 46 | + public SwitchParameter Aws { get; set; } |
| 47 | + |
| 48 | + [ValidateNotNull] |
| 49 | + [Parameter(Mandatory = true, ParameterSetName = AmazonSet)] |
| 50 | + public string S3BucketName { get; set; } |
| 51 | + |
| 52 | + [ValidateNotNull] |
| 53 | + [Parameter(Mandatory = true, ParameterSetName = AmazonSet)] |
| 54 | + public string EndpointRegion { get; set; } |
| 55 | + |
| 56 | + [ValidateNotNull] |
| 57 | + [Parameter(Mandatory = true, ParameterSetName = AmazonSet)] |
| 58 | + [Parameter(Mandatory = true, ParameterSetName = MinioSet)] |
| 59 | + public string AccessKey { get; set; } |
| 60 | + |
| 61 | + [ValidateNotNull] |
| 62 | + [Parameter(Mandatory = true, ParameterSetName = AmazonSet)] |
| 63 | + [Parameter(Mandatory = true, ParameterSetName = MinioSet)] |
| 64 | + public string SecretKey { get; set; } |
| 65 | + |
| 66 | + [Parameter(Mandatory = true, ParameterSetName = AzureSet)] |
| 67 | + public SwitchParameter Azure { get; set; } |
| 68 | + |
| 69 | + [Parameter(Mandatory = true, ParameterSetName = AzureSet)] |
| 70 | + [Parameter(Mandatory = true, ParameterSetName = MinioSet)] |
| 71 | + public string ContainerName { get; set; } |
| 72 | + |
| 73 | + [Parameter(Mandatory = true, ParameterSetName = AzureSet)] |
| 74 | + public string AccountName { get; set; } |
| 75 | + |
| 76 | + [Parameter(Mandatory = true, ParameterSetName = AzureSet)] |
| 77 | + public string AccountKey { get; set; } |
| 78 | + |
| 79 | + [Parameter(Mandatory = true, ParameterSetName = AzureSet)] |
| 80 | + public string EndpointSuffix { get; set; } |
| 81 | + |
| 82 | + [Parameter(Mandatory = true, ParameterSetName = MinioSet)] |
| 83 | + public SwitchParameter MinIO { get; set; } |
| 84 | + |
| 85 | + [Parameter(Mandatory = true, ParameterSetName = MinioSet)] |
| 86 | + public string Server { get; set; } |
| 87 | + |
| 88 | + [Parameter(ParameterSetName = AzureSet)] |
| 89 | + [Parameter(ParameterSetName = AmazonSet)] |
| 90 | + [Parameter(ParameterSetName = MinioSet)] |
| 91 | + public CredentialStore CredentialStore { get; set; } |
| 92 | + |
| 93 | + protected override void ProcessRecord() |
| 94 | + { |
| 95 | + var dto = new BucketDto |
| 96 | + { |
| 97 | + Name = Name, |
| 98 | + Description = Description, |
| 99 | + Options = BucketDtoOptions.None, |
| 100 | + Identifier = Guid.NewGuid(), |
| 101 | + }; |
| 102 | + |
| 103 | + if (ReadOnly.IsPresent) |
| 104 | + { |
| 105 | + dto.Options |= BucketDtoOptions.ReadOnly; |
| 106 | + } |
| 107 | + |
| 108 | + if (AuditReadAccess.IsPresent) |
| 109 | + { |
| 110 | + dto.Options |= BucketDtoOptions.AuditReadAccess; |
| 111 | + } |
| 112 | + |
| 113 | + switch(ParameterSetName) |
| 114 | + { |
| 115 | + case FileSystemSet: |
| 116 | + dto.StorageProvider = "FileSystem"; |
| 117 | + dto.StorageParameters = $"RootPath={Path}"; |
| 118 | + break; |
| 119 | + case AmazonSet: |
| 120 | + dto.StorageProvider = "Amazon"; |
| 121 | + dto.Password = SecretKey; |
| 122 | + dto.StorageContainer = S3BucketName; |
| 123 | + dto.StorageParameters = $"EndpointRegion={EndpointRegion};accessKey={AccessKey};secretKey=$Password"; |
| 124 | + dto.CredentialStoreId = CredentialStore?.Id ?? this.GetDefaultCredentialStore(CredentialStoreResourceDtoType.BucketCredential.ToString()); |
| 125 | + break; |
| 126 | + case AzureSet: |
| 127 | + dto.StorageProvider = "Azure"; |
| 128 | + dto.Password = AccountKey; |
| 129 | + dto.StorageContainer = ContainerName; |
| 130 | + dto.StorageParameters = $"EndpointSuffix={EndpointSuffix};AccountName={AccountName};AccountKey=$Password"; |
| 131 | + dto.CredentialStoreId = CredentialStore?.Id ?? this.GetDefaultCredentialStore(CredentialStoreResourceDtoType.BucketCredential.ToString()); |
| 132 | + break; |
| 133 | + case MinioSet: |
| 134 | + dto.StorageProvider = "Minio"; |
| 135 | + dto.Password = SecretKey; |
| 136 | + dto.StorageContainer = ContainerName; |
| 137 | + dto.StorageParameters = $"host={Server};accessKey={AccessKey};secretKey=$Password"; |
| 138 | + dto.CredentialStoreId = CredentialStore?.Id ?? this.GetDefaultCredentialStore(CredentialStoreResourceDtoType.BucketCredential.ToString()); |
| 139 | + break; |
| 140 | + } |
| 141 | + |
| 142 | + dto = Api_20_4.Buckets.Post(dto); |
| 143 | + WriteObject(dto); |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments