示例#1
0
        public async Task <NetworkInterfaceInfo> RegisterAsync(RegisterNetworkInterfaceRequest request)
        {
            Ensure.Object(request, nameof(request)); // Validate the request

            var nic = new NetworkInterfaceInfo(
                id: await db.NetworkInterfaces.GetNextScopedIdAsync(request.NetworkId),
                ipAddresses: Array.Empty <IPAddress>(),
                macAddress: request.Mac,
                subnetId: request.SubnetId,
                securityGroupIds: request.SecurityGroupIds,
                resource: request.Resource
                );

            await db.NetworkInterfaces.InsertAsync(nic);

            return(nic);
        }
        public async Task <NetworkInterfaceInfo> GetAsync(ResourceProvider provider, string resourceId)
        {
            var record = await networkInterfaces.FindAsync(provider, resourceId);;

            if (record == null)
            {
                var nic = await ec2.DescribeNetworkInterfaceAsync(resourceId)
                          ?? throw ResourceError.NotFound(Aws, ResourceTypes.NetworkInterface, resourceId);

                var network = await networkService.GetAsync(Aws, nic.VpcId);

                var region = Locations.Get(network.LocationId);

                SubnetInfo subnet = nic.SubnetId != null
                   ? await subnetService.FindAsync(Aws, nic.SubnetId)
                   : null;

                var securityGroupIds = new long[nic.Groups.Length];

                for (var i = 0; i < securityGroupIds.Length; i++)
                {
                    var nsg = await nsgManager.GetAsync(network, nic.Groups[i]);;

                    securityGroupIds[i] = nsg.Id;
                }

                // TODO: Lookup host & create an attachment (ni.Attachment?.AttachTime) ...

                var registerRequest = new RegisterNetworkInterfaceRequest(
                    mac: MacAddress.Parse(nic.MacAddress),
                    subnetId: subnet?.Id ?? 0,
                    securityGroupIds: securityGroupIds,
                    resource: ManagedResource.NetworkInterface(region, nic.NetworkInterfaceId)
                    );

                record = await networkInterfaces.RegisterAsync(registerRequest);;
            }

            return(record);
        }