Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Add describe service #252

Merged
merged 9 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions cmd/describe/component.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package describe

import (
"encoding/json"
"fmt"
serviceBackend "github.com/dream11/odin/internal/service"
comp "github.com/dream11/odin/proto/gen/go/dream11/od/component/v1"
log "github.com/sirupsen/logrus"

"github.com/spf13/cobra"
)

var componentName string
var componentVersion string

var componentClient = serviceBackend.Component{}
var componentCmd = &cobra.Command{
Use: "component-type",
Short: "Describe component-type",
Args: func(cmd *cobra.Command, args []string) error {
return cobra.NoArgs(cmd, args)
},
Long: `Describe component-type schema and defaults with all flavours and operations`,
Run: func(cmd *cobra.Command, args []string) {
executeDescribeComponentType(cmd)
},
}

func init() {
componentCmd.Flags().StringVar(&componentName, "name", "", "name of the service")
componentCmd.Flags().StringVar(&componentVersion, "version", "", "version of the service")
err := componentCmd.MarkFlagRequired("name")
if err != nil {
log.Fatal("Error marking 'name' flag as required:", err)
}
err = componentCmd.MarkFlagRequired("version")
if err != nil {
log.Fatal("Error marking 'version' flag as required:", err)
}
describeCmd.AddCommand(componentCmd)
}

func executeDescribeComponentType(cmd *cobra.Command) {
params := map[string]string{
"version": componentVersion,
}
ctx := cmd.Context()
response, err := componentClient.DescribeComponentType(&ctx, &comp.DescribeComponentTypeRequest{
ComponentType: componentName,
Params: params,
})

if err != nil {
log.Fatal("Failed to describe service: ", err)
}

writeAsJSONDescribeComponentType(response)
}

func writeAsJSONDescribeComponentType(response *comp.DescribeComponentTypeResponse) {
output, err := json.MarshalIndent(response.Component, "", " ")
if err != nil {
log.Fatal("Error marshaling JSON:", err)
}
fmt.Println(string(output))
}

16 changes: 16 additions & 0 deletions cmd/describe/describe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package describe

import (
"github.com/dream11/odin/cmd"
"github.com/spf13/cobra"
)

var describeCmd = &cobra.Command{
Use: "describe",
Short: "Describe resources",
Long: `Describe resources`,
}

func init() {
cmd.RootCmd.AddCommand(describeCmd)
}
93 changes: 93 additions & 0 deletions cmd/describe/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package describe

import (
"encoding/json"
"fmt"
serviceBackend "github.com/dream11/odin/internal/service"
service "github.com/dream11/odin/proto/gen/go/dream11/od/service/v1"
"github.com/iancoleman/orderedmap"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"strconv"
)

var serviceName string
var serviceVersion string
var component string
var verbose bool

var serviceClient = serviceBackend.Service{}
var serviceCmd = &cobra.Command{
Use: "service",
Short: "Describe service",
Args: func(cmd *cobra.Command, args []string) error {
return cobra.NoArgs(cmd, args)
},
Long: `Describe definition and provisionig files of a service`,
Run: func(cmd *cobra.Command, args []string) {
execute(cmd)
},
}

func init() {
serviceCmd.Flags().StringVar(&serviceName, "name", "", "name of the service")
serviceCmd.Flags().StringVar(&serviceVersion, "version", "", "version of the service")
serviceCmd.Flags().StringVar(&component, "component", "", "Display the config of a specific component only")
serviceCmd.Flags().BoolVarP(&verbose, "verbose", "V", false, "display provisioning files data")
err := componentCmd.MarkFlagRequired("name")
if err != nil {
log.Fatal("Error marking 'name' flag as required:", err)
}
err = componentCmd.MarkFlagRequired("version")
if err != nil {
log.Fatal("Error marking 'version' flag as required:", err)
}
describeCmd.AddCommand(serviceCmd)
}

func execute(cmd *cobra.Command) {

params := map[string]string{
"verbose": strconv.FormatBool(verbose),
}

if component != "" {
params["component"] = component
}

ctx := cmd.Context()
response, err := serviceClient.DescribeService(&ctx, &service.DescribeServiceRequest{
ServiceName: serviceName,
Version: serviceVersion,
Params: params,
})

if err != nil {
log.Fatal("Failed to describe service: ", err)
}

writeAsJSON(response)
}

func writeAsJSON(response *service.DescribeServiceResponse) {
serviceData := orderedmap.New()
serviceData.Set("name", response.Service.Name)

if response.Service.Version != nil && *response.Service.Version != "" {
serviceData.Set("version", *response.Service.Version)
}

if response.Service.ServiceDefinition != nil && len(response.Service.ServiceDefinition.GetFields()) > 0 {
serviceData.Set("definition", response.Service.ServiceDefinition)
}
if len(response.Service.ProvisioningConfigFiles) > 0 {
serviceData.Set("provision", response.Service.ProvisioningConfigFiles)
}

output, err := json.MarshalIndent(serviceData, "", " ")
if err != nil {
log.Fatal("Error marshaling JSON:", err)
}
fmt.Println(string(output))
}

1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/iancoleman/orderedmap v0.3.0
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,8 @@ github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO
github.com/hashicorp/mdns v1.0.4/go.mod h1:mtBihi+LeNXGtG8L9dX59gAEa12BDtBQSp4v/YAJqrc=
github.com/hashicorp/memberlist v0.5.0/go.mod h1:yvyXLpo0QaGE59Y7hDTsTzDD25JYBZ4mHgHUZ8lrOI0=
github.com/hashicorp/serf v0.10.1/go.mod h1:yL2t6BqATOLGc5HF7qbFkTfXoPIY0WZdWHfEvMqbG+4=
github.com/iancoleman/orderedmap v0.3.0 h1:5cbR2grmZR/DiVt+VJopEhtVs9YGInGIxAoMJn+Ichc=
github.com/iancoleman/orderedmap v0.3.0/go.mod h1:XuLcCUkdL5owUCQeF2Ue9uuw1EptkJDkXXS7VoV7XGE=
github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
Expand Down
15 changes: 15 additions & 0 deletions internal/service/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,18 @@ func (e *Component) ListComponentType(ctx *context.Context, request *component.L

return response, nil
}

// DescribeComponentType List component types
func (e *Component) DescribeComponentType(ctx *context.Context, request *component.DescribeComponentTypeRequest) (*component.DescribeComponentTypeResponse, error) {
conn, requestCtx, err := grpcClient(ctx)
if err != nil {
return nil, err
}
client := component.NewComponentServiceClient(conn)
response, err := client.DescribeComponentType(*requestCtx, request)
if err != nil {
return nil, err
}

return response, nil
}
16 changes: 16 additions & 0 deletions internal/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
serviceDto "github.com/dream11/odin/proto/gen/go/dream11/od/dto/v1"
serviceProto "github.com/dream11/odin/proto/gen/go/dream11/od/service/v1"
log "github.com/sirupsen/logrus"

)

// Service performs operation on service like deploy. undeploy
Expand Down Expand Up @@ -266,3 +267,18 @@ func (e *Service) ConvertToDeployServiceSetRequest(serviceSet *serviceDto.Servic
Services: services,
}
}

// DescribeService describe service
func (e *Service) DescribeService(ctx *context.Context, request *serviceProto.DescribeServiceRequest) (*serviceProto.DescribeServiceResponse, error) {
conn, requestCtx, err := grpcClient(ctx)
if err != nil {
return nil, err
}
client := serviceProto.NewServiceServiceClient(conn)
response, err := client.DescribeService(*requestCtx, request)
if err != nil {
return nil, err
}

return response, nil
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
_ "github.com/dream11/odin/cmd/undeploy"
_ "github.com/dream11/odin/cmd/update"
_ "github.com/dream11/odin/internal/ui"
_ "github.com/dream11/odin/cmd/describe"
)

func main() {
Expand Down
10 changes: 10 additions & 0 deletions proto/dream11/od/component/v1/component.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ option go_package = "github.com/dream11/odin/proto/gen/go/dream11/od/component/v

service ComponentService {
rpc ListComponentType(ListComponentTypeRequest) returns (ListComponentTypeResponse) {}
rpc DescribeComponentType(DescribeComponentTypeRequest) returns (DescribeComponentTypeResponse) {}
}

message ListComponentTypeRequest {
Expand All @@ -16,3 +17,12 @@ message ListComponentTypeRequest {
message ListComponentTypeResponse {
repeated dream11.od.dto.v1.Component components = 1;
}

message DescribeComponentTypeRequest {
string component_type = 1;
map<string, string> params = 2;
}

message DescribeComponentTypeResponse {
dream11.od.dto.v1.Component component = 1;
}
27 changes: 22 additions & 5 deletions proto/dream11/od/dto/v1/component.proto
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
syntax = "proto3";
package dream11.od.dto.v1;

import "google/protobuf/struct.proto";
import "google/protobuf/timestamp.proto";

option go_package = "github.com/dream11/odin/proto/gen/go/dream11/od/dto/v1";

message Component {
int64 id = 1;
google.protobuf.Timestamp created_at = 2;
google.protobuf.Timestamp updated_at = 3;
string component_type = 4;
string component_version = 5;
optional int64 id = 1;
optional google.protobuf.Timestamp created_at = 2;
optional google.protobuf.Timestamp updated_at = 3;
optional string component_type = 6;
optional string component_version = 7;
optional google.protobuf.Struct common_schema = 8;
optional google.protobuf.Struct common_defaults = 9;
repeated Flavour flavours = 10;
}

message Flavour {
optional string name = 1;
optional google.protobuf.Struct schema = 2;
optional google.protobuf.Struct defaults = 3;
repeated Operation operations = 4;
}

message Operation {
optional string name = 1;
optional google.protobuf.Struct schema = 2;
optional google.protobuf.Struct defaults = 3;
}
13 changes: 13 additions & 0 deletions proto/dream11/od/dto/v1/service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ syntax = "proto3";
package dream11.od.dto.v1;

import "google/protobuf/struct.proto";
import "google/protobuf/timestamp.proto";

option go_package = "github.com/dream11/odin/proto/gen/go/dream11/od/dto/v1";

Expand Down Expand Up @@ -63,3 +64,15 @@ message AddComponentRequestOptions {
message RemoveComponentRequestOptions {
string component_name = 1;
}

message Service {
optional string name = 1;
optional string version = 2;
optional google.protobuf.Timestamp created_at = 3;
optional google.protobuf.Timestamp updated_at = 4;
optional string created_by = 5;
optional string updated_by = 6;
optional google.protobuf.Struct service_definition = 7;
repeated google.protobuf.Struct provisioning_config_files = 8;
repeated string versions = 9;
}
11 changes: 11 additions & 0 deletions proto/dream11/od/service/v1/service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ service ServiceService {
rpc OperateService(OperateServiceRequest) returns (stream OperateServiceResponse) {}
rpc UndeployService(UndeployServiceRequest) returns (stream UndeployServiceResponse) {}
rpc ListService(ListServiceRequest) returns (ListServiceResponse) {}
rpc DescribeService(DescribeServiceRequest) returns (DescribeServiceResponse) {}
}

message DeployServiceRequest {
Expand Down Expand Up @@ -118,3 +119,13 @@ message ListServiceRequest {
string team = 3;
string label = 4;
}

message DescribeServiceRequest {
string service_name = 1;
string version = 2;
map<string, string> params = 4;
}

message DescribeServiceResponse {
dream11.od.dto.v1.Service service = 1;
}
Loading
Loading