From cb78a660d1b9dcd585f497f5cd55c758514b0452 Mon Sep 17 00:00:00 2001 From: Nick Z <2420177+nickzelei@users.noreply.github.com> Date: Wed, 1 Mar 2023 11:57:32 -0800 Subject: [PATCH 1/3] filters out the current service --- cmd/services_dependencies.go | 52 ++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/cmd/services_dependencies.go b/cmd/services_dependencies.go index 7b80661..045ed36 100644 --- a/cmd/services_dependencies.go +++ b/cmd/services_dependencies.go @@ -50,17 +50,28 @@ var servicesDependenciesCmd = &cobra.Command{ // Set this after ensuring flags are correct cmd.SilenceUsage = true - servList, err := listRawServices(ctx, environmentName) + servList, err := getServiceNamesByEnvironment(ctx, environmentName) + if err != nil { + return err + } + nucleusConfig, err := config.GetNucleusConfig() if err != nil { return err } + filteredServices := []string{} + for _, svc := range servList { + if svc != nucleusConfig.Spec.ServiceName { + filteredServices = append(filteredServices, svc) + } + } + serviceQuestions := []*survey.Question{ { Name: "serviceType", Prompt: &survey.Select{ Message: "Select the service dependency: ", - Options: servList, + Options: filteredServices, }, Validate: survey.Required, }, @@ -88,7 +99,7 @@ func init() { servicesDependenciesCmd.Flags().StringP("env", "e", "", "set the nucleus environment") } -func listRawServices(ctx context.Context, environmentName string) ([]string, error) { +func getServiceNamesByEnvironment(ctx context.Context, environmentName string) ([]string, error) { conn, err := utils.NewApiConnectionByEnv(ctx, clienv.GetEnv()) if err != nil { return nil, err @@ -96,24 +107,23 @@ func listRawServices(ctx context.Context, environmentName string) ([]string, err defer conn.Close() cliClient := svcmgmtv1alpha1.NewServiceMgmtServiceClient(conn) - serviceList, err := cliClient.GetServices(ctx, &svcmgmtv1alpha1.GetServicesRequest{ + servicesResp, err := cliClient.GetServices(ctx, &svcmgmtv1alpha1.GetServicesRequest{ EnvironmentName: strings.TrimSpace(environmentName), }) if err != nil { return nil, err } - var simpleServiceList []string //just the service names - - for _, v := range serviceList.Services { - simpleServiceList = append(simpleServiceList, v.ServiceCustomerConfig.ServiceName) + serviceNames := []string{} + for _, v := range servicesResp.Services { + serviceNames = append(serviceNames, v.ServiceCustomerConfig.ServiceName) } - sort.Slice(simpleServiceList, func(i, j int) bool { - return simpleServiceList[i] < simpleServiceList[j] + sort.Slice(serviceNames, func(i, j int) bool { + return serviceNames[i] < serviceNames[j] }) - return simpleServiceList, nil + return serviceNames, nil } func storeServiceDependency(val string) error { @@ -122,24 +132,14 @@ func storeServiceDependency(val string) error { return err } - var allowedServices []string - - allowedServices = nucleusConfig.Spec.AllowedServices - - for _, v := range allowedServices { + for _, v := range nucleusConfig.Spec.AllowedServices { if val == v { - return fmt.Errorf("This service is already in the Allowed Services list") + fmt.Println("This service is already in the allowed services list") + return nil } } - allowedServices = append(allowedServices, val) - - nucleusConfig.Spec.AllowedServices = allowedServices - - err = config.SetNucleusConfig(nucleusConfig) + nucleusConfig.Spec.AllowedServices = append(nucleusConfig.Spec.AllowedServices, val) - if err != nil { - return err - } - return nil + return config.SetNucleusConfig(nucleusConfig) } From cb5314b1d74d51b26bc968c8d3b71105318d7416 Mon Sep 17 00:00:00 2001 From: Nick Z <2420177+nickzelei@users.noreply.github.com> Date: Wed, 1 Mar 2023 13:18:14 -0800 Subject: [PATCH 2/3] move to allow, add service filtering --- cmd/services_dependencies.go | 115 +----------------------- cmd/services_dependencies_allow.go | 139 +++++++++++++++++++++++++++++ 2 files changed, 142 insertions(+), 112 deletions(-) create mode 100644 cmd/services_dependencies_allow.go diff --git a/cmd/services_dependencies.go b/cmd/services_dependencies.go index 045ed36..195cd40 100644 --- a/cmd/services_dependencies.go +++ b/cmd/services_dependencies.go @@ -16,16 +16,6 @@ limitations under the License. package cmd import ( - "context" - "fmt" - "sort" - "strings" - - "github.com/AlecAivazis/survey/v2" - "github.com/nucleuscloud/cli/internal/config" - clienv "github.com/nucleuscloud/cli/internal/env" - "github.com/nucleuscloud/cli/internal/utils" - svcmgmtv1alpha1 "github.com/nucleuscloud/mgmt-api/gen/proto/go/servicemgmt/v1alpha1" "github.com/spf13/cobra" ) @@ -34,112 +24,13 @@ var servicesDependenciesCmd = &cobra.Command{ Aliases: []string{ "deps", }, - Short: "Add a service dependency.", - Long: "Call this command to add a service dependency to this service in order to authorize inter-service communication", + Short: "Parent command for service dependencies.", + Long: "This command must include a relevant sub command to invoke an action related to service dependencies", RunE: func(cmd *cobra.Command, args []string) error { - ctx := cmd.Context() - environmentName, err := cmd.Flags().GetString("env") - if err != nil { - return err - } - - if environmentName == "" { - return fmt.Errorf("must provide environment name") - } - - // Set this after ensuring flags are correct - cmd.SilenceUsage = true - - servList, err := getServiceNamesByEnvironment(ctx, environmentName) - if err != nil { - return err - } - nucleusConfig, err := config.GetNucleusConfig() - if err != nil { - return err - } - - filteredServices := []string{} - for _, svc := range servList { - if svc != nucleusConfig.Spec.ServiceName { - filteredServices = append(filteredServices, svc) - } - } - - serviceQuestions := []*survey.Question{ - { - Name: "serviceType", - Prompt: &survey.Select{ - Message: "Select the service dependency: ", - Options: filteredServices, - }, - Validate: survey.Required, - }, - } - - // ask the question - var serviceDeps string - err = survey.Ask(serviceQuestions, &serviceDeps, surveyIcons) - if err != nil { - return err - } - - err = storeServiceDependency(serviceDeps) - if err != nil { - return err - } - - return nil + return cmd.Help() }, } func init() { servicesCmd.AddCommand(servicesDependenciesCmd) - - servicesDependenciesCmd.Flags().StringP("env", "e", "", "set the nucleus environment") -} - -func getServiceNamesByEnvironment(ctx context.Context, environmentName string) ([]string, error) { - conn, err := utils.NewApiConnectionByEnv(ctx, clienv.GetEnv()) - if err != nil { - return nil, err - } - defer conn.Close() - - cliClient := svcmgmtv1alpha1.NewServiceMgmtServiceClient(conn) - servicesResp, err := cliClient.GetServices(ctx, &svcmgmtv1alpha1.GetServicesRequest{ - EnvironmentName: strings.TrimSpace(environmentName), - }) - if err != nil { - return nil, err - } - - serviceNames := []string{} - for _, v := range servicesResp.Services { - serviceNames = append(serviceNames, v.ServiceCustomerConfig.ServiceName) - } - - sort.Slice(serviceNames, func(i, j int) bool { - return serviceNames[i] < serviceNames[j] - }) - - return serviceNames, nil -} - -func storeServiceDependency(val string) error { - nucleusConfig, err := config.GetNucleusConfig() - if err != nil { - return err - } - - for _, v := range nucleusConfig.Spec.AllowedServices { - if val == v { - fmt.Println("This service is already in the allowed services list") - return nil - } - } - - nucleusConfig.Spec.AllowedServices = append(nucleusConfig.Spec.AllowedServices, val) - - return config.SetNucleusConfig(nucleusConfig) } diff --git a/cmd/services_dependencies_allow.go b/cmd/services_dependencies_allow.go new file mode 100644 index 0000000..ff3a2fd --- /dev/null +++ b/cmd/services_dependencies_allow.go @@ -0,0 +1,139 @@ +/* +Copyright © 2022 NAME HERE + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package cmd + +import ( + "context" + "fmt" + "sort" + "strings" + + "github.com/AlecAivazis/survey/v2" + "github.com/nucleuscloud/cli/internal/config" + clienv "github.com/nucleuscloud/cli/internal/env" + "github.com/nucleuscloud/cli/internal/utils" + svcmgmtv1alpha1 "github.com/nucleuscloud/mgmt-api/gen/proto/go/servicemgmt/v1alpha1" + "github.com/spf13/cobra" +) + +var servicesDependenciesAllowCmd = &cobra.Command{ + Use: "allow", + Aliases: []string{ + "a", + }, + Short: "Add a service dependency to the service's allow list.", + Long: "Call this command to add a service dependency to this service in order to authorize inter-service communication", + RunE: func(cmd *cobra.Command, args []string) error { + ctx := cmd.Context() + environmentName, err := cmd.Flags().GetString("env") + if err != nil { + return err + } + + if environmentName == "" { + return fmt.Errorf("must provide environment name") + } + + // Set this after ensuring flags are correct + cmd.SilenceUsage = true + + servList, err := getServiceNamesByEnvironment(ctx, environmentName) + if err != nil { + return err + } + nucleusConfig, err := config.GetNucleusConfig() + if err != nil { + return err + } + + serviceExcludeSet := map[string]struct{}{ + nucleusConfig.Spec.ServiceName: {}, + } + for _, svc := range nucleusConfig.Spec.AllowedServices { + serviceExcludeSet[svc] = struct{}{} + } + + filteredServices := []string{} + for _, svc := range servList { + if _, ok := serviceExcludeSet[svc]; !ok { + filteredServices = append(filteredServices, svc) + } + } + + var depName string + err = survey.AskOne(&survey.Select{ + Message: "Select the service dependency", + Options: filteredServices, + }, &depName, surveyIcons) + if err != nil { + return err + } + + return storeServiceDependency(depName) + }, +} + +func init() { + servicesDependenciesCmd.AddCommand(servicesDependenciesAllowCmd) + + servicesDependenciesAllowCmd.Flags().StringP("env", "e", "", "set the nucleus environment") +} + +func getServiceNamesByEnvironment(ctx context.Context, environmentName string) ([]string, error) { + conn, err := utils.NewApiConnectionByEnv(ctx, clienv.GetEnv()) + if err != nil { + return nil, err + } + defer conn.Close() + + cliClient := svcmgmtv1alpha1.NewServiceMgmtServiceClient(conn) + servicesResp, err := cliClient.GetServices(ctx, &svcmgmtv1alpha1.GetServicesRequest{ + EnvironmentName: strings.TrimSpace(environmentName), + }) + if err != nil { + return nil, err + } + + serviceNames := []string{} + for _, service := range servicesResp.Services { + if service.ServiceCustomerConfig.ServiceName != "" { + serviceNames = append(serviceNames, service.ServiceCustomerConfig.ServiceName) + } + } + + sort.Slice(serviceNames, func(i, j int) bool { + return serviceNames[i] < serviceNames[j] + }) + + return serviceNames, nil +} + +func storeServiceDependency(val string) error { + nucleusConfig, err := config.GetNucleusConfig() + if err != nil { + return err + } + + for _, v := range nucleusConfig.Spec.AllowedServices { + if val == v { + fmt.Println("This service is already in the allowed services list") + return nil + } + } + + nucleusConfig.Spec.AllowedServices = append(nucleusConfig.Spec.AllowedServices, val) + return config.SetNucleusConfig(nucleusConfig) +} From 53f1535024d402d8359dc39ee06c809f82fa9f13 Mon Sep 17 00:00:00 2001 From: Nick Z <2420177+nickzelei@users.noreply.github.com> Date: Wed, 1 Mar 2023 13:20:07 -0800 Subject: [PATCH 3/3] update naming --- cmd/services_dependencies_allow.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/services_dependencies_allow.go b/cmd/services_dependencies_allow.go index ff3a2fd..e631b9b 100644 --- a/cmd/services_dependencies_allow.go +++ b/cmd/services_dependencies_allow.go @@ -127,8 +127,8 @@ func storeServiceDependency(val string) error { return err } - for _, v := range nucleusConfig.Spec.AllowedServices { - if val == v { + for _, allowedSvc := range nucleusConfig.Spec.AllowedServices { + if allowedSvc == val { fmt.Println("This service is already in the allowed services list") return nil }