-
Notifications
You must be signed in to change notification settings - Fork 6
/
post.go
145 lines (115 loc) · 5.11 KB
/
post.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package certmanager
import (
"context"
"fmt"
"os"
"github.com/ionos-cloud/ionosctl/v6/internal/constants"
"github.com/ionos-cloud/ionosctl/v6/internal/core"
"github.com/ionos-cloud/ionosctl/v6/internal/printer/json2table/jsonpaths"
"github.com/ionos-cloud/ionosctl/v6/internal/printer/jsontabwriter"
"github.com/ionos-cloud/ionosctl/v6/internal/printer/tabheaders"
sdkgo "github.com/ionos-cloud/sdk-go-cert-manager"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var createProperties = sdkgo.CertificatePostPropertiesDto{}
func CertCreateCmd() *core.Command {
cmd := core.NewCommand(context.TODO(), nil, core.CommandBuilder{
Namespace: "certmanager",
Resource: "certificates",
Verb: "add",
Aliases: []string{"a"},
ShortDesc: "Add a new Certificate",
LongDesc: "Use this command to add a Certificate.",
Example: "ionosctl certificate-manager add --certificate-name my-cert --certificate-path /path/to/cert --certificate-chain-path /path/to/cert-chain --private-key-path /path/to/private-key" +
"\n" + "ionosctl certificate-manager add --certificate-name my-cert --certificate <certificate> --certificate-chain <certificate chain> --private-key <private key>",
PreCmdRun: PreCmdPost,
CmdRun: CmdPost,
InitClient: true,
})
cmd.AddStringFlag(FlagCertName, "n", "", "Specify name of the certificate", core.RequiredFlagOption())
cmd.AddStringFlag(FlagCert, "", "", "Specify the certificate itself (required either this or --certificate-path)")
cmd.AddStringFlag(FlagCertChain, "", "", "Specify the certificate chain (required either this or --certificate-chain-path)")
cmd.AddStringFlag(FlagPrivateKey, "", "", "Specify the private key (required either this or --private-key-path)")
cmd.AddStringFlag(FlagCertPath, "", "", "Specify the certificate itself from a file (required either this or --certificate)")
cmd.AddStringFlag(FlagCertChainPath, "", "", "Specify the certificate chain from a file (required either this or --certificate-chain)")
cmd.AddStringFlag(FlagPrivateKeyPath, "", "", "Specify the private key from a file (required either this or --private-key)")
cmd.Command.Flags().StringSlice(constants.ArgCols, nil, tabheaders.ColsMessage(defaultCertificateCols))
_ = cmd.Command.RegisterFlagCompletionFunc(constants.ArgCols, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return defaultCertificateCols, cobra.ShellCompDirectiveNoFileComp
})
return cmd
}
func GetPropertyWithFallback(c *core.CommandConfig, property string, propertyPath string) (string, error) {
propertyValue, err := c.Command.Command.Flags().GetString(property)
if err != nil {
return "", err
}
if propertyValue != "" {
return propertyValue, nil
}
propertyValuePath, err := c.Command.Command.Flags().GetString(propertyPath)
if err != nil {
return "", err
}
if propertyValuePath == "" {
return "", fmt.Errorf("either --%s or --%s must be set", property, propertyPath)
}
propertyBytes, err := os.ReadFile(propertyValuePath)
if err != nil {
return "", err
}
return string(propertyBytes), nil
}
func CmdPost(c *core.CommandConfig) error {
fmt.Fprintf(c.Command.Command.ErrOrStderr(), jsontabwriter.GenerateVerboseOutput("Adding Certificate..."))
var name, certificate, certificateChain, privateKey string
name, err := c.Command.Command.Flags().GetString(FlagCertName)
if err != nil {
return err
}
certificate, err = GetPropertyWithFallback(c, FlagCert, FlagCertPath)
if err != nil {
return err
}
certificateChain, err = GetPropertyWithFallback(c, FlagCertChain, FlagCertChainPath)
if err != nil {
return err
}
privateKey, err = GetPropertyWithFallback(c, FlagPrivateKey, FlagPrivateKeyPath)
if err != nil {
return err
}
createProperties.SetName(name)
createProperties.SetCertificate(certificate)
createProperties.SetCertificateChain(certificateChain)
createProperties.SetPrivateKey(privateKey)
Dto := sdkgo.NewCertificatePostDtoWithDefaults()
Dto.SetProperties(createProperties)
cert, _, err := c.CertificateManagerServices.Certs().Post(*Dto)
if err != nil {
return err
}
cols, _ := c.Command.Command.Flags().GetStringSlice(constants.ArgCols)
out, err := jsontabwriter.GenerateOutput("", jsonpaths.CertManagerCertificate, cert,
tabheaders.GetHeadersAllDefault(defaultCertificateCols, cols))
if err != nil {
return err
}
fmt.Fprintf(c.Command.Command.OutOrStdout(), out)
return nil
}
func PreCmdPost(c *core.PreCommandConfig) error {
var err error
if !viper.IsSet(core.GetFlagName(c.NS, FlagCert)) && !viper.IsSet(core.GetFlagName(c.NS, FlagCertPath)) {
err = fmt.Errorf("%veither --%s or --%s must be set", err, FlagCert, FlagCertPath)
}
if !viper.IsSet(core.GetFlagName(c.NS, FlagCertChain)) && !viper.IsSet(core.GetFlagName(c.NS, FlagCertChainPath)) {
err = fmt.Errorf("%veither --%s or --%s must be set", err, FlagCertChain, FlagCertChainPath)
}
if !viper.IsSet(core.GetFlagName(c.NS, FlagPrivateKey)) && !viper.IsSet(core.GetFlagName(c.NS, FlagPrivateKeyPath)) {
err = fmt.Errorf("%veither --%s or --%s must be set", err, FlagPrivateKey, FlagPrivateKeyPath)
}
err = c.Command.Command.MarkFlagRequired(FlagCertName)
return err
}