billing

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: 20 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.

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 Billing Plan",
	Args:    cobra.ExactArgs(1),
	RunE: func(cmd *cobra.Command, args []string) error {

		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 plan pb.Plan
		err = json.Unmarshal(template, &plan)
		if err != nil {
			fmt.Println("Error while parsing template")
			return err
		}

		ctx, client := MakeBillingServiceClientOrFail()
		res, err := client.CreatePlan(ctx, &plan)
		if err != nil {
			return err
		}

		if printJson, _ := cmd.Flags().GetBool("json"); printJson {
			json.Marshal(map[string]string{"uuid": res.Uuid})
			return nil
		}

		fmt.Println("Result:", res.Uuid)

		return nil
	},
}
View Source
var CreateTransactionCmd = &cobra.Command{
	Use:     "create",
	Aliases: []string{"c", "new", "crt", "add"},
	Short:   "Create the new Transaction",
	RunE: func(cmd *cobra.Command, args []string) error {
		acc, _ := cmd.Flags().GetString("account")
		if acc == "" {
			return fmt.Errorf("Account is required")
		}

		total, _ := cmd.Flags().GetFloat64("total")
		if total == 0 {
			return fmt.Errorf("Total is required and must be not null")
		}

		meta := make(map[string]*structpb.Value)
		raw_meta, _ := cmd.Flags().GetString("meta")
		if raw_meta != "" {
			err := json.Unmarshal([]byte(raw_meta), &meta)
			if err != nil {
				return err
			}
		}

		exec, _ := cmd.Flags().GetInt64("exec")
		if delta, _ := cmd.Flags().GetString("delta"); delta != "" {
			re := regexp.MustCompile(`(?P<sign>^-?)(?P<num>\d+)(?P<mult>[smhdw]{1})`)
			matches := re.FindStringSubmatch(delta)
			if len(matches) == 0 {
				return errors.New("Invalid delta format")
			}
			multiplicators := map[string]int{
				"s": 1,
				"m": 60,
				"h": 3600,
				"d": 86400,
				"w": 604800,
			}
			t, err := strconv.Atoi(matches[2])
			if err != nil {
				return err
			}
			d := multiplicators[matches[3]] * t
			if matches[1] == "-" {
				d *= -1
			}

			exec += int64(d)
		}

		ctx, client := MakeBillingServiceClientOrFail()
		r, err := client.CreateTransaction(ctx, &pb.Transaction{
			Account: acc,
			Total:   total,
			Meta:    meta,
			Exec:    exec,
		})
		if err != nil {
			return err
		}

		if printJson, _ := cmd.Flags().GetBool("json"); printJson {
			data, err := json.Marshal(r)
			if err != nil {
				return err
			}
			fmt.Println(string(data))
		} else {
			meta, _ := cmd.Flags().GetBool("meta")
			PrintTransactions([]*pb.Transaction{r}, meta)
		}

		return nil
	},
}
View Source
var DeleteCmd = &cobra.Command{
	Use:     "delete [UUID]",
	Aliases: []string{"del", "rm", "r"},
	Short:   "Delete NoCloud Billing Plan",
	Args:    cobra.MinimumNArgs(1),
	RunE: func(cmd *cobra.Command, args []string) error {
		ctx, client := MakeBillingServiceClientOrFail()
		res, err := client.DeletePlan(ctx, &pb.Plan{
			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.Println("Success delete ", res.Uuid)
		}

		return nil
	},
}

DeleteCmd represents the delete command

View Source
var GetCmd = &cobra.Command{
	Use:   "get [UUID]",
	Short: "Get NoCloud BillingPlan",
	Args:  cobra.MinimumNArgs(1),
	RunE: func(cmd *cobra.Command, args []string) error {
		ctx, client := MakeBillingServiceClientOrFail()
		res, err := client.GetPlan(ctx, &pb.Plan{
			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 {
			PrintPlan(res)
		}

		return nil
	},
}

GetCmd represents the get command

View Source
var ListCmd = &cobra.Command{
	Use:     "list",
	Aliases: []string{"l", "ls"},
	Short:   "List NoCloud Billing Plans",
	RunE: func(cmd *cobra.Command, args []string) error {
		ctx, client := MakeBillingServiceClientOrFail()
		request := pb.ListRequest{}

		res, err := client.ListPlans(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 {
			fmt.Println("--------------------------------")
			for _, plan := range res.GetPool() {
				PrintPlan(plan)
				fmt.Println("--------------------------------")
			}
		}

		return nil
	},
}

ListCmd represents the list command

View Source
var ListTransactionsCmd = &cobra.Command{
	Use:     "list",
	Aliases: []string{"l", "ls"},
	Short:   "List NoCloud Billing Plans",
	RunE: func(cmd *cobra.Command, args []string) error {
		ctx, client := MakeBillingServiceClientOrFail()
		request := pb.GetTransactionsRequest{}

		account, _ := cmd.Flags().GetString("account")
		if account != "" {
			request.Account = &account
		}

		service, _ := cmd.Flags().GetString("service")
		if service != "" {
			request.Service = &service
		}

		res, err := client.GetTransactions(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 {
			meta, _ := cmd.Flags().GetBool("meta")
			PrintTransactions(res.GetPool(), meta)
		}

		return nil
	},
}

ListCmd represents the list command

View Source
var ReprocessTransactionsCmd = &cobra.Command{
	Use:     "reprocess",
	Aliases: []string{"r", "re", "rep", "repr", "repro"},
	Short:   "Reprocess transactions for Account",
	Args:    cobra.ExactArgs(1),
	RunE: func(cmd *cobra.Command, args []string) error {
		ctx, client := MakeBillingServiceClientOrFail()
		r, err := client.Reprocess(ctx, &pb.ReprocessTransactionsRequest{
			Account: args[0],
		})
		if err != nil {
			return err
		}

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

		return nil
	},
}
View Source
var TransactionsCmd = &cobra.Command{
	Use:     "transactions",
	Aliases: []string{"t", "transaction", "tr", "trx"},
	Short:   "Manage transactions",
}

Functions

func MakeBillingServiceClientOrFail

func MakeBillingServiceClientOrFail() (context.Context, pb.BillingServiceClient)

func MakeTrRow

func MakeTrRow(t *pb.Transaction) table.Row

func MakeTrRowWithMeta

func MakeTrRowWithMeta(t *pb.Transaction) table.Row

func PrintPlan

func PrintPlan(p *pb.Plan)

func PrintTransactions

func PrintTransactions(pool []*pb.Transaction, meta bool)

Types

This section is empty.

Jump to

Keyboard shortcuts

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