Skip to content

Commit c75562c

Browse files
Optimize search external cmdlets by not always retrieving the ExternalConnection (#4556)
* Changing retrieval of ExternalConnection through the pipebind, which adds an extra call and requires ExternalConnection permissions * Applied optimization to more places --------- Co-authored-by: Gautam Sheth <gautamdsheth@outlook.com>
1 parent 3f5ae44 commit c75562c

9 files changed

+24
-19
lines changed

src/Commands/Base/PipeBinds/SearchExternalConnectionPipeBind.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ public SearchExternalConnectionPipeBind(string identity)
2323
_identity = identity;
2424
}
2525

26+
public string GetExternalConnectionId(PSCmdlet cmdlet, PnPConnection connection, string accessToken)
27+
{
28+
return _identity ?? _searchExternalConnection?.Id ?? GetExternalConnection(cmdlet, connection, accessToken)?.Id;
29+
}
30+
2631
public Model.Graph.MicrosoftSearch.ExternalConnection GetExternalConnection(PSCmdlet cmdlet, PnPConnection connection, string accessToken)
2732
{
2833
if(_searchExternalConnection != null)

src/Commands/Search/GetSearchExternalConnection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace PnP.PowerShell.Commands.Search
1313
[OutputType(typeof(Model.Graph.MicrosoftSearch.ExternalConnection))]
1414
public class GetSearchExternalConnection : PnPGraphCmdlet
1515
{
16-
[Parameter(Mandatory = false)]
16+
[Parameter(Mandatory = false, Position = 0, ValueFromPipeline = true)]
1717
public string Identity;
1818

1919
protected override void ExecuteCmdlet()

src/Commands/Search/GetSearchExternalItem.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class GetSearchExternalItem : PnPGraphCmdlet
2525

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

3030
var searchQuery = new Model.Graph.MicrosoftSearch.SearchRequests
3131
{
@@ -39,7 +39,7 @@ protected override void ExecuteCmdlet()
3939
],
4040
ContentSources =
4141
[
42-
$"/external/connections/{externalConnection.Id}"
42+
$"/external/connections/{externalConnectionId}"
4343
],
4444
Query = new Model.Graph.MicrosoftSearch.SearchRequestQuery
4545
{
@@ -59,11 +59,11 @@ protected override void ExecuteCmdlet()
5959

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

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

6868
var externalItems = hits.Select(s => new Model.Graph.MicrosoftSearch.ExternalItem {
6969
Id = s.Resource.Properties["fileID"].ToString()[(s.Resource.Properties["fileID"].ToString().LastIndexOf(',') + 1)..],

src/Commands/Search/GetSearchExternalSchema.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ public class GetSearchExternalSchema : PnPGraphCmdlet
1515

1616
protected override void ExecuteCmdlet()
1717
{
18-
var searchExternalConnection = ConnectionId.GetExternalConnection(this, Connection, AccessToken);
19-
var graphApiUrl = $"v1.0/external/connections/{searchExternalConnection.Id}/schema";
18+
var externalConnectionId = ConnectionId.GetExternalConnectionId(this, Connection, AccessToken) ?? throw new PSArgumentException("No valid external connection specified", nameof(ConnectionId));
19+
var graphApiUrl = $"v1.0/external/connections/{externalConnectionId}/schema";
2020
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" } });
2121
WriteObject(result, false);
2222
}

src/Commands/Search/RemoveSearchExternalConnection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public class RemoveSearchExternalConnection : PnPGraphCmdlet
1414

1515
protected override void ExecuteCmdlet()
1616
{
17-
var externalConnection = Identity.GetExternalConnection(this, Connection, AccessToken);
18-
Utilities.REST.GraphHelper.Delete(this, Connection, $"v1.0/external/connections/{externalConnection.Id}", AccessToken);
17+
var externalConnectionId = Identity.GetExternalConnectionId(this, Connection, AccessToken) ?? throw new PSArgumentException("No valid external connection specified", nameof(Identity));
18+
Utilities.REST.GraphHelper.Delete(this, Connection, $"v1.0/external/connections/{externalConnectionId}", AccessToken);
1919
}
2020
}
2121
}

src/Commands/Search/RemoveSearchExternalItem.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ public class RemoveSearchExternalItem : PnPGraphCmdlet
2121

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

2626
try
2727
{
28-
var response = GraphHelper.Delete(this, Connection, $"beta/external/connections/{connection.Id}/items/{ItemId}", AccessToken);
29-
WriteVerbose($"External item with ID '{ItemId}' successfully removed from external connection '{connection.Id}'");
28+
var response = GraphHelper.Delete(this, Connection, $"beta/external/connections/{externalConnectionId}/items/{ItemId}", AccessToken);
29+
WriteVerbose($"External item with ID '{ItemId}' successfully removed from external connection '{externalConnectionId}'");
3030
}
3131
catch (PSInvalidOperationException ex)
3232
{
33-
throw new PSInvalidOperationException($"Removing external item with ID '{ItemId}' from external connection '{connection.Id}' failed with message '{ex.Message}'", ex);
33+
throw new PSInvalidOperationException($"Removing external item with ID '{ItemId}' from external connection '{externalConnectionId}' failed with message '{ex.Message}'", ex);
3434
}
3535
}
3636
}

src/Commands/Search/SetSearchExternalConnection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ protected override void ExecuteCmdlet()
4141
var jsonContent = JsonContent.Create(bodyContent, null, new JsonSerializerOptions { DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull });
4242
WriteVerbose($"Constructed payload: {jsonContent.ReadAsStringAsync().GetAwaiter().GetResult()}");
4343

44-
var externalConnection = Identity.GetExternalConnection(this, Connection, AccessToken);
45-
var graphApiUrl = $"v1.0/external/connections/{externalConnection.Id}";
44+
var externalConnectionId = Identity.GetExternalConnectionId(this, Connection, AccessToken) ?? throw new PSArgumentException("No valid external connection specified", nameof(Identity));
45+
var graphApiUrl = $"v1.0/external/connections/{externalConnectionId}";
4646
Utilities.REST.GraphHelper.Patch(this, Connection, AccessToken, jsonContent, graphApiUrl);
4747
}
4848
}

src/Commands/Search/SetSearchExternalItem.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ protected override void ExecuteCmdlet()
105105
var jsonContent = JsonContent.Create(bodyContent);
106106
WriteVerbose($"Constructed payload: {jsonContent.ReadAsStringAsync().GetAwaiter().GetResult()}");
107107

108-
var searchExternalConnection = ConnectionId.GetExternalConnection(this, Connection, AccessToken);
109-
var graphApiUrl = $"v1.0/external/connections/{searchExternalConnection.Id}/items/{ItemId}";
108+
var externalConnectionId = ConnectionId.GetExternalConnectionId(this, Connection, AccessToken) ?? throw new PSArgumentException("No valid external connection specified", nameof(ConnectionId));
109+
var graphApiUrl = $"v1.0/external/connections/{externalConnectionId}/items/{ItemId}";
110110
var results = Utilities.REST.GraphHelper.Put<Model.Graph.MicrosoftSearch.ExternalItem>(this, Connection, graphApiUrl, AccessToken, jsonContent);
111111
WriteObject(results, false);
112112
}

src/Commands/Search/SetSearchExternalSchema.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class SetSearchExternalSchema : PnPGraphCmdlet
3737

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

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

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

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

0 commit comments

Comments
 (0)