Skip to content

Optimize search external cmdlets by not always retrieving the ExternalConnection #4556

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -23,6 +23,11 @@ public SearchExternalConnectionPipeBind(string identity)
_identity = identity;
}

public string GetExternalConnectionId(PSCmdlet cmdlet, PnPConnection connection, string accessToken)
{
return _identity ?? _searchExternalConnection?.Id ?? GetExternalConnection(cmdlet, connection, accessToken)?.Id;
}

public Model.Graph.MicrosoftSearch.ExternalConnection GetExternalConnection(PSCmdlet cmdlet, PnPConnection connection, string accessToken)
{
if(_searchExternalConnection != null)
2 changes: 1 addition & 1 deletion src/Commands/Search/GetSearchExternalConnection.cs
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ namespace PnP.PowerShell.Commands.Search
[OutputType(typeof(Model.Graph.MicrosoftSearch.ExternalConnection))]
public class GetSearchExternalConnection : PnPGraphCmdlet
{
[Parameter(Mandatory = false)]
[Parameter(Mandatory = false, Position = 0, ValueFromPipeline = true)]
public string Identity;

protected override void ExecuteCmdlet()
8 changes: 4 additions & 4 deletions src/Commands/Search/GetSearchExternalItem.cs
Original file line number Diff line number Diff line change
@@ -25,7 +25,7 @@ public class GetSearchExternalItem : PnPGraphCmdlet

protected override void ExecuteCmdlet()
{
var externalConnection = ConnectionId.GetExternalConnection(this, Connection, AccessToken);
var externalConnectionId = ConnectionId.GetExternalConnectionId(this, Connection, AccessToken) ?? throw new PSArgumentException("No valid external connection specified", nameof(ConnectionId));

var searchQuery = new Model.Graph.MicrosoftSearch.SearchRequests
{
@@ -39,7 +39,7 @@ protected override void ExecuteCmdlet()
],
ContentSources =
[
$"/external/connections/{externalConnection.Id}"
$"/external/connections/{externalConnectionId}"
],
Query = new Model.Graph.MicrosoftSearch.SearchRequestQuery
{
@@ -59,11 +59,11 @@ protected override void ExecuteCmdlet()

if(hits == null || hits.Count == 0)
{
WriteVerbose($"No external items found{(ParameterSpecified(nameof(Identity)) ? $" with the identity '{Identity}'" : "")} on external connection '{externalConnection.Id}'");
WriteVerbose($"No external items found{(ParameterSpecified(nameof(Identity)) ? $" with the identity '{Identity}'" : "")} on external connection '{externalConnectionId}'");
return;
}

WriteVerbose($"Found {hits.Count} external item{(hits.Count != 1 ? "s" : "")}{(ParameterSpecified(nameof(Identity)) ? $" with the identity '{Identity}'" : "")} on external connection '{externalConnection.Id}'");
WriteVerbose($"Found {hits.Count} external item{(hits.Count != 1 ? "s" : "")}{(ParameterSpecified(nameof(Identity)) ? $" with the identity '{Identity}'" : "")} on external connection '{externalConnectionId}'");

var externalItems = hits.Select(s => new Model.Graph.MicrosoftSearch.ExternalItem {
Id = s.Resource.Properties["fileID"].ToString()[(s.Resource.Properties["fileID"].ToString().LastIndexOf(',') + 1)..],
4 changes: 2 additions & 2 deletions src/Commands/Search/GetSearchExternalSchema.cs
Original file line number Diff line number Diff line change
@@ -15,8 +15,8 @@ public class GetSearchExternalSchema : PnPGraphCmdlet

protected override void ExecuteCmdlet()
{
var searchExternalConnection = ConnectionId.GetExternalConnection(this, Connection, AccessToken);
var graphApiUrl = $"v1.0/external/connections/{searchExternalConnection.Id}/schema";
var externalConnectionId = ConnectionId.GetExternalConnectionId(this, Connection, AccessToken) ?? throw new PSArgumentException("No valid external connection specified", nameof(ConnectionId));
var graphApiUrl = $"v1.0/external/connections/{externalConnectionId}/schema";
var result = Utilities.REST.GraphHelper.Get<Model.Graph.MicrosoftSearch.ExternalSchema>(this, Connection, graphApiUrl, AccessToken, additionalHeaders: new System.Collections.Generic.Dictionary<string, string> { { "Prefer", "include-unknown-enum-members" } });
WriteObject(result, false);
}
4 changes: 2 additions & 2 deletions src/Commands/Search/RemoveSearchExternalConnection.cs
Original file line number Diff line number Diff line change
@@ -14,8 +14,8 @@ public class RemoveSearchExternalConnection : PnPGraphCmdlet

protected override void ExecuteCmdlet()
{
var externalConnection = Identity.GetExternalConnection(this, Connection, AccessToken);
Utilities.REST.GraphHelper.Delete(this, Connection, $"v1.0/external/connections/{externalConnection.Id}", AccessToken);
var externalConnectionId = Identity.GetExternalConnectionId(this, Connection, AccessToken) ?? throw new PSArgumentException("No valid external connection specified", nameof(Identity));
Utilities.REST.GraphHelper.Delete(this, Connection, $"v1.0/external/connections/{externalConnectionId}", AccessToken);
}
}
}
8 changes: 4 additions & 4 deletions src/Commands/Search/RemoveSearchExternalItem.cs
Original file line number Diff line number Diff line change
@@ -21,16 +21,16 @@ public class RemoveSearchExternalItem : PnPGraphCmdlet

protected override void ExecuteCmdlet()
{
var connection = ConnectionId.GetExternalConnection(this, Connection, AccessToken);
var externalConnectionId = ConnectionId.GetExternalConnectionId(this, Connection, AccessToken) ?? throw new PSArgumentException("No valid external connection specified", nameof(ConnectionId));

try
{
var response = GraphHelper.Delete(this, Connection, $"beta/external/connections/{connection.Id}/items/{ItemId}", AccessToken);
WriteVerbose($"External item with ID '{ItemId}' successfully removed from external connection '{connection.Id}'");
var response = GraphHelper.Delete(this, Connection, $"beta/external/connections/{externalConnectionId}/items/{ItemId}", AccessToken);
WriteVerbose($"External item with ID '{ItemId}' successfully removed from external connection '{externalConnectionId}'");
}
catch (PSInvalidOperationException ex)
{
throw new PSInvalidOperationException($"Removing external item with ID '{ItemId}' from external connection '{connection.Id}' failed with message '{ex.Message}'", ex);
throw new PSInvalidOperationException($"Removing external item with ID '{ItemId}' from external connection '{externalConnectionId}' failed with message '{ex.Message}'", ex);
}
}
}
4 changes: 2 additions & 2 deletions src/Commands/Search/SetSearchExternalConnection.cs
Original file line number Diff line number Diff line change
@@ -41,8 +41,8 @@ protected override void ExecuteCmdlet()
var jsonContent = JsonContent.Create(bodyContent, null, new JsonSerializerOptions { DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull });
WriteVerbose($"Constructed payload: {jsonContent.ReadAsStringAsync().GetAwaiter().GetResult()}");

var externalConnection = Identity.GetExternalConnection(this, Connection, AccessToken);
var graphApiUrl = $"v1.0/external/connections/{externalConnection.Id}";
var externalConnectionId = Identity.GetExternalConnectionId(this, Connection, AccessToken) ?? throw new PSArgumentException("No valid external connection specified", nameof(Identity));
var graphApiUrl = $"v1.0/external/connections/{externalConnectionId}";
Utilities.REST.GraphHelper.Patch(this, Connection, AccessToken, jsonContent, graphApiUrl);
}
}
4 changes: 2 additions & 2 deletions src/Commands/Search/SetSearchExternalItem.cs
Original file line number Diff line number Diff line change
@@ -105,8 +105,8 @@ protected override void ExecuteCmdlet()
var jsonContent = JsonContent.Create(bodyContent);
WriteVerbose($"Constructed payload: {jsonContent.ReadAsStringAsync().GetAwaiter().GetResult()}");

var searchExternalConnection = ConnectionId.GetExternalConnection(this, Connection, AccessToken);
var graphApiUrl = $"v1.0/external/connections/{searchExternalConnection.Id}/items/{ItemId}";
var externalConnectionId = ConnectionId.GetExternalConnectionId(this, Connection, AccessToken) ?? throw new PSArgumentException("No valid external connection specified", nameof(ConnectionId));
var graphApiUrl = $"v1.0/external/connections/{externalConnectionId}/items/{ItemId}";
var results = Utilities.REST.GraphHelper.Put<Model.Graph.MicrosoftSearch.ExternalItem>(this, Connection, graphApiUrl, AccessToken, jsonContent);
WriteObject(results, false);
}
4 changes: 2 additions & 2 deletions src/Commands/Search/SetSearchExternalSchema.cs
Original file line number Diff line number Diff line change
@@ -37,7 +37,7 @@ public class SetSearchExternalSchema : PnPGraphCmdlet

protected override void ExecuteCmdlet()
{
var searchExternalConnection = ConnectionId.GetExternalConnection(this, Connection, AccessToken);
var externalConnectionId = ConnectionId.GetExternalConnectionId(this, Connection, AccessToken) ?? throw new PSArgumentException("No valid external connection specified", nameof(ConnectionId));

switch(ParameterSetName)
{
@@ -53,7 +53,7 @@ protected override void ExecuteCmdlet()
var jsonContent = new StringContent(SchemaAsText);
WriteVerbose($"Constructed payload: {jsonContent.ReadAsStringAsync().GetAwaiter().GetResult()}");

var graphApiUrl = $"v1.0/external/connections/{searchExternalConnection.Id}/schema";
var graphApiUrl = $"v1.0/external/connections/{externalConnectionId}/schema";
var results = Utilities.REST.GraphHelper.Patch(this, Connection, AccessToken, jsonContent, graphApiUrl);

WriteVerbose("Trying to retrieve location header from response which can be used to poll for the status of the schema operation");