certificate

package
v1.40.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2023 License: MIT Imports: 16 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(client hcapi2.Client) *cobra.Command {
		cmd := &cobra.Command{
			Use:   "create [FLAGS]",
			Short: "Create or upload a Certificate",
			Args:  cobra.ExactArgs(0),
		}

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

		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(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, strings []string) (*hcloud.Response, any, error) {
		certType, err := cmd.Flags().GetString("type")
		if err != nil {
			return nil, nil, err
		}
		switch hcloud.CertificateType(certType) {
		case hcloud.CertificateTypeManaged:
			response, err := createManaged(ctx, client, waiter, cmd)
			return response, nil, err
		default:
			response, err := createUploaded(ctx, client, cmd)
			return response, nil, err
		}
	},
	PrintResource: func(_ context.Context, _ hcapi2.Client, _ *cobra.Command, _ any) {

	},
}
View Source
var DeleteCmd = base.DeleteCmd{
	ResourceNameSingular: "certificate",
	ShortDescription:     "Delete a certificate",
	NameSuggestions:      func(c hcapi2.Client) func() []string { return c.Firewall().Names },
	Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) {
		return client.Certificate().Get(ctx, idOrName)
	},
	Delete: func(ctx context.Context, client hcapi2.Client, _ state.ActionWaiter, cmd *cobra.Command, resource interface{}) error {
		certificate := resource.(*hcloud.Certificate)
		if _, err := client.Certificate().Delete(ctx, certificate); err != nil {
			return err
		}
		return nil
	},
}
View Source
var DescribeCmd = base.DescribeCmd{
	ResourceNameSingular: "certificate",
	ShortDescription:     "Describe an certificate",
	JSONKeyGetByID:       "certificate",
	JSONKeyGetByName:     "certificates",
	NameSuggestions:      func(c hcapi2.Client) func() []string { return c.Certificate().Names },
	Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) {
		return client.Certificate().Get(ctx, idOrName)
	},
	PrintText: func(_ context.Context, client hcapi2.Client, cmd *cobra.Command, resource interface{}) error {
		cert := resource.(*hcloud.Certificate)
		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 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", client.LoadBalancer().LoadBalancerName(ub.ID))
			}
		}
		return nil
	},
}
View Source
var LabelCmds = base.LabelCmds{
	ResourceNameSingular:   "certificate",
	ShortDescriptionAdd:    "Add a label to an certificate",
	ShortDescriptionRemove: "Remove a label from an 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 },
	FetchLabels: func(ctx context.Context, client hcapi2.Client, idOrName string) (map[string]string, int64, error) {
		certificate, _, err := client.Certificate().Get(ctx, idOrName)
		if err != nil {
			return nil, 0, err
		}
		if certificate == nil {
			return nil, 0, fmt.Errorf("certificate not found: %s", idOrName)
		}
		return certificate.Labels, certificate.ID, nil
	},
	SetLabels: func(ctx context.Context, client hcapi2.Client, id int64, labels map[string]string) error {
		opts := hcloud.CertificateUpdateOpts{
			Labels: labels,
		}
		_, _, err := client.Certificate().Update(ctx, &hcloud.Certificate{ID: id}, opts)
		return err
	},
}
View Source
var ListCmd = base.ListCmd{
	ResourceNamePlural: "Certificates",
	JSONKeyGetByName:   "certificates",
	DefaultColumns:     []string{"id", "name", "type", "domain_names", "not_valid_after", "age"},

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

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

	OutputTable: func(_ hcapi2.Client) *output.Table {
		return output.NewTable().
			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())
			}))
	},

	JSONSchema: func(resources []interface{}) interface{} {
		certSchemas := make([]schema.Certificate, 0, len(resources))
		for _, resource := range resources {
			cert := resource.(*hcloud.Certificate)
			certSchema := schema.Certificate{
				ID:             cert.ID,
				Certificate:    cert.Certificate,
				Created:        cert.Created,
				DomainNames:    cert.DomainNames,
				Fingerprint:    cert.Fingerprint,
				Labels:         cert.Labels,
				Name:           cert.Name,
				Type:           string(cert.Type),
				NotValidAfter:  cert.NotValidAfter,
				NotValidBefore: cert.NotValidBefore,
			}
			if len(cert.UsedBy) > 0 {
				certSchema.UsedBy = make([]schema.CertificateUsedByRef, len(cert.UsedBy))
				for i, ub := range cert.UsedBy {
					certSchema.UsedBy[i] = schema.CertificateUsedByRef{
						ID:   ub.ID,
						Type: string(ub.Type),
					}
				}
			}
			certSchemas = append(certSchemas, certSchema)
		}

		return certSchemas
	},
}
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(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) {
		return client.Certificate().Get(ctx, idOrName)
	},
	DefineFlags: func(cmd *cobra.Command) {
		cmd.Flags().String("name", "", "Certificate Name")
	},
	Update: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, resource interface{}, flags map[string]pflag.Value) error {
		certificate := resource.(*hcloud.Certificate)
		updOpts := hcloud.CertificateUpdateOpts{
			Name: flags["name"].String(),
		}
		_, _, err := client.Certificate().Update(ctx, certificate, updOpts)
		if err != nil {
			return err
		}
		return nil
	},
}

Functions

func NewCommand

func NewCommand(cli *state.State, client hcapi2.Client) *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