Skip to content
This repository was archived by the owner on Feb 22, 2024. It is now read-only.
Merged
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
115 changes: 3 additions & 112 deletions cmd/services_dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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 := 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
return cmd.Help()
},
}

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 _, v := range serviceList.Services {
simpleServiceList = append(simpleServiceList, v.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

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

err = config.SetNucleusConfig(nucleusConfig)

if err != nil {
return err
}
return nil
}
139 changes: 139 additions & 0 deletions cmd/services_dependencies_allow.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>

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 _, allowedSvc := range nucleusConfig.Spec.AllowedServices {
if allowedSvc == val {
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)
}