Skip to content

Commit

Permalink
feat: use v1beta endpoint (#16)
Browse files Browse the repository at this point in the history
Co-authored-by: Shubha Rajan <shubhadayini@google.com>
  • Loading branch information
enocom and shubha-rajan authored Apr 20, 2022
1 parent a33923a commit bfe5fe5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
4 changes: 2 additions & 2 deletions internal/alloydbapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func NewClient(ctx context.Context, opts ...option.ClientOption) (*Client, error

func (c *Client) InstanceGet(ctx context.Context, project, region, cluster, instance string) (InstanceGetResponse, error) {
u := fmt.Sprintf(
"%s/v1alpha1/projects/%s/locations/%s/clusters/%s/instances/%s",
"%s/v1beta/projects/%s/locations/%s/clusters/%s/instances/%s",
c.endpoint, project, region, cluster, instance,
)
req, err := http.NewRequestWithContext(ctx, "GET", u, nil)
Expand Down Expand Up @@ -112,7 +112,7 @@ func (c *Client) InstanceGet(ctx context.Context, project, region, cluster, inst

func (c *Client) GenerateClientCert(ctx context.Context, project, region, cluster string, csr []byte) (GenerateClientCertificateResponse, error) {
u := fmt.Sprintf(
"%s/v1alpha1/projects/%s/locations/%s/clusters/%s:generateClientCertificate",
"%s/v1beta/projects/%s/locations/%s/clusters/%s:generateClientCertificate",
c.endpoint, project, region, cluster,
)
body, err := json.Marshal(GenerateClientCertificateRequest{PemCSR: string(csr)})
Expand Down
51 changes: 33 additions & 18 deletions internal/mock/alloydbadmin.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"fmt"
"io/ioutil"
"math/big"
"net"
"net/http"
"net/http/httptest"
"sync"
Expand Down Expand Up @@ -218,7 +219,7 @@ func InstanceGetSuccess(i FakeAlloyDBInstance, ct int) *Request {
"projects/%s/locations/%s/clusters/%s/instances/%s",
i.project, i.region, i.cluster, i.name,
)
p := fmt.Sprintf("/v1alpha1/projects/%s/locations/%s/clusters/%s/instances/%s",
p := fmt.Sprintf("/v1beta/projects/%s/locations/%s/clusters/%s/instances/%s",
i.project, i.region, i.cluster, i.name)
return &Request{
reqMethod: http.MethodGet,
Expand All @@ -237,7 +238,7 @@ func CreateEphemeralSuccess(i FakeAlloyDBInstance, ct int) *Request {
return &Request{
reqMethod: http.MethodPost,
reqPath: fmt.Sprintf(
"/v1alpha1/projects/%s/locations/%s/clusters/%s:generateClientCertificate",
"/v1beta/projects/%s/locations/%s/clusters/%s:generateClientCertificate",
i.project, i.region, i.cluster),
reqCt: ct,
handle: func(resp http.ResponseWriter, req *http.Request) {
Expand Down Expand Up @@ -344,24 +345,38 @@ func HTTPClient(requests ...*Request) (*http.Client, string, func() error) {
// on all interfaces, configured with TLS as specified by the
// FakeAlloyDBInstance. Callers should invoke the returned function to clean up
// all resources.
func StartServerProxy(t *testing.T, i FakeAlloyDBInstance) func() {
func StartServerProxy(t *testing.T, inst FakeAlloyDBInstance) func() {
pool := x509.NewCertPool()
pool.AddCert(i.rootCACert)
ln, err := tls.Listen("tcp", ":5433", &tls.Config{
Certificates: []tls.Certificate{
tls.Certificate{
Certificate: [][]byte{i.serverCert.Raw, i.rootCACert.Raw},
PrivateKey: i.serverKey,
Leaf: i.serverCert,
},
},
ServerName: "FIXME", // FIXME: this will become the instance UID
ClientAuth: tls.RequireAndVerifyClientCert,
ClientCAs: pool,
})
if err != nil {
pool.AddCert(inst.rootCACert)
tryListen := func(t *testing.T, attempts int) net.Listener {
var (
ln net.Listener
err error
)
for i := 0; i < attempts; i++ {
ln, err = tls.Listen("tcp", ":5433", &tls.Config{
Certificates: []tls.Certificate{
tls.Certificate{
Certificate: [][]byte{inst.serverCert.Raw, inst.rootCACert.Raw},
PrivateKey: inst.serverKey,
Leaf: inst.serverCert,
},
},
ServerName: "FIXME", // FIXME: this will become the instance UID
ClientAuth: tls.RequireAndVerifyClientCert,
ClientCAs: pool,
})
if err != nil {
t.Log("listener failed to start, waiting 100ms")
time.Sleep(100 * time.Millisecond)
continue
}
return ln
}
t.Fatalf("failed to start listener: %v", err)
return nil
}
ln := tryListen(t, 10)
ctx, cancel := context.WithCancel(context.Background())
go func() {
for {
Expand All @@ -374,7 +389,7 @@ func StartServerProxy(t *testing.T, i FakeAlloyDBInstance) func() {
t.Logf("fake server proxy will close listener after error: %v", err)
return
}
conn.Write([]byte(i.name))
conn.Write([]byte(inst.name))
conn.Close()
}
}
Expand Down

0 comments on commit bfe5fe5

Please sign in to comment.