You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by wu...@apache.org on 2022/01/14 06:52:31 UTC

[skywalking-cli] branch master updated: adapt v9 protocols (#134)

This is an automated email from the ASF dual-hosted git repository.

wusheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/skywalking-cli.git


The following commit(s) were added to refs/heads/master by this push:
     new 3278a47  adapt v9 protocols (#134)
3278a47 is described below

commit 3278a47ab49e672f4ef7eb2ab5c806faf3c48dd5
Author: mrproliu <74...@qq.com>
AuthorDate: Fri Jan 14 14:52:26 2022 +0800

    adapt v9 protocols (#134)
---
 CHANGES.md                                         |  3 ++
 ...SearchService.graphql => FindEndpoints.graphql} |  8 +--
 .../{SearchService.graphql => FindService.graphql} |  4 +-
 ...archService.graphql => GetEndpointInfo.graphql} | 10 ++--
 .../v2/{Instances.graphql => GetInstance.graphql}  |  6 +--
 assets/graphqls/metadata/v2/Instances.graphql      |  2 +-
 internal/commands/endpoint/endpoint.go             |  1 +
 internal/commands/endpoint/{endpoint.go => get.go} | 31 +++++++++---
 internal/commands/instance/{instance.go => get.go} | 32 +++++++++---
 internal/commands/instance/instance.go             |  1 +
 pkg/graphql/metadata/metadata.go                   | 57 ++++++++++++++++++----
 test/cases/9.0.0/expected/endpoint-get.yml         | 19 ++++++++
 test/cases/9.0.0/expected/instance-get.yml         | 25 ++++++++++
 test/cases/9.0.0/test.yaml                         |  4 ++
 14 files changed, 168 insertions(+), 35 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index 32119ba..8135bde 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -10,6 +10,9 @@ Release Notes.
 - Add the sub-command `dependency instance` to query instance relationships (#117)
 - Add the sub-command `service layer` to query services according to layer.(#133)
 - Add the sub-command `layer list` to query layer list.(#133)
+- Add the sub-command `instance get` to query single instance.(#134)
+- Add the sub-command `endpoint get` to query single endpoint info.(#134)
+- Change the GraphQL method to the v9 version according to the server version.(#134)
 
 ### Bug Fixes
 
diff --git a/assets/graphqls/metadata/v2/SearchService.graphql b/assets/graphqls/metadata/v2/FindEndpoints.graphql
similarity index 83%
copy from assets/graphqls/metadata/v2/SearchService.graphql
copy to assets/graphqls/metadata/v2/FindEndpoints.graphql
index f40d2c1..f53c225 100644
--- a/assets/graphqls/metadata/v2/SearchService.graphql
+++ b/assets/graphqls/metadata/v2/FindEndpoints.graphql
@@ -15,8 +15,8 @@
 # specific language governing permissions and limitations
 # under the License.
 
-query searchService($serviceCode: String!) {
-    result: searchService(serviceCode: $serviceCode) {
-        id name group shortName layers
+query ($keyword: String!, $serviceId: ID!, $limit: Int!) {
+    result: findEndpoint(keyword: $keyword, serviceId: $serviceId, limit: $limit) {
+        id name
     }
-}
+}
\ No newline at end of file
diff --git a/assets/graphqls/metadata/v2/SearchService.graphql b/assets/graphqls/metadata/v2/FindService.graphql
similarity index 89%
copy from assets/graphqls/metadata/v2/SearchService.graphql
copy to assets/graphqls/metadata/v2/FindService.graphql
index f40d2c1..8d89566 100644
--- a/assets/graphqls/metadata/v2/SearchService.graphql
+++ b/assets/graphqls/metadata/v2/FindService.graphql
@@ -15,8 +15,8 @@
 # specific language governing permissions and limitations
 # under the License.
 
-query searchService($serviceCode: String!) {
-    result: searchService(serviceCode: $serviceCode) {
+query findService($serviceName: String!) {
+    result: findService(serviceName: $serviceName) {
         id name group shortName layers
     }
 }
diff --git a/assets/graphqls/metadata/v2/SearchService.graphql b/assets/graphqls/metadata/v2/GetEndpointInfo.graphql
similarity index 84%
rename from assets/graphqls/metadata/v2/SearchService.graphql
rename to assets/graphqls/metadata/v2/GetEndpointInfo.graphql
index f40d2c1..26ecbae 100644
--- a/assets/graphqls/metadata/v2/SearchService.graphql
+++ b/assets/graphqls/metadata/v2/GetEndpointInfo.graphql
@@ -15,8 +15,12 @@
 # specific language governing permissions and limitations
 # under the License.
 
-query searchService($serviceCode: String!) {
-    result: searchService(serviceCode: $serviceCode) {
-        id name group shortName layers
+
+query ($endpointId: ID!) {
+    result: getEndpointInfo(endpointId: $endpointId) {
+        id
+        name
+        serviceId
+        serviceName
     }
 }
diff --git a/assets/graphqls/metadata/v2/Instances.graphql b/assets/graphqls/metadata/v2/GetInstance.graphql
similarity index 88%
copy from assets/graphqls/metadata/v2/Instances.graphql
copy to assets/graphqls/metadata/v2/GetInstance.graphql
index 0a78de3..45722c4 100644
--- a/assets/graphqls/metadata/v2/Instances.graphql
+++ b/assets/graphqls/metadata/v2/GetInstance.graphql
@@ -16,8 +16,8 @@
 # under the License.
 
 
-query ($serviceId: ID!, $duration: Duration!) {
-    result: getServiceInstances(duration: $duration, serviceId: $serviceId) {
+query ($instanceId: String!) {
+    result: getInstance(instanceId: $instanceId) {
         id
         name
         language
@@ -28,4 +28,4 @@ query ($serviceId: ID!, $duration: Duration!) {
             value
         }
     }
-}
+}
\ No newline at end of file
diff --git a/assets/graphqls/metadata/v2/Instances.graphql b/assets/graphqls/metadata/v2/Instances.graphql
index 0a78de3..cf6d9cb 100644
--- a/assets/graphqls/metadata/v2/Instances.graphql
+++ b/assets/graphqls/metadata/v2/Instances.graphql
@@ -17,7 +17,7 @@
 
 
 query ($serviceId: ID!, $duration: Duration!) {
-    result: getServiceInstances(duration: $duration, serviceId: $serviceId) {
+    result: listInstances(duration: $duration, serviceId: $serviceId) {
         id
         name
         language
diff --git a/internal/commands/endpoint/endpoint.go b/internal/commands/endpoint/endpoint.go
index c1b9675..808207a 100644
--- a/internal/commands/endpoint/endpoint.go
+++ b/internal/commands/endpoint/endpoint.go
@@ -27,5 +27,6 @@ var Command = &cli.Command{
 	Usage:   "Endpoint related sub-command",
 	Subcommands: cli.Commands{
 		ListCommand,
+		GetCommand,
 	},
 }
diff --git a/internal/commands/endpoint/endpoint.go b/internal/commands/endpoint/get.go
similarity index 53%
copy from internal/commands/endpoint/endpoint.go
copy to internal/commands/endpoint/get.go
index c1b9675..ec6a09c 100644
--- a/internal/commands/endpoint/endpoint.go
+++ b/internal/commands/endpoint/get.go
@@ -18,14 +18,33 @@
 package endpoint
 
 import (
+	"fmt"
+
 	"github.com/urfave/cli/v2"
+
+	"github.com/apache/skywalking-cli/pkg/display"
+	"github.com/apache/skywalking-cli/pkg/display/displayable"
+	"github.com/apache/skywalking-cli/pkg/graphql/metadata"
 )
 
-var Command = &cli.Command{
-	Name:    "endpoint",
-	Aliases: []string{"e"},
-	Usage:   "Endpoint related sub-command",
-	Subcommands: cli.Commands{
-		ListCommand,
+var GetCommand = &cli.Command{
+	Name:  "get",
+	Usage: `get monitored endpoint of the given <endpoint-id>`,
+	UsageText: `This command get single endpoint, via endpoint-id.
+
+Examples:
+1. get single endpoint by endpoint id "cHJvdmlkZXI=.1_L3VzZXJz":
+$ swctl endpoint get cHJvdmlkZXI=.1_L3VzZXJz`,
+	Action: func(ctx *cli.Context) error {
+		if ctx.Args().Len() == 0 {
+			return fmt.Errorf("endpoint-id must be provide")
+		}
+
+		endpointInfo, err := metadata.GetEndpointInfo(ctx, ctx.Args().First())
+		if err != nil {
+			return err
+		}
+
+		return display.Display(ctx, &displayable.Displayable{Data: endpointInfo})
 	},
 }
diff --git a/internal/commands/instance/instance.go b/internal/commands/instance/get.go
similarity index 53%
copy from internal/commands/instance/instance.go
copy to internal/commands/instance/get.go
index 856fa15..766c2c9 100644
--- a/internal/commands/instance/instance.go
+++ b/internal/commands/instance/get.go
@@ -18,15 +18,33 @@
 package instance
 
 import (
+	"fmt"
+
 	"github.com/urfave/cli/v2"
+
+	"github.com/apache/skywalking-cli/pkg/display"
+	"github.com/apache/skywalking-cli/pkg/display/displayable"
+	"github.com/apache/skywalking-cli/pkg/graphql/metadata"
 )
 
-var Command = &cli.Command{
-	Name:    "instance",
-	Aliases: []string{"i"},
-	Usage:   "Instance related sub-command",
-	Subcommands: cli.Commands{
-		ListCommand,
-		SearchCommand,
+var GetCommand = &cli.Command{
+	Name:  "get",
+	Usage: `get monitored instance of the given <instance-id>`,
+	UsageText: `This command get single instance, via instance-id.
+
+Examples:
+1. get single instance by instance id "cHJvdmlkZXI=.1_cHJvdmlkZXIx":
+$ swctl instance get cHJvdmlkZXI=.1_cHJvdmlkZXIx`,
+	Action: func(ctx *cli.Context) error {
+		if ctx.Args().Len() == 0 {
+			return fmt.Errorf("instance-id must be provide")
+		}
+
+		instance, err := metadata.GetInstance(ctx, ctx.Args().First())
+		if err != nil {
+			return err
+		}
+
+		return display.Display(ctx, &displayable.Displayable{Data: instance})
 	},
 }
diff --git a/internal/commands/instance/instance.go b/internal/commands/instance/instance.go
index 856fa15..85f4602 100644
--- a/internal/commands/instance/instance.go
+++ b/internal/commands/instance/instance.go
@@ -28,5 +28,6 @@ var Command = &cli.Command{
 	Subcommands: cli.Commands{
 		ListCommand,
 		SearchCommand,
+		GetCommand,
 	},
 }
diff --git a/pkg/graphql/metadata/metadata.go b/pkg/graphql/metadata/metadata.go
index af5a171..2a997d9 100644
--- a/pkg/graphql/metadata/metadata.go
+++ b/pkg/graphql/metadata/metadata.go
@@ -52,12 +52,18 @@ func AllServices(cliCtx *cli.Context, duration api.Duration) ([]api.Service, err
 func SearchService(cliCtx *cli.Context, serviceCode string) (service api.Service, err error) {
 	var response map[string]api.Service
 
-	version, err := protocolVersion(cliCtx)
+	majorVersion, err := backendMajorVersion(cliCtx)
 	if err != nil {
 		return api.Service{}, err
 	}
-	request := graphql.NewRequest(assets.Read("graphqls/metadata/" + version + "/SearchService.graphql"))
-	request.Var("serviceCode", serviceCode)
+	var request *graphql.Request
+	if majorVersion >= 9 {
+		request = graphql.NewRequest(assets.Read("graphqls/metadata/v2/FindService.graphql"))
+		request.Var("serviceName", serviceCode)
+	} else {
+		request = graphql.NewRequest(assets.Read("graphqls/metadata/v1/SearchService.graphql"))
+		request.Var("serviceCode", serviceCode)
+	}
 
 	err = client.ExecuteQuery(cliCtx, request, &response)
 
@@ -109,13 +115,24 @@ func SearchBrowserService(cliCtx *cli.Context, serviceCode string) (service api.
 func SearchEndpoints(cliCtx *cli.Context, serviceID, keyword string, limit int) ([]api.Endpoint, error) {
 	var response map[string][]api.Endpoint
 
-	request := graphql.NewRequest(assets.Read("graphqls/metadata/v1/SearchEndpoints.graphql"))
-	request.Var("serviceId", serviceID)
-	request.Var("keyword", keyword)
-	request.Var("limit", limit)
-
-	err := client.ExecuteQuery(cliCtx, request, &response)
+	majorVersion, err := backendMajorVersion(cliCtx)
+	if err != nil {
+		return nil, err
+	}
+	var request *graphql.Request
+	if majorVersion >= 9 {
+		request = graphql.NewRequest(assets.Read("graphqls/metadata/v2/FindEndpoints.graphql"))
+		request.Var("serviceId", serviceID)
+		request.Var("keyword", keyword)
+		request.Var("limit", limit)
+	} else {
+		request = graphql.NewRequest(assets.Read("graphqls/metadata/v1/SearchEndpoints.graphql"))
+		request.Var("serviceId", serviceID)
+		request.Var("keyword", keyword)
+		request.Var("limit", limit)
+	}
 
+	err = client.ExecuteQuery(cliCtx, request, &response)
 	return response["result"], err
 }
 
@@ -135,6 +152,28 @@ func Instances(cliCtx *cli.Context, serviceID string, duration api.Duration) ([]
 	return response["result"], err
 }
 
+func GetInstance(cliCtx *cli.Context, instanceID string) (api.ServiceInstance, error) {
+	var response map[string]api.ServiceInstance
+
+	request := graphql.NewRequest(assets.Read("graphqls/metadata/v2/GetInstance.graphql"))
+	request.Var("instanceId", instanceID)
+
+	err := client.ExecuteQuery(cliCtx, request, &response)
+
+	return response["result"], err
+}
+
+func GetEndpointInfo(cliCtx *cli.Context, endpointID string) (api.EndpointInfo, error) {
+	var response map[string]api.EndpointInfo
+
+	request := graphql.NewRequest(assets.Read("graphqls/metadata/v2/GetEndpointInfo.graphql"))
+	request.Var("endpointId", endpointID)
+
+	err := client.ExecuteQuery(cliCtx, request, &response)
+
+	return response["result"], err
+}
+
 func ServerTimeInfo(cliCtx *cli.Context) (api.TimeInfo, error) {
 	var response map[string]api.TimeInfo
 
diff --git a/test/cases/9.0.0/expected/endpoint-get.yml b/test/cases/9.0.0/expected/endpoint-get.yml
new file mode 100644
index 0000000..20c28e8
--- /dev/null
+++ b/test/cases/9.0.0/expected/endpoint-get.yml
@@ -0,0 +1,19 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+id: {{ b64enc "provider" }}.1_{{ b64enc "/users" }}
+name: /users
+serviceid: {{ b64enc "provider" }}.1
+servicename: provider
\ No newline at end of file
diff --git a/test/cases/9.0.0/expected/instance-get.yml b/test/cases/9.0.0/expected/instance-get.yml
new file mode 100644
index 0000000..2451a46
--- /dev/null
+++ b/test/cases/9.0.0/expected/instance-get.yml
@@ -0,0 +1,25 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+id: {{ b64enc "provider" }}.1_{{ b64enc "provider1" }}
+name: provider1
+attributes:
+{{- range .attributes }}
+  - name: "{{ .name }}"
+    value: "{{ .value }}"
+{{- end }}
+language: {{ .language }}
+instanceuuid: {{ b64enc "provider" }}.1_{{ b64enc "provider1" }}
+layer: GENERAL
diff --git a/test/cases/9.0.0/test.yaml b/test/cases/9.0.0/test.yaml
index b40d07f..03f497f 100644
--- a/test/cases/9.0.0/test.yaml
+++ b/test/cases/9.0.0/test.yaml
@@ -51,6 +51,8 @@ verify:
 
     - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql endpoint ls --service-name provider
       expected: expected/endpoint-list.yml
+    - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql endpoint get cHJvdmlkZXI=.1_L3VzZXJz
+      expected: expected/endpoint-get.yml
 
     - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql instance ls --service-name provider
       expected: expected/instance-list.yml
@@ -58,6 +60,8 @@ verify:
       expected: expected/instance-list.yml
     - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql instance search --service-name provider --regex not-exist
       expected: expected/empty-array.yml
+    - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql instance get cHJvdmlkZXI=.1_cHJvdmlkZXIx
+      expected: expected/instance-get.yml
 
     - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics top --name service_sla 5
       expected: expected/metrics-top-service-sla.yml