Repository: milvus-io/milvus-sdk-go
Pull request #332 (2022-11-09 09:05:29)

Add a NewDefaultGrpcClientWithURI API
/kind improvment
Signed-off-by: yun.zhang <yun.zhang@zilliz.com>

sre-ci-robot: @jaime0815: The label(s) `kind/improvment` cannot be applied, because the repository doesn't have them.

<details>

In response to [this](https://github.com/milvus-io/milvus-sdk-go/pull/332):

>/kind improvment
>Signed-off-by: yun.zhang <yun.zhang@zilliz.com>


Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md).  If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository.
</details>
sre-ci-robot: [APPROVALNOTIFIER] This PR is **NOT APPROVED**

This pull-request has been approved by: *<a href="https://github.com/milvus-io/milvus-sdk-go/pull/332#" title="Author self-approved">jaime0815</a>*
To complete the [pull request process](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process), please assign **sunby** after the PR has been reviewed.
You can assign the PR to them by writing `/assign @sunby` in a comment when ready.

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=milvus-io%2Fmilvus-sdk-go).

<details open>
Needs approval from an approver in each of these files:

- **[OWNERS](https://github.com/milvus-io/milvus-sdk-go/blob/master/OWNERS)**

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":["sunby"]} -->
mergify[bot]: @jaime0815 Please associate the related issue to the body of your Pull Request.  (eg. “issue: #187”)
codecov[bot]: # [Codecov](https://codecov.io/gh/milvus-io/milvus-sdk-go/pull/332?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=milvus-io) Report
> Merging [#332](https://codecov.io/gh/milvus-io/milvus-sdk-go/pull/332?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=milvus-io) (4ee81b1) into [master](https://codecov.io/gh/milvus-io/milvus-sdk-go/commit/2ae70c2a436e8e829e18fafe4eb190b591652fa7?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=milvus-io) (2ae70c2) will **increase** coverage by `0.01%`.
> The diff coverage is `85.00%`.

[![Impacted file tree graph](https://codecov.io/gh/milvus-io/milvus-sdk-go/pull/332/graphs/tree.svg?width=650&height=150&src=pr&token=WJK43PV390&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=milvus-io)](https://codecov.io/gh/milvus-io/milvus-sdk-go/pull/332?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=milvus-io)

```diff
@@            Coverage Diff             @@
##           master     #332      +/-   ##
==========================================
+ Coverage   82.57%   82.58%   +0.01%     
==========================================
  Files          30       30              
  Lines        3305     3325      +20     
==========================================
+ Hits         2729     2746      +17     
- Misses        474      477       +3     
  Partials      102      102              
```


| [Impacted Files](https://codecov.io/gh/milvus-io/milvus-sdk-go/pull/332?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=milvus-io) | Coverage Δ | |
|---|---|---|
| [client/client.go](https://codecov.io/gh/milvus-io/milvus-sdk-go/pull/332/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=milvus-io#diff-Y2xpZW50L2NsaWVudC5nbw==) | `75.80% <85.00%> (+4.37%)` | :arrow_up: |

congqixia: /kind improvement
sre-ci-robot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/milvus-io/milvus-sdk-go/pull/332#pullrequestreview-1176856699" title="LGTM">congqixia</a>*, *<a href="https://github.com/milvus-io/milvus-sdk-go/pull/332#" title="Author self-approved">jaime0815</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=milvus-io%2Fmilvus-sdk-go).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com/milvus-io/milvus-sdk-go/blob/master/OWNERS)~~ [congqixia]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->

Patch:
diff --git a/client/client.go b/client/client.go
index 57ac668..6305778 100644
--- a/client/client.go
+++ b/client/client.go
@@ -16,6 +16,8 @@ import (
 	"context"
 	"crypto/tls"
 	"math"
+	"net/url"
+	"strings"
 	"time"
 
 	grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
@@ -216,6 +218,19 @@ func NewDefaultGrpcClient(ctx context.Context, addr string) (Client, error) {
 	return c, nil
 }
 
+func NewDefaultGrpcClientWithURI(ctx context.Context, uri, username, password string) (Client, error) {
+	addr, inSecure, err := parseURI(uri)
+	if err != nil {
+		return nil, err
+	}
+
+	if inSecure {
+		return NewDefaultGrpcClientWithTLSAuth(ctx, addr, username, password)
+	}
+
+	return NewDefaultGrpcClientWithAuth(ctx, addr, username, password)
+}
+
 // NewDefaultGrpcClientWithTLSAuth enable transport security
 func NewDefaultGrpcClientWithTLSAuth(ctx context.Context, addr, username, password string) (Client, error) {
 	defaultOpts := getDefaultAuthOpts(username, password, true)
@@ -251,3 +266,27 @@ func getDefaultAuthOpts(username, password string, enableTLS bool) []grpc.DialOp
 	)
 	return defaultOpts
 }
+
+func parseURI(uri string) (string, bool, error) {
+	hasPrefix := false
+	inSecure := false
+	if strings.HasPrefix(uri, "https://") {
+		inSecure = true
+		hasPrefix = true
+	}
+
+	if strings.HasPrefix(uri, "http://") {
+		inSecure = false
+		hasPrefix = true
+	}
+
+	if hasPrefix {
+		url, err := url.Parse(uri)
+		if err != nil {
+			return "", inSecure, err
+		}
+		return url.Host, inSecure, nil
+	}
+
+	return uri, inSecure, nil
+}


Test patch:
diff --git a/client/client_test.go b/client/client_test.go
index 979b4bd..655d57c 100644
--- a/client/client_test.go
+++ b/client/client_test.go
@@ -339,3 +339,57 @@ func TestClient_getDefaultAuthOpts(t *testing.T) {
 	defaultOpts = getDefaultAuthOpts(username, password, false)
 	assert.True(t, len(defaultOpts) == 6)
 }
+
+func TestClient_NewDefaultGrpcClientWithURI(t *testing.T) {
+	username := "u"
+	password := "p"
+	t.Run("create grpc client fail", func(t *testing.T) {
+		uri := "https://localhost:port"
+		ctx := context.Background()
+		client, err := NewDefaultGrpcClientWithURI(ctx, uri, username, password)
+		assert.Nil(t, client)
+		assert.Error(t, err)
+	})
+}
+
+func TestClient_parseURI(t *testing.T) {
+	t.Run("https uri parse ok", func(t *testing.T) {
+		uri := "https://localhost:8080"
+		addr, inSecure, err := parseURI(uri)
+		assert.Equal(t, "localhost:8080", addr)
+		assert.True(t, inSecure)
+		assert.Nil(t, err)
+	})
+
+	t.Run("https uri parse fail", func(t *testing.T) {
+		uri := "https://localhost:port"
+		addr, inSecure, err := parseURI(uri)
+		assert.Equal(t, "", addr)
+		assert.True(t, inSecure)
+		assert.NotNil(t, err)
+	})
+
+	t.Run("http uri parse ok", func(t *testing.T) {
+		uri := "http://localhost:8080"
+		addr, inSecure, err := parseURI(uri)
+		assert.Equal(t, "localhost:8080", addr)
+		assert.False(t, inSecure)
+		assert.Nil(t, err)
+	})
+
+	t.Run("http uri parse fail", func(t *testing.T) {
+		uri := "http://localhost:port"
+		addr, inSecure, err := parseURI(uri)
+		assert.Equal(t, "", addr)
+		assert.False(t, inSecure)
+		assert.NotNil(t, err)
+	})
+
+	t.Run("no prefix uri parse ok", func(t *testing.T) {
+		uri := "localhost:8080"
+		addr, inSecure, err := parseURI(uri)
+		assert.Equal(t, "localhost:8080", addr)
+		assert.False(t, inSecure)
+		assert.Nil(t, err)
+	})
+}