This repository has been archived by the owner on Nov 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
/
create_application.go
86 lines (76 loc) · 2.29 KB
/
create_application.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
///////////////////////////////////////////////////////////////////////
// Copyright (c) 2017 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
///////////////////////////////////////////////////////////////////////
package cmd
import (
"encoding/json"
"fmt"
"io"
"github.com/go-openapi/swag"
"github.com/spf13/cobra"
"golang.org/x/net/context"
"github.com/vmware/dispatch/pkg/api/v1"
"github.com/vmware/dispatch/pkg/application-manager/gen/client/application"
"github.com/vmware/dispatch/pkg/dispatchcli/i18n"
)
var (
createApplicationLong = i18n.T(
`Create dispatch application.
Note:
Import your own tls certificates if you want to use your own domain name with HTTPS secure connection
`)
// TODO: add examples
createApplicationExample = i18n.T(``)
)
// NewCmdCreateApplication creates command responsible for dispatch application creation.
func NewCmdCreateApplication(out io.Writer, errOut io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "application NAME",
Short: i18n.T("Create application"),
Long: createApplicationLong,
Example: createApplicationExample,
Args: cobra.ExactArgs(1),
Aliases: []string{"app"},
Run: func(cmd *cobra.Command, args []string) {
err := createApplication(out, errOut, cmd, args)
CheckErr(err)
},
}
return cmd
}
// CallCreateApplication makes the API call to create an application
func CallCreateApplication(i interface{}) error {
client := applicationManagerClient()
body := i.(*v1.Application)
params := &application.AddAppParams{
Body: body,
Context: context.Background(),
}
created, err := client.Application.AddApp(params, GetAuthInfoWriter())
if err != nil {
return formatAPIError(err, params)
}
*body = *created.Payload
return nil
}
func createApplication(out, errOut io.Writer, cmd *cobra.Command, args []string) error {
body := &v1.Application{
Name: swag.String(args[0]),
Tags: []*v1.Tag{},
}
if cmdFlagApplication != "" {
body.Tags = append(body.Tags, &v1.Tag{Key: "Application", Value: cmdFlagApplication})
}
err := CallCreateApplication(body)
if err != nil {
return err
}
if dispatchConfig.JSON {
encoder := json.NewEncoder(out)
encoder.SetIndent("", " ")
return encoder.Encode(*body)
}
fmt.Fprintf(out, "created application: %s\n", *body.Name)
return nil
}