targetgroup

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2024 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Command = cli.Command{
	Name:        "targetgroup",
	Aliases:     []string{"tg"},
	Description: "Manage Target Groups",
	Usage:       "Manage Target Groups",
	Subcommands: []*cli.Command{
		&CreateCommand,
		&LinkCommand,
		&UnlinkCommand,
		&ListCommand,
		&DeleteCommand,
		&RoutesCommand,
	},
}
View Source
var CreateCommand = cli.Command{
	Name:        "create",
	Description: "Create a target group",
	Usage:       "Create a target group",
	Flags: []cli.Flag{
		&cli.StringFlag{Name: "id"},
		&cli.StringFlag{Name: "kind", Usage: "the target kind that the provider grants access to"},
		&cli.StringFlag{Name: "provider", Usage: "publisher/name@version"},
		&cli.BoolFlag{Name: "ok-if-exists", Value: false},
	},
	Action: func(c *cli.Context) error {
		ctx := c.Context

		id := c.String("id")
		if id == "" {
			err := survey.AskOne(&survey.Input{Message: "Enter an ID for the Target Group"}, &id)
			if err != nil {
				return err
			}
		}

		registry, err := registryclient.New(ctx)
		if err != nil {
			return errors.Wrap(err, "configuring provider registry client")
		}

		var provider *providerregistrysdk.ProviderDetail

		providerInput := c.String("provider")
		if providerInput != "" {
			p, err := providerregistrysdk.ParseProvider(providerInput)
			if err != nil {
				return err
			}

			res, err := registry.GetProviderWithResponse(ctx, p.Publisher, p.Name, p.Version)
			if err != nil {
				return err
			}

			provider = res.JSON200
		} else {
			provider, err = prompt.Provider(ctx, registry)
			if err != nil {
				return err
			}
		}

		kind := c.String("kind")
		if kind == "" {
			kind, err = prompt.Kind(*provider)
			if err != nil {
				return err
			}
		}

		clio.Infof("Using schema from %s, kind %s", provider, kind)

		cfg, err := config.Load()
		if err != nil {
			return err
		}

		cf, err := client.FromConfig(ctx, cfg)
		if err != nil {
			return err
		}

		res, err := cf.AdminCreateTargetGroupWithResponse(ctx, types.AdminCreateTargetGroupJSONRequestBody{
			Id: id,
			From: types.TargetGroupFrom{
				Kind:      kind,
				Name:      provider.Name,
				Publisher: provider.Publisher,
				Version:   provider.Version,
			},
		})
		if err != nil {
			return err
		}
		switch res.StatusCode() {
		case http.StatusCreated:
			clio.Successf("Successfully created the targetgroup: %s", id)
		case http.StatusConflict:

			if c.Bool("ok-if-exists") {
				clio.Infof("Targetgroup with that ID already exists: '%s'", id)

				return nil
			}

			return clierr.New(fmt.Sprintf("Duplicate targetgroup ID provided. Targetgroup with that ID '%s' already exist", id))
		case http.StatusUnauthorized:
			return errors.New(res.JSON401.Error)
		case http.StatusInternalServerError:
			return errors.New(res.JSON500.Error)
		default:
			return clierr.New("Unhandled response from the Common Fate API", clierr.Infof("Status Code: %d", res.StatusCode()), clierr.Error(string(res.Body)))
		}
		return nil

	},
}
View Source
var DeleteCommand = cli.Command{
	Name:        "delete",
	Description: "Delete a target group",
	Usage:       "Delete a target group",
	Flags: []cli.Flag{
		&cli.StringFlag{Name: "id"},
	},
	Action: func(c *cli.Context) error {
		ctx := c.Context
		cfg, err := config.Load()
		if err != nil {
			return err
		}

		cf, err := client.FromConfig(ctx, cfg)
		if err != nil {
			return err
		}
		id := c.String("id")
		if id == "" {
			tg, err := prompt.TargetGroup(ctx, cf)
			if err != nil {
				return err
			}
			id = tg.Id
		}
		_, err = cf.AdminDeleteTargetGroupWithResponse(ctx, id)
		if err != nil {
			return err
		}

		clio.Successf("Deleted target group %s", id)

		return nil
	},
}
View Source
var LinkCommand = cli.Command{
	Name:        "link",
	Description: "Link a handler to a target group",
	Usage:       "Link a handler to a target group",
	Flags: []cli.Flag{
		&cli.StringFlag{Name: "target-group-id"},
		&cli.StringFlag{Name: "handler-id"},
		&cli.StringFlag{Name: "kind", Required: true},
		&cli.IntFlag{Name: "priority", Value: 100},
	},
	Action: func(c *cli.Context) error {

		ctx := c.Context
		cfg, err := config.Load()
		if err != nil {
			return err
		}

		cf, err := client.FromConfig(ctx, cfg)
		if err != nil {
			return err
		}
		tgID := c.String("target-group-id")
		if tgID == "" {
			tg, err := prompt.TargetGroup(ctx, cf)
			if err != nil {
				return err
			}
			tgID = tg.Id
		}
		hID := c.String("handler-id")
		if hID == "" {
			h, err := prompt.Handler(ctx, cf)
			if err != nil {
				return err
			}
			hID = h.Id
		}

		var kind = c.String("kind")
		if kind == "" {
			err := survey.AskOne(&survey.Input{Message: "Enter the kind for the handler"}, &kind)
			if err != nil {
				return err
			}
		}

		_, err = cf.AdminCreateTargetGroupLinkWithResponse(ctx, tgID, types.AdminCreateTargetGroupLinkJSONRequestBody{
			DeploymentId: hID,
			Priority:     c.Int("priority"),
			Kind:         kind,
		})
		if err != nil {
			return err
		}

		clio.Successf("Successfully linked the handler '%s' with target group '%s' using kind: '%s'", hID, tgID, c.String("kind"))

		return nil
	},
}
View Source
var ListCommand = cli.Command{
	Name:        "list",
	Aliases:     []string{"ls"},
	Description: "List target groups",
	Usage:       "List target groups",
	Action: cli.ActionFunc(func(c *cli.Context) error {
		ctx := c.Context
		cfg, err := config.Load()
		if err != nil {
			return err
		}

		cf, err := client.FromConfig(ctx, cfg)
		if err != nil {
			return err
		}

		res, err := cf.AdminListTargetGroupsWithResponse(ctx)
		if err != nil {
			return err
		}
		tbl := table.New(os.Stderr)
		tbl.Columns("ID", "Target Schema")
		for _, tg := range res.JSON200.TargetGroups {
			from := fmt.Sprintf("%s/%s@%s/%s", tg.From.Publisher, tg.From.Name, tg.From.Version, tg.From.Kind)
			tbl.Row(tg.Id, from)
		}
		return tbl.Flush()

	}),
}
View Source
var ListRoutesCommand = cli.Command{
	Name:        "list",
	Aliases:     []string{"ls"},
	Description: "List target group routes",
	Usage:       "List target group routes",
	Flags: []cli.Flag{
		&cli.StringFlag{Name: "target-group-id"},
	},
	Action: cli.ActionFunc(func(c *cli.Context) error {
		ctx := c.Context

		cfg, err := config.Load()
		if err != nil {
			return err
		}

		cf, err := client.FromConfig(ctx, cfg)
		if err != nil {
			return err
		}

		tgID := c.String("target-group-id")
		if tgID == "" {
			tg, err := prompt.TargetGroup(ctx, cf)
			if err != nil {
				return err
			}
			tgID = tg.Id
		}

		res, err := cf.AdminListTargetRoutesWithResponse(ctx, tgID)
		if err != nil {
			return err
		}
		tbl := table.New(os.Stderr)
		tbl.Columns("Target Group", "Handler", "Kind", "Priority", "Valid", "Diagnostics")
		for _, route := range res.JSON200.Routes {
			tbl.Row(route.TargetGroupId, route.HandlerId, route.Kind, strconv.Itoa(route.Priority), strconv.FormatBool(route.Valid), fmt.Sprintf("%v", route.Diagnostics))
		}
		return tbl.Flush()
	}),
}
View Source
var RoutesCommand = cli.Command{
	Name:        "routes",
	Description: "Manage Target Groups Routes",
	Usage:       "Manage Target Groups Routes",
	Subcommands: []*cli.Command{
		&ListRoutesCommand,
	},
}
View Source
var UnlinkCommand = cli.Command{
	Name:        "unlink",
	Description: "Unlink a handler from a target group",
	Usage:       "Unlink a handler from a target group",
	Flags: []cli.Flag{
		&cli.StringFlag{Name: "handler-id"},
		&cli.StringFlag{Name: "target-group-id"},
		&cli.StringFlag{Name: "kind", Required: true},
	},
	Action: func(c *cli.Context) error {
		ctx := c.Context
		cfg, err := config.Load()
		if err != nil {
			return err
		}

		cf, err := client.FromConfig(ctx, cfg)
		if err != nil {
			return err
		}

		tgID := c.String("target-group-id")
		if tgID == "" {
			tg, err := prompt.TargetGroup(ctx, cf)
			if err != nil {
				return err
			}
			tgID = tg.Id
		}
		hID := c.String("handler-id")
		if hID == "" {
			h, err := prompt.Handler(ctx, cf)
			if err != nil {
				return err
			}
			hID = h.Id
		}

		_, err = cf.AdminRemoveTargetGroupLinkWithResponse(ctx, tgID, &types.AdminRemoveTargetGroupLinkParams{
			DeploymentId: hID,
			Kind:         c.String("kind"),
		})
		if err != nil {
			return err
		}

		clio.Successf("Unlinked handler %s from target group %s", hID, tgID)

		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