certificate

package
v1.51.0-rc.2 Latest Latest
Warning

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

Go to latest
Published: May 22, 2025 License: MIT Imports: 17 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CreateCmd = base.CreateCmd{
	BaseCobraCommand: func(hcapi2.Client) *cobra.Command {
		cmd := &cobra.Command{
			Use:   "create [options] --name <name> (--type managed --domain <domain> | --type uploaded --cert-file <file> --key-file <file>)",
			Short: "Create or upload a Certificate",
		}

		cmd.Flags().String("name", "", "Certificate name (required)")
		_ = cmd.MarkFlagRequired("name")

		cmd.Flags().StringToString("label", nil, "User-defined labels ('key=value') (can be specified multiple times)")

		cmd.Flags().StringP("type", "t", string(hcloud.CertificateTypeUploaded),
			fmt.Sprintf("Type of Certificate to create. Valid choices: %v, %v",
				hcloud.CertificateTypeUploaded, hcloud.CertificateTypeManaged))
		_ = cmd.RegisterFlagCompletionFunc(
			"type",
			cmpl.SuggestCandidates(string(hcloud.CertificateTypeUploaded), string(hcloud.CertificateTypeManaged)),
		)

		cmd.Flags().String("cert-file", "", "File containing the PEM encoded certificate (required if type is uploaded)")
		cmd.Flags().String("key-file", "",
			"File containing the PEM encoded private key for the certificate (required if type is uploaded)")
		cmd.Flags().StringSlice("domain", nil, "One or more domains the Certificate is valid for.")

		return cmd
	},
	Run: func(s state.State, cmd *cobra.Command, _ []string) (any, any, error) {
		certType, err := cmd.Flags().GetString("type")
		if err != nil {
			return nil, nil, err
		}
		var cert *hcloud.Certificate
		switch hcloud.CertificateType(certType) {
		case hcloud.CertificateTypeManaged:
			cert, err = createManaged(s, cmd)
		default:
			cert, err = createUploaded(s, cmd)
		}
		if err != nil {
			return nil, nil, err
		}
		return cert, util.Wrap("certificate", hcloud.SchemaFromCertificate(cert)), nil
	},
}
View Source
var DeleteCmd = base.DeleteCmd{
	ResourceNameSingular: "Certificate",
	ResourceNamePlural:   "Certificates",
	ShortDescription:     "Delete a Certificate",
	NameSuggestions:      func(c hcapi2.Client) func() []string { return c.Firewall().Names },
	Fetch: func(s state.State, _ *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) {
		return s.Client().Certificate().Get(s, idOrName)
	},
	Delete: func(s state.State, _ *cobra.Command, resource interface{}) (*hcloud.Action, error) {
		certificate := resource.(*hcloud.Certificate)
		_, err := s.Client().Certificate().Delete(s, certificate)
		return nil, err
	},
}
View Source
var DescribeCmd = base.DescribeCmd[*hcloud.Certificate]{
	ResourceNameSingular: "Certificate",
	ShortDescription:     "Describe a Certificate",
	NameSuggestions:      func(c hcapi2.Client) func() []string { return c.Certificate().Names },
	Fetch: func(s state.State, _ *cobra.Command, idOrName string) (*hcloud.Certificate, any, error) {
		cert, _, err := s.Client().Certificate().Get(s, idOrName)
		if err != nil {
			return nil, nil, err
		}
		return cert, hcloud.SchemaFromCertificate(cert), nil
	},
	PrintText: func(s state.State, cmd *cobra.Command, cert *hcloud.Certificate) error {
		cmd.Printf("ID:\t\t\t%d\n", cert.ID)
		cmd.Printf("Name:\t\t\t%s\n", cert.Name)
		cmd.Printf("Type:\t\t\t%s\n", cert.Type)
		cmd.Printf("Fingerprint:\t\t%s\n", cert.Fingerprint)
		cmd.Printf("Created:\t\t%s (%s)\n", util.Datetime(cert.Created), humanize.Time(cert.Created))
		cmd.Printf("Not valid before:\t%s (%s)\n", util.Datetime(cert.NotValidBefore), humanize.Time(cert.NotValidBefore))
		cmd.Printf("Not valid after:\t%s (%s)\n", util.Datetime(cert.NotValidAfter), humanize.Time(cert.NotValidAfter))
		if cert.Status != nil {
			cmd.Printf("Status:\n")
			cmd.Printf("  Issuance:\t%s\n", cert.Status.Issuance)
			cmd.Printf("  Renewal:\t%s\n", cert.Status.Renewal)
			if cert.Status.IsFailed() {
				cmd.Printf("  Failure reason: %s\n", cert.Status.Error.Message)
			}
		}
		cmd.Printf("Domain names:\n")
		for _, domainName := range cert.DomainNames {
			cmd.Printf("  - %s\n", domainName)
		}
		cmd.Print("Labels:\n")
		if len(cert.Labels) == 0 {
			cmd.Print("  No labels\n")
		} else {
			for key, value := range util.IterateInOrder(cert.Labels) {
				cmd.Printf("  %s:\t%s\n", key, value)
			}
		}
		cmd.Println("Used By:")
		if len(cert.UsedBy) == 0 {
			cmd.Println("  Certificate unused")
		} else {
			for _, ub := range cert.UsedBy {
				cmd.Printf("  - Type: %s\n", ub.Type)

				if ub.Type != hcloud.CertificateUsedByRefTypeLoadBalancer {
					cmd.Printf("  - ID: %d\n", ub.ID)
					continue
				}
				cmd.Printf("  - Name: %s\n", s.Client().LoadBalancer().LoadBalancerName(ub.ID))
			}
		}
		return nil
	},
}
View Source
var LabelCmds = base.LabelCmds[*hcloud.Certificate]{
	ResourceNameSingular:   "Certificate",
	ShortDescriptionAdd:    "Add a label to a Certificate",
	ShortDescriptionRemove: "Remove a label from a Certificate",
	NameSuggestions:        func(c hcapi2.Client) func() []string { return c.Certificate().Names },
	LabelKeySuggestions:    func(c hcapi2.Client) func(idOrName string) []string { return c.Certificate().LabelKeys },
	Fetch: func(s state.State, idOrName string) (*hcloud.Certificate, error) {
		certificate, _, err := s.Client().Certificate().Get(s, idOrName)
		if err != nil {
			return nil, err
		}
		if certificate == nil {
			return nil, fmt.Errorf("Certificate not found: %s", idOrName)
		}
		return certificate, nil
	},
	SetLabels: func(s state.State, cert *hcloud.Certificate, labels map[string]string) error {
		opts := hcloud.CertificateUpdateOpts{
			Labels: labels,
		}
		_, _, err := s.Client().Certificate().Update(s, cert, opts)
		return err
	},
	GetLabels: func(cert *hcloud.Certificate) map[string]string {
		return cert.Labels
	},
	GetIDOrName: func(cert *hcloud.Certificate) string {
		return strconv.FormatInt(cert.ID, 10)
	},
}
View Source
var ListCmd = base.ListCmd{
	ResourceNamePlural: "Certificates",
	JSONKeyGetByName:   "certificates",
	DefaultColumns:     []string{"id", "name", "type", "domain_names", "not_valid_after", "age"},
	SortOption:         config.OptionSortCertificate,

	Fetch: func(s state.State, _ *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) {
		opts := hcloud.CertificateListOpts{ListOpts: listOpts}
		if len(sorts) > 0 {
			opts.Sort = sorts
		}
		certificates, err := s.Client().Certificate().AllWithOpts(s, opts)

		var resources []interface{}
		for _, n := range certificates {
			resources = append(resources, n)
		}
		return resources, err
	},

	OutputTable: func(t *output.Table, _ hcapi2.Client) {
		t.
			AddAllowedFields(hcloud.Certificate{}).
			RemoveAllowedField("certificate", "chain").
			AddFieldFn("labels", output.FieldFn(func(obj interface{}) string {
				cert := obj.(*hcloud.Certificate)
				return util.LabelsToString(cert.Labels)
			})).
			AddFieldFn("not_valid_before", func(obj interface{}) string {
				cert := obj.(*hcloud.Certificate)
				return util.Datetime(cert.NotValidBefore)
			}).
			AddFieldFn("not_valid_after", func(obj interface{}) string {
				cert := obj.(*hcloud.Certificate)
				return util.Datetime(cert.NotValidAfter)
			}).
			AddFieldFn("issuance_status", func(obj interface{}) string {
				cert := obj.(*hcloud.Certificate)
				if cert.Type != hcloud.CertificateTypeManaged {
					return "n/a"
				}
				return string(cert.Status.Issuance)
			}).
			AddFieldFn("renewal_status", func(obj interface{}) string {
				cert := obj.(*hcloud.Certificate)
				if cert.Type != hcloud.CertificateTypeManaged ||
					cert.Status.Renewal == hcloud.CertificateStatusTypeUnavailable {
					return "n/a"
				}
				return string(cert.Status.Renewal)
			}).
			AddFieldFn("domain_names", func(obj interface{}) string {
				cert := obj.(*hcloud.Certificate)
				return strings.Join(cert.DomainNames, ", ")
			}).
			AddFieldFn("created", output.FieldFn(func(obj interface{}) string {
				cert := obj.(*hcloud.Certificate)
				return util.Datetime(cert.Created)
			})).
			AddFieldFn("age", output.FieldFn(func(obj interface{}) string {
				cert := obj.(*hcloud.Certificate)
				return util.Age(cert.Created, time.Now())
			}))
	},

	Schema: func(resources []interface{}) interface{} {
		certSchemas := make([]schema.Certificate, 0, len(resources))
		for _, resource := range resources {
			cert := resource.(*hcloud.Certificate)
			certSchemas = append(certSchemas, hcloud.SchemaFromCertificate(cert))
		}

		return certSchemas
	},
}
View Source
var RetryCmd = base.Cmd{
	BaseCobraCommand: func(_ hcapi2.Client) *cobra.Command {
		return &cobra.Command{
			Use:                   "retry <certificate>",
			Short:                 "Retry a managed Certificate's issuance",
			TraverseChildren:      true,
			DisableFlagsInUseLine: true,
		}
	},
	Run: func(s state.State, cmd *cobra.Command, args []string) error {
		idOrName := args[0]
		certificate, _, err := s.Client().Certificate().Get(s, idOrName)
		if err != nil {
			return err
		}
		if certificate == nil {
			return fmt.Errorf("Certificate not found: %s", idOrName)
		}

		action, _, err := s.Client().Certificate().RetryIssuance(s, certificate)
		if err != nil {
			return err
		}

		if err := s.WaitForActions(s, cmd, action); err != nil {
			return err
		}

		cmd.Printf("Retried issuance of Certificate %s\n", certificate.Name)
		return nil
	},
}
View Source
var UpdateCmd = base.UpdateCmd{
	ResourceNameSingular: "Certificate",
	ShortDescription:     "Update a Certificate",
	NameSuggestions:      func(c hcapi2.Client) func() []string { return c.Firewall().Names },
	Fetch: func(s state.State, _ *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) {
		return s.Client().Certificate().Get(s, idOrName)
	},
	DefineFlags: func(cmd *cobra.Command) {
		cmd.Flags().String("name", "", "Certificate Name")
	},
	Update: func(s state.State, _ *cobra.Command, resource interface{}, flags map[string]pflag.Value) error {
		certificate := resource.(*hcloud.Certificate)
		updOpts := hcloud.CertificateUpdateOpts{
			Name: flags["name"].String(),
		}
		_, _, err := s.Client().Certificate().Update(s, certificate, updOpts)
		if err != nil {
			return err
		}
		return nil
	},
}

Functions

func NewCommand

func NewCommand(s state.State) *cobra.Command

Types

This section is empty.

Jump to

Keyboard shortcuts

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