BidderInstanceBuilder extends InstanceBuilder { public Instance build( ProjectUser projectUser, ResourceId zoneId, String instanceName, @Nullable CustomBidderResource customBidderResource) { Project project = projectUser.getProject(); Instance instance = new Instance(); String zoneName = zoneId.getResourceName(); if (!Strings.isNullOrEmpty(project.getBidderMachineType(zoneName))) { ResourceName machineTypeName = ComputeResourceType.MACHINE_TYPE.buildName( project, zoneName, project.getBidderMachineType(zoneName)); instance.setMachineType(machineTypeName.getResourceUrl()); } String zoneUrl = ResourceName.buildName(projectUser, zoneId).getResourceUrl(); instance.setName(instanceName); instance.setDescription(buildDescription(projectUser)); instance.setZone(zoneUrl); instance.setTags(new Tags().setItems(asList(TAG))); instance.setNetworkInterfaces(buildInstanceNetwork(project, bidderParameters.isEnableExternalIps())); Set<String> bidderOauth2Scopes = new HashSet<>(); bidderOauth2Scopes.addAll(project.getBidderOauth2Scopes()); bidderOauth2Scopes.addAll(bidderParameters.getDefaultOauth2Scopes()); instance.setServiceAccounts(buildServiceAccounts(ImmutableList.copyOf(bidderOauth2Scopes))); Joiner joiner = Joiner.on(","); ImmutableMap.Builder<String, String> metadata = createMetadata(project, zoneId) .put(METADATA_PLATFORM, Platform.GOOGLE_COMPUTE.name()) .put(METADATA_LISTEN_PORT, project.getBidderRequestPort()) .put(METADATA_ADMIN_PORT, project.getBidderAdminPort()) .put(METADATA_JVM_PARAMETERS, project.getVmParameters().replaceAll("\\s+", " ")) .put(METADATA_PROJECT_ID, project.getApiProjectId()) .put(METADATA_IMAGE, project.getBidderImage()) .put(METADATA_IMAGE_TYPE, project.getIsBidderImageDefault() ? IMAGE_DEFAULT : IMAGE_CUSTOM); if (project.getApiProjectNumber() != null) { metadata.put(METADATA_PROJECT_NUMBER, project.getApiProjectNumber().toString()); } if (customBidderResource != null && customBidderResource.hasMainParameters()) { metadata.put(METADATA_MAIN_PARAMETERS, customBidderResource.getMainParameters().replaceAll("\\s+", " ")); } else { metadata.put(METADATA_MAIN_PARAMETERS, project.getMainParameters().replaceAll("\\s+", " ")); } if (project.getBidInterceptors() != null && !project.getBidInterceptors().isEmpty()) { metadata.put(METADATA_BID_INTERCEPTORS, joiner.join(project.getBidInterceptors())); } if (project.getImpressionInterceptors() != null && !project.getImpressionInterceptors().isEmpty()) { metadata.put(METADATA_IMPRESSION_INTERCEPTORS, joiner.join(project.getImpressionInterceptors())); } if (project.getClickInterceptors() != null && !project.getClickInterceptors().isEmpty()) { metadata.put(METADATA_CLICK_INTERCEPTORS, joiner.join(project.getClickInterceptors())); } String host = project.getZoneHost(zoneId.getResourceName()); if (!Strings.isNullOrEmpty(host)) { metadata.put(METADATA_BALANCER_HOST, host); String port = project.getLoadBalancerRequestPort(); if (!Strings.isNullOrEmpty(port)) { metadata.put(METADATA_BALANCER_PORT, port); } } if (!Strings.isNullOrEmpty(project.getEncryptionKey())) { metadata.put(METADATA_DC_ENCRYPTION_KEY, project.getEncryptionKey()); } if (!Strings.isNullOrEmpty(project.getIntegrityKey())) { metadata.put(METADATA_DC_INTEGRITY_KEY, project.getIntegrityKey()); } if (!Strings.isNullOrEmpty(project.getCookieMatchNid()) && !Strings.isNullOrEmpty(project.getCookieMatchUrl())) { if (project.getMatchInterceptors() != null && !project.getMatchInterceptors().isEmpty()) { metadata.put(METADATA_DC_MATCH_INTERCEPTORS, joiner.join(project.getMatchInterceptors())); } metadata.put(METADATA_DC_MATCH_NID, project.getCookieMatchNid()); metadata.put(METADATA_DC_MATCH_URL, project.getCookieMatchUrl()); } instance.setMetadata(buildMetadata(metadata.build())); return instance; } @Inject  BidderInstanceBuilder(
      Clock clock,
      BidderParameters bidderParameters,
      @Value("${OpenBidder.Project.Version}") String projectVersion); Instance build(
      ProjectUser projectUser,
      ResourceId zoneId,
      String instanceName,
      @Nullable CustomBidderResource customBidderResource); static @Nullable String getLoadBalancerUri(Map<String, String> metadata); static final String TAG; static final String METADATA_PLATFORM; static final String METADATA_LISTEN_PORT; static final String METADATA_ADMIN_PORT; static final String METADATA_BALANCER_HOST; static final String METADATA_BALANCER_PORT; static final String METADATA_BID_INTERCEPTORS; static final String METADATA_CLICK_INTERCEPTORS; static final String METADATA_IMPRESSION_INTERCEPTORS; static final String METADATA_JVM_PARAMETERS; static final String METADATA_MAIN_PARAMETERS; static final String METADATA_PROJECT_ID; static final String METADATA_PROJECT_NUMBER; static final String METADATA_DC_ENCRYPTION_KEY; static final String METADATA_DC_INTEGRITY_KEY; static final String METADATA_DC_MATCH_INTERCEPTORS; static final String METADATA_DC_MATCH_NID; static final String METADATA_DC_MATCH_URL; }
@Test public void testBuildNoExternalIp() { BidderParameters bidderParameters = new BidderParameters( "-Dfoo=bar", "--listen_port=18081 --admin_port=18082", ImmutableList.<String>of("com.google.openbidder.DefaultInterceptor"), ImmutableList.<String>of(), ImmutableList.<String>of(), ImmutableList.<String>of(), loadBalancerParameters.getDefaultOauth2Scopes(), false, new MockResource("#!/bin/bash!"), REDIRECT_URL); BidderInstanceBuilder builder = newInstanceBuilder(bidderParameters); ProjectUser projectUser = buildBasicProject(bidderParameters); ResourceId zoneId = ResourceType.ZONE.getResourceId("123", "us-east-a"); Instance instance = builder.build(projectUser, zoneId, "some name", null); assertEquals( ImmutableSet.of(BidderInstanceBuilder.TAG), ImmutableSet.copyOf(instance.getTags().getItems())); assertNotNull(instance.getDescription()); assertTrue(instance.getDescription().contains(projectUser.getEmail())); assertEquals(1, instance.getNetworkInterfaces().size()); NetworkInterface network = instance.getNetworkInterfaces().get(0); assertNotNull(network.getNetwork()); assertNull(network.getAccessConfigs()); ImmutableMap<String, String> metadata = ComputeUtils.toMap(instance.getMetadata()); assertEquals("-Dfoo=bar", metadata.get(BidderInstanceBuilder.METADATA_JVM_PARAMETERS)); }
@Test public void testBuildExternalIpExternalJar() { BidderParameters bidderParameters = new BidderParameters( "-Dfoo=bar", "--listen_port=18081 --admin_port=18082", ImmutableList.<String>of(), ImmutableList.<String>of(), ImmutableList.<String>of(), ImmutableList.<String>of(), loadBalancerParameters.getDefaultOauth2Scopes(), true, new MockResource("#!/bin/bash!"), REDIRECT_URL); BidderInstanceBuilder builder = newInstanceBuilder(bidderParameters); ProjectUser projectUser = buildBasicProject(bidderParameters); Project project = projectUser.getProject(); project.setUserDistUri("gs: ResourceId zoneId = ResourceType.ZONE.getResourceId("123", "us-east-a"); Instance instance = builder.build(projectUser, zoneId, "some name", null); assertEquals( ImmutableSet.of(BidderInstanceBuilder.TAG), ImmutableSet.copyOf(instance.getTags().getItems())); assertEquals(1, instance.getNetworkInterfaces().size()); NetworkInterface network = instance.getNetworkInterfaces().get(0); assertNotNull(network.getNetwork()); assertNotNull(network.getAccessConfigs()); ImmutableMap<String, String> metadata = ComputeUtils.toMap(instance.getMetadata()); assertEquals("gs: assertEquals("-Dfoo=bar", metadata.get(BidderInstanceBuilder.METADATA_JVM_PARAMETERS)); project.setUserDistUri("gs: instance = builder.build(projectUser, zoneId, "some name", null); metadata = ComputeUtils.toMap(instance.getMetadata()); assertEquals("gs: }
@Test public void testOpenBidderVersionMacro_noExpansion() { BidderParameters bidderParameters = new BidderParameters( "-Dfoo=bar", "--listen_port=18081 --admin_port=18082", ImmutableList.<String>of(), ImmutableList.<String>of(), ImmutableList.<String>of(), ImmutableList.<String>of(), loadBalancerParameters.getDefaultOauth2Scopes(), true, new MockResource("#!/bin/bash!"), REDIRECT_URL); BidderInstanceBuilder builder = newInstanceBuilder(bidderParameters); ProjectUser projectUser = buildBasicProject(bidderParameters); projectUser.getProject().setUserDistUri("gs: ResourceId zoneId = ResourceType.ZONE.getResourceId("123", "us-east-a"); Instance instance = builder.build(projectUser, zoneId, "some name", null); ImmutableMap<String, String> metadata = ComputeUtils.toMap(instance.getMetadata()); assertEquals("gs: }
@Test public void testInterceptors() { BidderParameters bidderParameters = new BidderParameters( "-Dfoo=bar", "--listen_port=18081 --admin_port=18082", ImmutableList.<String>of(), ImmutableList.<String>of(), ImmutableList.<String>of(), ImmutableList.<String>of(), loadBalancerParameters.getDefaultOauth2Scopes(), true, new MockResource("#!/bin/bash!"), REDIRECT_URL); BidderInstanceBuilder builder = newInstanceBuilder(bidderParameters); ProjectUser projectUser = buildBasicProject(bidderParameters); Project project = projectUser.getProject(); project.setBidInterceptors(ImmutableList.of("A", "B")); project.setImpressionInterceptors(ImmutableList.of("C", "D")); project.setZoneHost("us-east-a", "myhost"); ResourceId zoneId = ResourceType.ZONE.getResourceId("123", "us-east-a"); Instance instance = builder.build(projectUser, zoneId, "some name", null); assertEquals( ImmutableSet.of(BidderInstanceBuilder.TAG), ImmutableSet.copyOf(instance.getTags().getItems())); ImmutableMap<String, String> metadata = ComputeUtils.toMap(instance.getMetadata()); assertEquals("A,B", metadata.get(BidderInstanceBuilder.METADATA_BID_INTERCEPTORS)); assertEquals("C,D", metadata.get(BidderInstanceBuilder.METADATA_IMPRESSION_INTERCEPTORS)); assertEquals("E,F", metadata.get(BidderInstanceBuilder.METADATA_CLICK_INTERCEPTORS)); assertEquals("myhost", metadata.get(BidderInstanceBuilder.METADATA_BALANCER_HOST)); assertEquals( String.valueOf(LoadBalancerPort.DEFAULT), metadata.get(BidderInstanceBuilder.METADATA_BALANCER_PORT)); }