/// <summary> /// Constructs the resource /// </summary> private JToken GetResource(string resourceId, string apiVersion) { var resource = this.GetExistingResource(resourceId, apiVersion).Result.ToResource(); var policyDefinitionObject = new PolicyDefinition { Name = this.Name ?? ResourceIdUtility.GetResourceName(this.Id), Properties = new PolicyDefinitionProperties { Description = this.Description ?? (resource.Properties["description"] != null ? resource.Properties["description"].ToString() : null), DisplayName = this.DisplayName ?? (resource.Properties["displayName"] != null ? resource.Properties["displayName"].ToString() : null) } }; if (!string.IsNullOrEmpty(this.Policy)) { policyDefinitionObject.Properties.PolicyRule = JObject.Parse(GetPolicyRuleObject().ToString()); } else { policyDefinitionObject.Properties.PolicyRule = JObject.Parse(resource.Properties["policyRule"].ToString()); } return(policyDefinitionObject.ToJToken()); }
/// <summary> /// Constructs the resource /// </summary> private JToken GetResource(string resourceId, string apiVersion) { var resource = this.GetExistingResource(resourceId, apiVersion).Result.ToResource(); var policyRuleJson = string.IsNullOrEmpty(this.Policy) ? resource.Properties["policyRule"]?.ToString() : GetObjectFromParameter(this.Policy).ToString(); var metaDataJson = string.IsNullOrEmpty(this.Metadata) ? resource.Properties["metadata"]?.ToString() : GetObjectFromParameter(this.Metadata).ToString(); var parameterJson = string.IsNullOrEmpty(this.Parameter) ? resource.Properties["parameters"]?.ToString() : GetObjectFromParameter(this.Parameter).ToString(); PolicyDefinitionMode tempMode; PolicyDefinitionMode?existingMode = null; if (Enum.TryParse(resource.Properties["mode"]?.ToString(), true, out tempMode)) { existingMode = tempMode; } var policyDefinitionObject = new PolicyDefinition { Name = this.Name ?? resource.Name, Properties = new PolicyDefinitionProperties { Description = this.Description ?? resource.Properties["description"]?.ToString(), DisplayName = this.DisplayName ?? resource.Properties["displayName"]?.ToString(), PolicyRule = string.IsNullOrEmpty(policyRuleJson) ? null : JObject.Parse(policyRuleJson), Metadata = string.IsNullOrEmpty(metaDataJson) ? null : JObject.Parse(metaDataJson), Parameters = string.IsNullOrEmpty(parameterJson) ? null : JObject.Parse(parameterJson), Mode = this.Mode ?? existingMode } }; return(policyDefinitionObject.ToJToken()); }
/// <summary> /// Constructs the policy definition by combining command line parameters and json object /// </summary> private JToken GetResource() { var policyDefinitionObject = new PolicyDefinition { Name = this.Name, Properties = new PolicyDefinitionProperties() }; var policyObject = this.GetObjectFromParameter(this.Policy, nameof(this.Policy)); if (policyObject["policyRule"] != null) { // policy parameter was a full policy object, populate the properties from it, override from other command line parameters policyDefinitionObject.Properties.Description = this.Description ?? policyObject["description"]?.ToString(); policyDefinitionObject.Properties.DisplayName = this.DisplayName ?? policyObject["displayName"]?.ToString(); policyDefinitionObject.Properties.PolicyRule = policyObject["policyRule"] as JObject; policyDefinitionObject.Properties.Metadata = this.Metadata == null ? policyObject["metadata"] as JObject : this.GetObjectFromParameter(this.Metadata, nameof(this.Metadata)); policyDefinitionObject.Properties.Parameters = this.Parameter == null ? policyObject["parameters"] as JObject : this.GetObjectFromParameter(this.Parameter, nameof(this.Parameter)); policyDefinitionObject.Properties.Mode = string.IsNullOrEmpty(this.Mode) ? policyObject["mode"]?.ToString() ?? PolicyDefinitionMode.All : this.Mode; } else { // policy parameter was a rule object, populate policy rule from it and the properties from command line parameters policyDefinitionObject.Properties.Description = this.Description; policyDefinitionObject.Properties.DisplayName = this.DisplayName; policyDefinitionObject.Properties.PolicyRule = this.GetObjectFromParameter(this.Policy, nameof(this.Policy)); policyDefinitionObject.Properties.Metadata = this.Metadata == null ? null : this.GetObjectFromParameter(this.Metadata, nameof(this.Metadata)); policyDefinitionObject.Properties.Parameters = this.Parameter == null ? null : this.GetObjectFromParameter(this.Parameter, nameof(this.Parameter)); policyDefinitionObject.Properties.Mode = string.IsNullOrEmpty(this.Mode) ? PolicyDefinitionMode.All : this.Mode; } return(policyDefinitionObject.ToJToken()); }
/// <summary> /// Constructs the resource /// </summary> private JToken GetResource() { var policyDefinitionObject = new PolicyDefinition { Name = this.Name, Properties = new PolicyDefinitionProperties { Description = this.Description ?? null, DisplayName = this.DisplayName ?? null, PolicyRule = JObject.Parse(GetPolicyRuleObject().ToString()) } }; return(policyDefinitionObject.ToJToken()); }
/// <summary> /// Constructs the policy definition by combining command line parameters and existing policy definition /// </summary> private JToken GetResource(string resourceId, string apiVersion) { var resource = this.GetExistingResource(resourceId, apiVersion).Result.ToResource(); // apply incoming object properties if present if (this.InputObject != null) { resource.Properties = this.InputObject.Properties.ToJToken(); } var policyDefinitionObject = new PolicyDefinition { Name = this.Name ?? resource.Name, Properties = new PolicyDefinitionProperties() }; JObject policyObject = this.Policy != null?this.GetObjectFromParameter(this.Policy, nameof(this.Policy)) : null; if (policyObject != null && policyObject["policyRule"] != null) { // policy parameter was a full policy object, populate the properties from it, override from other command line parameters policyDefinitionObject.Properties.Description = this.Description ?? policyObject["description"]?.ToString() ?? resource.Properties["description"]?.ToString(); policyDefinitionObject.Properties.DisplayName = this.DisplayName ?? policyObject["displayName"]?.ToString() ?? resource.Properties["displayName"]?.ToString(); policyDefinitionObject.Properties.PolicyRule = policyObject["policyRule"] as JObject ?? resource.Properties["policyRule"] as JObject; policyDefinitionObject.Properties.Metadata = this.Metadata == null ? policyObject["metadata"] as JObject ?? resource.Properties["metadata"] as JObject : this.GetObjectFromParameter(this.Metadata, nameof(this.Metadata)); policyDefinitionObject.Properties.Parameters = this.Parameter == null ? policyObject["parameters"] as JObject ?? resource.Properties["metadata"] as JObject : this.GetObjectFromParameter(this.Parameter, nameof(this.Parameter)); policyDefinitionObject.Properties.Mode = string.IsNullOrEmpty(this.Mode) ? policyObject["mode"]?.ToString() ?? resource.Properties["mode"]?.ToString() ?? PolicyDefinitionMode.All : this.Mode; } else { // policy parameter was a rule object, populate policy rule from it and the properties from command line parameters policyDefinitionObject.Properties.Description = this.Description ?? resource.Properties["description"]?.ToString(); policyDefinitionObject.Properties.DisplayName = this.DisplayName ?? resource.Properties["displayName"]?.ToString(); policyDefinitionObject.Properties.PolicyRule = policyObject ?? resource.Properties["policyRule"] as JObject; policyDefinitionObject.Properties.Metadata = this.Metadata == null ? resource.Properties["metadata"] as JObject : this.GetObjectFromParameter(this.Metadata, nameof(this.Metadata)); policyDefinitionObject.Properties.Parameters = this.Parameter == null ? resource.Properties["parameters"] as JObject : this.GetObjectFromParameter(this.Parameter, nameof(this.Parameter)); policyDefinitionObject.Properties.Mode = string.IsNullOrEmpty(this.Mode) ? resource.Properties["mode"]?.ToString() ?? PolicyDefinitionMode.All : this.Mode; } return(policyDefinitionObject.ToJToken()); }
/// <summary> /// Constructs the resource /// </summary> private JToken GetResource() { var policyDefinitionObject = new PolicyDefinition { Name = this.Name, Properties = new PolicyDefinitionProperties { Description = this.Description ?? null, DisplayName = this.DisplayName ?? null, PolicyRule = JObject.Parse(this.GetObjectFromParameter(this.Policy).ToString()), Metadata = this.Metadata == null ? null : JObject.Parse(this.GetObjectFromParameter(this.Metadata).ToString()), Parameters = this.Parameter == null ? null : JObject.Parse(this.GetObjectFromParameter(this.Parameter).ToString()), Mode = this.Mode.HasValue ? this.Mode : PolicyDefinitionMode.All } }; return(policyDefinitionObject.ToJToken()); }
/// <summary> /// Constructs the resource /// </summary> private JToken GetResource() { var policyDefinitionObject = new PolicyDefinition { Name = this.Name, Properties = new PolicyDefinitionProperties { Description = this.Description ?? null, DisplayName = this.DisplayName ?? null, PolicyRule = this.GetObjectFromParameter(this.Policy, nameof(this.Policy)), Metadata = this.Metadata == null ? null : this.GetObjectFromParameter(this.Metadata, nameof(this.Metadata)), Parameters = this.Parameter == null ? null : this.GetObjectFromParameter(this.Parameter, nameof(this.Parameter)), Mode = string.IsNullOrEmpty(this.Mode) ? PolicyDefinitionMode.All : this.Mode } }; return(policyDefinitionObject.ToJToken()); }