-
Notifications
You must be signed in to change notification settings - Fork 21
/
configurationVersion.go
179 lines (147 loc) · 5.33 KB
/
configurationVersion.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
Copyright © 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
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 controller
import (
"context"
"fmt"
"os"
"github.com/awslabs/tecli/cobra/aid"
"github.com/awslabs/tecli/cobra/dao"
"github.com/awslabs/tecli/helper"
"github.com/hashicorp/go-tfe"
"github.com/spf13/cobra"
)
var configurationVersionValidArgs = []string{"list", "create", "read", "upload"}
// ConfigurationVersionCmd command to display tecli current version
func ConfigurationVersionCmd() *cobra.Command {
man, err := helper.GetManual("configuration-version", configurationVersionValidArgs)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
cmd := &cobra.Command{
Use: man.Use,
Short: man.Short,
Long: man.Long,
Example: man.Example,
ValidArgs: configurationVersionValidArgs,
Args: cobra.OnlyValidArgs,
PreRunE: configurationVersionPreRun,
RunE: configurationVersionRun,
SilenceUsage: true,
}
aid.SetConfigurationVersionFlags(cmd)
cmd.SetOut(os.Stdout)
cmd.SetErr(os.Stderr)
return cmd
}
func configurationVersionPreRun(cmd *cobra.Command, args []string) error {
if err := helper.ValidateCmdArgs(cmd, args, "configuration-version"); err != nil {
return err
}
fArg := args[0]
switch fArg {
case "list", "create":
if err := helper.ValidateCmdArgAndFlag(cmd, args, "configuration-version", fArg, "workspace-id"); err != nil {
return err
}
case "read":
if err := helper.ValidateCmdArgAndFlag(cmd, args, "configuration-version", fArg, "id"); err != nil {
return err
}
case "upload":
if err := helper.ValidateCmdArgAndFlag(cmd, args, "configuration-version", fArg, "url"); err != nil {
return err
}
if err := helper.ValidateCmdArgAndFlag(cmd, args, "configuration-version", fArg, "path"); err != nil {
return err
}
}
return nil
}
func configurationVersionRun(cmd *cobra.Command, args []string) error {
token := dao.GetTeamToken(profile)
client := aid.GetTFEClient(token)
fArg := args[0]
switch fArg {
case "list":
workspaceID, err := cmd.Flags().GetString("workspace-id")
if err != nil {
return fmt.Errorf("unable to get flag workspace-id\n%v", err)
}
list, err := configurationVersionList(client, workspaceID, tfe.ConfigurationVersionListOptions{})
if err == nil {
aid.PrintConfigurationVersionList(list)
} else {
return fmt.Errorf("no configurationVersion was found")
}
case "create":
workspaceID, err := cmd.Flags().GetString("workspace-id")
if err != nil {
return fmt.Errorf("unable to get flag workspace-id\n%v", err)
}
options := aid.GetConfigurationVersionCreateOptions(cmd)
cv, err := configurationVersionCreate(client, workspaceID, options)
if err == nil && cv.ID != "" {
fmt.Println(aid.ToJSON(cv))
} else {
return fmt.Errorf("unable to create configuration version\n%v", err)
}
case "read":
id, err := cmd.Flags().GetString("id")
if err != nil {
return fmt.Errorf("unable to get flag id\n%v", err)
}
cv, err := configurationVersionRead(client, id)
if err == nil {
fmt.Println(aid.ToJSON(cv))
} else {
return fmt.Errorf("configuration version %s not found\n%v", id, err)
}
case "upload":
url, err := cmd.Flags().GetString("url")
if err != nil {
return fmt.Errorf("unable to get flag url\n%v", err)
}
path, err := cmd.Flags().GetString("path")
if err != nil {
return fmt.Errorf("unable to get flag path\n%v", err)
}
err = configurationVersionUpload(client, url, path)
if err == nil {
fmt.Println("upload completed successfully")
} else {
return fmt.Errorf("unable to upload to configuration version\n%v", err)
}
}
return nil
}
// List returns all configuration versions of a workspace.
func configurationVersionList(client *tfe.Client, workspaceID string, options tfe.ConfigurationVersionListOptions) (*tfe.ConfigurationVersionList, error) {
return client.ConfigurationVersions.List(context.Background(), workspaceID, options)
}
// Create is used to create a new configuration version. The created
// configuration version will be usable once data is uploaded to it.
func configurationVersionCreate(client *tfe.Client, workspaceID string, options tfe.ConfigurationVersionCreateOptions) (*tfe.ConfigurationVersion, error) {
return client.ConfigurationVersions.Create(context.Background(), workspaceID, options)
}
// Read a configuration version by its ID.
func configurationVersionRead(client *tfe.Client, cvID string) (*tfe.ConfigurationVersion, error) {
return client.ConfigurationVersions.Read(context.Background(), cvID)
}
// Upload packages and uploads Terraform configuration files. It requires
// the upload URL from a configuration version and the full path to the
// configuration files on disk.
func configurationVersionUpload(client *tfe.Client, url string, path string) error {
return client.ConfigurationVersions.Upload(context.Background(), url, path)
}