public override void ExecuteCmdlet()
        {
            TemplateImageResult response = null;
            TemplateImageDetails details = null;
            TemplateImage matchingTemplate = null;

            matchingTemplate = FilterTemplateImage(ImageName, Operation.Update);

            details = new TemplateImageDetails()
            {
                Id = matchingTemplate.Id,
                Region = matchingTemplate.RegionList[0],
                Name = NewName
            };

            response = CallClient(() => Client.TemplateImages.Set(details), Client.TemplateImages);

            if (response != null)
            {
                WriteObject(response.TemplateImage);
            }
        }
 /// <summary>
 /// Sets a template image details.  If the template image already
 /// exists, only the Name can be changed.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.RemoteApp.ITemplateImageOperations.
 /// </param>
 /// <param name='imageDetails'>
 /// Required. Details for the template image to be created or updated.
 /// </param>
 /// <returns>
 /// Operation response for TemplateImageOperations.Get.
 /// </returns>
 public static Task<TemplateImageResult> SetAsync(this ITemplateImageOperations operations, TemplateImageDetails imageDetails)
 {
     return operations.SetAsync(imageDetails, CancellationToken.None);
 }
 /// <summary>
 /// Sets a template image details.  If the template image already
 /// exists, only the Name can be changed.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.RemoteApp.ITemplateImageOperations.
 /// </param>
 /// <param name='imageDetails'>
 /// Required. Details for the template image to be created or updated.
 /// </param>
 /// <returns>
 /// Operation response for TemplateImageOperations.Get.
 /// </returns>
 public static TemplateImageResult Set(this ITemplateImageOperations operations, TemplateImageDetails imageDetails)
 {
     return Task.Factory.StartNew((object s) => 
     {
         return ((ITemplateImageOperations)s).SetAsync(imageDetails);
     }
     , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
 }
        public void CanCreateTemplateImagesSuccessfully()
        {
            using (UndoContext context = UndoContext.Current)
            {
                context.Start();
                string imageName = "my_unit_test_created_templateimage";
                string returnedImageName = null;
                HttpRecorderMode mode = HttpMockServer.GetCurrentMode();
                RemoteAppManagementClient client = GetRemoteAppManagementClient();
                TemplateImageResult setResponse;
                AzureOperationResponse deleteResponse;
                TemplateImageListResult listResponse = null;
                IList<TemplateImage> imageList;
                TemplateImage myImage = null;
                bool foundTestImage = false;
                string newName = "renamed_my_test_image";
                TemplateImageDetails renameDetails = null;

                TemplateImageDetails newImageDetails = new TemplateImageDetails()
                {
                    Name = imageName,
                    Region = "West US"
                };

                Assert.DoesNotThrow(() =>
                {
                    setResponse = client.TemplateImages.Set(newImageDetails);
                });

                // now check the list
                Assert.DoesNotThrow(() =>
                {
                    listResponse = client.TemplateImages.List();
                    imageList = listResponse.RemoteAppTemplateImageList;

                    foreach (TemplateImage image in imageList)
                    {
                        if ((image.Name == imageName))
                        {
                            foreach (string region in image.RegionList)
                            {
                                if (region == newImageDetails.Region)
                                {
                                    foundTestImage = true;
                                    // cleanup
                                    returnedImageName = image.Name;
                                    myImage = image;

                                    break;
                                }
                            }
                        }
                    }
                });

                Assert.True(foundTestImage);
                Assert.False(string.IsNullOrEmpty(returnedImageName));
                Assert.NotNull(myImage);

                // rename the image
                renameDetails = new TemplateImageDetails()
                {
                    Id = myImage.Id,
                    Name = newName,
                    Region = "West US"
                };

                setResponse = null;

                Assert.DoesNotThrow(() =>
                {
                    setResponse = client.TemplateImages.Set(renameDetails);
                });

                Assert.NotNull(setResponse);
                Assert.NotNull(setResponse.TemplateImage);
                Assert.Equal(System.Net.HttpStatusCode.OK, setResponse.StatusCode);

                // verify that we have an image with the new name
                TemplateImageResult imageResponse = null;

                Assert.DoesNotThrow(() =>
                {
                    imageResponse = client.TemplateImages.Get(newName);
                });

                Assert.NotNull(imageResponse);
                Assert.NotNull(imageResponse.TemplateImage);
                Assert.Equal(newName, imageResponse.TemplateImage.Name);
                Assert.True(imageResponse.TemplateImage.RegionList.Contains("West US"), "Unexpected region of the renamed image: " + String.Join(", ", imageResponse.TemplateImage.RegionList));

                Assert.DoesNotThrow(() =>
                {
                    deleteResponse = client.TemplateImages.Delete(newName);
                });
            }
        }
        private void ImportTemplateImage()
        {
            TemplateImageResult response = null;
            TemplateImageDetails details = null;

            EnsureStorageInRegion(Location);
            FilterTemplateImage(ImageName, Operation.Create);

            details = new TemplateImageDetails()
            {
                Name = ImageName,
                Region = Location,
                SourceImageSasUri = GetAzureVmSasUri(AzureVmImageName)
            };

            response = CallClient(() => Client.TemplateImages.Set(details), Client.TemplateImages);

            if (response != null)
            {
                WriteObject(response.TemplateImage);
            }
        }
        private TemplateImage StartTemplateUpload(TemplateImage image)
        {
            TemplateImageResult response = null;
            TemplateImageDetails details = null;
            TemplateImage templateImage = null;

            EnsureStorageInRegion(Location);

            if (Resume)
            {
                templateImage = image;
            }
            else
            {
                details = new TemplateImageDetails()
                {
                    Name = ImageName,
                    Region = Location
                };

                response = CallClient_ThrowOnError(() => Client.TemplateImages.Set(details));

                templateImage = response.TemplateImage;
                if (templateImage == null)
                {
                    throw new RemoteAppServiceException(String.Format(
                        System.Globalization.CultureInfo.InvariantCulture,
                        Commands_RemoteApp.TemplateImageCreationFailedErrorFormat,
                        ImageName,
                        Location)
                        , ErrorCategory.InvalidResult);
                }
            }

            return templateImage;
        }