Skip to content

Commit

Permalink
Fix/accounts (#262)
Browse files Browse the repository at this point in the history
* hotfix for release

* fix for accounts to be mandatory

* git review resolved

* git review resolved

* issue with multiple component status output resolved (#263)

* Fix/operate comp status (#264)

* issue with multiple component status output resolved

* removing wrong code
  • Loading branch information
yashjaind11 authored Oct 23, 2024
1 parent ff9b10f commit 653191f
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 17 deletions.
25 changes: 24 additions & 1 deletion cmd/create/environment.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package create

import (
"fmt"
"strings"

"github.com/dream11/odin/internal/service"
"github.com/dream11/odin/pkg/util"
environmentProto "github.com/dream11/odin/proto/gen/go/dream11/od/environment/v1"
Expand All @@ -26,6 +29,19 @@ var environmentCmd = &cobra.Command{
},
}

func validateAccounts(accounts string) error {
if accounts == "" {
return fmt.Errorf("accounts parameter cannot be an empty string")
}
accountList := strings.Split(accounts, ",")
for _, account := range accountList {
if account == "" {
return fmt.Errorf("accounts parameter should not end with a comma")
}
}
return nil
}

func init() {
environmentCmd.Flags().StringVar(&envName, "name", "", "name of the environment to be created")
environmentCmd.Flags().StringVar(&accounts, "accounts", "", "list of comma separated cloud provider accounts")
Expand All @@ -37,12 +53,19 @@ func init() {
if err := environmentCmd.MarkFlagRequired("provisioning-type"); err != nil {
log.Fatal("Error marking 'provisioning-type' flag as required:", err)
}
err = environmentCmd.MarkFlagRequired("accounts")
if err != nil {
log.Fatal("Error marking 'accounts' flag as required:", err)
}
createCmd.AddCommand(environmentCmd)
}

func execute(cmd *cobra.Command) {
ctx := cmd.Context()

// Validate accounts parameter
if err := validateAccounts(accounts); err != nil {
log.Fatal("Invalid accounts parameter: ", err)
}
err := environmentClient.CreateEnvironment(&ctx, &environmentProto.CreateEnvironmentRequest{
EnvName: envName,
Accounts: util.SplitProviderAccount(accounts),
Expand Down
5 changes: 2 additions & 3 deletions cmd/describe/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ 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"
)

Expand Down Expand Up @@ -47,7 +47,7 @@ func executeDescribeComponentType(cmd *cobra.Command) {
ctx := cmd.Context()
response, err := componentClient.DescribeComponentType(&ctx, &comp.DescribeComponentTypeRequest{
ComponentType: componentName,
Params: params,
Params: params,
})

if err != nil {
Expand All @@ -64,4 +64,3 @@ func writeAsJSONDescribeComponentType(response *comp.DescribeComponentTypeRespon
}
fmt.Println(string(output))
}

8 changes: 4 additions & 4 deletions cmd/describe/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ 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"
"github.com/iancoleman/orderedmap"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"strconv"
)

var serviceName string
Expand Down Expand Up @@ -58,8 +59,8 @@ func execute(cmd *cobra.Command) {
ctx := cmd.Context()
response, err := serviceClient.DescribeService(&ctx, &service.DescribeServiceRequest{
ServiceName: serviceName,
Version: serviceVersion,
Params: params,
Version: serviceVersion,
Params: params,
})

if err != nil {
Expand Down Expand Up @@ -90,4 +91,3 @@ func writeAsJSON(response *service.DescribeServiceResponse) {
}
fmt.Println(string(output))
}

6 changes: 3 additions & 3 deletions cmd/list/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ func writeListServiceAsJSON(response *serviceProto.ListServiceResponse) {
var services []map[string]interface{}
for _, serviceEntity := range response.Services {
services = append(services, map[string]interface{}{
"name": serviceEntity.Name,
"version": serviceEntity.Version,
"Tags": serviceEntity.Tags,
"name": serviceEntity.Name,
"version": serviceEntity.Version,
"Tags": serviceEntity.Tags,
})
}
output, _ := json.MarshalIndent(services, "", " ")
Expand Down
4 changes: 2 additions & 2 deletions internal/service/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"context"
"errors"
"fmt"
"github.com/dream11/odin/pkg/util"
"io"

"github.com/briandowns/spinner"
"github.com/dream11/odin/pkg/constant"
"github.com/dream11/odin/pkg/util"
component "github.com/dream11/odin/proto/gen/go/dream11/od/component/v1"
serviceProto "github.com/dream11/odin/proto/gen/go/dream11/od/service/v1"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -46,7 +46,7 @@ func (e *Component) OperateComponent(ctx *context.Context, request *serviceProto
return err
}
if response != nil {
message = util.GenerateResponseMessage(response.GetServiceResponse())
message = util.GenerateResponseMessageComponentSpecific(response.GetServiceResponse(), []string{request.GetComponentName()})
spinnerInstance.Prefix = fmt.Sprintf(" %s ", message)
spinnerInstance.Start()
}
Expand Down
9 changes: 6 additions & 3 deletions internal/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"context"
"errors"
"fmt"
"github.com/dream11/odin/pkg/util"
"io"

"github.com/briandowns/spinner"
"github.com/dream11/odin/pkg/constant"
"github.com/dream11/odin/pkg/util"
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"
Expand Down Expand Up @@ -276,14 +276,17 @@ func (e *Service) ReleaseService(ctx *context.Context, request *serviceProto.Rel
message = response.Message
message += fmt.Sprintf("\n Service %s %s", response.ServiceStatus.ServiceAction, response.ServiceStatus)
for _, compMessage := range response.ComponentsStatus {
message += fmt.Sprintf("\n Component %s %s %s", compMessage.ComponentName, compMessage.ComponentAction, compMessage.ComponentStatus)
message += fmt.Sprintf("\n Component %s %s %s %s", compMessage.ComponentName, compMessage.ComponentAction, compMessage.ComponentStatus, compMessage.Error)
if compMessage.ComponentStatus == "FAILED" {
return errors.New(compMessage.Error)
}
}

spinnerInstance.Prefix = fmt.Sprintf(" %s ", message)
spinnerInstance.Start()
}
}
log.Info("Service released successfully !")

return err
}

Expand Down
24 changes: 23 additions & 1 deletion pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package util

import (
"fmt"
v1 "github.com/dream11/odin/proto/gen/go/dream11/od/service/v1"
"net"
"strings"

v1 "github.com/dream11/odin/proto/gen/go/dream11/od/service/v1"
)

// SplitProviderAccount splits string into list of cloud provider accounts
Expand All @@ -29,3 +30,24 @@ func GenerateResponseMessage(response *v1.ServiceResponse) string {
}
return message
}

// contains checks if a string is present in an array of strings
func contains(str string, arr []string) bool {
for _, item := range arr {
if item == str {
return true
}
}
return false
}

// GenerateResponseMessageComponentSpecific generate response message from ServiceResponse
func GenerateResponseMessageComponentSpecific(response *v1.ServiceResponse, components []string) string {
message := fmt.Sprintf("\n Service %s %s", response.ServiceStatus.ServiceAction, response.ServiceStatus)
for _, compMessage := range response.ComponentsStatus {
if contains(compMessage.ComponentName, components) {
message += fmt.Sprintf("\n Component %s %s %s %s", compMessage.ComponentName, compMessage.ComponentAction, compMessage.ComponentStatus, compMessage.Error)
}
}
return message
}

0 comments on commit 653191f

Please sign in to comment.