Example #1
0
  @Override
  public List<IpAddressToServiceIndex> getIpToIndex(Service service) {
    MultiRecordMapper<IpAddressToServiceIndex> mapper =
        new MultiRecordMapper<IpAddressToServiceIndex>() {
          @Override
          protected IpAddressToServiceIndex map(List<Object> input) {
            ServiceIndex index = (ServiceIndex) input.get(0);
            IpAddress ip = (IpAddress) input.get(1);
            Subnet subnet = (Subnet) input.get(2);
            IpAddressToServiceIndex data = new IpAddressToServiceIndex(index, ip, subnet);
            return data;
          }
        };

    ServiceIndexTable serviceIndex = mapper.add(SERVICE_INDEX);
    IpAddressTable ipAddress = mapper.add(IP_ADDRESS);
    SubnetTable subnet = mapper.add(SUBNET);
    ServiceExposeMapTable exposeMap = mapper.add(SERVICE_EXPOSE_MAP, SERVICE_EXPOSE_MAP.REMOVED);

    return create()
        .select(mapper.fields())
        .from(INSTANCE)
        .join(exposeMap)
        .on(exposeMap.INSTANCE_ID.eq(INSTANCE.ID))
        .join(NIC)
        .on(NIC.INSTANCE_ID.eq(exposeMap.INSTANCE_ID))
        .join(IP_ADDRESS_NIC_MAP)
        .on(IP_ADDRESS_NIC_MAP.NIC_ID.eq(NIC.ID))
        .join(ipAddress)
        .on(IP_ADDRESS_NIC_MAP.IP_ADDRESS_ID.eq(ipAddress.ID))
        .join(serviceIndex)
        .on(serviceIndex.ID.eq(INSTANCE.SERVICE_INDEX_ID))
        .join(subnet)
        .on(ipAddress.SUBNET_ID.eq(subnet.ID))
        .where(exposeMap.SERVICE_ID.eq(service.getId()))
        .and(exposeMap.REMOVED.isNull())
        .and(NIC.REMOVED.isNull())
        .and(ipAddress.REMOVED.isNull())
        .and(ipAddress.ADDRESS.isNotNull())
        .and(INSTANCE.REMOVED.isNull())
        .and(ipAddress.ROLE.eq(IpAddressConstants.ROLE_PRIMARY))
        .fetch()
        .map(mapper);
  }
Example #2
0
  protected List<HostInstanceIpData> getHostContainerIpData(long accountId) {
    Network hostNtwk =
        objManager.findAny(
            Network.class,
            NETWORK.ACCOUNT_ID,
            accountId,
            NETWORK.REMOVED,
            null,
            NETWORK.KIND,
            "dockerHost");
    if (hostNtwk == null) {
      return new ArrayList<HostInstanceIpData>();
    }
    MultiRecordMapper<HostInstanceIpData> mapper =
        new MultiRecordMapper<HostInstanceIpData>() {
          @Override
          protected HostInstanceIpData map(List<Object> input) {
            HostInstanceIpData data = new HostInstanceIpData();
            data.setIpAddress((IpAddress) input.get(0));
            data.setInstanceHostMap((InstanceHostMap) input.get(1));
            return data;
          }
        };

    IpAddressTable ipAddress = mapper.add(IP_ADDRESS);
    InstanceHostMapTable instanceHostMap = mapper.add(INSTANCE_HOST_MAP);
    return create()
        .select(mapper.fields())
        .from(HOST_IP_ADDRESS_MAP)
        .join(instanceHostMap)
        .on(HOST_IP_ADDRESS_MAP.HOST_ID.eq(instanceHostMap.HOST_ID))
        .join(NIC)
        .on(NIC.INSTANCE_ID.eq(instanceHostMap.INSTANCE_ID))
        .join(ipAddress)
        .on(HOST_IP_ADDRESS_MAP.IP_ADDRESS_ID.eq(ipAddress.ID))
        .where(instanceHostMap.REMOVED.isNull())
        .and(NIC.REMOVED.isNull())
        .and(HOST_IP_ADDRESS_MAP.REMOVED.isNull())
        .and(ipAddress.REMOVED.isNull())
        .and(NIC.NETWORK_ID.eq(hostNtwk.getId()))
        .fetch()
        .map(mapper);
  }
Example #3
0
  @Override
  public List<DnsEntryData> getInstanceLinksDnsData(final Instance instance) {
    MultiRecordMapper<DnsEntryData> mapper =
        new MultiRecordMapper<DnsEntryData>() {
          @Override
          protected DnsEntryData map(List<Object> input) {
            DnsEntryData data = new DnsEntryData();
            Map<String, Map<String, String>> resolve = new HashMap<>();
            Map<String, String> ips = new HashMap<>();
            String targetInstanceName =
                input.get(4) == null ? null : ((Instance) input.get(4)).getName();
            ips.put(((IpAddress) input.get(1)).getAddress(), targetInstanceName);
            // add all instance links
            resolve.put(((InstanceLink) input.get(0)).getLinkName(), ips);
            data.setSourceIpAddress(((IpAddress) input.get(2)).getAddress());
            data.setResolveServicesAndContainers(resolve);
            data.setInstance((Instance) input.get(3));
            return data;
          }
        };

    InstanceLinkTable instanceLink = mapper.add(INSTANCE_LINK);
    IpAddressTable targetIpAddress = mapper.add(IP_ADDRESS);
    IpAddressTable clientIpAddress = mapper.add(IP_ADDRESS);
    InstanceTable clientInstance = mapper.add(INSTANCE);
    InstanceTable targetInstance = mapper.add(INSTANCE);
    NicTable clientNic = NIC.as("client_nic");
    NicTable targetNic = NIC.as("target_nic");
    IpAddressNicMapTable clientNicIpTable = IP_ADDRESS_NIC_MAP.as("client_nic_ip");
    IpAddressNicMapTable targetNicIpTable = IP_ADDRESS_NIC_MAP.as("target_nic_ip");

    return create()
        .select(mapper.fields())
        .from(NIC)
        .join(clientNic)
        .on(NIC.VNET_ID.eq(clientNic.VNET_ID))
        .join(instanceLink)
        .on(instanceLink.INSTANCE_ID.eq(clientNic.INSTANCE_ID))
        .join(targetNic)
        .on(targetNic.INSTANCE_ID.eq(instanceLink.TARGET_INSTANCE_ID))
        .join(targetInstance)
        .on(targetNic.INSTANCE_ID.eq(targetInstance.ID))
        .join(targetNicIpTable)
        .on(targetNicIpTable.NIC_ID.eq(targetNic.ID))
        .join(targetIpAddress)
        .on(targetNicIpTable.IP_ADDRESS_ID.eq(targetIpAddress.ID))
        .join(clientNicIpTable)
        .on(clientNicIpTable.NIC_ID.eq(clientNic.ID))
        .join(clientIpAddress)
        .on(clientNicIpTable.IP_ADDRESS_ID.eq(clientIpAddress.ID))
        .join(clientInstance)
        .on(clientNic.INSTANCE_ID.eq(clientInstance.ID))
        .where(
            NIC.INSTANCE_ID
                .eq(instance.getId())
                .and(NIC.VNET_ID.isNotNull())
                .and(NIC.REMOVED.isNull())
                .and(targetIpAddress.ROLE.eq(IpAddressConstants.ROLE_PRIMARY))
                .and(targetIpAddress.REMOVED.isNull())
                .and(clientIpAddress.ROLE.eq(IpAddressConstants.ROLE_PRIMARY))
                .and(clientIpAddress.REMOVED.isNull())
                .and(targetNicIpTable.REMOVED.isNull())
                .and(clientNic.REMOVED.isNull())
                .and(targetNic.REMOVED.isNull())
                .and(instanceLink.REMOVED.isNull())
                .and(instanceLink.SERVICE_CONSUME_MAP_ID.isNull())
                .and(
                    targetInstance.STATE.in(
                        InstanceConstants.STATE_RUNNING, InstanceConstants.STATE_STARTING))
                .and(
                    targetInstance
                        .HEALTH_STATE
                        .isNull()
                        .or(
                            targetInstance.HEALTH_STATE.eq(
                                HealthcheckConstants.HEALTH_STATE_HEALTHY))))
        .fetch()
        .map(mapper);
  }