private static async Task CreateResourcesAsync(HttpClient httpClient, IList <ResourceDescription> resourceDescriptions, Guid spaceId) { if (spaceId == Guid.Empty) { throw new ArgumentException($"Resources must have a {nameof( spaceId )}"); } var space = await SpaceHelpers.GetSpaceAsync(httpClient, spaceId); IReadOnlyCollection <Resource> existingResources = await space.GetExistingChildResourcesAsync(httpClient); var resourceIds = new List <Guid>(); var resourcesToCreate = resourceDescriptions.Where(rd => !existingResources.Any(er => er.Type.Equals(rd.type, StringComparison.OrdinalIgnoreCase))) .ToArray(); foreach (ResourceDescription resourceDescription in resourcesToCreate) { Resource resource = resourceDescription.ToDigitalTwins(spaceId); Guid createdId = await CreateResourceAsync(httpClient, resource); if (createdId != Guid.Empty) { resourceIds.Add(createdId); } else { Console.WriteLine($"Failed to create resource. Please try manually: {resourceDescription.type}"); } } if (resourceIds.Any()) { // wait until all the resources are created and ready to use in case downstream operations (like device creation) // are dependent on it. Console.WriteLine("Polling until all resources are no longer in the provisioning state."); IEnumerable <Task <bool> > statusVerificationTasks = resourceIds.Select(resourceId => ResourcesHelpers.WaitTillResourceCreationCompletedAsync(httpClient, resourceId)); await Task.WhenAll(statusVerificationTasks); } }
private async Task CreatePropertiesAsync(HttpClient httpClient, IList <PropertyDescription> properties, Guid spaceId) { if (spaceId == Guid.Empty) { throw new ArgumentException($"Property must have a {nameof( spaceId )}"); } Space space = await SpaceHelpers.GetSpaceAsync(httpClient, spaceId, "Properties"); var propertiesToCreate = properties.Where(pd => !space.Properties.Any(ep => ep.Name.Equals(pd.name, StringComparison.OrdinalIgnoreCase))) .ToArray(); foreach (PropertyDescription propertyDescription in propertiesToCreate) { Property property = propertyDescription.ToDigitalTwins(); await property.CreatePropertyAsync(spaceId, httpClient, JsonSerializerSettings); } }
private async Task CreatePropertyKeysAsync(HttpClient httpClient, IList <PropertyKeyDescription> propertyKeys, Guid spaceId) { if (spaceId == Guid.Empty) { throw new ArgumentException($"PropertyKey must have a {nameof( spaceId )}"); } var space = await SpaceHelpers.GetSpaceAsync(httpClient, spaceId); IReadOnlyCollection <PropertyKey> existingPropertyKeys = await space.GetExistingPropertyKeysAsync(httpClient); var propertyKeysToCreate = propertyKeys.Where(pkd => !existingPropertyKeys.Any(epk => epk.Name.Equals(pkd.name, StringComparison.OrdinalIgnoreCase))) .ToArray(); foreach (PropertyKeyDescription propertyKeyDescription in propertyKeysToCreate) { PropertyKey propertyKey = propertyKeyDescription.ToDigitalTwins(spaceId); await propertyKey.CreatePropertyKeyAsync(spaceId, httpClient, JsonSerializerSettings); } }
private static async Task CreateMatchersAsync(HttpClient httpClient, IList <MatcherDescription> matchers, Guid spaceId) { if (spaceId == Guid.Empty) { throw new ArgumentException($"Matchers must have a {nameof( spaceId )}"); } var space = await SpaceHelpers.GetSpaceAsync(httpClient, spaceId); IReadOnlyCollection <Matcher> existingMatchers = await space.GetExistingMatchersAsync(httpClient); var matchersToCreate = matchers.Where(md => !existingMatchers.Any(em => em.Name.Equals(md.name, StringComparison.OrdinalIgnoreCase))) .ToArray(); foreach (MatcherDescription matcherDescription in matchersToCreate) { Matcher matcher = matcherDescription.ToDigitalTwins(spaceId); await matcher.CreateMatcherAsync(httpClient, JsonSerializerSettings); } }
private static async Task CreateTypesAsync(HttpClient httpClient, IList <TypeDescription> typeDescriptions, Guid spaceId) { if (spaceId == Guid.Empty) { throw new ArgumentException($"Types must have a {nameof( spaceId )}"); } var space = await SpaceHelpers.GetSpaceAsync(httpClient, spaceId); IReadOnlyCollection <Type> existingTypes = await space.GetExistingTypesAsync(httpClient); var typesToCreate = typeDescriptions.Where(td => !existingTypes.Any(et => et.Name.Equals(td.name, StringComparison.OrdinalIgnoreCase) && et.Category.Equals(td.category, StringComparison.OrdinalIgnoreCase))) .ToArray(); foreach (TypeDescription typeDescription in typesToCreate) { Type type = typeDescription.ToDigitalTwins(spaceId); await type.CreateTypeAsync(httpClient, JsonSerializerSettings); } }
private async Task CreateBlobAsync(HttpClient httpClient, IList <BlobDescription> blobDescriptions, Guid spaceId) { if (spaceId == Guid.Empty) { throw new ArgumentException($"Blob must have a {nameof( spaceId )}"); } Space space = await SpaceHelpers.GetSpaceAsync(httpClient, spaceId, "Properties"); Property imageBlobIdProperty = space.Properties.FirstOrDefault(p => p.Name == PropertyKeyDescription.ImageBlobId); Property detailedImageBlobIdProperty = space.Properties.FirstOrDefault(p => p.Name == PropertyKeyDescription.DetailedImageBlobId); foreach (BlobDescription blobDescription in blobDescriptions) { Property desiredBlobIdProperty; string desiredImagePathPropertyName; string desitedImageBlobIdPropertyName; if (blobDescription.isPrimaryBlob) { desiredBlobIdProperty = imageBlobIdProperty; desiredImagePathPropertyName = PropertyKeyDescription.ImagePath; desitedImageBlobIdPropertyName = PropertyKeyDescription.ImageBlobId; } else { desiredBlobIdProperty = detailedImageBlobIdProperty; desiredImagePathPropertyName = PropertyKeyDescription.DetailedImagePath; desitedImageBlobIdPropertyName = PropertyKeyDescription.DetailedImageBlobId; } Metadata metadata = blobDescription.ToDigitalTwinsMetadata(spaceId); var multipartContent = new MultipartFormDataContent("USER_DEFINED_BOUNDARY"); var metadataContent = new StringContent(JsonConvert.SerializeObject(metadata), Encoding.UTF8, "application/json"); metadataContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); multipartContent.Add(metadataContent, "metadata"); string blobContentFilePath = blobDescription.filepath; if (!Path.IsPathFullyQualified(blobContentFilePath)) { blobContentFilePath = Path.Combine(_directoryContainingDigitalTwinsProvisioningFile, blobContentFilePath); } using (FileStream s = File.OpenRead(blobContentFilePath)) { var blobContent = new StreamContent(s); blobContent.Headers.ContentType = MediaTypeHeaderValue.Parse(blobDescription.contentType); multipartContent.Add(blobContent, "contents"); Console.WriteLine(); HttpRequestMessage request; if (desiredBlobIdProperty == null) { Console.WriteLine($"Creating new blob for Space ({spaceId}) from file: {blobDescription.filepath}"); request = new HttpRequestMessage(HttpMethod.Post, "spaces/blobs") { Content = multipartContent }; } else { Console.WriteLine($"Updating blob for Space ({spaceId}) from file: {blobDescription.filepath}"); request = new HttpRequestMessage(HttpMethod.Patch, $"spaces/blobs/{desiredBlobIdProperty.Value}") { Content = multipartContent }; } var response = await httpClient.SendAsync(request); Guid blobId = await response.GetIdAsync(); if (desiredBlobIdProperty == null) { var uriBuilder = new UriBuilder($"{httpClient.BaseAddress}spaces/blobs/{blobId}/contents/latest"); var imagePathProperty = new Property { Name = desiredImagePathPropertyName, Value = uriBuilder.Uri.ToString() }; await imagePathProperty.CreateOrUpdatePropertyAsync(spaceId, httpClient, JsonSerializerSettings); imageBlobIdProperty = new Property { Name = desitedImageBlobIdPropertyName, Value = blobId.ToString() }; await imageBlobIdProperty.CreateOrUpdatePropertyAsync(spaceId, httpClient, JsonSerializerSettings); } } } }