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

Describe service #209

Open
wants to merge 4 commits into
base: 2.x.x
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion api/configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type SecretKeys struct {

// Configuration interface
type Configuration struct {
BackendAddress string `toml:"backend_address,omitempty" mapstructure:"backend_addr,omitempty"`
BackendAddress string `toml:"backend_address,omitempty" mapstructure:"backend_address,omitempty"`
Keys SecretKeys
AccessToken string `toml:"access_token,omitempty" mapstructure:"access_token,omitempty"`
EnvName string `toml:"envName,omitempty" mapstructure:"envName,omitempty"`
Expand Down
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)
}
118 changes: 118 additions & 0 deletions cmd/describe/service.go
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
Copy link
Collaborator

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?

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)
Copy link
Collaborator

Choose a reason for hiding this comment

The 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))
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require (
)

require (
github.com/iancoleman/orderedmap v0.3.0
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.16.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,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
25 changes: 25 additions & 0 deletions internal/service/service.go
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

Check failure on line 12 in internal/service/service.go

View workflow job for this annotation

GitHub Actions / lint

comment on exported method Service.DescribeService should be of the form "DescribeService ..."
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
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package main
import (
"github.com/dream11/odin/cmd"
_ "github.com/dream11/odin/cmd/list"
_ "github.com/dream11/odin/cmd/describe"
_ "github.com/dream11/odin/cmd/configure"
_ "github.com/dream11/odin/internal/ui"
)
Expand Down
20 changes: 20 additions & 0 deletions proto/dream11/od/dto/v1/service.proto
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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we have both version and versions?

}
21 changes: 21 additions & 0 deletions proto/dream11/od/service/v1/service.proto
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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we be consistent in naming these eg. either have name and version OR service_name and service_version?

string version = 2;
map<string, string> labels = 3;
map<string, string> params = 4;
}

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