public async Task <HostInfo> RegisterAsync(RegisterHostRequest request) { Validate.NotNull(request, nameof(request)); var provider = ResourceProvider.Get(request.Resource.ProviderId); var host = await hostService.FindAsync(provider, request.Resource.ResourceId);; if (host == null) { host = await hostService.RegisterAsync(request); } else { await TransitionStateAsync(host, request.Status);; } return(host); }
private async Task <RegisterHostRequest> GetRegistrationAsync( Instance instance, Cluster cluster, IImage image = null, ILocation location = null, IMachineType machineType = null) { Validate.NotNull(instance, nameof(instance)); // Ensure the instance is inside a VPC (no classic instances) if (instance.VpcId == null) { throw new ArgumentException("Must belong to a VPC", nameof(instance)); } #region Data Binding / Mappings if (location == null) { location = Locations.Get(aws, instance.Placement.AvailabilityZone); } if (image == null) { // "imageId": "ami-1647537c", image = await imageService.GetAsync(aws, instance.ImageId);; } if (machineType == null) { machineType = AwsInstanceType.Get(instance.InstanceType); } var network = await db.Networks.FindAsync(aws, instance.VpcId);; #endregion // instance.LaunchTime int addressCount = 1; if (instance.IpAddress != null) { addressCount++; } var addresses = new string[addressCount]; addresses[0] = instance.PrivateIpAddress; if (instance.IpAddress != null) { // the instance was assigned a public IP addresses[1] = instance.IpAddress; } var registerRequest = new RegisterHostRequest( addresses: addresses, cluster: cluster, image: image, machineType: machineType, program: null, location: location, status: instance.InstanceState.ToStatus(), ownerId: 1, resource: ManagedResource.Host(location, instance.InstanceId) ); #region Network Interfaces try { var nics = new RegisterNetworkInterfaceRequest[instance.NetworkInterfaces.Length]; for (var nicIndex = 0; nicIndex < nics.Length; nicIndex++) { var ec2Nic = instance.NetworkInterfaces[nicIndex]; nics[nicIndex] = new RegisterNetworkInterfaceRequest( mac: MacAddress.Parse(ec2Nic.MacAddress), subnetId: 0, // TODO: lookup subnet securityGroupIds: Array.Empty <long>(), // TODO: lookup security groupds resource: ManagedResource.NetworkInterface(location, ec2Nic.NetworkInterfaceId) ); } registerRequest.NetworkInterfaces = nics; } catch { } #endregion #region Volumes try { var volumes = new RegisterVolumeRequest[instance.BlockDeviceMappings.Length]; for (var volumeIndex = 0; volumeIndex < volumes.Length; volumeIndex++) { var device = instance.BlockDeviceMappings[volumeIndex]; if (device.Ebs == null) { continue; } var volumeSize = device.Ebs.VolumeSize is int ebsSize ? ByteSize.FromGiB(ebsSize) : ByteSize.Zero; volumes[volumeIndex] = new RegisterVolumeRequest( ownerId: 1, size: volumeSize, resource: ManagedResource.Volume(location, device.Ebs.VolumeId) ); } registerRequest.Volumes = volumes; } catch { } #endregion return(registerRequest); }