Skip to content
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

add metadata param for package publish #331

Merged
merged 3 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
9 changes: 7 additions & 2 deletions Meadow.CLI.Core/CloudServices/CollectionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@ public CollectionService(IConfiguration config, IdentityManager identityManager)
_identityManager = identityManager;
}

public async Task<List<Collection>> GetOrgCollections(string orgId, CancellationToken cancellationToken)
public async Task<List<Collection>> GetOrgCollections(string orgId, string host, CancellationToken cancellationToken)
{
if (string.IsNullOrEmpty(host))
{
host = _config["meadowCloudHost"];
}

var authToken = await _identityManager.GetAccessToken(cancellationToken).ConfigureAwait(false);
if (string.IsNullOrEmpty(authToken))
{
Expand All @@ -36,7 +41,7 @@ public async Task<List<Collection>> GetOrgCollections(string orgId, Cancellation
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authToken);

var result = await httpClient.GetStringAsync($"{_config["meadowCloudHost"]}/api/orgs/{orgId}/collections");
var result = await httpClient.GetStringAsync($"{host}/api/orgs/{orgId}/collections");
return JsonSerializer.Deserialize<List<Collection>>(result);
}
}
9 changes: 7 additions & 2 deletions Meadow.CLI.Core/CloudServices/DeviceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ public DeviceService(IConfiguration config, IdentityManager identityManager) : b
_config = config;
}

public async Task<(bool isSuccess, string message)> AddDevice(string orgId, string id, string publicKey, string collectionId)
public async Task<(bool isSuccess, string message)> AddDevice(string orgId, string id, string publicKey, string collectionId, string host)
{
if (string.IsNullOrEmpty(host))
{
host = _config["meadowCloudHost"];
}

var httpClient = await AuthenticatedHttpClient();

dynamic payload = new
Expand All @@ -34,7 +39,7 @@ public DeviceService(IConfiguration config, IdentityManager identityManager) : b
var json = JsonSerializer.Serialize<dynamic>(payload);

var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync($"{_config["meadowCloudHost"]}/api/devices", content);
var response = await httpClient.PostAsync($"{host}/api/devices", content);

if (response.IsSuccessStatusCode)
{
Expand Down
31 changes: 24 additions & 7 deletions Meadow.CLI.Core/CloudServices/PackageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,20 @@ public class PackageService : CloudServiceBase
{
IConfiguration _config;
IdentityManager _identityManager;

public PackageService(IConfiguration config, IdentityManager identityManager) : base(identityManager)
{
_config = config;
_identityManager = identityManager;
}

public async Task<Package> UploadPackage(string mpakPath, string orgId, string description, CancellationToken cancellationToken)
public async Task<Package> UploadPackage(string mpakPath, string orgId, string description, string host, CancellationToken cancellationToken)
{
if (string.IsNullOrEmpty(host))
{
host = _config["meadowCloudHost"];
lamebrain marked this conversation as resolved.
Show resolved Hide resolved
}

if (!File.Exists(mpakPath))
{
throw new ArgumentException($"Invalid path: {mpakPath}");
Expand All @@ -54,7 +59,7 @@ public async Task<Package> UploadPackage(string mpakPath, string orgId, string d
multipartFormContent.Add(fileStreamContent, name: "file", fileName: fi.Name);
multipartFormContent.Add(new StringContent(json), "json");

var response = await httpClient.PostAsync($"{_config["meadowCloudHost"]}/api/packages", multipartFormContent);
var response = await httpClient.PostAsync($"{host}/api/packages", multipartFormContent);
if (response.IsSuccessStatusCode)
{
var package = JsonSerializer.Deserialize<Package>(await response.Content.ReadAsStringAsync());
Expand All @@ -68,8 +73,13 @@ public async Task<Package> UploadPackage(string mpakPath, string orgId, string d
}
}

public async Task PublishPackage(string packageId, string collectionId, CancellationToken cancellationToken)
public async Task PublishPackage(string packageId, string collectionId, string metadata, string host, CancellationToken cancellationToken)
{
if (string.IsNullOrEmpty(host))
{
host = _config["meadowCloudHost"];
}

var authToken = await _identityManager.GetAccessToken(cancellationToken).ConfigureAwait(false);
if (string.IsNullOrEmpty(authToken))
{
Expand All @@ -79,7 +89,9 @@ public async Task PublishPackage(string packageId, string collectionId, Cancella
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authToken);

var response = await httpClient.PostAsync($"{_config["meadowCloudHost"]}/api/packages/{packageId}/publish/{collectionId}", null);
var payload = new { metadata, collectionId };
var content = new StringContent(JsonSerializer.Serialize(payload), Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync($"{host}/api/packages/{packageId}/publish", content);

if (!response.IsSuccessStatusCode)
{
Expand All @@ -88,8 +100,13 @@ public async Task PublishPackage(string packageId, string collectionId, Cancella
}
}

public async Task<List<Package>> GetOrgPackages(string orgId, CancellationToken cancellationToken)
public async Task<List<Package>> GetOrgPackages(string orgId, string host, CancellationToken cancellationToken)
{
if (string.IsNullOrEmpty(host))
{
host = _config["meadowCloudHost"];
}

var authToken = await _identityManager.GetAccessToken(cancellationToken).ConfigureAwait(false);
if (string.IsNullOrEmpty(authToken))
{
Expand All @@ -99,7 +116,7 @@ public async Task<List<Package>> GetOrgPackages(string orgId, CancellationToken
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authToken);

var result = await httpClient.GetStringAsync($"{_config["meadowCloudHost"]}/api/orgs/{orgId}/packages");
var result = await httpClient.GetStringAsync($"{host}/api/orgs/{orgId}/packages");
return JsonSerializer.Deserialize<List<Package>>(result);
}
}
Expand Down
9 changes: 7 additions & 2 deletions Meadow.CLI.Core/CloudServices/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@ public UserService(IConfiguration config, IdentityManager identityManager) : bas
_config = config;
}

public async Task<List<UserOrg>> GetUserOrgs(CancellationToken cancellationToken)
public async Task<List<UserOrg>> GetUserOrgs(string host, CancellationToken cancellationToken)
{
if (string.IsNullOrEmpty(host))
{
host = _config["meadowCloudHost"];
}

var httpClient = await AuthenticatedHttpClient();

var response = await httpClient.GetAsync($"{_config["meadowCloudHost"]}/api/users/me/orgs");
var response = await httpClient.GetAsync($"{host}/api/users/me/orgs");

if (response.IsSuccessStatusCode)
{
Expand Down
8 changes: 5 additions & 3 deletions Meadow.CLI/Commands/Cloud/Collection/ListCollectionCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public ListCommand(ILoggerFactory loggerFactory, UserService userService, Collec

[CommandOption("orgId", 'o', Description = "Organization Id", IsRequired = false)]
public string OrgId { get; set; }
[CommandOption("host", Description = "Optionally set a host (default is https://www.meadowcloud.co)", IsRequired = false)]
public string Host { get; set; }

public async ValueTask ExecuteAsync(IConsole console)
{
Expand All @@ -40,7 +42,7 @@ public async ValueTask ExecuteAsync(IConsole console)

try
{
var userOrgs = await _userService.GetUserOrgs(cancellationToken).ConfigureAwait(false);
var userOrgs = await _userService.GetUserOrgs(Host, cancellationToken).ConfigureAwait(false);
if (!userOrgs.Any())
{
_logger.LogInformation($"Please visit {_config["meadowCloudHost"]} to register your account.");
Expand All @@ -67,8 +69,8 @@ public async ValueTask ExecuteAsync(IConsole console)
_logger.LogInformation($"You must be signed in to execute this command.");
return;
}

var collections = await _collectionService.GetOrgCollections(OrgId, cancellationToken);
var collections = await _collectionService.GetOrgCollections(OrgId, Host, cancellationToken);

if (collections == null || collections.Count() == 0)
{
Expand Down
10 changes: 6 additions & 4 deletions Meadow.CLI/Commands/Cloud/Package/ListPackagesCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ public ListCommand(ILoggerFactory loggerFactory, UserService userService, Packag

[CommandOption("orgId", 'o', Description = "Organization Id", IsRequired = false)]
public string OrgId { get; set; }

[CommandOption("host", Description = "Optionally set a host (default is https://www.meadowcloud.co)", IsRequired = false)]
public string Host { get; set; }

public async ValueTask ExecuteAsync(IConsole console)
{
var cancellationToken = console.RegisterCancellationHandler();
Expand All @@ -49,7 +51,7 @@ public async ValueTask ExecuteAsync(IConsole console)

try
{
var userOrgs = await _userService.GetUserOrgs(cancellationToken).ConfigureAwait(false);
var userOrgs = await _userService.GetUserOrgs(Host, cancellationToken).ConfigureAwait(false);
if (!userOrgs.Any())
{
_logger.LogInformation($"Please visit {_config["meadowCloudHost"]} to register your account.");
Expand Down Expand Up @@ -77,8 +79,8 @@ public async ValueTask ExecuteAsync(IConsole console)
_logger.LogInformation($"You must be signed in to execute this command.");
return;
}

var packages = await _packageService.GetOrgPackages(OrgId, cancellationToken);
var packages = await _packageService.GetOrgPackages(OrgId, Host, cancellationToken);

if(packages == null || packages.Count() == 0)
{
Expand Down
6 changes: 5 additions & 1 deletion Meadow.CLI/Commands/Cloud/Package/PublishPackageCommend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public PublishCommand(ILoggerFactory loggerFactory, PackageService packageServic
public string PackageId { get; init; }
[CommandOption("collectionId", 'c', Description = "The target collection for publishing", IsRequired = true)]
public string CollectionId { get; set; }
[CommandOption("metadata", 'm', Description = "Pass through metadata", IsRequired = false)]
public string Metadata { get; set; }
[CommandOption("host", Description = "Optionally set a host (default is https://www.meadowcloud.co)", IsRequired = false)]
public string Host { get; set; }

public async ValueTask ExecuteAsync(IConsole console)
{
Expand All @@ -45,7 +49,7 @@ public async ValueTask ExecuteAsync(IConsole console)

try
{
await _packageService.PublishPackage(PackageId, CollectionId, cancellationToken);
await _packageService.PublishPackage(PackageId, CollectionId, Metadata, Host, cancellationToken);
_logger.LogInformation("Publish successful.");
}
catch(MeadowCloudException mex)
Expand Down
7 changes: 4 additions & 3 deletions Meadow.CLI/Commands/Cloud/Package/UploadPackageCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public UploadCommand(ILoggerFactory loggerFactory, UserService userService, Pack

[CommandOption("description", 'd', Description = "Description of the package", IsRequired = false)]
public string Description { get; set; }

[CommandOption("host", Description = "Optionally set a host (default is https://www.meadowcloud.co)", IsRequired = false)]
public string Host { get; set; }
public async ValueTask ExecuteAsync(IConsole console)
{
var cancellationToken = console.RegisterCancellationHandler();
Expand All @@ -55,7 +56,7 @@ public async ValueTask ExecuteAsync(IConsole console)

try
{
var userOrgs = await _userService.GetUserOrgs(cancellationToken).ConfigureAwait(false);
var userOrgs = await _userService.GetUserOrgs(Host, cancellationToken).ConfigureAwait(false);
if (!userOrgs.Any())
{
_logger.LogInformation($"Please visit {_config["meadowCloudHost"]} to register your account.");
Expand Down Expand Up @@ -86,7 +87,7 @@ public async ValueTask ExecuteAsync(IConsole console)

try
{
var package = await _packageService.UploadPackage(MpakPath, OrgId, Description, cancellationToken);
var package = await _packageService.UploadPackage(MpakPath, OrgId, Description, Host, cancellationToken);
_logger.LogInformation($"Upload complete. Package Id: {package.Id}");
}
catch (MeadowCloudException mex)
Expand Down
11 changes: 7 additions & 4 deletions Meadow.CLI/Commands/DeviceManagement/ProvisionDeviceCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,17 @@ public ProvisionDeviceCommand(DownloadManager downloadManager, ILoggerFactory lo
public string OrgId { get; set; }
[CommandOption("collectionId", 'c', Description = "The target collection for device registration", IsRequired = false)]
public string CollectionId { get; set; }

[CommandOption("host", 'h', Description = "Optionally set a host (default is https://www.meadowcloud.co)", IsRequired = false)]
public string Host { get; set; }

public override async ValueTask ExecuteAsync(IConsole console)
{
await base.ExecuteAsync(console);
var cancellationToken = console.RegisterCancellationHandler();

try
{
var userOrgs = await _userService.GetUserOrgs(cancellationToken).ConfigureAwait(false);
var userOrgs = await _userService.GetUserOrgs(Host, cancellationToken).ConfigureAwait(false);
if (!userOrgs.Any())
{
_logger.LogInformation($"Please visit {_config["meadowCloudHost"]} to register your account.");
Expand Down Expand Up @@ -76,12 +78,13 @@ public override async ValueTask ExecuteAsync(IConsole console)
return;
}


using var device = await MeadowDeviceManager.GetMeadowForSerialPort(this.SerialPortName, true, _logger);
var publicKey = await device.CloudRegisterDevice(cancellationToken);
var delim = "-----END PUBLIC KEY-----\n";
publicKey = publicKey.Substring(0, publicKey.IndexOf(delim) + delim.Length);

var result = await _deviceService.AddDevice(OrgId, device.DeviceInfo.ProcessorId, publicKey, CollectionId);
var result = await _deviceService.AddDevice(OrgId, device.DeviceInfo.ProcessorId, publicKey, CollectionId, Host);

if (result.isSuccess)
{
Expand Down
6 changes: 3 additions & 3 deletions Meadow.CLI/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"DeviceProvision": {
"commandName": "Project",
"commandLineArgs": "device provision"
"commandLineArgs": "device provision -o 5b1d2b0dab744a04b79b245d881e18b8"
},
"DeviceInfo": {
"commandName": "Project",
Expand Down Expand Up @@ -82,11 +82,11 @@
},
"Package List": {
"commandName": "Project",
"commandLineArgs": "package list"
"commandLineArgs": "package list -o 5b1d2b0dab744a04b79b245d881e18b8"
},
"Package Publish": {
"commandName": "Project",
"commandLineArgs": "package publish -p 485f06def49740ea9615208ba6107d2a -c 4965ba4d6b504c879928494e9bbe415d"
"commandLineArgs": "package publish -p 7aedf4b3e304482e97f2a43041ebc30b -c 4965ba4d6b504c879928494e9bbe415d -m \"this is my metadata\""
},
"Package Upload": {
"commandName": "Project",
Expand Down