export

package
v0.1.11 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 8, 2023 License: Apache-2.0 Imports: 15 Imported by: 1

Documentation

Index

Constants

View Source
const (
	NamespacesKey           = "namespaces"
	NamespacesShorthand     = "n"
	OutputLocationKey       = "output"
	OutputLocationShorthand = "o"
	OutputTypeKey           = "output-type"
	OutputTypeDefault       = OutputTypeSingleFile
	OutputTypeSingleFile    = "single-file"
	OutputTypeDirectory     = "dir"
	OutputFormatKey         = "format"
	OutputFormatDefault     = OutputFormatYAML
	OutputFormatYAML        = "yaml"
	OutputFormatJSON        = "json"
)

Variables

View Source
var ExportCmd = &cobra.Command{
	Use:   "export",
	Short: "Export Otterize intents from network mapper",
	Args:  cobra.NoArgs,
	RunE: func(cmd *cobra.Command, args []string) error {
		return mapperclient.WithClient(func(c *mapperclient.Client) error {
			err := validateOutputFlags()
			if err != nil {
				return err
			}

			ctxTimeout, cancel := context.WithTimeout(context.Background(), 10*time.Second)
			defer cancel()
			namespacesFilter := viper.GetStringSlice(NamespacesKey)
			intentsFromMapper, err := c.ServiceIntents(ctxTimeout, namespacesFilter)
			if err != nil {
				return err
			}

			outputList := make([]v1alpha2.ClientIntents, 0)

			for _, serviceIntents := range intentsFromMapper {
				intentList := make([]v1alpha2.Intent, 0)

				for _, serviceIntent := range serviceIntents.Intents {
					intent := v1alpha2.Intent{
						Name: serviceIntent.Name,
					}

					if len(serviceIntent.Namespace) != 0 && serviceIntent.Namespace != serviceIntents.Client.Namespace {
						intent.Name = fmt.Sprintf("%s.%s", serviceIntent.Name, serviceIntent.Namespace)
					}
					intentList = append(intentList, intent)
				}

				intentsOutput := v1alpha2.ClientIntents{
					TypeMeta: v1.TypeMeta{
						Kind:       consts.IntentsKind,
						APIVersion: consts.IntentsAPIVersion,
					},
					ObjectMeta: v1.ObjectMeta{
						Name:      serviceIntents.Client.Name,
						Namespace: serviceIntents.Client.Namespace,
					},
					Spec: &v1alpha2.IntentsSpec{Service: v1alpha2.Service{Name: serviceIntents.Client.Name}},
				}

				if len(intentList) != 0 {
					intentsOutput.Spec.Calls = intentList
				}

				outputList = append(outputList, intentsOutput)
			}

			if viper.GetString(OutputLocationKey) != "" {
				switch outputTypeVal := viper.GetString(OutputTypeKey); {
				case outputTypeVal == OutputTypeSingleFile:
					err := writeIntentsFile(viper.GetString(OutputLocationKey), outputList)
					if err != nil {
						return err
					}
					output.PrintStderr("Successfully wrote intents into %s", viper.GetString(OutputLocationKey))
				case outputTypeVal == OutputTypeDirectory:
					err := os.MkdirAll(viper.GetString(OutputLocationKey), 0700)
					if err != nil {
						return fmt.Errorf("could not create dir %s: %w", viper.GetString(OutputLocationKey), err)
					}

					for _, intent := range outputList {
						filePath := fmt.Sprintf("%s.%s.yaml", intent.Name, intent.Namespace)
						if err != nil {
							return err
						}
						filePath = filepath.Join(viper.GetString(OutputLocationKey), filePath)
						err := writeIntentsFile(filePath, []v1alpha2.ClientIntents{intent})
						if err != nil {
							return err
						}
					}
					output.PrintStderr("Successfully wrote intents into %s", viper.GetString(OutputLocationKey))
				default:
					return fmt.Errorf("unexpected output type %s, use one of (%s, %s)", outputTypeVal, OutputTypeSingleFile, OutputTypeDirectory)
				}

			} else {
				formatted, err := getFormattedIntents(outputList)
				if err != nil {
					return err
				}
				output.PrintStdout(formatted)
			}
			return nil
		})
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL