Exemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the Sample class.
        /// </summary>
        /// <exception cref="ArgumentNullException">Thrown when one or more required arguments are null.</exception>
        /// <param name="tenantId">Identifier for the tenant.</param>
        /// <param name="clientId">Identifier for the client.</param>
        /// <param name="clientSecret">The client secret.</param>
        /// <param name="accountEndpoint">The account endpoint.</param>
        /// <param name="instance">The instance.</param>
        /// <param name="connectionString">The connection string.</param>
        /// <param name="deviceId">Identifier for the device.</param>
        /// <param name="deviceTag">Device tag.</param>
        /// <param name="delete">Boolean flag to indicate whether the update should be deleted when finished.</param>
        public Sample(string tenantId, string clientId, string clientSecret, string accountEndpoint, string instance, string connectionString, string deviceId, string deviceTag, bool delete)
        {
            if (tenantId == null)
            {
                throw new ArgumentNullException(nameof(tenantId));
            }
            if (clientId == null)
            {
                throw new ArgumentNullException(nameof(clientId));
            }
            if (clientSecret == null)
            {
                throw new ArgumentNullException(nameof(clientSecret));
            }

            _accountEndpoint  = accountEndpoint ?? throw new ArgumentNullException(nameof(accountEndpoint));
            _instanceId       = instance ?? throw new ArgumentNullException(nameof(instance));
            _connectionString = connectionString ?? throw new ArgumentNullException(nameof(connectionString));
            _deviceId         = deviceId ?? throw new ArgumentNullException(nameof(deviceId));
            _deviceTag        = deviceTag ?? throw new ArgumentNullException(nameof(deviceTag));
            _delete           = delete;

            var credentials = new ClientSecretCredential(tenantId, clientId, clientSecret);

            _updatesClient    = new DeviceUpdateClient(accountEndpoint, instance, credentials);
            _managementClient = new DeviceManagementClient(accountEndpoint, instance, credentials);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Device Update for IoT Hub Sample: Enumerate updates
        /// </summary>
        static async Task Main()
        {
            Console.WriteLine("Device Update for IoT Hub Sample: Enumerate updates");
            Console.WriteLine();

            var credentials = new InteractiveBrowserCredential(Constant.TenantId, Constant.ClientId);
            var client      = new DeviceUpdateClient(Constant.AccountEndpoint, Constant.Instance, credentials);

            Console.WriteLine($"Provider: {Constant.Provider}");
            Console.WriteLine($"Name    : {Constant.Name}");
            Console.WriteLine($"Versions: ");
            try
            {
                var response = client.GetVersionsAsync(Constant.Provider, Constant.Name);
                await foreach (var version in response)
                {
                    var versionDoc = JsonDocument.Parse(version.ToMemory());
                    Console.WriteLine("\t" + versionDoc.RootElement.GetString());
                }
            }
            catch (RequestFailedException e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
        public async Task AccountCrudTests()
        {
            using MockContext context = MockContext.Start(GetType());
            DeviceUpdateClient client = CreateDeviceUpdateClient(context);

            // 1. Create Account

            Account account = new Account(this.Location);

            account = await client.Accounts.CreateAsync(ResourceGroupName, AccountName, account, this.CancellationToken);

            Assert.NotNull(account);

            // 2. Get Account

            account = await client.Accounts.GetAsync(ResourceGroupName, AccountName, this.CancellationToken);

            Assert.NotNull(account);

            // 3. List Accounts by Resource Group

            IPage <Account> accounts = await client.Accounts.ListByResourceGroupAsync(ResourceGroupName, this.CancellationToken);

            Assert.NotEmpty(accounts);

            // 4. Delete Account

            await client.Accounts.DeleteAsync(ResourceGroupName, AccountName, this.CancellationToken);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Device Update for IoT Hub Sample: Delete update version
        /// </summary>
        /// <param name="updateVersion">Update version to delete.</param>
        static async Task Main(string updateVersion)
        {
            Console.WriteLine("Device Update for IoT Hub Sample: Delete update version");
            Console.WriteLine();

            if (string.IsNullOrWhiteSpace(updateVersion))
            {
                throw new ArgumentException("You have to provider a valid update version.");
            }

            var credentials = new InteractiveBrowserCredential(Constant.TenantId, Constant.ClientId);
            var client      = new DeviceUpdateClient(Constant.AccountEndpoint, Constant.Instance, credentials);

            Console.WriteLine("Deleting update:");
            Console.WriteLine($"    Provider: {Constant.Provider}");
            Console.WriteLine($"    Name    : {Constant.Name}");
            Console.WriteLine($"    Version : {updateVersion}");

            try
            {
                var response = await client.DeleteUpdateAsync(true, Constant.Provider, Constant.Name, updateVersion);

                var doc = JsonDocument.Parse(response.Value.ToMemory());
                Console.WriteLine(doc.RootElement.GetProperty("status").ToString());
            }
            catch (RequestFailedException e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Device Update for IoT Hub Sample: Get update version
        /// </summary>
        /// <param name="updateVersion">Update version to retrieve.</param>
        static async Task Main(string updateVersion)
        {
            Console.WriteLine("Device Update for IoT Hub Sample: Get update version");
            Console.WriteLine();

            if (string.IsNullOrWhiteSpace(updateVersion))
            {
                throw new ArgumentException("You have to provider a valid update version.");
            }

            var credentials = new InteractiveBrowserCredential(Constant.TenantId, Constant.ClientId);
            var client      = new DeviceUpdateClient(Constant.AccountEndpoint, Constant.Instance, credentials);

            Console.WriteLine("Retrieve update:");
            Console.WriteLine($"    Provider: {Constant.Provider}");
            Console.WriteLine($"    Name    : {Constant.Name}");
            Console.WriteLine($"    Version : {updateVersion}");

            Console.WriteLine();
            Console.WriteLine("Update content:");
            try
            {
                var response = await client.GetUpdateAsync(Constant.Provider, Constant.Name, updateVersion);

                Console.WriteLine(response.Content.ToString());
            }
            catch (RequestFailedException e)
            {
                Console.WriteLine(e);
                throw;
            }

            Console.WriteLine();
            Console.WriteLine("Update files:");
            try
            {
                var files = client.GetFilesAsync(Constant.Provider, Constant.Name, updateVersion);
                await foreach (var fileItem in files)
                {
                    var doc  = JsonDocument.Parse(fileItem.ToMemory());
                    var file = await client.GetFileAsync(Constant.Provider, Constant.Name, updateVersion, doc.RootElement.GetString());

                    Console.WriteLine(file.Content.ToString());
                }
            }
            catch (RequestFailedException e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
Exemplo n.º 6
0
        public async Task InstanceCrudTests()
        {
            using MockContext context = MockContext.Start(GetType());
            DeviceUpdateClient client = CreateDeviceUpdateClient(context);

            // 1. Create Account

            Account account = new Account(this.Location);

            account = await client.Accounts.CreateAsync(ResourceGroupName, AccountName, account, this.CancellationToken);

            Assert.NotNull(account);

            // 2. Create Instance

            Instance instance = new Instance(this.Location)
            {
                IotHubs = new[]
                {
                    new IotHubSettings(
                        resourceId: "/subscriptions/cba097a0-e1ce-4c43-8bf6-a9dc778278e0/resourceGroups/DeviceUpdateResourceGroup/providers/Microsoft.Devices/IotHubs/orange-aducpsdktestaccount2-iothub",
                        ioTHubConnectionString: "HostName=orange-aducpsdktestaccount2-iothub.azure-devices.net;SharedAccessKeyName=deviceupdateservice;SharedAccessKey=xyz=",
                        eventHubConnectionString: "Endpoint=sb://orange-aducpsdktestaccount2-b93fc34123.servicebus.windows.net/;SharedAccessKeyName=iothubowner;SharedAccessKey=xyz=;EntityPath=orange-aducpsdktestaccount2")
                }
            };

            instance = await client.Instances.CreateAsync(ResourceGroupName, AccountName, InstanceName, instance, this.CancellationToken);

            Assert.NotNull(instance);

            // 3. Get Instance

            instance = await client.Instances.GetAsync(ResourceGroupName, AccountName, InstanceName, this.CancellationToken);

            Assert.NotNull(instance);

            // 4. List Instances by Account

            IPage <Instance> instances = await client.Instances.ListByAccountAsync(ResourceGroupName, AccountName, this.CancellationToken);

            Assert.NotEmpty(instances);

            // 5. Delete Instance

            await client.Instances.DeleteAsync(ResourceGroupName, AccountName, InstanceName, this.CancellationToken);

            // 6. Delete Account

            await client.Accounts.DeleteAsync(ResourceGroupName, AccountName, this.CancellationToken);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Device Update for IoT Hub Sample: Import new update
        /// </summary>
        /// <param name="connectionString">Azure Storage connection string to access blob storage to upload payload files.</param>
        /// <param name="blobContainer">Azure Blob Container name to upload payload files to.</param>
        static async Task Main(string connectionString, string blobContainer)
        {
            Console.WriteLine("Device Update for IoT Hub Sample: Import new update version");
            Console.WriteLine();

            if (string.IsNullOrWhiteSpace(connectionString))
            {
                throw new ArgumentException("You have to provider a valid Azure Storage connection string.");
            }
            if (string.IsNullOrWhiteSpace(blobContainer))
            {
                throw new ArgumentException("You have to provider a valid Azure Blob Container name.");
            }

            var credentials = new InteractiveBrowserCredential(Constant.TenantId, Constant.ClientId);
            var client      = new DeviceUpdateClient(Constant.AccountEndpoint, Constant.Instance, credentials);

            var updateVersion = DateTime.Now.ToString("yyyy.Mdd.hmm.s");

            Console.WriteLine("New update:");
            Console.WriteLine($"    Provider: {Constant.Provider}");
            Console.WriteLine($"    Name    : {Constant.Name}");
            Console.WriteLine($"    Version : {updateVersion}");

            Console.WriteLine();
            Console.WriteLine("Generating the new update...");
            var contentFactory = new ContentFactory();
            // payload
            var    payloadPath     = contentFactory.CreateAduPayloadFile(PayloadFilename);
            long   payloadFileSize = GetFileSize(payloadPath);
            string payloadFileHash = GetFileHash(payloadPath);
            // import manifest
            var manifestPath = contentFactory.CreateImportManifestContent(
                Constant.Provider, Constant.Name, updateVersion,
                PayloadFilename,
                payloadFileSize,
                payloadFileHash);
            long   manifestFileSize = GetFileSize(manifestPath);
            string manifestFileHash = GetFileHash(manifestPath);

            Console.WriteLine();
            Console.WriteLine($"Uploading the new update artifacts to Azure Storage blob container '{blobContainer}'...");
            // upload payload file
            var payloadUrl = await UploadFileAsync(connectionString, blobContainer, GenerateStorageId("payload"), payloadPath);

            // upload manifest file
            var manifestUrl = await UploadFileAsync(connectionString, blobContainer, GenerateStorageId("manifest"), manifestPath);

            Console.WriteLine();
            Console.WriteLine("Importing the new update...");
            //   - import request body
            var body = contentFactory.CreateImportBody(manifestUrl, manifestFileSize, manifestFileHash, PayloadFilename, payloadUrl);

            try
            {
                var response = await client.ImportUpdateAsync(true, "import", RequestContent.Create(body));

                var doc = JsonDocument.Parse(response.Value.ToMemory());
                Console.WriteLine(doc.RootElement.GetProperty("status").ToString());
            }
            catch (RequestFailedException e)
            {
                Console.WriteLine(e);
                throw;
            }
        }