public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();
            ExecuteClientAction(() =>
            {
                if (ShouldProcess(this.Name, VerbsCommon.New))
                {
                    string resourceGroupName = this.ResourceGroupName;;
                    string galleryName       = this.Name;
                    Gallery gallery          = new Gallery();
                    gallery.Location         = this.Location;

                    if (this.IsParameterBound(c => c.Description))
                    {
                        gallery.Description = this.Description;
                    }

                    if (this.IsParameterBound(c => c.Tag))
                    {
                        gallery.Tags = this.Tag.Cast <DictionaryEntry>().ToDictionary(ht => (string)ht.Key, ht => (string)ht.Value);
                    }

                    var result   = GalleriesClient.CreateOrUpdate(resourceGroupName, galleryName, gallery);
                    var psObject = new PSGallery();
                    ComputeAutomationAutoMapperProfile.Mapper.Map <Gallery, PSGallery>(result, psObject);
                    WriteObject(psObject);
                }
            });
        }
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();
            ExecuteClientAction(() =>
            {
                if (ShouldProcess(this.Name, VerbsData.Update))
                {
                    string resourceGroupName;
                    string galleryName;
                    switch (this.ParameterSetName)
                    {
                    case "ResourceIdParameter":
                        resourceGroupName = GetResourceGroupName(this.ResourceId);
                        galleryName       = GetResourceName(this.ResourceId, "Microsoft.Compute/Galleries");
                        break;

                    case "ObjectParameter":
                        resourceGroupName = GetResourceGroupName(this.InputObject.Id);
                        galleryName       = GetResourceName(this.InputObject.Id, "Microsoft.Compute/Galleries");
                        break;

                    default:
                        resourceGroupName = this.ResourceGroupName;
                        galleryName       = this.Name;
                        break;
                    }

                    Gallery gallery = new Gallery();

                    if (this.ParameterSetName == "ObjectParameter")
                    {
                        ComputeAutomationAutoMapperProfile.Mapper.Map <PSGallery, Gallery>(this.InputObject, gallery);
                    }
                    else
                    {
                        gallery = GalleriesClient.Get(resourceGroupName, galleryName);
                    }

                    if (this.IsParameterBound(c => c.Description))
                    {
                        gallery.Description = this.Description;
                    }

                    if (this.IsParameterBound(c => c.Tag))
                    {
                        gallery.Tags = this.Tag.Cast <DictionaryEntry>().ToDictionary(ht => (string)ht.Key, ht => (string)ht.Value);
                    }

                    var result   = GalleriesClient.CreateOrUpdate(resourceGroupName, galleryName, gallery);
                    var psObject = new PSGallery();
                    ComputeAutomationAutoMapperProfile.Mapper.Map <Gallery, PSGallery>(result, psObject);
                    WriteObject(psObject);
                }
            });
        }
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();
            ExecuteClientAction(() =>
            {
                string resourceGroupName;
                string galleryName;
                switch (this.ParameterSetName)
                {
                case "ResourceIdParameter":
                    resourceGroupName = GetResourceGroupName(this.ResourceId);
                    galleryName       = GetResourceName(this.ResourceId, "Microsoft.Compute/Galleries");
                    break;

                default:
                    resourceGroupName = this.ResourceGroupName;
                    galleryName       = this.Name;
                    break;
                }

                if (!string.IsNullOrEmpty(resourceGroupName) && !string.IsNullOrEmpty(galleryName))
                {
                    var result   = GalleriesClient.Get(resourceGroupName, galleryName);
                    var psObject = new PSGallery();
                    ComputeAutomationAutoMapperProfile.Mapper.Map <Gallery, PSGallery>(result, psObject);
                    WriteObject(psObject);
                }
                else if (!string.IsNullOrEmpty(resourceGroupName))
                {
                    var result       = GalleriesClient.ListByResourceGroup(resourceGroupName);
                    var resultList   = result.ToList();
                    var nextPageLink = result.NextPageLink;
                    while (!string.IsNullOrEmpty(nextPageLink))
                    {
                        var pageResult = GalleriesClient.ListByResourceGroupNext(nextPageLink);
                        foreach (var pageItem in pageResult)
                        {
                            resultList.Add(pageItem);
                        }
                        nextPageLink = pageResult.NextPageLink;
                    }
                    var psObject = new List <PSGalleryList>();
                    foreach (var r in resultList)
                    {
                        psObject.Add(ComputeAutomationAutoMapperProfile.Mapper.Map <Gallery, PSGalleryList>(r));
                    }
                    WriteObject(psObject, true);
                }
                else
                {
                    var result       = GalleriesClient.List();
                    var resultList   = result.ToList();
                    var nextPageLink = result.NextPageLink;
                    while (!string.IsNullOrEmpty(nextPageLink))
                    {
                        var pageResult = GalleriesClient.ListNext(nextPageLink);
                        foreach (var pageItem in pageResult)
                        {
                            resultList.Add(pageItem);
                        }
                        nextPageLink = pageResult.NextPageLink;
                    }
                    var psObject = new List <PSGalleryList>();
                    foreach (var r in resultList)
                    {
                        psObject.Add(ComputeAutomationAutoMapperProfile.Mapper.Map <Gallery, PSGalleryList>(r));
                    }
                    WriteObject(psObject, true);
                }
            });
        }
Пример #4
0
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();
            ExecuteClientAction(() =>
            {
                if (ShouldProcess(this.Name, VerbsData.Update))
                {
                    string resourceGroupName;
                    string galleryName;
                    switch (this.ParameterSetName)
                    {
                    case "ResourceIdParameter":
                        resourceGroupName = GetResourceGroupName(this.ResourceId);
                        galleryName       = GetResourceName(this.ResourceId, "Microsoft.Compute/Galleries");
                        break;

                    case "ObjectParameter":
                        resourceGroupName = GetResourceGroupName(this.InputObject.Id);
                        galleryName       = GetResourceName(this.InputObject.Id, "Microsoft.Compute/Galleries");
                        break;

                    default:
                        resourceGroupName = this.ResourceGroupName;
                        galleryName       = this.Name;
                        break;
                    }

                    Gallery gallery = new Gallery();

                    if (this.ParameterSetName == "ObjectParameter")
                    {
                        ComputeAutomationAutoMapperProfile.Mapper.Map <PSGallery, Gallery>(this.InputObject, gallery);
                    }
                    else
                    {
                        gallery = GalleriesClient.Get(resourceGroupName, galleryName);
                    }

                    if (this.IsParameterBound(c => c.Description))
                    {
                        gallery.Description = this.Description;
                    }

                    if (this.IsParameterBound(c => c.Tag))
                    {
                        gallery.Tags = this.Tag.Cast <DictionaryEntry>().ToDictionary(ht => (string)ht.Key, ht => (string)ht.Value);
                    }
                    if (this.IsParameterBound(c => c.Permission))
                    {
                        if (gallery.SharingProfile == null)
                        {
                            gallery.SharingProfile = new SharingProfile();
                        }
                        gallery.SharingProfile.Permissions = this.Permission;
                    }


                    SharingUpdate sharingUpdate = new SharingUpdate();
                    if (this.Share.IsPresent)
                    {
                        if (this.Reset.IsPresent)
                        {
                            // if sub or tenant is present return error
                            if (this.IsParameterBound(c => c.Subscription) || this.IsParameterBound(c => c.Tenant))
                            {
                                throw new Exception("Parameter '-Reset' cannot be used with parameters '-Tenant' or '-Subscription'.");
                            }
                            else
                            {
                                sharingUpdate.OperationType = "Reset";
                            }
                        }
                        if (this.IsParameterBound(c => c.Subscription))
                        {
                            if (sharingUpdate.Groups == null)
                            {
                                sharingUpdate.Groups = new List <SharingProfileGroup>();
                            }
                            SharingProfileGroup sharingProfile = new SharingProfileGroup();
                            sharingProfile.Type = "Subscriptions";
                            sharingProfile.Ids  = new List <string>();
                            foreach (var id in this.Subscription)
                            {
                                sharingProfile.Ids.Add(id);
                            }
                            sharingUpdate.Groups.Add(sharingProfile);
                            sharingUpdate.OperationType = "Add";
                        }
                        if (this.IsParameterBound(c => c.Tenant))
                        {
                            if (sharingUpdate.Groups == null)
                            {
                                sharingUpdate.Groups = new List <SharingProfileGroup>();
                            }
                            SharingProfileGroup sharingProfile = new SharingProfileGroup();
                            sharingProfile.Type = "AADTenants";
                            sharingProfile.Ids  = new List <string>();
                            foreach (var id in this.Tenant)
                            {
                                sharingProfile.Ids.Add(id);
                            }
                            sharingUpdate.Groups.Add(sharingProfile);
                            sharingUpdate.OperationType = "Add";
                        }
                        if (this.IsParameterBound(c => c.RemoveTenant))
                        {
                            if (sharingUpdate.Groups == null)
                            {
                                sharingUpdate.Groups = new List <SharingProfileGroup>();
                            }
                            SharingProfileGroup sharingProfile = new SharingProfileGroup();
                            sharingProfile.Type = "AADTenants";
                            sharingProfile.Ids  = new List <string>();
                            foreach (var id in this.RemoveTenant)
                            {
                                sharingProfile.Ids.Add(id);
                            }
                            sharingUpdate.Groups.Add(sharingProfile);
                            sharingUpdate.OperationType = "Remove";
                        }
                        if (this.IsParameterBound(c => c.RemoveSubscription))
                        {
                            if (sharingUpdate.Groups == null)
                            {
                                sharingUpdate.Groups = new List <SharingProfileGroup>();
                            }
                            SharingProfileGroup sharingProfile = new SharingProfileGroup();
                            sharingProfile.Type = "Subscriptions";
                            sharingProfile.Ids  = new List <string>();
                            foreach (var id in this.RemoveSubscription)
                            {
                                sharingProfile.Ids.Add(id);
                            }
                            sharingUpdate.Groups.Add(sharingProfile);
                            sharingUpdate.OperationType = "Remove";
                        }
                    }
                    else if (this.IsParameterBound(c => c.Subscription) || this.IsParameterBound(c => c.Tenant) || this.Reset.IsPresent || this.IsParameterBound(c => c.RemoveSubscription) || this.IsParameterBound(c => c.RemoveTenant))
                    {
                        throw new Exception("Parameters '-Subscription', '-Tenant', '-RemoveSubscription', '-RemoveTenant', and '-Reset' must be used with '-Share' parameter.");
                    }

                    var result = GalleriesClient.CreateOrUpdate(resourceGroupName, galleryName, gallery);
                    if (this.Share.IsPresent)
                    {
                        GallerySharingProfileClient.Update(resourceGroupName, galleryName, sharingUpdate);
                        result = GalleriesClient.Get(ResourceGroupName, galleryName, "Permissions");
                    }
                    var psObject = new PSGallery();
                    ComputeAutomationAutoMapperProfile.Mapper.Map <Gallery, PSGallery>(result, psObject);
                    WriteObject(psObject);
                }
            });
        }
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();
            ExecuteClientAction(() =>
            {
                string resourceGroupName;
                string galleryName;
                switch (this.ParameterSetName)
                {
                case "ResourceIdParameter":
                    resourceGroupName = GetResourceGroupName(this.ResourceId);
                    galleryName       = GetResourceName(this.ResourceId, "Microsoft.Compute/galleries");
                    break;

                case "SharedGalleryParameterSet":
                    SharedGalleryGet();
                    return;

                default:
                    resourceGroupName = this.ResourceGroupName;
                    galleryName       = this.Name;
                    break;
                }

                if (ShouldGetByName(resourceGroupName, galleryName))
                {
                    var result = GalleriesClient.Get(resourceGroupName, galleryName, null, this.Expand);

                    var psObject = new PSGallery();
                    ComputeAutomationAutoMapperProfile.Mapper.Map <Gallery, PSGallery>(result, psObject);
                    WriteObject(psObject);
                }
                else if (ShouldListByResourceGroup(resourceGroupName, galleryName))
                {
                    var result       = GalleriesClient.ListByResourceGroup(resourceGroupName);
                    var resultList   = result.ToList();
                    var nextPageLink = result.NextPageLink;
                    while (!string.IsNullOrEmpty(nextPageLink))
                    {
                        var pageResult = GalleriesClient.ListByResourceGroupNext(nextPageLink);
                        foreach (var pageItem in pageResult)
                        {
                            resultList.Add(pageItem);
                        }
                        nextPageLink = pageResult.NextPageLink;
                    }
                    var psObject = new List <PSGalleryList>();
                    foreach (var r in resultList)
                    {
                        psObject.Add(ComputeAutomationAutoMapperProfile.Mapper.Map <Gallery, PSGalleryList>(r));
                    }
                    WriteObject(TopLevelWildcardFilter(resourceGroupName, galleryName, psObject), true);
                }
                else
                {
                    if (this.IsParameterBound(c => c.Name))
                    {
                        WriteWarning("To retrieve a specific Gallery resource, please provide '-ResourceGroupName'.\nUsing '-Name' parameter without '-ResourceGroupName' will default to listing all gallery resources in your current subscription.");
                    }
                    var result       = GalleriesClient.List();
                    var resultList   = result.ToList();
                    var nextPageLink = result.NextPageLink;
                    while (!string.IsNullOrEmpty(nextPageLink))
                    {
                        var pageResult = GalleriesClient.ListNext(nextPageLink);
                        foreach (var pageItem in pageResult)
                        {
                            resultList.Add(pageItem);
                        }
                        nextPageLink = pageResult.NextPageLink;
                    }
                    var psObject = new List <PSGalleryList>();
                    foreach (var r in resultList)
                    {
                        psObject.Add(ComputeAutomationAutoMapperProfile.Mapper.Map <Gallery, PSGalleryList>(r));
                    }
                    WriteObject(TopLevelWildcardFilter(resourceGroupName, galleryName, psObject), true);
                }
            });
        }