services

package
v0.2.6 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2022 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

Copyright © 2021-2022 Nikita Ivanovski info@slnt-opp.xyz

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.

Copyright © 2021-2022 Nikita Ivanovski info@slnt-opp.xyz

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.

Copyright © 2021-2022 Nikita Ivanovski info@slnt-opp.xyz

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.

Copyright © 2021-2022 Nikita Ivanovski info@slnt-opp.xyz

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.

Copyright © 2021-2022 Nikita Ivanovski info@slnt-opp.xyz

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.

Copyright © 2021-2022 Nikita Ivanovski info@slnt-opp.xyz

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.

Copyright © 2021-2022 Nikita Ivanovski info@slnt-opp.xyz

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.

Copyright © 2021-2022 Nikita Ivanovski info@slnt-opp.xyz

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.

Copyright © 2021-2022 Nikita Ivanovski info@slnt-opp.xyz

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.

Index

Constants

This section is empty.

Variables

View Source
var CreateCmd = &cobra.Command{
	Use:     "create [path to template] [flags]",
	Aliases: []string{"crt", "c"},
	Short:   "Create Service Config",
	Args:    cobra.ExactArgs(1),
	RunE: func(cmd *cobra.Command, args []string) (err error) {

		namespace, err := cmd.Flags().GetString("namespace")
		if err != nil {
			return err
		}
		if namespace == "" {
			return errors.New("Namespace UUID isn't given")
		}

		if _, err := os.Stat(args[0]); os.IsNotExist(err) {
			return errors.New("Template doesn't exist at path " + args[0])
		}

		var format string
		{
			pathSlice := strings.Split(args[0], ".")
			format = pathSlice[len(pathSlice)-1]
		}

		template, err := os.ReadFile(args[0])

		switch format {
		case "json":
		case "yml", "yaml":
			template, err = yaml.YAMLToJSON(template)
		default:
			return errors.New("Unsupported template format " + format)
		}

		if err != nil {
			fmt.Println("Error while parsing template")
			return err
		}
		var service pb.Service
		err = json.Unmarshal(template, &service)
		if err != nil {
			fmt.Println("Error while parsing template")
			return err
		}

		ctx, client := MakeServicesServiceClientOrFail()
		request := pb.CreateRequest{Service: &service, Namespace: namespace}
		res, err := client.Create(ctx, &request)
		if err != nil {
			return err
		}

		output, err := json.MarshalIndent(res, "-", " ")
		if err != nil {
			fmt.Println(res)
			return err
		}
		fmt.Println("Result: ", string(output))
		return nil
	},
}

createCmd represents the create command

View Source
var DeleteCmd = &cobra.Command{
	Use:     "delete [UUID]",
	Aliases: []string{"del", "rm", "r"},
	Short:   "Delete NoCloud Service",
	Args:    cobra.MinimumNArgs(1),
	RunE: func(cmd *cobra.Command, args []string) error {
		ctx, client := MakeServicesServiceClientOrFail()
		res, err := client.Delete(ctx, &pb.DeleteRequest{
			Uuid: args[0],
		})
		if err != nil {
			return err
		}

		if printJson, _ := cmd.Flags().GetBool("json"); printJson {
			data, err := json.Marshal(res)
			if err != nil {
				return err
			}
			fmt.Println(string(data))
		} else {
			fmt.Printf("Result: %t\n", res.GetResult())
			if !res.GetResult() {
				fmt.Printf("Error: %s\n", res.GetError())
			}
		}

		return nil
	},
}

DeleteCmd represents the delete command

View Source
var DownCmd = &cobra.Command{
	Use:     "down [uuid] [[flags]]",
	Aliases: []string{"d"},
	Short:   "NoCloud Service Down",
	Args:    cobra.ExactArgs(1),
	RunE: func(cmd *cobra.Command, args []string) error {
		ctx, client := MakeServicesServiceClientOrFail()
		request := pb.DownRequest{Uuid: args[0]}
		_, err := client.Down(ctx, &request)
		return err
	},
}

GetCmd represents the list command

View Source
var GetCmd = &cobra.Command{
	Use:   "get [uuid] [[flags]]",
	Short: "Get NoCloud Service",
	Args:  cobra.ExactArgs(1),
	RunE: func(cmd *cobra.Command, args []string) error {
		ctx, client := MakeServicesServiceClientOrFail()
		request := pb.GetRequest{Uuid: args[0]}
		res, err := client.Get(ctx, &request)

		if err != nil {
			return err
		}

		if printJson, _ := cmd.Flags().GetBool("json"); !printJson {
			return PrintService(res)
		}

		data, err := json.Marshal(res)
		if err != nil {
			return err
		}
		fmt.Println(string(data))
		return nil
	},
}

GetCmd represents the list command

View Source
var InvokeCmd = &cobra.Command{
	Use:     "invoke [uuid] [uuid] [uuid] [action] [[flags]]",
	Aliases: []string{"call", "perform"},
	Short:   "Invokes NoCloud Service Group Instance Action",
	Long:    `Invokes Instance method, requires fuul UUIDs path, so args are: <service uuid> <group uuid> <instance uuid> <action key> --meta <json data>`,
	Args:    cobra.ExactArgs(4),
	RunE: func(cmd *cobra.Command, args []string) error {
		ctx, client := MakeServicesServiceClientOrFail()
		request := pb.PerformActionRequest{
			Service:  args[0],
			Group:    args[1],
			Instance: args[2],
			Action:   args[3],
		}
		data, err := cmd.Flags().GetString("data")
		if err != nil {
			return err
		}
		var dataMap map[string]interface{}
		err = json.Unmarshal([]byte(data), &dataMap)
		if err != nil {
			return err
		}
		dataStruct, err := structpb.NewStruct(dataMap)
		if err != nil {
			return err
		}
		request.Data = dataStruct.GetFields()

		res, err := client.PerformServiceAction(ctx, &request)

		if err != nil {
			return err
		}

		if printJson, _ := cmd.Flags().GetBool("json"); !printJson {
			return PrintServiceActionResponse(res)
		}

		meta, err := json.Marshal(res)
		if err != nil {
			return err
		}
		fmt.Println(string(meta))
		return nil
	},
}

GetCmd represents the list command

View Source
var ListCmd = &cobra.Command{
	Use:     "list [[NAMESPACE]] [[flags]]",
	Aliases: []string{"l"},
	Short:   "List NoCloud Services",
	Long:    `Add namespace UUID after list command, to filter services by namespace`,
	RunE: func(cmd *cobra.Command, args []string) error {
		ctx, client := MakeServicesServiceClientOrFail()
		request := pb.ListRequest{}
		if len(args) > 0 {
			request.Namespace = &args[0]
		}

		a, _ := cmd.Flags().GetBool("show-deleted")
		if a {
			showDeletedValue := "true"
			request.ShowDeleted = &showDeletedValue
		}

		d, _ := cmd.Flags().GetInt32("depth")
		request.Depth = &d

		res, err := client.List(ctx, &request)

		if err != nil {
			return err
		}

		if printJson, _ := cmd.Flags().GetBool("json"); printJson {
			data, err := json.Marshal(res)
			if err != nil {
				return err
			}
			fmt.Println(string(data))
		} else {
			PrintServicesPool(res.GetPool())
		}

		return nil
	},
}

ListCmd represents the list command

View Source
var TestCmd = &cobra.Command{
	Use:   "test [path to template] [flags]",
	Short: "Test Service Config",
	Args:  cobra.ExactArgs(1),
	RunE: func(cmd *cobra.Command, args []string) (err error) {

		namespace, err := cmd.Flags().GetString("namespace")
		if err != nil {
			return err
		}
		if namespace == "" {
			return errors.New("Namespace UUID isn't given")
		}

		if _, err := os.Stat(args[0]); os.IsNotExist(err) {
			return errors.New("Template doesn't exist at path " + args[0])
		}

		var format string
		{
			pathSlice := strings.Split(args[0], ".")
			format = pathSlice[len(pathSlice)-1]
		}

		template, err := os.ReadFile(args[0])

		switch format {
		case "json":
		case "yml", "yaml":
			template, err = yaml.YAMLToJSON(template)
		default:
			return errors.New("Unsupported template format " + format)
		}

		if err != nil {
			fmt.Println("Error while parsing template")
			return err
		}
		var service pb.Service
		err = json.Unmarshal(template, &service)
		if err != nil {
			fmt.Println("Error while parsing template")
			return err
		}

		ctx, client := MakeServicesServiceClientOrFail()
		request := pb.CreateRequest{Service: &service, Namespace: namespace}
		res, err := client.TestConfig(ctx, &request)
		if err != nil {
			return err
		}

		fmt.Print("Result: ")
		if res.GetResult() {
			fmt.Println("Tests Passed")
			return nil
		}

		fmt.Println("Something has failed. Errors:")
		PrintTestErrors(res.GetErrors())
		return nil
	},
}

createCmd represents the create command

View Source
var UpCmd = &cobra.Command{
	Use:     "up [service_id] [flags]",
	Aliases: []string{"u"},
	Short:   "NoCloud Service Up",
	Args:    cobra.ExactArgs(1),
	RunE: func(cmd *cobra.Command, args []string) (err error) {
		ctx, client := MakeServicesServiceClientOrFail()

		req := pb.UpRequest{Uuid: args[0]}
		if rulesJson, _ := cmd.Flags().GetString("rules"); rulesJson != "" {
			fmt.Println("Rules as string given", rulesJson)
			json.Unmarshal([]byte(rulesJson), &req.DeployPolicies)
		} else if rulesFile, _ := cmd.Flags().GetString("rules-file"); rulesFile != "" {
			fmt.Println("Rules as File given", rulesFile)
			rulesJson, err := os.ReadFile(rulesFile)
			if err != nil {
				return err
			}
			json.Unmarshal(rulesJson, &req.DeployPolicies)
		} else {
			fmt.Println("Nothing given, selecting in interactive mode")
			r, err := SelectDeployPoliciesInteractive(ctx, cmd, client, args[0])
			if err != nil {
				return err
			}
			req.DeployPolicies = r
		}

		_, err = client.Up(ctx, &req)
		return err
	},
}

createCmd represents the create command

Functions

func MakeServicesServiceClientOrFail

func MakeServicesServiceClientOrFail() (context.Context, pb.ServicesServiceClient)

func PrintService

func PrintService(s *pb.Service) error

func PrintServiceActionResponse added in v0.2.0

func PrintServiceActionResponse(res *pb.PerformActionResponse) error

func PrintServicesPool

func PrintServicesPool(pool []*pb.Service)

func PrintTestErrors

func PrintTestErrors(pool []*pb.TestConfigError)

func SelectDeployPoliciesInteractive

func SelectDeployPoliciesInteractive(ctx context.Context, cmd *cobra.Command, client pb.ServicesServiceClient, id string) (res map[string]string, err error)

Types

This section is empty.

Jump to

Keyboard shortcuts

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