forked from clemensv/cloudevents-registry-azfn-cosmosdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Import.cs
91 lines (83 loc) · 3.26 KB
/
Import.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
using Azure.CloudEvents.Discovery;
using Azure.CloudEvents.Discovery.SystemTopicLoader;
using McMaster.Extensions.CommandLineUtils;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
namespace azcedisco
{
[Command()]
class Import : CommonOptions
{
public virtual async Task<int> OnExecuteAsync(CommandLineApplication app)
{
var cred = new AzureIdentityCredentialAdapter();
var rte = new ResourceTopicEnumerator(this.SubscriptionId, cred);
if (!this.DiscoveryEndpoint.EndsWith("/"))
{
this.DiscoveryEndpoint += "/";
}
var httpClient = new HttpClient();
if (!string.IsNullOrEmpty(this.FunctionsKey))
{
httpClient.DefaultRequestHeaders.Add("x-functions-key", this.FunctionsKey);
}
DiscoveryClient client = new DiscoveryClient(httpClient);
client.BaseUrl = this.DiscoveryEndpoint;
await foreach (var group in rte.EnumerateSystemDefinitionGroups(new Uri(this.DiscoveryEndpoint)))
{
group.Version = DateTime.UtcNow.ToFileTimeUtc();
Group createdGroup = null;
try
{
createdGroup = await client.PutGroupAsync(group, group.Id);
}
catch (ApiException apiException)
{
if (apiException.StatusCode != 409)
{
Console.WriteLine(apiException.Message);
}
}
Console.WriteLine(JsonConvert.SerializeObject(group, Formatting.Indented));
}
await foreach (var group in rte.EnumerateSystemDefinitionSchemaGroups(new Uri(this.DiscoveryEndpoint)))
{
group.Version = DateTime.UtcNow.ToFileTimeUtc();
SchemaGroup createdGroup = null;
try
{
createdGroup = await client.PutSchemaGroupAsync(group, group.Id);
}
catch (ApiException apiException)
{
if (apiException.StatusCode != 409)
{
Console.WriteLine(apiException.Message);
}
}
Console.WriteLine(JsonConvert.SerializeObject(group, Formatting.Indented));
}
await foreach (var endpoint in rte.EnumerateDiscoveryServicesAsync(new Uri(this.DiscoveryEndpoint), this.ResourceGroupName))
{
endpoint.Version = DateTime.UtcNow.ToFileTimeUtc();
Endpoint createdService = null;
try
{
createdService = await client.PutEndpointAsync(endpoint, endpoint.Id);
}
catch (ApiException apiException)
{
if (apiException.StatusCode != 409)
{
Console.WriteLine(apiException.Message);
}
}
Console.WriteLine(JsonConvert.SerializeObject(endpoint, Formatting.Indented));
}
return 0;
}
}
}