|
| 1 | +// ---------------------------------------------------------------------------------- |
| 2 | +// |
| 3 | +// Copyright Microsoft Corporation |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | +// ---------------------------------------------------------------------------------- |
| 14 | + |
| 15 | +using Microsoft.Azure.Management.Storage.Models; |
| 16 | +using Microsoft.WindowsAzure.Commands.Common.Attributes; |
| 17 | +using System; |
| 18 | +using System.Collections.Generic; |
| 19 | + |
| 20 | +namespace Microsoft.Azure.Commands.Management.Storage.Models |
| 21 | +{ |
| 22 | + /// <summary> |
| 23 | + /// Wrapper of SDK type ObjectReplicationPolicy |
| 24 | + /// </summary> |
| 25 | + public class PSObjectReplicationPolicy |
| 26 | + { |
| 27 | + [Ps1Xml(Label = "ResourceGroupName", Target = ViewControl.Table, Position = 0)] |
| 28 | + public string ResourceGroupName { get; } |
| 29 | + [Ps1Xml(Label = "StorageAccountName", Target = ViewControl.Table, Position = 1)] |
| 30 | + public string StorageAccountName { get; } |
| 31 | + public string ResourceId { get; } |
| 32 | + public string Name { get; } |
| 33 | + public string Type { get; } |
| 34 | + |
| 35 | + [Ps1Xml(Label = "PolicyId", Target = ViewControl.Table, Position = 2)] |
| 36 | + public string PolicyId { get; set; } |
| 37 | + [Ps1Xml(Label = "EnabledTime", Target = ViewControl.Table, Position = 3)] |
| 38 | + public DateTime? EnabledTime { get; } |
| 39 | + [Ps1Xml(Label = "SourceAccount", Target = ViewControl.Table, Position = 4)] |
| 40 | + public string SourceAccount { get; set; } |
| 41 | + [Ps1Xml(Label = "DestinationAccount", Target = ViewControl.Table, Position = 5)] |
| 42 | + public string DestinationAccount { get; set; } |
| 43 | + [Ps1Xml(Label = "Rules", Target = ViewControl.Table, ScriptBlock = "if (($_.Rules -ne $null) -and ($_.Rules.Count -ne 0)) {'[' + $_.Rules[0].RuleId + ',...]'} else {$null}", Position = 6)] |
| 44 | + public PSObjectReplicationPolicyRule[] Rules { get; set; } |
| 45 | + |
| 46 | + public PSObjectReplicationPolicy() |
| 47 | + { } |
| 48 | + |
| 49 | + public PSObjectReplicationPolicy(ObjectReplicationPolicy policy, string ResourceGroupName, string StorageAccountName) |
| 50 | + { |
| 51 | + this.ResourceGroupName = ResourceGroupName; |
| 52 | + this.StorageAccountName = StorageAccountName; |
| 53 | + this.ResourceId = policy.Id; |
| 54 | + this.Name = policy.Name; |
| 55 | + this.Type = policy.Type; |
| 56 | + this.PolicyId = policy.PolicyId; |
| 57 | + this.EnabledTime = policy.EnabledTime; |
| 58 | + this.SourceAccount = policy.SourceAccount; |
| 59 | + this.DestinationAccount = policy.DestinationAccount; |
| 60 | + this.Rules = PSObjectReplicationPolicyRule.GetPSObjectReplicationPolicyRules(policy.Rules); |
| 61 | + } |
| 62 | + |
| 63 | + public ObjectReplicationPolicy ParseObjectReplicationPolicy() |
| 64 | + { |
| 65 | + ObjectReplicationPolicy policy = new ObjectReplicationPolicy() |
| 66 | + { |
| 67 | + SourceAccount = this.SourceAccount, |
| 68 | + DestinationAccount = this.DestinationAccount, |
| 69 | + Rules = PSObjectReplicationPolicyRule.ParseObjectReplicationPolicyRules(this.Rules) |
| 70 | + }; |
| 71 | + return policy; |
| 72 | + } |
| 73 | + |
| 74 | + public static PSObjectReplicationPolicy[] GetPSObjectReplicationPolicies(IEnumerable<ObjectReplicationPolicy> policies, string ResourceGroupName, string StorageAccountName) |
| 75 | + { |
| 76 | + if (policies == null) |
| 77 | + { |
| 78 | + return null; |
| 79 | + } |
| 80 | + List<PSObjectReplicationPolicy> pspolicies = new List<PSObjectReplicationPolicy>(); |
| 81 | + foreach (ObjectReplicationPolicy policy in policies) |
| 82 | + { |
| 83 | + pspolicies.Add(new PSObjectReplicationPolicy(policy, ResourceGroupName, StorageAccountName)); |
| 84 | + } |
| 85 | + return pspolicies.ToArray(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /// <summary> |
| 90 | + /// Wrapper of SDK type ObjectReplicationPolicyRule |
| 91 | + /// </summary> |
| 92 | + public class PSObjectReplicationPolicyRule |
| 93 | + { |
| 94 | + [Ps1Xml(Label = "RuleId", Target = ViewControl.Table, Position = 0)] |
| 95 | + public string RuleId { get; set; } |
| 96 | + [Ps1Xml(Label = "SourceContainer", Target = ViewControl.Table, Position = 1)] |
| 97 | + public string SourceContainer { get; set; } |
| 98 | + [Ps1Xml(Label = "DestinationContainer", Target = ViewControl.Table, Position = 2)] |
| 99 | + public string DestinationContainer { get; set; } |
| 100 | + [Ps1Xml(Label = "Filter.PrefixMatch", Target = ViewControl.Table, ScriptBlock = "if (($_.Filter -ne $null) -and ($_.Filter.PrefixMatch -ne $null) -and ($_.Filter.PrefixMatch.Count -ne 0)) {'[' + ($_.Filter.PrefixMatch -join ', ') + ']'} else {$null}", Position = 3)] |
| 101 | + public PSObjectReplicationPolicyFilter Filters { get; set; } |
| 102 | + |
| 103 | + public PSObjectReplicationPolicyRule() |
| 104 | + { |
| 105 | + } |
| 106 | + |
| 107 | + public PSObjectReplicationPolicyRule(ObjectReplicationPolicyRule rule) |
| 108 | + { |
| 109 | + this.RuleId = rule.RuleId; |
| 110 | + this.SourceContainer = rule.SourceContainer; |
| 111 | + this.DestinationContainer = rule.DestinationContainer; |
| 112 | + this.Filters = rule.Filters is null ? null : new PSObjectReplicationPolicyFilter(rule.Filters); |
| 113 | + } |
| 114 | + |
| 115 | + public ObjectReplicationPolicyRule ParseObjectReplicationPolicyRule() |
| 116 | + { |
| 117 | + ObjectReplicationPolicyRule rule = new ObjectReplicationPolicyRule(); |
| 118 | + rule.RuleId = this.RuleId; |
| 119 | + rule.SourceContainer = this.SourceContainer; |
| 120 | + rule.DestinationContainer = this.DestinationContainer; |
| 121 | + rule.Filters = this.Filters is null ? null : this.Filters.ParseObjectReplicationPolicyFilter(); |
| 122 | + return rule; |
| 123 | + } |
| 124 | + |
| 125 | + public static PSObjectReplicationPolicyRule[] GetPSObjectReplicationPolicyRules(IList<ObjectReplicationPolicyRule> rules) |
| 126 | + { |
| 127 | + if (rules == null) |
| 128 | + { |
| 129 | + return null; |
| 130 | + } |
| 131 | + List<PSObjectReplicationPolicyRule> psrules = new List<PSObjectReplicationPolicyRule>(); |
| 132 | + foreach (ObjectReplicationPolicyRule rule in rules) |
| 133 | + { |
| 134 | + psrules.Add(new PSObjectReplicationPolicyRule(rule)); |
| 135 | + } |
| 136 | + return psrules.ToArray(); |
| 137 | + } |
| 138 | + |
| 139 | + public static List<ObjectReplicationPolicyRule> ParseObjectReplicationPolicyRules(PSObjectReplicationPolicyRule[] psrules) |
| 140 | + { |
| 141 | + if (psrules == null) |
| 142 | + { |
| 143 | + return null; |
| 144 | + } |
| 145 | + List<ObjectReplicationPolicyRule> rules = new List<ObjectReplicationPolicyRule>(); |
| 146 | + foreach (PSObjectReplicationPolicyRule psrule in psrules) |
| 147 | + { |
| 148 | + rules.Add(psrule.ParseObjectReplicationPolicyRule()); |
| 149 | + } |
| 150 | + return rules; |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + /// <summary> |
| 155 | + /// Wrapper of SDK type ObjectReplicationPolicyFilter |
| 156 | + /// </summary> |
| 157 | + public class PSObjectReplicationPolicyFilter |
| 158 | + { |
| 159 | + public string[] PrefixMatch { get; set; } |
| 160 | + public DateTime? MinCreationTime; |
| 161 | + |
| 162 | + public PSObjectReplicationPolicyFilter() |
| 163 | + { |
| 164 | + } |
| 165 | + |
| 166 | + public PSObjectReplicationPolicyFilter(ObjectReplicationPolicyFilter filter) |
| 167 | + { |
| 168 | + if (filter != null) |
| 169 | + { |
| 170 | + this.PrefixMatch = filter.PrefixMatch is null ? null : new List<string>(filter.PrefixMatch).ToArray(); |
| 171 | + if (string.IsNullOrEmpty(filter.MinCreationTime)) |
| 172 | + { |
| 173 | + this.MinCreationTime = null; |
| 174 | + } |
| 175 | + else |
| 176 | + { |
| 177 | + if (filter.MinCreationTime.ToUpper()[filter.MinCreationTime.Length - 1] != 'Z') |
| 178 | + { |
| 179 | + filter.MinCreationTime = filter.MinCreationTime + "Z"; |
| 180 | + } |
| 181 | + this.MinCreationTime = Convert.ToDateTime(filter.MinCreationTime); |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + public ObjectReplicationPolicyFilter ParseObjectReplicationPolicyFilter() |
| 186 | + { |
| 187 | + return new ObjectReplicationPolicyFilter() |
| 188 | + { |
| 189 | + PrefixMatch = this.PrefixMatch is null ? null : new List<string>(this.PrefixMatch), |
| 190 | + //must be in format: 2020-02-19T16:05:00Z |
| 191 | + MinCreationTime = this.MinCreationTime is null ? null : this.MinCreationTime.Value.ToUniversalTime().ToString("s") + "Z" |
| 192 | + }; |
| 193 | + } |
| 194 | + } |
| 195 | +} |
0 commit comments