public override void ExecuteCmdlet()
        {
            try
            {
                switch (ParameterSetName)
                {
                case ByResourceGroupAndCluster:
                    var managedServiceList = this.ReturnListByPageResponse(
                        this.SfrpMcClient.Services.ListByApplications(this.ResourceGroupName, this.ClusterName, this.ApplicationName),
                        this.SfrpMcClient.Services.ListByApplicationsNext);
                    WriteObject(managedServiceList.Select(service => PSManagedService.GetInstance(service)), true);
                    break;

                case ByName:
                    GetByName();
                    break;

                case ByResourceId:
                    SetParametersByResourceId(this.ResourceId);
                    GetByName();
                    break;

                default:
                    throw new PSArgumentException("Invalid ParameterSetName");
                }
            }
            catch (Exception ex)
            {
                this.PrintSdkExceptionDetail(ex);
                throw;
            }
        }
 public override void ExecuteCmdlet()
 {
     if (ShouldProcess(target: this.Name, action: $"Create new Service. name {this.Name} in application: {this.ApplicationName}, cluster {this.ClusterName} in resource group {this.ResourceGroupName}"))
     {
         try
         {
             ManagedCluster cluster = SafeGetResource(() => this.SfrpMcClient.ManagedClusters.Get(this.ResourceGroupName, this.ClusterName));
             if (cluster == null)
             {
                 WriteError(new ErrorRecord(new InvalidOperationException($"Parent cluster '{this.ClusterName}' does not exist."),
                                            "ResourceDoesNotExist", ErrorCategory.InvalidOperation, null));
             }
             else
             {
                 var service = CreateService(cluster.Location);
                 WriteObject(PSManagedService.GetInstance(service), false);
             }
         }
         catch (Exception ex)
         {
             PrintSdkExceptionDetail(ex);
             throw;
         }
     }
 }
        public override void ExecuteCmdlet()
        {
            try
            {
                this.SetParams();
                ServiceResource updatedServiceParams = null;
                switch (ParameterSetName)
                {
                case StatefulByResourceGroup:
                case StatelessByResourceGroup:
                case StatefulByResourceId:
                case StatelessByResourceId:
                    updatedServiceParams = this.GetUpdatedServiceParams();
                    break;

                case StatefulByInputObject:
                case StatelessByInputObject:
                    updatedServiceParams = this.GetUpdatedServiceParams(this.InputObject);
                    break;

                default:
                    throw new ArgumentException("Invalid parameter set", ParameterSetName);
                }
                if (updatedServiceParams != null && ShouldProcess(target: this.Name, action: $"Update managed service name {this.Name}, cluster: {this.ClusterName} in resource group {this.ResourceGroupName}"))
                {
                    var beginRequestResponse = this.SfrpMcClient.Services.BeginCreateOrUpdateWithHttpMessagesAsync(this.ResourceGroupName, this.ClusterName, this.ApplicationName, this.Name, updatedServiceParams)
                                               .GetAwaiter().GetResult();

                    var managedService = this.PollLongRunningOperation(beginRequestResponse);

                    WriteObject(PSManagedService.GetInstance(managedService), false);
                }
            }
            catch (Exception ex)
            {
                PrintSdkExceptionDetail(ex);
                throw;
            }
        }
        private ServiceResource GetUpdatedServiceParams(PSManagedService inputObject = null)
        {
            ServiceResource currentService;

            if (inputObject == null)
            {
                currentService = SafeGetResource(() =>
                                                 this.SfrpMcClient.Services.Get(
                                                     this.ResourceGroupName,
                                                     this.ClusterName,
                                                     this.ApplicationName,
                                                     this.Name),
                                                 false);

                if (currentService == null)
                {
                    WriteError(new ErrorRecord(new InvalidOperationException($"Managed Service '{this.Name}' does not exist."),
                                               "ResourceDoesNotExist", ErrorCategory.InvalidOperation, null));
                    return(currentService);
                }
            }
            else
            {
                currentService = inputObject.ToServiceResource();
            }

            WriteVerbose($"Updating managed service '{this.Name}.'");

            if (this.IsParameterBound(c => c.Tag))
            {
                currentService.Tags = this.Tag?.Cast <DictionaryEntry>().ToDictionary(d => d.Key as string, d => d.Value as string);
            }
            ServiceResourceProperties properties = currentService.Properties;

            if (this.Stateless.ToBool())
            {
                StatelessServiceProperties statelessProperties = properties as  StatelessServiceProperties;

                if (this.IsParameterBound(c => c.InstanceCount))
                {
                    statelessProperties.InstanceCount = this.InstanceCount;
                }

                if (this.IsParameterBound(c => c.MinInstancePercentage))
                {
                    statelessProperties.MinInstancePercentage = this.MinInstancePercentage;
                }
                if (this.IsParameterBound(c => c.MinInstanceCount))
                {
                    statelessProperties.MinInstanceCount = this.MinInstanceCount;
                }
            }
            else if (this.Stateful.ToBool())
            {
                StatefulServiceProperties statefulProperties = properties as StatefulServiceProperties;
                if (this.IsParameterBound(c => c.ServicePlacementTimeLimit))
                {
                    statefulProperties.ServicePlacementTimeLimit = this.ServicePlacementTimeLimit.ToString();
                }
                if (this.IsParameterBound(c => c.StandByReplicaKeepDuration))
                {
                    statefulProperties.StandByReplicaKeepDuration = this.StandByReplicaKeepDuration.ToString();
                }
                if (this.IsParameterBound(c => c.QuorumLossWaitDuration))
                {
                    statefulProperties.QuorumLossWaitDuration = this.QuorumLossWaitDuration.ToString();
                }
                if (this.IsParameterBound(c => c.ReplicaRestartWaitDuration))
                {
                    statefulProperties.ReplicaRestartWaitDuration = this.ReplicaRestartWaitDuration.ToString();
                }
                if (this.IsParameterBound(c => c.HasPersistedState))
                {
                    statefulProperties.HasPersistedState = this.HasPersistedState.ToBool();
                }
                if (this.IsParameterBound(c => c.MinReplicaSetSize))
                {
                    statefulProperties.MinReplicaSetSize = this.MinReplicaSetSize;
                }
                if (this.IsParameterBound(c => c.TargetReplicaSetSize))
                {
                    statefulProperties.TargetReplicaSetSize = this.TargetReplicaSetSize;
                }
            }
            SetCommonProperties(properties);

            return(currentService);
        }
        private void GetByName()
        {
            var managedService = this.SfrpMcClient.Services.Get(this.ResourceGroupName, this.ClusterName, this.ApplicationName, this.Name);

            WriteObject(PSManagedService.GetInstance(managedService), false);
        }