Skip to content

Commit

Permalink
Merge pull request #68 from Carrotman42/master
Browse files Browse the repository at this point in the history
Fix panic when the region is omitted
  • Loading branch information
Carrotman42 authored Mar 15, 2017
2 parents 3bb383c + 8805f88 commit 2e269df
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 21 deletions.
21 changes: 17 additions & 4 deletions cmd/cloud_sql_proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,28 @@ func parseInstanceConfig(dir, instance string, cl *http.Client) (instanceConfig,
ret.Address = fmt.Sprintf("%s:%s", spl[1], spl[2])
}
} else {
sql, err := sqladmin.New(cl)
if err != nil {
return instanceConfig{}, err
}

ret.Instance = instance
// Default to unix socket.
ret.Network = "unix"
spl := strings.SplitN(instance, ":", 3)
sql, err := sqladmin.New(cl)
if err != nil {
return instanceConfig{}, err

var proj, name string
if len(spl) < 2 {
return instanceConfig{}, fmt.Errorf("invalid instance name: must be in the form `project:region:instance-name`; invalid name was %q", instance)
} else if len(spl) == 2 {
// We allow people to omit the region due to historical reasons. It'll
// fail later in the code if this isn't allowed, so just assume it's
// allowed until we actually need the region in this API call.
proj, name = spl[0], spl[1]
} else { // if len(spl) == 3
proj, name = spl[0], spl[2]
}
in, err := sql.Instances.Get(spl[0], spl[2]).Do()
in, err := sql.Instances.Get(proj, name).Do()
if err != nil {
return instanceConfig{}, err
}
Expand Down
37 changes: 20 additions & 17 deletions cmd/cloud_sql_proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,28 +51,28 @@ func TestCreateInstanceConfigs(t *testing.T) {
"", true, nil, "", true,
}, {
"setting -fuse, -dir, and -instances",
"dir", true, []string{"x"}, "", true,
"dir", true, []string{"proj:reg:x"}, "", true,
}, {
"setting -fuse, -dir, and -instances_metadata",
"dir", true, nil, "md", true,
}, {
"setting -dir and -instances (unix socket)",
"dir", false, []string{"x"}, "", false,
"dir", false, []string{"proj:reg:x"}, "", false,
}, {
"Seting -instance (unix socket)",
"", false, []string{"x"}, "", true,
"", false, []string{"proj:reg:x"}, "", true,
}, {
"setting -instance (tcp socket)",
"", false, []string{"x=tcp:1234"}, "", false,
"", false, []string{"proj:reg:x=tcp:1234"}, "", false,
}, {
"setting -instance (tcp socket) and -instances_metadata",
"", false, []string{"x=tcp:1234"}, "md", true,
"", false, []string{"proj:reg:x=tcp:1234"}, "md", true,
}, {
"setting -dir, -instance (tcp socket), and -instances_metadata",
"dir", false, []string{"x=tcp:1234"}, "md", false,
"dir", false, []string{"proj:reg:x=tcp:1234"}, "md", false,
}, {
"setting -dir, -instance (unix socket), and -instances_metadata",
"dir", false, []string{"x"}, "md", false,
"dir", false, []string{"proj:reg:x"}, "md", false,
}, {
"setting -dir and -instances_metadata",
"dir", false, nil, "md", false,
Expand Down Expand Up @@ -103,31 +103,31 @@ func TestParseInstanceConfig(t *testing.T) {
wantErr bool
}{
{
"/x", "my-instance",
instanceConfig{"my-instance", "unix", "/x/my-instance"},
"/x", "my-proj:my-reg:my-instance",
instanceConfig{"my-proj:my-reg:my-instance", "unix", "/x/my-proj:my-reg:my-instance"},
false,
}, {
"/x", "my-instance=tcp:1234",
instanceConfig{"my-instance", "tcp", "127.0.0.1:1234"},
"/x", "my-proj:my-reg:my-instance=tcp:1234",
instanceConfig{"my-proj:my-reg:my-instance", "tcp", "127.0.0.1:1234"},
false,
}, {
"/x", "my-instance=tcp:my-host:1111",
instanceConfig{"my-instance", "tcp", "my-host:1111"},
"/x", "my-proj:my-reg:my-instance=tcp:my-host:1111",
instanceConfig{"my-proj:my-reg:my-instance", "tcp", "my-host:1111"},
false,
}, {
"/x", "my-instance=",
"/x", "my-proj:my-reg:my-instance=",
instanceConfig{},
true,
}, {
"/x", "my-instance=cool network",
"/x", "my-proj:my-reg:my-instance=cool network",
instanceConfig{},
true,
}, {
"/x", "my-instance=cool network:1234",
"/x", "my-proj:my-reg:my-instance=cool network:1234",
instanceConfig{},
true,
}, {
"/x", "my-instance=oh:so:many:colons",
"/x", "my-proj:my-reg:my-instance=oh:so:many:colons",
instanceConfig{},
true,
},
Expand All @@ -138,6 +138,9 @@ func TestParseInstanceConfig(t *testing.T) {
t.Errorf("parseInstanceConfig(%s, %s) = %+v, wanted error", got)
}
continue
} else if err != nil {
t.Errorf("parseInstanceConfig(%s, %s) had unexpected error: %v", v.dir, v.instance, err)
continue
}
if got != v.wantCfg {
t.Errorf("parseInstanceConfig(%s, %s) = %+v, want %+v", v.dir, v.instance, got, v.wantCfg)
Expand Down

0 comments on commit 2e269df

Please sign in to comment.