From 778eb85803f7e56e9208c4cc6e6ab6fdee0f99e8 Mon Sep 17 00:00:00 2001 From: Evis Drenova Date: Mon, 20 Feb 2023 13:41:35 -0800 Subject: [PATCH 1/2] service deps --- cmd/services_dependencies.go | 139 +++++++++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 cmd/services_dependencies.go diff --git a/cmd/services_dependencies.go b/cmd/services_dependencies.go new file mode 100644 index 0000000..0e7ec0e --- /dev/null +++ b/cmd/services_dependencies.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 servicesDependenciesCmd = &cobra.Command{ + Use: "dependency", + 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", + 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 := listRawServices(ctx, environmentName) + if err != nil { + return err + } + + serviceQuestions := []*survey.Question{ + { + Name: "serviceType", + Prompt: &survey.Select{ + Message: "Select the service dependency: ", + Options: servList, + }, + 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 + }, +} + +func init() { + servicesCmd.AddCommand(servicesDependenciesCmd) + + servicesDependenciesCmd.Flags().StringP("env", "e", "", "set the nucleus environment") +} + +func listRawServices(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) + serviceList, err := cliClient.GetServices(ctx, &svcmgmtv1alpha1.GetServicesRequest{ + EnvironmentName: strings.TrimSpace(environmentName), + }) + if err != nil { + return nil, err + } + + var simpleServiceList []string //just the service names + + for _, k := range serviceList.Services { + simpleServiceList = append(simpleServiceList, k.ServiceCustomerConfig.ServiceName) + } + + sort.Slice(simpleServiceList, func(i, j int) bool { + return simpleServiceList[i] < simpleServiceList[j] + }) + + return simpleServiceList, nil +} + +func storeServiceDependency(val string) error { + nucleusConfig, err := config.GetNucleusConfig() + if err != nil { + return err + } + + var allowedServices []string + + allowedServices = nucleusConfig.Spec.AllowedServices + + allowedServices = append(allowedServices, val) + + nucleusConfig.Spec.AllowedServices = allowedServices + + err = config.SetNucleusConfig(nucleusConfig) + + if err != nil { + return err + } + return nil +} From fa05449da869fceb8559e40666d55c16c8ced09d Mon Sep 17 00:00:00 2001 From: Evis Drenova Date: Mon, 20 Feb 2023 13:52:06 -0800 Subject: [PATCH 2/2] handling duplicates --- cmd/services_dependencies.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/cmd/services_dependencies.go b/cmd/services_dependencies.go index 0e7ec0e..7b80661 100644 --- a/cmd/services_dependencies.go +++ b/cmd/services_dependencies.go @@ -105,8 +105,8 @@ func listRawServices(ctx context.Context, environmentName string) ([]string, err var simpleServiceList []string //just the service names - for _, k := range serviceList.Services { - simpleServiceList = append(simpleServiceList, k.ServiceCustomerConfig.ServiceName) + for _, v := range serviceList.Services { + simpleServiceList = append(simpleServiceList, v.ServiceCustomerConfig.ServiceName) } sort.Slice(simpleServiceList, func(i, j int) bool { @@ -126,6 +126,12 @@ func storeServiceDependency(val string) error { allowedServices = nucleusConfig.Spec.AllowedServices + for _, v := range allowedServices { + if val == v { + return fmt.Errorf("This service is already in the Allowed Services list") + } + } + allowedServices = append(allowedServices, val) nucleusConfig.Spec.AllowedServices = allowedServices