public PSResource(Resource <JToken> resource) { this.Name = resource.Name; this.ResourceName = resource.Name; this.ResourceId = resource.Id; this.Id = resource.Id; this.Type = string.IsNullOrEmpty(resource.Id) ? null : ResourceIdUtility.GetResourceType(resource.Id); this.ResourceType = Type; this.ExtensionResourceName = string.IsNullOrEmpty(resource.Id) ? null : ResourceIdUtility.GetExtensionResourceName(resource.Id); this.ExtensionResourceType = string.IsNullOrEmpty(resource.Id) ? null : ResourceIdUtility.GetExtensionResourceType(resource.Id); this.Kind = resource.Kind; this.ResourceGroupName = string.IsNullOrEmpty(resource.Id) ? null : ResourceIdUtility.GetResourceGroupName(resource.Id); this.Location = resource.Location; this.SubscriptionId = string.IsNullOrEmpty(resource.Id) ? null : ResourceIdUtility.GetSubscriptionId(resource.Id); this.Tags = TagsHelper.GetTagsDictionary(TagsHelper.GetTagsHashtable(resource.Tags)); this.Properties = resource.Properties == null ? null : resource.Properties.ToPsObject(); this.CreatedTime = resource.CreatedTime; this.ChangedTime = resource.ChangedTime; this.ETag = resource.ETag; this.Plan = resource.Plan == null ? null : new Plan { Name = resource.Plan.Name, Publisher = resource.Plan.Publisher, Product = resource.Plan.Product, PromotionCode = resource.Plan.PromotionCode, Version = resource.Plan.Version }; this.Sku = resource.Sku == null ? null : new Sku { Name = resource.Sku.Name, Tier = resource.Sku.Tier, Size = resource.Sku.Size, Family = resource.Sku.Family, Capacity = resource.Sku.Capacity }; if (resource.Identity != null) { this.Identity = new Identity(resource.Identity.PrincipalId, resource.Identity.TenantId); if (Enum.TryParse(resource.Identity.Type, out Management.ResourceManager.Models.ResourceIdentityType type)) { this.Identity.Type = type; } } }
/// <summary> /// Converts a <see cref="Resource{JToken}"/> object into a <see cref="PSObject"/> object. /// </summary> /// <param name="resource">The <see cref="Resource{JToken}"/> object.</param> internal static PSObject ToPsObject(this Resource <JToken> resource) { var resourceType = string.IsNullOrEmpty(resource.Id) ? null : ResourceIdUtility.GetResourceType(resource.Id); var extensionResourceType = string.IsNullOrEmpty(resource.Id) ? null : ResourceIdUtility.GetExtensionResourceType(resource.Id); var objectDefinition = new Dictionary <string, object> { { "Name", resource.Name }, { "ResourceId", string.IsNullOrEmpty(resource.Id) ? null : resource.Id }, { "ResourceName", string.IsNullOrEmpty(resource.Id) ? null : ResourceIdUtility.GetResourceName(resource.Id) }, { "ResourceType", resourceType }, { "ExtensionResourceName", string.IsNullOrEmpty(resource.Id) ? null : ResourceIdUtility.GetExtensionResourceName(resource.Id) }, { "ExtensionResourceType", extensionResourceType }, { "Kind", resource.Kind }, { "ResourceGroupName", string.IsNullOrEmpty(resource.Id) ? null : ResourceIdUtility.GetResourceGroupName(resource.Id) }, { "Location", resource.Location }, { "SubscriptionId", string.IsNullOrEmpty(resource.Id) ? null : ResourceIdUtility.GetSubscriptionId(resource.Id) }, { "Tags", TagsHelper.GetTagsHashtable(resource.Tags) }, { "Plan", resource.Plan.ToJToken().ToPsObject() }, { "Properties", ResourceExtensions.GetProperties(resource) }, { "CreatedTime", resource.CreatedTime }, { "ChangedTime", resource.ChangedTime }, { "ETag", resource.ETag }, { "Sku", resource.Sku.ToJToken().ToPsObject() }, { "Zones", resource.Zones }, }; var resourceTypeName = resourceType == null && extensionResourceType == null ? null : (resourceType + extensionResourceType).Replace('/', '.'); var psObject = PowerShellUtilities.ConstructPSObject( resourceTypeName, objectDefinition.Where(kvp => kvp.Value != null).SelectManyArray(kvp => new[] { kvp.Key, kvp.Value })); psObject.TypeNames.Add(Constants.MicrosoftAzureResource); return(psObject); }