|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.search.crossproject; |
| 11 | + |
| 12 | +import java.util.Map; |
| 13 | + |
| 14 | +/** |
| 15 | + * Project tags used for cross-project search routing. |
| 16 | + * @param tags the map of tags -- contains both built-in (Elastic-supplied) and custom user-defined tags. |
| 17 | + * All built-in tags are prefixed with an underscore (_). |
| 18 | + */ |
| 19 | +public record ProjectTags(Map<String, String> tags) { |
| 20 | + public static final String PROJECT_ID_TAG = "_id"; |
| 21 | + public static final String PROJECT_ALIAS = "_alias"; |
| 22 | + public static final String PROJECT_TYPE_TAG = "_type"; |
| 23 | + public static final String ORGANIZATION_ID_TAG = "_organization"; |
| 24 | + |
| 25 | + public String projectId() { |
| 26 | + return tags.get(PROJECT_ID_TAG); |
| 27 | + } |
| 28 | + |
| 29 | + public String organizationId() { |
| 30 | + return tags.get(ORGANIZATION_ID_TAG); |
| 31 | + } |
| 32 | + |
| 33 | + public String projectType() { |
| 34 | + return tags.get(PROJECT_TYPE_TAG); |
| 35 | + } |
| 36 | + |
| 37 | + public String projectAlias() { |
| 38 | + return tags.get(PROJECT_ALIAS); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Validate that all required tags are present. |
| 43 | + */ |
| 44 | + public static void validateTags(String projectId, Map<String, String> tags) { |
| 45 | + if (false == tags.containsKey(PROJECT_ID_TAG)) { |
| 46 | + throw missingTagException(projectId, PROJECT_ID_TAG); |
| 47 | + } |
| 48 | + if (false == tags.containsKey(PROJECT_TYPE_TAG)) { |
| 49 | + throw missingTagException(projectId, PROJECT_TYPE_TAG); |
| 50 | + } |
| 51 | + if (false == tags.containsKey(ORGANIZATION_ID_TAG)) { |
| 52 | + throw missingTagException(projectId, ORGANIZATION_ID_TAG); |
| 53 | + } |
| 54 | + if (false == tags.containsKey(PROJECT_ALIAS)) { |
| 55 | + throw missingTagException(projectId, PROJECT_ALIAS); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private static IllegalStateException missingTagException(String projectId, String tagKey) { |
| 60 | + return new IllegalStateException("Project configuration for [" + projectId + "] is missing required tag [" + tagKey + "]"); |
| 61 | + } |
| 62 | +} |
0 commit comments