This repository has been archived by the owner on May 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 387
/
provision_cmd.go
99 lines (83 loc) · 3.06 KB
/
provision_cmd.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
/*
Copyright 2018 The Kubernetes Authors.
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.
*/
package instance
import (
"fmt"
"github.com/kubernetes-incubator/service-catalog/cmd/svcat/command"
"github.com/kubernetes-incubator/service-catalog/cmd/svcat/output"
"github.com/kubernetes-incubator/service-catalog/cmd/svcat/parameters"
"github.com/spf13/cobra"
)
type provisonCmd struct {
*command.Context
ns string
instanceName string
className string
planName string
rawParams []string
params map[string]string
rawSecrets []string
secrets map[string]string
}
// NewProvisionCmd builds a "svcat provision" command
func NewProvisionCmd(cxt *command.Context) *cobra.Command {
provisionCmd := &provisonCmd{Context: cxt}
cmd := &cobra.Command{
Use: "provision NAME --plan PLAN --class CLASS",
Short: "Create a new instance of a service",
Example: `
svcat provision wordpress-mysql-instance --class mysqldb --plan free -p location=eastus -p sslEnforcement=disabled
svcat provision wordpress-mysql-instance --class mysqldb --plan free -s mysecret[dbparams]
`,
RunE: func(cmd *cobra.Command, args []string) error {
return provisionCmd.run(args)
},
}
cmd.Flags().StringVarP(&provisionCmd.ns, "namespace", "n", "default",
"The namespace in which to create the instance")
cmd.Flags().StringVar(&provisionCmd.className, "class", "",
"The class name (Required)")
cmd.MarkFlagRequired("class")
cmd.Flags().StringVar(&provisionCmd.planName, "plan", "",
"The plan name (Required)")
cmd.MarkFlagRequired("plan")
cmd.Flags().StringArrayVarP(&provisionCmd.rawParams, "param", "p", nil,
"Additional parameter to use when provisioning the service, format: NAME=VALUE")
cmd.Flags().StringArrayVarP(&provisionCmd.rawSecrets, "secret", "s", nil,
"Additional parameter, whose value is stored in a secret, to use when provisioning the service, format: SECRET[KEY]")
return cmd
}
func (c *provisonCmd) run(args []string) error {
if len(args) == 0 {
return fmt.Errorf("an instance name is required")
}
c.instanceName = args[0]
var err error
c.params, err = parameters.ParseVariableAssignments(c.rawParams)
if err != nil {
return fmt.Errorf("invalid --param value (%s)", err)
}
c.secrets, err = parameters.ParseKeyMaps(c.rawSecrets)
if err != nil {
return fmt.Errorf("invalid --secret value (%s)", err)
}
return c.provision()
}
func (c *provisonCmd) provision() error {
instance, err := c.App.Provision(c.ns, c.instanceName, c.className, c.planName, c.params, c.secrets)
if err != nil {
return err
}
output.WriteInstanceDetails(c.Output, instance)
return nil
}