Ejemplo n.º 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);
  }
Ejemplo n.º 2
0
  private List<PublicEndpoint> getPublicEndpointsInternal(
      long accountId, Long serviceId, Long hostId) {
    MultiRecordMapper<PublicEndpoint> mapper =
        new MultiRecordMapper<PublicEndpoint>() {
          @Override
          protected PublicEndpoint map(List<Object> input) {
            Instance instance = (Instance) input.get(0);
            Port port = (Port) input.get(1);
            Host host = (Host) input.get(2);

            String address = "";
            IpAddress ip = (IpAddress) input.get(3);
            if (ip != null) {
              address = ip.getAddress();
            } else {
              address = DataAccessor.fieldString(port, PortConstants.FIELD_BIND_ADDR);
            }

            ServiceExposeMap exposeMap = (ServiceExposeMap) input.get(4);
            Long serviceId = exposeMap != null ? exposeMap.getServiceId() : null;
            PublicEndpoint data =
                new PublicEndpoint(
                    address, port.getPublicPort(), host.getId(), instance.getId(), serviceId);
            return data;
          }
        };

    InstanceTable instance = mapper.add(INSTANCE, INSTANCE.ID, INSTANCE.ACCOUNT_ID);
    PortTable port = mapper.add(PORT);
    HostTable host = mapper.add(HOST, HOST.ID);
    IpAddressTable ipAddress = mapper.add(IP_ADDRESS, IP_ADDRESS.ID, IP_ADDRESS.ADDRESS);
    ServiceExposeMapTable exposeMap =
        mapper.add(
            SERVICE_EXPOSE_MAP, SERVICE_EXPOSE_MAP.INSTANCE_ID, SERVICE_EXPOSE_MAP.SERVICE_ID);

    Condition condition = null;
    if (serviceId != null && hostId != null) {
      condition = host.ID.eq(hostId).and(exposeMap.SERVICE_ID.eq(serviceId));
    } else if (hostId != null) {
      condition = host.ID.eq(hostId);
    } else if (serviceId != null) {
      condition = (exposeMap.SERVICE_ID.eq(serviceId));
    }

    return create()
        .select(mapper.fields())
        .from(instance)
        .join(port)
        .on(port.INSTANCE_ID.eq(instance.ID))
        .join(INSTANCE_HOST_MAP)
        .on(INSTANCE_HOST_MAP.INSTANCE_ID.eq(instance.ID))
        .join(host)
        .on(INSTANCE_HOST_MAP.HOST_ID.eq(host.ID))
        .leftOuterJoin(ipAddress)
        .on(port.PUBLIC_IP_ADDRESS_ID.eq(ipAddress.ID))
        .leftOuterJoin(exposeMap)
        .on(exposeMap.INSTANCE_ID.eq(instance.ID))
        .where(instance.ACCOUNT_ID.eq(accountId))
        .and(instance.REMOVED.isNull())
        .and(port.REMOVED.isNull())
        .and(host.REMOVED.isNull())
        .and(ipAddress.REMOVED.isNull())
        .and(exposeMap.REMOVED.isNull())
        .and(port.PUBLIC_PORT.isNotNull())
        .and(
            port.STATE.in(
                CommonStatesConstants.ACTIVATING,
                CommonStatesConstants.ACTIVE,
                CommonStatesConstants.UPDATING_ACTIVE))
        .and(condition)
        .fetch()
        .map(mapper);
  }
Ejemplo n.º 3
0
  protected List<ServiceInstanceData> getServiceInstanceInstancesData(
      long accountId, boolean client, long vnetId) {
    MultiRecordMapper<ServiceInstanceData> mapper =
        new MultiRecordMapper<ServiceInstanceData>() {
          @Override
          protected ServiceInstanceData map(List<Object> input) {
            Service service = (Service) input.get(0);
            IpAddress ip = (IpAddress) input.get(1);
            Instance instance = (Instance) input.get(2);
            ServiceExposeMap exposeMap = (ServiceExposeMap) input.get(3);
            Nic nic = (Nic) input.get(4);
            ServiceInstanceData data =
                new ServiceInstanceData(service, ip, instance, exposeMap, nic);
            return data;
          }
        };

    ServiceTable service = mapper.add(SERVICE);
    IpAddressTable ipAddress = mapper.add(IP_ADDRESS);
    InstanceTable instance = mapper.add(INSTANCE);
    ServiceExposeMapTable exposeMap = mapper.add(SERVICE_EXPOSE_MAP);
    NicTable nic = mapper.add(NIC);

    Condition condition = null;
    if (client) {
      condition = ipAddress.ROLE.eq(IpAddressConstants.ROLE_PRIMARY).and(nic.VNET_ID.eq(vnetId));
    } else {
      condition =
          (ipAddress.ROLE.isNull().and(ipAddress.ADDRESS.isNull()))
              .or(ipAddress.ROLE.eq(IpAddressConstants.ROLE_PRIMARY))
              .and(
                  instance
                      .HEALTH_STATE
                      .isNull()
                      .or(instance.HEALTH_STATE.eq(HealthcheckConstants.HEALTH_STATE_HEALTHY)));
    }

    return create()
        .select(mapper.fields())
        .from(service)
        .join(exposeMap)
        .on(service.ID.eq(exposeMap.SERVICE_ID))
        .leftOuterJoin(instance)
        .on(instance.ID.eq(exposeMap.INSTANCE_ID))
        .leftOuterJoin(nic)
        .on(nic.INSTANCE_ID.eq(exposeMap.INSTANCE_ID))
        .leftOuterJoin(IP_ADDRESS_NIC_MAP)
        .on(IP_ADDRESS_NIC_MAP.NIC_ID.eq(nic.ID))
        .leftOuterJoin(ipAddress)
        .on(IP_ADDRESS_NIC_MAP.IP_ADDRESS_ID.eq(ipAddress.ID))
        .where(service.ACCOUNT_ID.eq(accountId))
        .and(service.REMOVED.isNull())
        .and(exposeMap.REMOVED.isNull())
        .and(nic.REMOVED.isNull())
        .and(ipAddress.REMOVED.isNull())
        .and(instance.REMOVED.isNull())
        .and(
            instance
                .STATE
                .isNull()
                .or(
                    instance.STATE.in(
                        InstanceConstants.STATE_RUNNING, InstanceConstants.STATE_STARTING)))
        .and(condition)
        .fetch()
        .map(mapper);
  }