-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathNew-S3Bucket-Cmdlet.cs
238 lines (205 loc) · 9.96 KB
/
New-S3Bucket-Cmdlet.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*******************************************************************************
* Copyright 2012-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
* this file except in compliance with the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file.
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
* *****************************************************************************
*
* AWS Tools for Windows (TM) PowerShell (TM)
*
*/
using System;
using System.Management.Automation;
using Amazon.PowerShell.Common;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
namespace Amazon.PowerShell.Cmdlets.S3
{
/// <summary>
/// Creates a new bucket with the supplied name and permissions.
/// </summary>
[Cmdlet("New", "S3Bucket", SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.Low)]
[OutputType("Amazon.S3.Model.S3Bucket")]
[AWSCmdlet("Creates a new bucket in Amazon S3.", Operation = new [] {"PutBucket"}, SelectReturnType = typeof(Amazon.S3.Model.PutBucketResponse))]
[AWSCmdletOutput("Amazon.S3.Model.S3Bucket or Amazon.S3.Model.PutBucketResponse",
"Returns an Amazon.S3.Model.S3Bucket instance representing the new bucket.",
"The service response (type Amazon.S3.Model.PutBucketResponse) is added to the cmdlet entry in the $AWSHistory stack."
)]
public class NewS3BucketCmdlet : AmazonS3ClientCmdlet, IExecutor
{
#region Parameter BucketName
/// <summary>
/// The name that will be given to the new bucket. This name needs to be
/// unique across Amazon S3.
/// </summary>
[Parameter(Position = 0, ValueFromPipelineByPropertyName = true, ValueFromPipeline = true, Mandatory = true)]
[Amazon.PowerShell.Common.AWSRequiredParameter]
public System.String BucketName { get; set; }
#endregion
#region Parameter CannedACLName
/// <summary>
/// Specifies the name of the canned ACL (access control list) of permissions to be applied to the S3 bucket.
/// Please refer to <a href="http://docs.aws.amazon.com/sdkfornet/v3/apidocs/Index.html?page=S3/TS3_S3CannedACL.html&tocid=Amazon_S3_S3CannedACL">Amazon.S3.Model.S3CannedACL</a> for information on S3 Canned ACLs.
/// </summary>
[Parameter(ValueFromPipelineByPropertyName = true)]
[AWSConstantClassSource("Amazon.S3.S3CannedACL")]
public Amazon.S3.S3CannedACL CannedACLName { get; set; }
#endregion
#region Parameter PublicReadOnly
/// <summary>
/// If set, applies an ACL making the bucket public with read-only permissions
/// This parameter is obsolete. Use CannedACLName instead.
/// </summary>
[Parameter(ValueFromPipelineByPropertyName = true)]
[Obsolete("This parameter is obsolete. Use CannedACLName instead.")]
public SwitchParameter PublicReadOnly { get; set; }
#endregion
#region Parameter PublicReadWrite
/// <summary>
/// If set, applies an ACL making the bucket public with read-write permissions.
/// This parameter is obsolete. Use CannedACLName instead.
/// </summary>
[Parameter(ValueFromPipelineByPropertyName = true)]
[Obsolete("This parameter is obsolete. Use CannedACLName instead.")]
public SwitchParameter PublicReadWrite { get; set; }
#endregion
#region Parameter Force
/// <summary>
/// This parameter overrides confirmation prompts to force
/// the cmdlet to continue its operation. This parameter should always
/// be used with caution.
/// </summary>
[Parameter(ValueFromPipelineByPropertyName = true)]
public SwitchParameter Force { get; set; }
#endregion
#region Parameter ObjectLockEnabledForBucket
/// <summary>
/// Specifies whether you want S3 Object Lock to be enabled for the new bucket.
/// </summary>
[Parameter(ValueFromPipelineByPropertyName = true)]
public bool? ObjectLockEnabledForBucket { get; set; }
#endregion
#region Parameter Select
/// <summary>
/// Use the -Select parameter to control the cmdlet output.
/// Specifying -Select '*' will result in the cmdlet returning the whole service response (Amazon.S3.Model.PutBucketResponse).
/// Specifying -Select '^ParameterName' will result in the cmdlet returning the selected cmdlet parameter value.
/// </summary>
[System.Management.Automation.Parameter]
public string Select { get; set; } = "*";
#endregion
protected override void ProcessRecord()
{
base.ProcessRecord();
if (!ConfirmShouldProceed(this.Force.IsPresent, this.BucketName, "New-S3Bucket (PutBucket)"))
return;
var context = new CmdletContext
{
BucketName = this.BucketName
};
if (ParameterWasBound(nameof(this.Select)))
{
context.Select = CreateSelectDelegate<Amazon.S3.Model.PutBucketResponse, NewS3BucketCmdlet>(Select) ??
throw new System.ArgumentException("Invalid value for -Select parameter.", nameof(this.Select));
}
if (!string.IsNullOrEmpty(this.CannedACLName))
context.CannedACL = this.CannedACLName;
#pragma warning disable CS0618, CS0612 //A class member was marked with the Obsolete attribute
else if (this.PublicReadOnly.IsPresent)
context.CannedACL = S3CannedACL.PublicRead;
else if (this.PublicReadWrite.IsPresent)
context.CannedACL = S3CannedACL.PublicReadWrite;
#pragma warning restore CS0618, CS0612 //A class member was marked with the Obsolete attribute
context.ObjectLockEnabledForBucket = this.ObjectLockEnabledForBucket;
var output = Execute(context) as CmdletOutput;
ProcessOutput(output);
}
#region IExecutor Members
public object Execute(ExecutorContext context)
{
var cmdletContext = context as CmdletContext;
var request = new PutBucketRequest();
if (cmdletContext.BucketName != null)
request.BucketName = cmdletContext.BucketName;
if (cmdletContext.CannedACL != null)
request.CannedACL = cmdletContext.CannedACL.Value;
// Forcibly set a location constraint to match whatever Region argument was used
// with the cmdlet; this will prevent S3 issuing the "unspecified location constraint
// didn't match" message, potentially confusing new users.
request.BucketRegionName = _RegionEndpoint.SystemName;
if (cmdletContext.ObjectLockEnabledForBucket != null)
{
request.ObjectLockEnabledForBucket = cmdletContext.ObjectLockEnabledForBucket.Value;
}
ServiceCalls.PushServiceRequest(request, this.MyInvocation);
using (var client = Client ?? CreateClient(_CurrentCredentials, _RegionEndpoint))
{
CmdletOutput output;
try
{
var response = CallAWSServiceOperation(client, request);
object pipelineOutput = null;
pipelineOutput = cmdletContext.Select(response, this);
output = new CmdletOutput
{
PipelineOutput = pipelineOutput,
ServiceResponse = response
};
}
catch (Exception e)
{
output = new CmdletOutput { ErrorResponse = e };
this.WriteError(new ErrorRecord(e, $"Failed to create the specified bucket.\r\nAmazon S3 error: {e.Message}", ErrorCategory.InvalidOperation, this));
}
return output;
}
}
public ExecutorContext CreateContext()
{
return new CmdletContext();
}
#endregion
#region AWS Service Operation Call
private Amazon.S3.Model.PutBucketResponse CallAWSServiceOperation(IAmazonS3 client, Amazon.S3.Model.PutBucketRequest request)
{
Utils.Common.WriteVerboseEndpointMessage(this, client.Config, "Amazon S3", "PutBucket");
try
{
#if DESKTOP
return client.PutBucket(request);
#elif CORECLR
return client.PutBucketAsync(request).GetAwaiter().GetResult();
#else
#error "Unknown build edition"
#endif
}
catch (AmazonServiceException exc)
{
var webException = exc.InnerException as System.Net.WebException;
if (webException != null)
{
throw new Exception(Utils.Common.FormatNameResolutionFailureMessage(client.Config, webException.Message), webException);
}
throw;
}
}
#endregion
internal class CmdletContext : ExecutorContext
{
public String BucketName { get; set; }
public S3CannedACL CannedACL { get; set; }
public bool? ObjectLockEnabledForBucket { get; set; }
public System.Func<Amazon.S3.Model.PutBucketResponse, NewS3BucketCmdlet, object> Select { get; set; } =
// since S3 bucket has only a name and a creation date, take a simple route to
// emit the instance rather than a ListBuckets/search approach
(response, cmdlet) => new S3Bucket { BucketName = cmdlet.BucketName, CreationDate = DateTime.UtcNow };
}
}
}