Exemplo n.º 1
0
 public static PSMediaService ToPSMediaService(this RestMediaService mediaService)
 {
     return(new PSMediaService
     {
         Id = mediaService.Id,
         AccountName = mediaService.Name,
         Type = mediaService.Type,
         Location = mediaService.Location,
         Tags = mediaService.Tags.ToHashTableTags(),
         ApiEndpoints = mediaService.ApiEndpoints.Select(x => x.ToPSApiEndpoint()).ToList(),
         StorageAccounts = mediaService.StorageAccounts.Select(x => x.ToPSStorageAccount()).ToList()
     });
 }
        public override void ExecuteCmdlet()
        {
            if (ShouldProcess(AccountName, NewMediaServiceWhatIfMessage))
            {
                try
                {
                    var mediaService = MediaServicesManagementClient.MediaService.Get(ResourceGroupName, AccountName);
                    if (mediaService != null)
                    {
                        throw new ArgumentException(string.Format(Properties.Resource.InvalidMediaServiceAccount,
                                                                  AccountName,
                                                                  SubscrptionName,
                                                                  ResourceGroupName));
                    }
                }
                catch (ApiErrorException exception)
                {
                    if (exception.Response != null && exception.Response.StatusCode.Equals(HttpStatusCode.NotFound))
                    {
                        var restMediaService = new RestMediaService(
                            Location,
                            Tags.ToDictionaryTags(),
                            null,
                            null,
                            MediaServiceType);

                        switch (ParameterSetName)
                        {
                        case PrimaryStorageAccountParamSet:
                            restMediaService.StorageAccounts = new List <StorageAccount>
                            {
                                new StorageAccount
                                {
                                    Id        = StorageAccountId,
                                    IsPrimary = true
                                }
                            };
                            break;

                        case StorageAccountsParamSet:
                            if (StorageAccounts.Count(x => x.IsPrimary.HasValue && x.IsPrimary.Value) != 1)
                            {
                                throw new ArgumentException(Properties.Resource.OnlyOnePrimaryStorageAccountAllowed);
                            }

                            restMediaService.StorageAccounts = StorageAccounts.Select(x => x.ToStorageAccount()).ToList();
                            break;

                        default:
                            throw new ArgumentException("Bad ParameterSet Name");
                        }

                        var mediaServiceCreated = MediaServicesManagementClient.MediaService.Create(ResourceGroupName, AccountName, restMediaService);
                        WriteObject(mediaServiceCreated.ToPSMediaService(), true);
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }
Exemplo n.º 3
0
        public override void ExecuteCmdlet()
        {
            if (ShouldProcess(AccountName, SetMediaServiceWhatIfMessage))
            {
                var mediaServiceParams = new RestMediaService();

                if (Tags != null)
                {
                    mediaServiceParams.Tags = Tags.ToDictionaryTags();
                }

                if (StorageAccounts != null)
                {
                    // check storage accounts parameter
                    var primaryStorageAccounts = StorageAccounts.Where(x => x.IsPrimary.HasValue && x.IsPrimary.Value).ToArray();
                    if (primaryStorageAccounts.Count() != 1)
                    {
                        throw new ArgumentException(Properties.Resource.OnlyOnePrimaryStorageAccountAllowed);
                    }

                    var mediaService = MediaServicesManagementClient.MediaService.Get(ResourceGroupName, AccountName);
                    if (mediaService == null)
                    {
                        throw new ArgumentException(string.Format(Properties.Resource.InvalidMediaServiceAccount,
                                                                  AccountName,
                                                                  SubscrptionName,
                                                                  ResourceGroupName));
                    }

                    var primaryStorageAccount = mediaService.StorageAccounts.FirstOrDefault(x => (x.IsPrimary.HasValue && x.IsPrimary.Value));
                    // there must be a primary storage account associated with the media service account
                    if (primaryStorageAccount == null)
                    {
                        throw new Exception(string.Format(
                                                Properties.Resource.InvalidMediaServiceAccount,
                                                AccountName,
                                                SubscrptionName,
                                                ResourceGroupName));
                    }

                    if (!string.Equals(primaryStorageAccount.Id, primaryStorageAccounts[0].Id, StringComparison.OrdinalIgnoreCase))
                    {
                        throw new ArgumentException("Primary storage account cann't be changed");
                    }

                    mediaServiceParams.StorageAccounts = StorageAccounts.Select(x => x.ToStorageAccount()).ToList();
                }

                try
                {
                    var mediaServiceUpdated = MediaServicesManagementClient.MediaService.Update(ResourceGroupName,
                                                                                                AccountName, mediaServiceParams);
                    WriteObject(mediaServiceUpdated.ToPSMediaService(), true);
                }
                catch (ApiErrorException exception)
                {
                    if (exception.Response.StatusCode.Equals(HttpStatusCode.NotFound))
                    {
                        throw new ArgumentException(string.Format(Properties.Resource.InvalidMediaServiceAccount,
                                                                  AccountName,
                                                                  SubscrptionName,
                                                                  ResourceGroupName));
                    }
                    throw;
                }
            }
        }
        public override void ExecuteCmdlet()
        {
            if (ShouldProcess(AccountName, SetMediaServiceWhatIfMessage))
            {
                var mediaServiceParams = new RestMediaService();

                if (Tags != null)
                {
                    mediaServiceParams.Tags = Tags.ToDictionaryTags();
                }

                if (StorageAccounts != null)
                {
                    // check storage accounts parameter
                    var primaryStorageAccounts = StorageAccounts.Where(x => x.IsPrimary.HasValue && x.IsPrimary.Value).ToArray();
                    if (primaryStorageAccounts.Count() != 1)
                    {
                        throw new ArgumentException(Properties.Resource.OnlyOnePrimaryStorageAccountAllowed);
                    }

                    var mediaService = MediaServicesManagementClient.MediaService.Get(ResourceGroupName, AccountName);
                    if (mediaService == null)
                    {
                        throw new ArgumentException(string.Format(Properties.Resource.InvalidMediaServiceAccount,
                            AccountName,
                            SubscrptionName,
                            ResourceGroupName));
                    }

                    var primaryStorageAccount = mediaService.StorageAccounts.FirstOrDefault(x => (x.IsPrimary.HasValue && x.IsPrimary.Value));
                    // there must be a primary storage account associated with the media service account
                    if (primaryStorageAccount == null)
                    {
                        throw new Exception(string.Format(
                            Properties.Resource.InvalidMediaServiceAccount,
                            AccountName,
                            SubscrptionName,
                            ResourceGroupName));
                    }

                    if (!string.Equals(primaryStorageAccount.Id, primaryStorageAccounts[0].Id, StringComparison.OrdinalIgnoreCase))
                    {
                        throw new ArgumentException("Primary storage account cann't be changed");
                    }

                    mediaServiceParams.StorageAccounts = StorageAccounts.Select(x => x.ToStorageAccount()).ToList();
                }

                try
                {
                    var mediaServiceUpdated = MediaServicesManagementClient.MediaService.Update(ResourceGroupName,
                                AccountName, mediaServiceParams);
                    WriteObject(mediaServiceUpdated.ToPSMediaService(), true);
                }
                catch (ApiErrorException exception)
                {
                    if (exception.Response.StatusCode.Equals(HttpStatusCode.NotFound))
                    {
                        throw new ArgumentException(string.Format(Properties.Resource.InvalidMediaServiceAccount,
                            AccountName,
                            SubscrptionName,
                            ResourceGroupName));
                    }
                    throw;
                }
            }
        }
        public override void ExecuteCmdlet()
        {
            if (ShouldProcess(AccountName, NewMediaServiceWhatIfMessage))
            {
                try
                {
                    var mediaService = MediaServicesManagementClient.MediaService.Get(ResourceGroupName, AccountName);
                    if (mediaService != null)
                    {
                        throw new ArgumentException(string.Format(Properties.Resource.InvalidMediaServiceAccount,
                            AccountName,
                            SubscrptionName,
                            ResourceGroupName));
                    }
                }
                catch (ApiErrorException exception)
                {
                    if (exception.Response != null && exception.Response.StatusCode.Equals(HttpStatusCode.NotFound))
                    {
                        var restMediaService = new RestMediaService(
                            Location,
                            Tags.ToDictionaryTags(),
                            null,
                            null,
                            MediaServiceType);

                        switch (ParameterSetName)
                        {
                            case PrimaryStorageAccountParamSet:
                                restMediaService.StorageAccounts = new List<StorageAccount>
                            {
                                new StorageAccount
                                {
                                    Id = StorageAccountId,
                                    IsPrimary = true
                                }
                            };
                                break;

                            case StorageAccountsParamSet:
                                if (StorageAccounts.Count(x => x.IsPrimary.HasValue && x.IsPrimary.Value) != 1)
                                {
                                    throw new ArgumentException(Properties.Resource.OnlyOnePrimaryStorageAccountAllowed);
                                }

                                restMediaService.StorageAccounts = StorageAccounts.Select(x => x.ToStorageAccount()).ToList();
                                break;

                            default:
                                throw new ArgumentException("Bad ParameterSet Name");
                        }

                        var mediaServiceCreated = MediaServicesManagementClient.MediaService.Create(ResourceGroupName, AccountName, restMediaService);
                        WriteObject(mediaServiceCreated.ToPSMediaService(), true);
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }