-
Notifications
You must be signed in to change notification settings - Fork 458
/
WorkloadClient.cs
73 lines (56 loc) · 3.68 KB
/
WorkloadClient.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
// Copyright (c) Microsoft. All rights reserved.
namespace Microsoft.Azure.Devices.Edge.Util.Edged
{
using System;
using System.Threading.Tasks;
public class WorkloadClient
{
readonly WorkloadClientVersioned inner;
public WorkloadClient(Uri serverUri, string serverSupportedApiVersion, string clientSupportedApiVersion, string moduleId, string moduleGenerationId)
{
Preconditions.CheckNotNull(serverUri, nameof(serverUri));
Preconditions.CheckNonWhiteSpace(serverSupportedApiVersion, nameof(serverSupportedApiVersion));
Preconditions.CheckNonWhiteSpace(clientSupportedApiVersion, nameof(clientSupportedApiVersion));
Preconditions.CheckNonWhiteSpace(moduleId, nameof(moduleId));
Preconditions.CheckNonWhiteSpace(moduleGenerationId, nameof(moduleGenerationId));
this.inner = this.GetVersionedWorkloadClient(serverUri, serverSupportedApiVersion, clientSupportedApiVersion, moduleId, moduleGenerationId);
}
public Task<ServerCertificateResponse> CreateServerCertificateAsync(string hostname, DateTime expiration) => this.inner.CreateServerCertificateAsync(hostname, expiration);
public Task<string> GetTrustBundleAsync() => this.inner.GetTrustBundleAsync();
public Task<string> GetManifestTrustBundleAsync() => this.inner.GetManifestTrustBundleAsync();
public Task<string> EncryptAsync(string initializationVector, string plainText) => this.inner.EncryptAsync(initializationVector, plainText);
public Task<string> DecryptAsync(string initializationVector, string encryptedText) => this.inner.DecryptAsync(initializationVector, encryptedText);
public Task<string> SignAsync(string keyId, string algorithm, string data) => this.inner.SignAsync(keyId, algorithm, data);
internal WorkloadClientVersioned GetVersionedWorkloadClient(Uri workloadUri, string serverSupportedApiVersion, string clientSupportedApiVersion, string moduleId, string moduleGenerationId)
{
ApiVersion supportedVersion = this.GetSupportedVersion(serverSupportedApiVersion, clientSupportedApiVersion);
if (supportedVersion == ApiVersion.Version20180628)
{
return new Version_2018_06_28.WorkloadClient(workloadUri, supportedVersion, moduleId, moduleGenerationId);
}
if (supportedVersion.CompareTo(ApiVersion.Version20190130) >= 0)
{
return new Version_2019_01_30.WorkloadClient(workloadUri, supportedVersion, moduleId, moduleGenerationId);
}
if (supportedVersion == ApiVersion.Version20200707)
{
return new Version_2020_07_07.WorkloadClient(workloadUri, supportedVersion, moduleId, moduleGenerationId);
}
return new Version_2018_06_28.WorkloadClient(workloadUri, supportedVersion, moduleId, moduleGenerationId);
}
ApiVersion GetSupportedVersion(string serverSupportedApiVersion, string clientSupportedApiVersion)
{
var serverVersion = ApiVersion.ParseVersion(serverSupportedApiVersion);
var clientVersion = ApiVersion.ParseVersion(clientSupportedApiVersion);
if (clientVersion == ApiVersion.VersionUnknown)
{
throw new InvalidOperationException($"Client version {clientSupportedApiVersion} is not supported.");
}
if (serverVersion == ApiVersion.VersionUnknown)
{
return clientVersion;
}
return serverVersion.Value < clientVersion.Value ? serverVersion : clientVersion;
}
}
}