Beispiel #1
0
 /// <summary>
 /// Create a swagger path description
 /// </summary>
 public SwaggerPathDefinition(ServiceOperationDescription description)
 {
     this.Consumes   = new List <string>(description.Accepts);
     this.Produces   = new List <string>(description.Produces);
     this.Tags       = new List <string>(description.Tags);
     this.Parameters = description.Parameters.Select(o => new SwaggerParameter(o)).ToList();
     this.Responses  = description.Responses.ToDictionary(o => (int)o.Key, o => new SwaggerSchemaElement(o.Value));
     if (description.RequiresAuth)
     {
         this.Security = new List <SwaggerPathSecurity>()
         {
             new SwaggerPathSecurity()
             {
                 { "oauth_user", new List <string>()
                   {
                       PermissionPolicyIdentifiers.Login
                   } }
             }
         };
     }
 }
        private ServiceUpdateDescription CreateServiceUpdateDescription(ServiceOperationDescription serviceOperationDescription)
        {
            string errorMessage;
            ServiceUpdateDescription serviceUpdateDescription;

            switch (serviceOperationDescription.ServiceKind)
            {
            case ArmServiceKind.Stateful:
                if (!this.ValidObjectType <StatefulServiceOperationDescription>(serviceOperationDescription, out errorMessage))
                {
                    throw new InvalidCastException(errorMessage);
                }

                var statefulOperation = (StatefulServiceOperationDescription)serviceOperationDescription;
                serviceUpdateDescription = new StatefulServiceUpdateDescription()
                {
                    MinReplicaSetSize          = statefulOperation.MinReplicaSetSize,
                    QuorumLossWaitDuration     = statefulOperation.QuorumLossWaitDuration,
                    ReplicaRestartWaitDuration = statefulOperation.ReplicaRestartWaitDuration,
                    StandByReplicaKeepDuration = statefulOperation.StandByReplicaKeepDuration,
                    TargetReplicaSetSize       = statefulOperation.TargetReplicaSetSize
                };

                break;

            case ArmServiceKind.Stateless:
                if (!this.ValidObjectType <StatelessServiceOperationDescription>(serviceOperationDescription, out errorMessage))
                {
                    throw new InvalidCastException(errorMessage);
                }

                var statelessOperation = (StatelessServiceOperationDescription)serviceOperationDescription;
                serviceUpdateDescription = new StatelessServiceUpdateDescription()
                {
                    InstanceCount = statelessOperation.InstanceCount
                };

                break;

            default:
                throw new ArgumentOutOfRangeException(
                          nameof(serviceOperationDescription.ServiceKind),
                          serviceOperationDescription.ServiceKind,
                          $"{this.TraceType}: Unexpected ArmServiceKind");
            }

            if (serviceOperationDescription.DefaultMoveCost.HasValue)
            {
                serviceUpdateDescription.DefaultMoveCost =
                    (MoveCost)Enum.Parse(
                        typeof(MoveCost),
                        serviceOperationDescription.DefaultMoveCost.Value.ToString());
            }

            serviceUpdateDescription.PlacementConstraints = serviceOperationDescription.PlacementConstraints;

            if (serviceOperationDescription.CorrelationScheme != null)
            {
                serviceUpdateDescription.Correlations = new List <ServiceCorrelationDescription>();
                foreach (var scheme in serviceOperationDescription.CorrelationScheme)
                {
                    serviceUpdateDescription.Correlations.Add(this.GetServiceCorrelationDescription(scheme));
                }
            }

            if (serviceOperationDescription.ServiceLoadMetrics != null)
            {
                serviceUpdateDescription.Metrics = new KeyedItemCollection <string, ServiceLoadMetricDescription>(n => n.Name);
                foreach (var metric in serviceOperationDescription.ServiceLoadMetrics)
                {
                    serviceUpdateDescription.Metrics.Add(this.GetServiceLoadMetricDescription(metric));
                }
            }

            if (serviceOperationDescription.ServicePlacementPolicies != null)
            {
                serviceUpdateDescription.PlacementPolicies = new List <ServicePlacementPolicyDescription>();
                foreach (var policy in serviceOperationDescription.ServicePlacementPolicies)
                {
                    serviceUpdateDescription.PlacementPolicies.Add(this.GetServicePlacementPolicyDescription(policy));
                }
            }

            return(serviceUpdateDescription);
        }
        private ServiceDescription CreateServiceDescription(ServiceOperationDescription serviceOperationDescription)
        {
            string             errorMessage;
            ServiceDescription serviceDescription;

            switch (serviceOperationDescription.ServiceKind)
            {
            case ArmServiceKind.Stateful:
                if (!this.ValidObjectType <StatefulServiceOperationDescription>(serviceOperationDescription, out errorMessage))
                {
                    throw new InvalidCastException(errorMessage);
                }

                var statefulOperation = (StatefulServiceOperationDescription)serviceOperationDescription;
                serviceDescription = new StatefulServiceDescription()
                {
                    HasPersistedState          = statefulOperation.HasPersistedState,
                    MinReplicaSetSize          = statefulOperation.MinReplicaSetSize,
                    TargetReplicaSetSize       = statefulOperation.TargetReplicaSetSize,
                    QuorumLossWaitDuration     = statefulOperation.QuorumLossWaitDuration,
                    ReplicaRestartWaitDuration = statefulOperation.ReplicaRestartWaitDuration,
                    StandByReplicaKeepDuration = statefulOperation.StandByReplicaKeepDuration
                };

                break;

            case ArmServiceKind.Stateless:
                if (!this.ValidObjectType <StatelessServiceOperationDescription>(serviceOperationDescription, out errorMessage))
                {
                    throw new InvalidCastException(errorMessage);
                }

                var statelessOperation = (StatelessServiceOperationDescription)serviceOperationDescription;
                serviceDescription = new StatelessServiceDescription()
                {
                    InstanceCount = statelessOperation.InstanceCount
                };

                break;

            default:
                throw new ArgumentOutOfRangeException(
                          nameof(serviceOperationDescription.ServiceKind),
                          serviceOperationDescription.ServiceKind,
                          $"{this.TraceType}: Unexpected ArmServiceKind");
            }

            serviceDescription.ApplicationName = serviceOperationDescription.ApplicationName;
            serviceDescription.ServiceName     = serviceOperationDescription.ServiceName;

            if (serviceOperationDescription.DefaultMoveCost.HasValue)
            {
                serviceDescription.DefaultMoveCost =
                    (MoveCost)Enum.Parse(
                        typeof(MoveCost),
                        serviceOperationDescription.DefaultMoveCost.Value.ToString());
            }

            serviceDescription.PlacementConstraints = serviceOperationDescription.PlacementConstraints;
            serviceDescription.ServiceTypeName      = serviceOperationDescription.ServiceTypeName;

            if (serviceOperationDescription.CorrelationScheme != null)
            {
                foreach (var scheme in serviceOperationDescription.CorrelationScheme)
                {
                    serviceDescription.Correlations.Add(this.GetServiceCorrelationDescription(scheme));
                }
            }

            if (serviceOperationDescription.ServiceLoadMetrics != null)
            {
                foreach (var metric in serviceOperationDescription.ServiceLoadMetrics)
                {
                    serviceDescription.Metrics.Add(this.GetServiceLoadMetricDescription(metric));
                }
            }

            if (serviceOperationDescription.ServicePlacementPolicies != null)
            {
                foreach (var policy in serviceOperationDescription.ServicePlacementPolicies)
                {
                    serviceDescription.PlacementPolicies.Add(this.GetServicePlacementPolicyDescription(policy));
                }
            }

            PartitionSchemeDescription partitionSchemeDescription;

            switch (serviceOperationDescription.PartitionDescription.PartitionScheme)
            {
            case ArmPartitionScheme.Named:
                if (!this.ValidObjectType <ArmNamedPartitionDescription>(serviceOperationDescription.PartitionDescription, out errorMessage))
                {
                    throw new InvalidCastException(errorMessage);
                }

                var namedOperation = (ArmNamedPartitionDescription)serviceOperationDescription.PartitionDescription;
                partitionSchemeDescription = new NamedPartitionSchemeDescription(namedOperation.Names);
                break;

            case ArmPartitionScheme.Singleton:
                partitionSchemeDescription = new SingletonPartitionSchemeDescription();
                break;

            case ArmPartitionScheme.UniformInt64Range:
                if (!this.ValidObjectType <ArmUniformInt64PartitionDescription>(serviceOperationDescription.PartitionDescription, out errorMessage))
                {
                    throw new InvalidCastException(errorMessage);
                }

                var uniformOperation = (ArmUniformInt64PartitionDescription)serviceOperationDescription.PartitionDescription;
                partitionSchemeDescription = new UniformInt64RangePartitionSchemeDescription()
                {
                    HighKey        = uniformOperation.HighKey,
                    LowKey         = uniformOperation.LowKey,
                    PartitionCount = uniformOperation.Count
                };

                break;

            default:
                throw new ArgumentOutOfRangeException(
                          nameof(serviceOperationDescription.PartitionDescription.PartitionScheme),
                          serviceOperationDescription.PartitionDescription.PartitionScheme,
                          $"{this.TraceType}: Unexpected ArmPartitionScheme");
            }

            serviceDescription.PartitionSchemeDescription = partitionSchemeDescription;

            return(serviceDescription);
        }