@@ -31,25 +31,76 @@ class CloudSqlInstanceName {
31
31
// Some legacy project ids are domain-scoped (e.g. "example.com:PROJECT:REGION:INSTANCE")
32
32
private static final Pattern CONNECTION_NAME =
33
33
Pattern .compile ("([^:]+(:[^:]+)?):([^:]+):([^:]+)" );
34
+
35
+ /**
36
+ * The domain name pattern in accordance with RFC 1035, RFC 1123 and RFC 2181.
37
+ *
38
+ * <p>Explanation of the Regex:
39
+ *
40
+ * <p>^: Matches the beginning of the string.<br>
41
+ * (?=.{1,255}$): Positive lookahead assertion to ensure the domain name is between 1 and 255
42
+ * characters long.<br>
43
+ * (?!-): Negative lookahead assertion to prevent hyphens at the beginning.<br>
44
+ * [A-Za-z0-9-]+: Matches one or more alphanumeric characters or hyphens.<br>
45
+ * (\\.[A-Za-z0-9-]+)*: Matches zero or more occurrences of a period followed by one or more
46
+ * alphanumeric characters or hyphens (for subdomains).<br>
47
+ * \\.: Matches a period before the TLD.<br>
48
+ * [A-Za-z]{2,}: Matches two or more letters for the TLD.<br>
49
+ * $: Matches the end of the string.<br>
50
+ */
51
+ private static final Pattern DOMAIN_NAME =
52
+ Pattern .compile ("^(?=.{1,255}$)(?!-)[A-Za-z0-9-]+(\\ .[A-Za-z0-9-]+)*\\ .[A-Za-z]{2,}$" );
53
+
34
54
private final String projectId ;
35
55
private final String regionId ;
36
56
private final String instanceId ;
37
57
private final String connectionName ;
58
+ private final String domainName ;
59
+
60
+ /**
61
+ * Validates that a string is a well-formed domain name.
62
+ *
63
+ * @param domain the domain name to check
64
+ * @return true if domain is a well-formed domain name.
65
+ */
66
+ public static boolean isValidDomain (String domain ) {
67
+ Matcher matcher = DOMAIN_NAME .matcher (domain );
68
+ return matcher .matches ();
69
+ }
38
70
39
71
/**
40
72
* Initializes a new CloudSqlInstanceName class.
41
73
*
42
74
* @param connectionName instance connection name in the format "PROJECT_ID:REGION_ID:INSTANCE_ID"
43
75
*/
44
76
CloudSqlInstanceName (String connectionName ) {
77
+ this (connectionName , null );
78
+ }
79
+
80
+ /**
81
+ * Initializes a new CloudSqlInstanceName class containing the domain name.
82
+ *
83
+ * @param connectionName instance connection name in the format "PROJECT_ID:REGION_ID:INSTANCE_ID"
84
+ * @param domainName the domain name used to look up the instance, or null.
85
+ */
86
+ CloudSqlInstanceName (String connectionName , String domainName ) {
45
87
this .connectionName = connectionName ;
88
+ this .domainName = domainName ;
46
89
Matcher matcher = CONNECTION_NAME .matcher (connectionName );
47
90
checkArgument (
48
91
matcher .matches (),
49
92
String .format (
50
93
"[%s] Cloud SQL connection name is invalid, expected string in the form of"
51
94
+ " \" <PROJECT_ID>:<REGION_ID>:<INSTANCE_ID>\" ." ,
52
95
connectionName ));
96
+
97
+ if (domainName != null ) {
98
+ Matcher domainMatcher = DOMAIN_NAME .matcher (domainName );
99
+ checkArgument (
100
+ domainMatcher .matches (),
101
+ String .format ("[%s] Domain name is invalid, expected a valid domain name" , domainName ));
102
+ }
103
+
53
104
this .projectId = matcher .group (1 );
54
105
this .regionId = matcher .group (3 );
55
106
this .instanceId = matcher .group (4 );
@@ -71,6 +122,10 @@ String getInstanceId() {
71
122
return instanceId ;
72
123
}
73
124
125
+ String getDomainName () {
126
+ return domainName ;
127
+ }
128
+
74
129
@ Override
75
130
public String toString () {
76
131
return connectionName ;
0 commit comments