예제 #1
0
        public void LocationRange()
        {
            var s = LocationId.Create(Aws, 1, 000);
            var e = LocationId.Create(Aws, 1, 255);

            Assert.Equal(33554688, s.Value);
            Assert.Equal(33554943, e.Value);
        }
        public static LocationId GetLocationId(long locationId)
        {
            var locationExistenceValidatorMock = new Mock <ILocationExistenceValidator>();

            locationExistenceValidatorMock
            .Setup(validator => validator.Exist(It.IsAny <long>()))
            .Returns(true);
            return(LocationId.Create(locationId, locationExistenceValidatorMock.Object).Value);
        }
예제 #3
0
        public void MultiRegion()
        {
            var eu = Locations.Gcp.EU;

            var id = LocationId.Create(eu.Id);

            Assert.Equal(3, id.ProviderId);
            Assert.Equal(0, id.RegionNumber);
            Assert.Equal(2, id.ZoneNumber);
        }
예제 #4
0
        public void ShouldReturnExpectedResult(long locationId, bool exist, bool isSuccess)
        {
            var locationExistenceValidator = new Mock <ILocationExistenceValidator>();

            locationExistenceValidator
            .Setup(validator => validator.Exist(It.IsAny <long>()))
            .Returns(exist);

            var locationIdResult = LocationId.Create(locationId, locationExistenceValidator.Object);

            locationIdResult.IsSuccess.Should().Be(isSuccess);

            if (isSuccess)
            {
                locationIdResult.Value.Value.Should().Be(locationId);
            }
        }
예제 #5
0
        public async Task <IHost[]> LaunchAsync(Cluster cluster, ISecurityContext context)
        {
            Validate.NotNull(cluster, nameof(cluster));
            Validate.NotNull(context, nameof(context));

            var zoneId = LocationId.Create(cluster.LocationId).WithZoneNumber(1);

            var zone = Locations.Get(zoneId);

            // TODO:
            // if the location is a region, balance hosts within available zones

            var template = await hostTemplateService.GetAsync(cluster.HostTemplateId);

            var request = new LaunchHostRequest(cluster, zone, template);

            return(await LaunchAsync(request, context));;
        }
예제 #6
0
        public void LocationIdTest()
        {
            var ab = LocationId.Create(33554688); // Amazon_USEast1

            Assert.Equal(2, ab.ProviderId);       // Amazon
            Assert.Equal(1, ab.RegionNumber);     // 1
            Assert.Equal(0, ab.ZoneNumber);       // 0
            Assert.Equal(33554688, ab.Value);

            Assert.Equal(33554689, Locations.Aws.USEast1.WithZone('A').Id);
            Assert.Equal(33554690, Locations.Aws.USEast1.WithZone('B').Id);
            Assert.Equal(33554691, Locations.Aws.USEast1.WithZone('C').Id);
            Assert.Equal(33554692, Locations.Aws.USEast1.WithZone('D').Id);
            Assert.Equal(33554693, Locations.Aws.USEast1.WithZone('E').Id);

            var zone = new Location(ab, "us-east-1");

            Assert.Equal(Aws.Id, zone.ProviderId);
            Assert.Equal(LocationType.Region, zone.Type);
        }
        public static Result <CreateExamCommand> Create(CreateExamRequest request,
                                                        ISubjectExistenceValidator subjectExistenceValidator,
                                                        ILocationExistenceValidator locationExistenceValidator)
        {
            var subjectId = SubjectId.Create(request.SubjectId, subjectExistenceValidator)
                            .BindErrorsTo(nameof(request.SubjectId));
            var locationId = LocationId.Create(request.LocationId, locationExistenceValidator)
                             .BindErrorsTo(nameof(request.LocationId));
            var examDateTime = UtcDateTime.Create(request.ExamDateTime)
                               .BindErrorsTo(nameof(request.ExamDateTime));
            var capacity = Capacity.Create(request.Capacity)
                           .BindErrorsTo(nameof(request.Capacity));
            var registrationStartDate = UtcDate.Create(request.RegistrationStartDate)
                                        .BindErrorsTo(nameof(request.RegistrationStartDate));
            var registrationEndDate = UtcDate.Create(request.RegistrationEndDate)
                                      .BindErrorsTo(nameof(request.RegistrationEndDate));

            return(Result.Combine(subjectId, locationId, examDateTime, capacity, registrationStartDate,
                                  registrationEndDate)
                   .OnSuccess(() => new CreateExamCommand(subjectId.Value, locationId.Value, examDateTime.Value,
                                                          capacity.Value, registrationStartDate.Value, registrationEndDate.Value)));
        }
예제 #8
0
            // South Africa West
            // South Africa North
            // Australia Central

            public static Location Create(ushort regionNumber, string name)
            {
                return(new Location(LocationId.Create(ResourceProvider.Azure, regionNumber), name));
            }
예제 #9
0
 public void BorgZones()
 {
     Assert.Equal(16777472, LocationId.Create(Borg, 1));
     Assert.Equal(16777217, LocationId.Create(Borg, 0, 1));
 }
예제 #10
0
        public async Task <IHost[]> LaunchAsync(LaunchHostRequest launchRequest, ISecurityContext context)
        {
            Validate.NotNull(launchRequest, nameof(launchRequest));
            Validate.NotNull(context, nameof(context));

            var zoneId = LocationId.Create(launchRequest.Location.Id);

            if (zoneId.ZoneNumber == 0)
            {
                throw new Exception("Must launch within in availability zone. Was a region:" + launchRequest.Location.Name);
            }

            var cluster  = launchRequest.Cluster;
            var zone     = launchRequest.Location;
            var template = launchRequest.Template;

            var region = Locations.Get(zoneId.WithZoneNumber(0));

            var image = await imageService.GetAsync(template.ImageId);;

            var machineType = AwsInstanceType.Get(template.MachineTypeId);

            var request = new RunInstancesRequest {
                ClientToken       = Guid.NewGuid().ToString(),
                InstanceType      = machineType.Name,
                ImageId           = image.ResourceId,
                MinCount          = launchRequest.LaunchCount,
                MaxCount          = launchRequest.LaunchCount,
                Placement         = new Placement(availabilityZone: zone.Name),
                TagSpecifications = new[] {
                    new TagSpecification(
                        resourceType: "instance",
                        tags: new[] { new Amazon.Ec2.Tag("envId", cluster.EnvironmentId.ToString()) }
                        )
                }
            };

            var startupScript = launchRequest.StartupScript ?? template.StartupScript;

            if (startupScript != null)
            {
                request.UserData = Convert.ToBase64String(Encoding.UTF8.GetBytes(startupScript));
            }

            #region AWS Specific Properties

            foreach (var property in template.Properties)
            {
                switch (property.Key)
                {
                case HostTemplateProperties.IamRole:
                    // NOTE: This requires the PassRole permission
                    // https://aws.amazon.com/blogs/security/granting-permission-to-launch-ec2-instances-with-iam-roles-passrole-permission/

                    request.IamInstanceProfile = new IamInstanceProfileSpecification(property.Value);

                    break;

                case HostTemplateProperties.KernelId:
                    request.KernelId = property.Value;

                    break;

                case HostTemplateProperties.SecurityGroupIds:
                    request.SecurityGroupIds = property.Value.ToArrayOf <string>();

                    break;

                case HostTemplateProperties.EbsOptimized:
                    request.EbsOptimized = (bool)property.Value;

                    break;

                case HostTemplateProperties.Monitoring:
                    request.Monitoring = new RunInstancesMonitoringEnabled((bool)property.Value);

                    break;

                case HostTemplateProperties.Volume:
                    var volSpec = property.Value.As <AwsVolumeSpecification>();

                    // TODO: Device Name
                    request.BlockDeviceMappings = new[] {
                        new BlockDeviceMapping(BlockDeviceNames.Root, new EbsBlockDevice(
                                                   volumeType: volSpec.Type,
                                                   volumeSize: (int)volSpec.Size
                                                   ))
                    };

                    break;

                case HostTemplateProperties.KeyName:
                    request.KeyName = property.Value;

                    break;
                }
            }

            #endregion

            var runInstancesResponse = await ec2.RunInstancesAsync(request);;

            var hosts = new IHost[runInstancesResponse.Instances.Length];

            for (var i = 0; i < hosts.Length; i++)
            {
                var registerRequest = await GetRegistrationAsync(
                    instance : runInstancesResponse.Instances[i],
                    cluster : cluster,
                    image : image,
                    machineType : machineType,
                    location : zone
                    );

                hosts[i] = await hostService.RegisterAsync(registerRequest);;
            }

            #region Logging

            await eventLog.CreateAsync(new Event(
                                           action   : "launch",
                                           resource : "hosts",
                                           userId   : context.UserId)
                                       );;

            #endregion

            return(hosts);
        }