/// <summary>
        /// Executes the cmdlet.
        /// </summary>
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();

            ExecuteClientAction(() =>
            {
                // Validate Storage Account Resource Id
                var storageAccountResourceIdentifier = new ResourceIdentifier(StorageAccountResourceId);

                if (string.IsNullOrEmpty(storageAccountResourceIdentifier?.ResourceName))
                {
                    throw new PSArgumentException(nameof(StorageAccountResourceId));
                }

                PSADServicePrincipal servicePrincipal = StorageSyncClientWrapper.EnsureServicePrincipal();
                RoleAssignment roleAssignment         = StorageSyncClientWrapper.EnsureRoleAssignment(servicePrincipal, StorageAccountResourceId);

                var parentResourceIdentifier = default(ResourceIdentifier);

                if (this.IsParameterBound(c => c.ParentResourceId))
                {
                    parentResourceIdentifier = new ResourceIdentifier(ParentResourceId);

                    if (!string.Equals(StorageSyncConstants.SyncGroupType, parentResourceIdentifier.ResourceType, System.StringComparison.OrdinalIgnoreCase))
                    {
                        throw new PSArgumentException(StorageSyncResources.MissingParentResourceIdErrorMessage);
                    }
                }

                var createParameters = new CloudEndpointCreateParameters()
                {
                    StorageAccountResourceId = StorageAccountResourceId,
                    AzureFileShareName       = AzureFileShareName,
                    StorageAccountTenantId   = (StorageAccountTenantId ?? DefaultContext.Tenant?.Id)
                };

                string resourceGroupName      = ResourceGroupName ?? ParentObject?.ResourceGroupName ?? parentResourceIdentifier.ResourceGroupName;
                string storageSyncServiceName = StorageSyncServiceName ?? ParentObject?.StorageSyncServiceName ?? parentResourceIdentifier.GetParentResourceName(StorageSyncConstants.StorageSyncServiceTypeName, 0);
                string syncGroupName          = SyncGroupName ?? ParentObject?.SyncGroupName ?? parentResourceIdentifier.ResourceName;

                Target = string.Join("/", resourceGroupName, storageSyncServiceName, syncGroupName, Name);

                if (ShouldProcess(Target, ActionMessage))
                {
                    StorageSyncModels.CloudEndpoint resource = StorageSyncClientWrapper.StorageSyncManagementClient.CloudEndpoints.Create(
                        resourceGroupName,
                        storageSyncServiceName,
                        syncGroupName,
                        Name,
                        createParameters);

                    WriteObject(resource);
                }
            });
        }
 /// <summary>
 /// Writes the object.
 /// </summary>
 /// <param name="resource">The resource.</param>
 protected void WriteObject(StorageSyncModels.CloudEndpoint resource)
 {
     WriteObject(new CloudEndpointConverter().Convert(resource));
 }
Example #3
0
        /// <summary>
        /// Executes the cmdlet.
        /// </summary>
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();

            ExecuteClientAction(() =>
            {
                // Validate Storage Account Resource Id
                var storageAccountResourceIdentifier = new ResourceIdentifier(StorageAccountResourceId);

                if (string.IsNullOrEmpty(storageAccountResourceIdentifier?.ResourceName))
                {
                    throw new PSArgumentException(nameof(StorageAccountResourceId));
                }

                if (this.IsParameterBound(c => c.StorageAccountTenantId))
                {
                    if (StorageAccountTenantId != AzureContext.Tenant.Id)
                    {
                        throw new PSArgumentException(string.Format(StorageSyncResources.NewCloudEndpointCrossTenantErrorFormat, StorageAccountTenantId, AzureContext.Tenant.Id));
                    }
                }

                if (storageAccountResourceIdentifier.Subscription != AzureContext.Subscription.Id)
                {
                    WriteWarning(string.Format(StorageSyncResources.NewCloudEndpointCrossSubscriptionWarningFormat, storageAccountResourceIdentifier.Subscription, AzureContext.Subscription.Id));

                    if (!StorageSyncClientWrapper.TryRegisterProvider(AzureContext.Subscription.Id, StorageSyncConstants.ResourceProvider, storageAccountResourceIdentifier.Subscription))
                    {
                        WriteWarning(string.Format(StorageSyncResources.NewCloudEndpointUnableToRegisterErrorFormat, storageAccountResourceIdentifier.Subscription));
                    }
                }

                MicrosoftGraphServicePrincipal servicePrincipal = StorageSyncClientWrapper.GetServicePrincipalOrNull();

                if (servicePrincipal == null)
                {
                    throw new PSArgumentException(StorageSyncResources.MissingServicePrincipalResourceIdErrorMessage);
                }
                RoleAssignment roleAssignment = StorageSyncClientWrapper.EnsureRoleAssignment(servicePrincipal, storageAccountResourceIdentifier.Subscription, StorageAccountResourceId);

                var parentResourceIdentifier = default(ResourceIdentifier);

                if (this.IsParameterBound(c => c.ParentResourceId))
                {
                    parentResourceIdentifier = new ResourceIdentifier(ParentResourceId);

                    if (!string.Equals(StorageSyncConstants.SyncGroupType, parentResourceIdentifier.ResourceType, System.StringComparison.OrdinalIgnoreCase))
                    {
                        throw new PSArgumentException(StorageSyncResources.MissingParentResourceIdErrorMessage);
                    }
                }

                var createParameters = new CloudEndpointCreateParameters()
                {
                    StorageAccountResourceId = StorageAccountResourceId,
                    AzureFileShareName       = AzureFileShareName,
                    StorageAccountTenantId   = (StorageAccountTenantId ?? AzureContext.Tenant.Id)
                };

                string resourceGroupName      = ResourceGroupName ?? ParentObject?.ResourceGroupName ?? parentResourceIdentifier.ResourceGroupName;
                string storageSyncServiceName = StorageSyncServiceName ?? ParentObject?.StorageSyncServiceName ?? parentResourceIdentifier.GetParentResourceName(StorageSyncConstants.StorageSyncServiceTypeName, 0);
                string syncGroupName          = SyncGroupName ?? ParentObject?.SyncGroupName ?? parentResourceIdentifier.ResourceName;

                Target = string.Join("/", resourceGroupName, storageSyncServiceName, syncGroupName, Name);

                if (ShouldProcess(Target, ActionMessage))
                {
                    StorageSyncModels.CloudEndpoint resource = StorageSyncClientWrapper.StorageSyncManagementClient.CloudEndpoints.Create(
                        resourceGroupName,
                        storageSyncServiceName,
                        syncGroupName,
                        Name,
                        createParameters);

                    WriteObject(resource);
                }
            });
        }