You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by GitBox <gi...@apache.org> on 2019/02/15 02:43:09 UTC

[GitHub] ChinX commented on a change in pull request #534: Issues-531 add registration and discovery for client

ChinX commented on a change in pull request #534: Issues-531 add registration and discovery for client
URL: https://github.com/apache/servicecomb-service-center/pull/534#discussion_r257088952
 
 

 ##########
 File path: pkg/client/sc/microservice.go
 ##########
 @@ -0,0 +1,122 @@
+package sc
+
+import (
+	"context"
+	"encoding/json"
+	"fmt"
+	"io/ioutil"
+	"net/http"
+	"net/url"
+
+	"github.com/apache/servicecomb-service-center/server/core"
+	pb "github.com/apache/servicecomb-service-center/server/core/proto"
+	scerr "github.com/apache/servicecomb-service-center/server/error"
+)
+
+const (
+	apiExistenceURL     = "/v4/%s/registry/existence"
+	apiMicroServicesURL = "/v4/%s/registry/microservices"
+	apiMicroServiceURL  = "/v4/%s/registry/microservices/%s"
+
+	MicroServiceType existenceType = "microservice"
+	SchemaType       existenceType = "schema"
+)
+
+type existenceType string
+
+func (c *SCClient) CreateService(ctx context.Context, domainProject string, service *pb.MicroService) (string, *scerr.Error) {
+	domain, project := core.FromDomainProject(domainProject)
+	headers := c.CommonHeaders(ctx)
+	headers.Set("X-Domain-Name", domain)
+
+	reqBody, err := json.Marshal(&pb.CreateServiceRequest{Service: service})
+	if err != nil {
+		return "", scerr.NewError(scerr.ErrInternal, err.Error())
+	}
+
+	resp, err := c.RestDoWithContext(ctx, http.MethodPost,
+		fmt.Sprintf(apiMicroServicesURL, project),
+		headers, reqBody)
+	if err != nil {
+		return "", scerr.NewError(scerr.ErrInternal, err.Error())
+	}
+	defer resp.Body.Close()
+
+	body, err := ioutil.ReadAll(resp.Body)
+	if err != nil {
+		return "", scerr.NewError(scerr.ErrInternal, err.Error())
+	}
+
+	if resp.StatusCode != http.StatusOK {
+		return "", c.toError(body)
+	}
+
+	serviceResp := &pb.CreateServiceResponse{}
+	err = json.Unmarshal(body, serviceResp)
+	if err != nil {
+		return "", scerr.NewError(scerr.ErrInternal, err.Error())
+	}
+	return serviceResp.ServiceId, nil
+}
+
+func (c *SCClient) DeleteService(ctx context.Context, domainProject, serviceId string) *scerr.Error {
+	domain, project := core.FromDomainProject(domainProject)
+	headers := c.CommonHeaders(ctx)
+	headers.Set("X-Domain-Name", domain)
+
+	resp, err := c.RestDoWithContext(ctx, http.MethodDelete,
+		fmt.Sprintf(apiMicroServiceURL, project, serviceId),
+		headers, nil)
+	if err != nil {
+		return scerr.NewError(scerr.ErrInternal, err.Error())
+	}
+	defer resp.Body.Close()
+
+	body, err := ioutil.ReadAll(resp.Body)
+	if err != nil {
+		return scerr.NewError(scerr.ErrInternal, err.Error())
+	}
+
+	if resp.StatusCode != http.StatusOK {
+		return c.toError(body)
+	}
+
+	return nil
+}
+
+func (c *SCClient) ServiceExistence(ctx context.Context, domainProject string, env existenceType, appId, serviceName, versionRule string) (string, *scerr.Error) {
+	domain, project := core.FromDomainProject(domainProject)
+	headers := c.CommonHeaders(ctx)
+	headers.Set("X-Domain-Name", domain)
+
+	query := url.Values{}
+	query.Set("type", string(env))
 
 Review comment:
   @little-cui Modified, please review

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services