public async Task <List <string> > GetRolesAsync(AzureSphereTenant tenant, string username, CancellationToken cancellationToken)
        {
            var jsonString = await GetAsync($"v2/tenants/{tenant.Id}/users/{username}/role", cancellationToken);

            Console.WriteLine("GetRolesAsync()");
            Console.WriteLine(jsonString);
            var json      = JToken.Parse(jsonString);
            var jsonRoles = json.Value <JArray>("Roles");

            if (jsonRoles.Count != 1)
            {
                throw new ApplicationException();
            }
            if (jsonRoles[0].Value <string>("TenantId") != tenant.Id)
            {
                throw new ApplicationException();
            }
            var jsonRoleNames = jsonRoles[0]["RoleNames"];

            var roleNames = new List <string>();

            foreach (var jsonRoleName in jsonRoleNames)
            {
                roleNames.Add(jsonRoleName.Value <string>());
            }

            return(roleNames);
        }
        public async Task <List <AzureSphereDeviceInsight> > GetDeviceInsightsAsync(AzureSphereTenant tenant, CancellationToken cancellationToken)
        {
            Console.WriteLine("GetDeviceInsightsAsync()");
            var deviceInsights = new List <AzureSphereDeviceInsight>();

            string continuationToken = null;

            while (true)
            {
                var jsonString = await MethodAsync($"v2/tenants/{tenant.Id}/getDeviceInsights", Method.GET, null, null, cancellationToken);

                Console.WriteLine(jsonString);
                var json = JToken.Parse(jsonString);
                var jsonDeviceInsights = json.Value <JArray>("Items");

                foreach (var jsonDeviceInsight in jsonDeviceInsights)
                {
                    deviceInsights.Add(new AzureSphereDeviceInsight(jsonDeviceInsight));
                }

                if (json["ContinuationToken"] == null || json["ContinuationToken"].Type == JTokenType.Null)
                {
                    break;
                }
                continuationToken = json.Value <string>("ContinuationToken");
            }

            return(deviceInsights);
        }
        public async Task <AzureSphereImage> GetImageAsync(AzureSphereTenant tenant, string imageId, CancellationToken cancellationToken)
        {
            var jsonString = await GetAsync($"v2/tenants/{tenant.Id}/images/{imageId}/metadata", cancellationToken);

            Console.WriteLine("GetImageAsync()");
            Console.WriteLine(jsonString);
            var json = JToken.Parse(jsonString);

            return(new AzureSphereImage(json));
        }
        public async Task <bool> DeleteDeviceGroupAsync(AzureSphereTenant tenant, AzureSphereDeviceGroup deviceGroup, CancellationToken cancellationToken)
        {
            Console.WriteLine("DeleteDeviceGroupAsync()");

            var jsonString = await MethodAsync($"v2/tenants/{tenant.Id}/devicegroups/{deviceGroup.Id}", Method.DELETE, null, null, cancellationToken);

            Console.WriteLine(jsonString);
            var operation = new AzureSphereOperation(JObject.Parse(jsonString));

            return(await GetAsyncOperation(tenant, operation.OperationId, cancellationToken));
        }
        public async Task <bool> PostCreateDeviceGroupAsync(AzureSphereTenant tenant, HttpContent jsonContent,
                                                            CancellationToken cancellationToken)
        {
            Console.WriteLine("PostCreateDeviceGroupAsync()");

            var jsonString = await MethodAsync($"v2/tenants/{tenant.Id}/devicegroups",
                                               Method.POST,
                                               jsonContent,
                                               null,
                                               cancellationToken);

            Console.WriteLine(jsonString);

            return(true);
        }
        public async Task <bool> PostDeployAsync(AzureSphereTenant tenant, AzureSphereDeviceGroup deviceGroup, HttpContent jsonContent,
                                                 CancellationToken cancellationToken)
        {
            Console.WriteLine("PostDeployAsync()");

            var jsonString = await MethodAsync($"v2/tenants/{tenant.Id}/devicegroups/{deviceGroup.Id}/deployments",
                                               Method.POST,
                                               jsonContent,
                                               null,
                                               cancellationToken);

            Console.WriteLine(jsonString);
            var operation = new AzureSphereOperation(JObject.Parse(jsonString));

            return(await GetAsyncOperation(tenant, operation.OperationId, cancellationToken));
        }
        public async Task <bool> PostCreateProductGroupAsync(AzureSphereTenant tenant, HttpContent jsonContent,
                                                             CancellationToken cancellationToken)
        {
            Console.WriteLine("PostCreateProductAsync()");

            var jsonString = await MethodAsync($"v2/tenants/{tenant.Id}/products",
                                               Method.POST,
                                               jsonContent,
                                               null,
                                               cancellationToken);

            Console.WriteLine(jsonString);
            var operation = new AzureSphereOperation(JObject.Parse(jsonString));

            return(await GetAsyncOperation(tenant, operation.OperationId, cancellationToken));
        }
        public async Task <List <AzureSphereProduct> > GetProductsAsync(AzureSphereTenant tenant, CancellationToken cancellationToken)
        {
            var jsonString = await GetAsync($"v2/tenants/{tenant.Id}/products", cancellationToken);

            Console.WriteLine("GetProductsAsync()");
            Console.WriteLine(jsonString);
            var json         = JToken.Parse(jsonString);
            var jsonProducts = json.Value <JArray>("Items");

            var products = new List <AzureSphereProduct>();

            foreach (var jsonProduct in jsonProducts)
            {
                products.Add(new AzureSphereProduct(jsonProduct));
            }

            return(products);
        }
        public async Task <List <AzureSphereUser> > GetUsersAsync(AzureSphereTenant tenant, CancellationToken cancellationToken)
        {
            var jsonString = await GetAsync($"v2/tenants/{tenant.Id}/users", cancellationToken);

            Console.WriteLine("GetUsersAsync()");
            Console.WriteLine(jsonString);
            var json      = JToken.Parse(jsonString);
            var jsonUsers = json.Value <JArray>("Principals");

            var users = new List <AzureSphereUser>();

            foreach (var jsonUser in jsonUsers)
            {
                users.Add(new AzureSphereUser(jsonUser));
            }

            return(users);
        }
        public async Task <List <AzureSphereDeployment> > GetDeploymentsAsync(AzureSphereTenant tenant, AzureSphereDeviceGroup deviceGroup, CancellationToken cancellationToken)
        {
            var jsonString = await GetAsync($"v2/tenants/{tenant.Id}/devicegroups/{deviceGroup.Id}/deployments", cancellationToken);

            Console.WriteLine("GetDeploymentsAsync()");
            Console.WriteLine(jsonString);
            var json            = JToken.Parse(jsonString);
            var jsonDeployments = json.Value <JArray>("Items");

            var deployments = new List <AzureSphereDeployment>();

            foreach (var jsonDeployment in jsonDeployments)
            {
                deployments.Add(new AzureSphereDeployment(jsonDeployment));
            }

            return(deployments);
        }
        private async Task <bool> GetAsyncOperation(AzureSphereTenant tenant, string operationId, CancellationToken cancellationToken)
        {
            bool continueOperationAsync = true;
            bool ret = false;

            while (continueOperationAsync)
            {
                var jsonString = await MethodAsync($"v2/tenants/{tenant.Id}/operations/{operationId}",
                                                   Method.GET,
                                                   null,
                                                   null,
                                                   cancellationToken);

                Console.WriteLine("GetAsyncOperation()");
                Console.WriteLine(jsonString);
                var      operation = new AzureSphereOperation(JObject.Parse(jsonString));
                StateNum state     = (StateNum)operation.State;

                switch (state)
                {
                case StateNum.Complete:
                    ret = true;
                    continueOperationAsync = false;
                    break;

                case StateNum.Failed:
                    ret = false;
                    continueOperationAsync = false;
                    break;

                default:
                    break;
                }
            }
            return(ret);
        }
 public async Task DeleteDeviceGroupAsync(AzureSphereTenant tenant, AzureSphereDeviceGroup deviceGroup, CancellationToken cancellationToken)
 {
     await DeleteAsync($"v2/tenants/{tenant.Id}/devicegroups/{deviceGroup.Id}", cancellationToken);
 }
 public async Task DeleteProductAsync(AzureSphereTenant tenant, AzureSphereProduct product, CancellationToken cancellationToken)
 {
     await DeleteAsync($"v2/tenants/{tenant.Id}/products/{product.Id}", cancellationToken);
 }