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);
                }
            });
        }
Beispiel #3
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);
                }
            });
        }