public void Gallery_WithSharingProfile_CRUD_Tests()
        {
            using (MockContext context = MockContext.Start(this.GetType()))
            {
                EnsureClientsInitialized(context);
                string rgName = ComputeManagementTestUtilities.GenerateName(ResourceGroupPrefix);

                m_ResourcesClient.ResourceGroups.CreateOrUpdate(rgName, new ResourceGroup {
                    Location = galleryHomeLocation
                });
                Trace.TraceInformation("Created the resource group: " + rgName);

                string  galleryName = ComputeManagementTestUtilities.GenerateName(GalleryNamePrefix);
                Gallery galleryIn   = GetTestInputSharedGallery();
                m_CrpClient.Galleries.CreateOrUpdate(rgName, galleryName, galleryIn);
                Trace.TraceInformation(string.Format("Created the  shared gallery: {0} in resource group: {1} with sharing profile permission: {2}",
                                                     galleryName, rgName, galleryIn.SharingProfile.Permissions));

                Gallery galleryOut = m_CrpClient.Galleries.Get(rgName, galleryName);
                Trace.TraceInformation("Got the gallery.");
                Assert.NotNull(galleryOut);
                ValidateGallery(galleryIn, galleryOut);

                Trace.TraceInformation("Update the sharing profile via post, add the sharing profile groups.");
                string newTenantId = "583d66a9-0041-4999-8838-75baece101d5";
                SharingProfileGroup tenantGroups = new SharingProfileGroup()
                {
                    Type = "AADTenants",
                    Ids  = new List <string> {
                        newTenantId
                    }
                };

                string newSubId = "640c5810-13bf-4b82-b94d-f38c2565e3bc";
                SharingProfileGroup subGroups = new SharingProfileGroup()
                {
                    Type = "Subscriptions",
                    Ids  = new List <string> {
                        newSubId
                    }
                };

                List <SharingProfileGroup> groups = new List <SharingProfileGroup> {
                    tenantGroups, subGroups
                };
                SharingUpdate sharingUpdate = new SharingUpdate()
                {
                    OperationType = SharingUpdateOperationTypes.Add,
                    Groups        = groups
                };

                m_CrpClient.GallerySharingProfile.Update(rgName, galleryName, sharingUpdate);

                Gallery galleryOutWithSharingProfile = m_CrpClient.Galleries.Get(rgName, galleryName, SelectPermissions.Permissions);
                Trace.TraceInformation("Got the gallery");
                Assert.NotNull(galleryOut);

                ValidateSharingProfile(galleryIn, galleryOutWithSharingProfile, groups);

                Trace.TraceInformation("Reset this gallery to private before deleting it.");
                SharingUpdate resetPrivateUpdate = new SharingUpdate()
                {
                    OperationType = SharingUpdateOperationTypes.Reset,
                    Groups        = null
                };

                m_CrpClient.GallerySharingProfile.Update(rgName, galleryName, resetPrivateUpdate);

                Trace.TraceInformation("Deleting this gallery.");
                m_CrpClient.Galleries.Delete(rgName, galleryName);
                // resource groups cleanup is taken cared by MockContext.Dispose() method.
            }
        }
Example #2
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);
                }
            });
        }