Skip to content

Commit

Permalink
Add support for subfield arguments (handwritten changes) (#65)
Browse files Browse the repository at this point in the history
* Prepare for next development iteration

* MSSQL cmdlet Updates (#63)

* Now works with Host/FCI/ID/List

* Allow for filtering by InstanceName

* Correct bugs on GetInstance and added ID to GetDatabase

* Updates to MSSQL

* Update to PR

* change log updates

* Update TOOLKIT_DEVELOPER_MANUAL.md about operations

* Expand interfaces in -AddField and -RemoveField

* Patched clusterConnection DETAIL profile

* Add support for subfield arguments (handwritten changes)

---------

Co-authored-by: Gui <[email protected]>
Co-authored-by: Chris Lumnah <[email protected]>
  • Loading branch information
3 people authored Apr 3, 2024
1 parent fd68b1f commit 1768823
Show file tree
Hide file tree
Showing 26 changed files with 719 additions and 363 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
# Changelog

## Version 0.30

Fixes:

- `-AddField` and `-RemoveField` now expand interfaces.
- Get-RscMssqlAvailabilityGroup - Now works for returning back list or by name.
- Get-RscMssqlLogShipping - Now works and no longer references Live Mount
- Remove-RscMssqlLogShippingSecondary - Now works and no longer references Live Mount
- Patched clusterConnection DETAIL profile.
- Added support for subfield arguments

Breaking Changes:

- Get-RscMssqlInstance - No longer accepts RscHost object any more. Not it accepts a string value for HostName and WindowsClusterName. This is because, there is no value in the RscHost object for getting the MSSQL Instance object as the only option is to query by name and not by ID. Additionally, we need the ability to search for a Instance based on a Windows Cluster Name. This new approach allows for a simpler user experience, as they do not need to make an extra call to get a Host object before requesting the Instance object.
- Added support for fields of type `Long`

## Version 0.29

New Features:
Expand Down Expand Up @@ -411,3 +427,4 @@ Fixes:
Breaking changes:

- `-InputProfile DETAILS` is now `-InputProfile DETAIL`

Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ public void TestFlattenField()
},
{
typeof(TypeC),
"Baz\nB.A.Foo\nB.A.Bar\nB.Baz\nInf"
"Baz\nB.A.Foo\nB.A.Bar\nB.Baz\nInf.Bar\nInf.Foo\nInf.Name"
// Note: it's Inf.Bar, Inf.Foo, Inf.Name
// Because FlattenField outputs all possible fields of
// an interface across all classes that implement it.
},
};
foreach (var testCase in testCases)
Expand All @@ -76,8 +79,7 @@ public void TestFlattenField()

List<string> expectedOutput = testCase.Value.Split("\n").ToList();
expectedOutput.Sort();
List<string> result = ReflectionUtils.FlattenField(inputType);
result.Sort();
List<string> result = ReflectionUtils.FlattenFieldFull(inputType);
if (!expectedOutput.SequenceEqual(result))
{
string expectedStr = string.Join(", ", expectedOutput);
Expand Down
15 changes: 12 additions & 3 deletions RubrikSecurityCloud/RubrikSecurityCloud.Common/BaseType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,19 @@ public virtual List<string> SelectedFields()

}

public virtual List<string> AllFields()
public virtual List<string> AllFields(int maxDepth = 0)
{
return StringUtils.FlattenFieldToFieldSpecList(
ReflectionUtils.FlattenField(this.GetType().FullName));
var fields = StringUtils.FlattenFieldToFieldSpecList(
ReflectionUtils.FlattenFieldFull(this.GetType().FullName));
if (maxDepth <= 0)
{
return fields;
}
else
{
return fields.Select(f =>
f.Split('.').Take(maxDepth).Aggregate((a, b) => a + "." + b)).Distinct().ToList();
}
}

public List<string> UnselectedFields()
Expand Down
4 changes: 3 additions & 1 deletion RubrikSecurityCloud/RubrikSecurityCloud.Common/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public static class Config
{"^email$" , null },
{"^endcursor$" , null },
{"id$" , null },
{"^jobid$" , null },
{"^message$", null },
{"^name$" , null },
{"^numworkloaddescendants$" , null },
Expand Down Expand Up @@ -83,7 +84,8 @@ public static class Config
"^items$",
"^nodes$",
"^pageinfo$",
"^asyncrequeststatus$"
"^asyncrequeststatus$",
"^jobids$",
};

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Collections;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using RubrikSecurityCloud.Types;

namespace RubrikSecurityCloud
{

public class GraphqlInlineArgsConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(DateTime) ||
objectType == typeof(string);
}

public override object ReadJson(
JsonReader reader,
Type objectType,
object? existingValue,
JsonSerializer serializer
)
{
throw new NotImplementedException();
}

public override void WriteJson(
JsonWriter writer,
object? value,
JsonSerializer serializer
)
{
if (value == null)
{
writer.WriteNull();
return;
}
Type objectType = value.GetType();
if (objectType == typeof(DateTime)) {
DateTime datetime = (DateTime)value;
serializer.Serialize(
writer,
datetime.ToString(
"\"yyyy-MM-ddTHH:mm:sszzz\"",
System.Globalization.CultureInfo.InvariantCulture
)
);
} else
{
string stringVal = (string)value;
serializer.Serialize(
writer,
"\"" + stringVal + "\""
);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ public static class JsonUtils
/// - Hashtables and Enumerable types are recursively processed.
/// - Defaults to ToString() on unrecognized types.
/// </summary>
public static System.Object JsonReady(System.Object obj)
public static System.Object? JsonReady(System.Object obj)
{
// basic types
if (
obj == null ||
obj is string ||
obj is int ||
obj is bool ||
obj is long ||
obj is Enum
)
{
Expand Down Expand Up @@ -58,7 +59,7 @@ obj is Enum
if (obj is VarDict varDict)
{
var vd = new VarDict();
foreach (KeyValuePair<string, object> entry in varDict)
foreach (KeyValuePair<string, object?> entry in varDict)
{
if (entry.Value != null)
{
Expand All @@ -73,7 +74,7 @@ obj is Enum
// Enumerable (array, list, etc.)
if (obj is IEnumerable enumerable)
{
var l = new List<object>();
var l = new List<object?>();
foreach (var item in enumerable)
{
l.Add(JsonUtils.JsonReady(item));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using Newtonsoft.Json.Serialization;
using RubrikSecurityCloud.Types;
using System.Linq;
using System.Management.Automation;
using System.Text;

namespace RubrikSecurityCloud
{
Expand Down Expand Up @@ -33,6 +35,16 @@ public class OperationVariableSet
ContractResolver = new SortedCamelCasePropertyNamesContractResolver()
};

private readonly JsonSerializerSettings _inlineVarsSettings =
new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
Converters = new List<JsonConverter> {
new Newtonsoft.Json.Converters.StringEnumConverter(),
new GraphqlInlineArgsConverter()
},
ContractResolver = new SortedCamelCasePropertyNamesContractResolver()
};

public string AsJson(IRscLogger? logger = null)
{
Expand All @@ -41,6 +53,19 @@ public string AsJson(IRscLogger? logger = null)

}

public string AsInlineVariables(IRscLogger? logger = null)
{
JObject variables = processVariables(logger);
string jsonStr = JsonConvert.SerializeObject(variables, _inlineVarsSettings);
string placeholder = "strPlaceholder";
jsonStr = jsonStr.Replace("\\\"\"", placeholder);
jsonStr = jsonStr.Replace("\"\\\"", placeholder);
jsonStr = jsonStr.Replace("\"", string.Empty);
jsonStr = jsonStr.Replace(placeholder, "\"");
jsonStr = jsonStr.Substring(1, jsonStr.Length - 2);
return jsonStr;
}

private JObject processVariables(IRscLogger? logger = null)
{
_logger = logger;
Expand Down Expand Up @@ -121,6 +146,14 @@ private JToken processVariable(object obj)
return arr;
}

if (obj is PSObject psObject)
{
return JObject.FromObject(
psObject.BaseObject,
JsonSerializer.Create(_serializerSettings)
);
}

// default:
return JObject.FromObject(
obj,
Expand Down
Loading

0 comments on commit 1768823

Please sign in to comment.