forked from oracle/oci-dotnet-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResourceSearchExample.cs
115 lines (96 loc) · 4.07 KB
/
ResourceSearchExample.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
* Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
* This software is dual-licensed to you under the Universal Permissive License (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose either license.
*/
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Oci.ResourcesearchService;
using Oci.ResourcesearchService.Models;
using Oci.ResourcesearchService.Requests;
using Oci.ResourcesearchService.Responses;
using Oci.Common;
using Oci.Common.Auth;
using Oci.Common.Retry;
namespace Oci.Examples
{
/**
* This class provides an example on how to use resource-search in the .NET SDK to:
* 1. List searchable types.
* 2. Get detail of searchable type.
* 3. Search resources cross type.
*/
public class ResourcesearchExample
{
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
public static async Task MainResourcesearch()
{
logger.Info("Starting example");
var provider = new ConfigFileAuthenticationDetailsProvider("DEFAULT");
var client = new ResourceSearchClient(provider);
logger.Info("List all resource types");
await ListTypes(client);
logger.Info("Get Group type details");
await GetTypeDetails(client, "Group");
logger.Info("Search for resource by freetext search");
await FreeTextSearch(client, "admin");
logger.Info("Search for resource by structed query search");
await StructuredQuerySearch(client, "query all resources");
logger.Info("End example");
}
private static async Task ListTypes(ResourceSearchClient client)
{
var listResourceTypesRequest = new ListResourceTypesRequest { };
var listResourceTypesResponse = await client.ListResourceTypes(listResourceTypesRequest);
foreach (var type in listResourceTypesResponse.Items)
{
logger.Info($"Resource: {type.Name}");
}
}
private static async Task GetTypeDetails(ResourceSearchClient client, string typeName)
{
var getResourceTypeRequest = new GetResourceTypeRequest
{
Name = typeName
};
var getResourceTypeResponse = await client.GetResourceType(getResourceTypeRequest);
foreach (var field in getResourceTypeResponse.ResourceType.Fields)
{
logger.Info($"FieldName: {field.FieldName}");
}
}
private static async Task FreeTextSearch(ResourceSearchClient client, string text)
{
var searchResourcesRequest = new SearchResourcesRequest
{
SearchDetails = new FreeTextSearchDetails
{
Text = text,
MatchingContextType = SearchDetails.MatchingContextTypeEnum.Highlights
}
};
var searchResourcesResponse = await client.SearchResources(searchResourcesRequest);
foreach (var resource in searchResourcesResponse.ResourceSummaryCollection.Items)
{
logger.Info($"Resource: {resource.DisplayName}");
}
}
private static async Task StructuredQuerySearch(ResourceSearchClient client, string query)
{
var searchResourcesRequest = new SearchResourcesRequest
{
SearchDetails = new StructuredSearchDetails
{
MatchingContextType = SearchDetails.MatchingContextTypeEnum.Highlights,
Query = query
}
};
var searchResourcesResponse = await client.SearchResources(searchResourcesRequest);
foreach (var resource in searchResourcesResponse.ResourceSummaryCollection.Items)
{
logger.Info($"Resource: {resource.DisplayName}");
}
}
}
}