Skip to content
This repository was archived by the owner on Feb 22, 2024. It is now read-only.
Merged
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
145 changes: 145 additions & 0 deletions cmd/services_dependencies.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*
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 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 _, 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
}