Skip to content

Commit 814378a

Browse files
authored
set project and instances to default for external hosts (#41)
* set project and instances to default for external hosts * readme typo * set config as default project and instance for external host * unit tests added * corrected project assignment * constants added for external host properties * constants added for external host in schema convertor
1 parent 131a86e commit 814378a

File tree

4 files changed

+86
-3
lines changed

4 files changed

+86
-3
lines changed

schema_converter/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ go run cql_to_spanner_schema_converter.go --project <PROJECT_ID> --instance <INS
6565
- `[--table]`: Optional flag to specify different table name for TableConfigurations.
6666
- `[--enableUsingTimestamp]`: Optional flag to enable 'Using Timestamp' features, default is false.
6767
- `[--enableUsingTTL]`: Optional flag to enable 'Using TTL' features, default is false.
68+
- `[--usePlainText]`: Optional flag to enable plain text connection for external host, default is false.
69+
- `[--caCertificate]`: Optional flag to specify the CA certificate file path required for TLS/mTLS connection for external hosts.
70+
- `[--clientCertificate]`: Optional flag to specify the client certificate file path required for mTLS connection for external hosts.
71+
- `[--clientKey]`: Optional flag to specify the client key file path required for mTLS connection for external hosts.
6872

6973
### 2. Example Commands
7074

schema_converter/cql_to_spanner_schema_converter.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"flag"
2121
"fmt"
2222
"log"
23+
"regexp"
2324
"time"
2425

2526
"bufio"
@@ -38,6 +39,11 @@ import (
3839
"google.golang.org/grpc/credentials/insecure"
3940
)
4041

42+
const (
43+
ExternalHostProjectID = "default"
44+
ExternalHostInstanceID = "default"
45+
)
46+
4147
type ColumnMetadata struct {
4248
ColumnName string
4349
CQLType string
@@ -150,10 +156,16 @@ func main() {
150156
caCertificate := flag.String("caCertificate", "", "The CA certificate file to use for TLS")
151157
clientCertificate := flag.String("clientCertificate", "", "The client certificate to establish mTLS for external hosts")
152158
clientKey := flag.String("clientKey", "", "The client key to establish mTLS for external hosts")
153-
externalHost := flag.Bool("externalHost", false, "Whether connection needs to be established with an externalHost")
154159

155160
flag.Parse()
156161

162+
isExternalHost := (*endpoint != "" && !regexp.MustCompile(`.*\.googleapis\.com.*`).MatchString(*endpoint))
163+
164+
if isExternalHost {
165+
*projectID = ExternalHostProjectID
166+
*instanceID = ExternalHostInstanceID
167+
}
168+
157169
// Check if all required flags are provided
158170
checkMissingFlags := func() []string {
159171
missingFlags := []string{}
@@ -181,7 +193,7 @@ func main() {
181193
}
182194

183195
// Ensure that GCP credentials are set except for spanner external host connections
184-
if !*externalHost {
196+
if !isExternalHost {
185197
if err := checkGCPCredentials(); err != nil {
186198
log.Fatalf("Error: %v", err)
187199
}

third_party/datastax/proxy/defaults.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
package proxy
22

3-
import "fmt"
3+
import (
4+
"fmt"
5+
"regexp"
6+
)
47

58
// Defaults for Spanner Settings.
69
const (
710
DefaultSpannerGrpcChannels = 4
811
DefaultSpannerMinSession = 100
912
DefaultSpannerMaxSession = 400
1013
DefaultConfigTableName = "TableConfigurations"
14+
ExternalHostProjectID = "default"
15+
ExternalHostInstanceID = "default"
1116
)
1217

1318
// ApplyDefaults applies default values to the configuration after it is loaded
@@ -50,6 +55,10 @@ func ValidateAndApplyDefaults(cfg *UserConfig) error {
5055
if cfg.Listeners[i].Spanner.DatabaseID == "" {
5156
return fmt.Errorf("database id is not defined for listener %s %d", cfg.Listeners[i].Name, cfg.Listeners[i].Port)
5257
}
58+
if cfg.CassandraToSpannerConfigs.Endpoint != "" && !regexp.MustCompile(`.*\.googleapis\.com.*`).MatchString(cfg.CassandraToSpannerConfigs.Endpoint) {
59+
cfg.Listeners[i].Spanner.ProjectID = ExternalHostProjectID
60+
cfg.Listeners[i].Spanner.InstanceID = ExternalHostInstanceID
61+
}
5362
if cfg.Listeners[i].Spanner.ProjectID == "" {
5463
if cfg.CassandraToSpannerConfigs.ProjectID == "" {
5564
return fmt.Errorf("project id is not defined for listener %s %d", cfg.Listeners[i].Name, cfg.Listeners[i].Port)

third_party/datastax/proxy/defaults_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,61 @@ func TestValidateAndApplyDefaultsMissingDatabaseID(t *testing.T) {
169169
t.Errorf("Expected error for missing DatabaseID, got: %v", err)
170170
}
171171
}
172+
173+
func TestValidateExternalHostEndpointSetsDefaults(t *testing.T) {
174+
cfg := &UserConfig{
175+
Listeners: []Listener{
176+
{
177+
Name: "Listener1",
178+
Port: 8080,
179+
Spanner: Spanner{
180+
DatabaseID: "db-1",
181+
InstanceID: "",
182+
ProjectID: "",
183+
},
184+
},
185+
},
186+
CassandraToSpannerConfigs: CassandraToSpannerConfigs{
187+
Endpoint: "localhost:9090", // ExternalHost endpoint
188+
ProjectID: "",
189+
},
190+
}
191+
192+
err := ValidateAndApplyDefaults(cfg)
193+
if err != nil {
194+
t.Errorf("Did not expect an error, got: %v", err)
195+
}
196+
197+
l := cfg.Listeners[0]
198+
if l.Spanner.InstanceID != "default" {
199+
t.Errorf("Expected InstanceID to be 'default', got: %s", l.Spanner.InstanceID)
200+
}
201+
if l.Spanner.ProjectID != "default" {
202+
t.Errorf("Expected ProjectID to be 'default', got: %s", cfg.CassandraToSpannerConfigs.ProjectID)
203+
}
204+
}
205+
206+
func TestValidateCloudSpannerEndpointMissingProjectAndInstance(t *testing.T) {
207+
cfg := &UserConfig{
208+
Listeners: []Listener{
209+
{
210+
Name: "Listener1",
211+
Port: 8080,
212+
Spanner: Spanner{
213+
DatabaseID: "db-1",
214+
InstanceID: "",
215+
ProjectID: "",
216+
},
217+
},
218+
},
219+
CassandraToSpannerConfigs: CassandraToSpannerConfigs{
220+
Endpoint: "spanner.googleapis.com:443", // Cloud Spanner endpoint
221+
ProjectID: "", // Empty ProjectID
222+
},
223+
}
224+
err := ValidateAndApplyDefaults(cfg)
225+
expectedError := "project id is not defined for listener Listener1 8080"
226+
if err == nil || err.Error() != expectedError {
227+
t.Errorf("Expected error for missing ProjectID and InstanceID with Cloud Spanner endpoint, got: %v", err)
228+
}
229+
}

0 commit comments

Comments
 (0)