-
Notifications
You must be signed in to change notification settings - Fork 0
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
Describe service #209
base: 2.x.x
Are you sure you want to change the base?
Describe service #209
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package describe | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strconv" | ||
|
||
serviceBackend "github.com/dream11/odin/internal/service" | ||
service "github.com/dream11/odin/proto/gen/go/dream11/od/service/v1" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
"github.com/iancoleman/orderedmap" | ||
) | ||
|
||
var serviceName string | ||
var serviceVersion string | ||
var labelsJSON string | ||
var labels map[string]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(&labelsJSON, "labels", "", "labels of the service in the artifactory") | ||
serviceCmd.Flags().StringVar(&component, "component", "", "Display the config of a specific component only") | ||
serviceCmd.Flags().BoolVarP(&verbose, "verbose", "V", false, "display provisioning files data") | ||
describeCmd.AddCommand(serviceCmd) | ||
} | ||
|
||
func execute(cmd *cobra.Command) { | ||
|
||
validateFlags() | ||
|
||
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, | ||
Labels: labels, | ||
Params: params, | ||
}) | ||
|
||
if err != nil { | ||
log.Fatal("Failed to describe service: ", err) | ||
} | ||
|
||
writeAsJSON(response) | ||
} | ||
|
||
func validateFlags() { | ||
|
||
if serviceName == "" { | ||
log.Fatal("Please pass the --name flag") | ||
} | ||
|
||
if serviceVersion == "" && labelsJSON == "" { | ||
log.Fatal("Please pass either --version flag or --labels flag") | ||
} | ||
|
||
if serviceVersion != "" && labelsJSON != "" { | ||
log.Fatal("Please pass either --version flag or --labels flag but not both") | ||
} | ||
|
||
if labelsJSON != "" { | ||
err := json.Unmarshal([]byte(labelsJSON), &labels) | ||
if err != nil { | ||
log.Fatal("Error parsing JSON, the the key and values should be strings: ", err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the input is not a valid json, then will this error message will be misleading? |
||
} | ||
} | ||
} | ||
|
||
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.Versions != nil && len(response.Service.Versions) > 0 { | ||
serviceData.Set("versions", response.Service.Versions) | ||
} | ||
if response.Service.Labels != nil { | ||
serviceData.Set("labels", response.Service.Labels) | ||
} | ||
if response.Service.ServiceDefinition != nil && len(response.Service.ServiceDefinition.GetFields()) > 0 { | ||
serviceData.Set("definition", response.Service.ServiceDefinition) | ||
} | ||
if response.Service.ProvisioningConfigFiles != nil && 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)) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package service | ||
|
||
import ( | ||
"context" | ||
|
||
service "github.com/dream11/odin/proto/gen/go/dream11/od/service/v1" | ||
) | ||
|
||
// Service performs operation on service like list, describe | ||
type Service struct{} | ||
|
||
// DescribeSerice Describe service | ||
func (e *Service) DescribeService(ctx *context.Context, request *service.DescribeServiceRequest) (*service.DescribeServiceResponse, error) { | ||
conn, requestCtx, err := grpcClient(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
client := service.NewServiceServiceClient(conn) | ||
response, err := client.DescribeService(*requestCtx, request) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return response, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
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 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 labels = 7; | ||
optional google.protobuf.Struct service_definition = 8; | ||
repeated google.protobuf.Struct provisioning_config_files = 9; | ||
repeated string versions = 10; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we have both |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
syntax = "proto3"; | ||
package dream11.od.service.v1; | ||
|
||
import "dream11/od/dto/v1/service.proto"; | ||
|
||
option go_package = "github.com/dream11/odin/proto/gen/go/dream11/od/service/v1"; | ||
|
||
service ServiceService { | ||
rpc DescribeService(DescribeServiceRequest) returns (DescribeServiceResponse) {} | ||
} | ||
|
||
message DescribeServiceRequest { | ||
string service_name = 1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we be consistent in naming these eg. either have |
||
string version = 2; | ||
map<string, string> labels = 3; | ||
map<string, string> params = 4; | ||
} | ||
|
||
message DescribeServiceResponse { | ||
dream11.od.dto.v1.Service service = 1; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we use camel case consistently?