forked from straubt1/tfx
/
registry_module_version.go
242 lines (210 loc) · 9.62 KB
/
registry_module_version.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Copyright © 2021 Tom Straub <github.com/straubt1>
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package cmd
import (
tfe "github.com/hashicorp/go-tfe"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
var (
// `tfx registry module version` commands
registryModuleVersionCmd = &cobra.Command{
Use: "version",
Short: "Module Versions in Private Registry Commands",
Long: "Commands to work with Module Versions in the Private Registry of a TFx Organization.",
}
// `tfx registry module version list` command
registryModuleVersionListCmd = &cobra.Command{
Use: "list",
Short: "List Module Versions",
Long: "List Modules Versions of a module in the Private Module Registry for a TFx Organization.",
RunE: func(cmd *cobra.Command, args []string) error {
return registryModuleVersionList(
getTfxClientContext(),
*viperString("name"),
*viperString("provider"))
},
}
// `tfx registry module version create` command
registryModuleVersionCreateCmd = &cobra.Command{
Use: "create",
Short: "Create a Module Version",
Long: "Create a Module Version of a module in the Private Module Registry for a TFx Organization.",
RunE: func(cmd *cobra.Command, args []string) error {
moduleVersion, err := viperSemanticVersionString("version")
if err != nil {
return errors.New("failed to parse semantic version")
}
if !isDirectory(*viperString("directory")) {
return errors.New("directory file does not exist")
}
return registryModuleVersionCreate(
getTfxClientContext(),
*viperString("name"),
*viperString("provider"),
moduleVersion,
*viperString("directory"))
},
}
// `tfx registry module version delete` command
registryModuleVersionDeleteCmd = &cobra.Command{
Use: "delete",
Short: "Delete a Module Version",
Long: "Delete a Module Version of a module in the Private Module Registry for a TFx Organization.",
RunE: func(cmd *cobra.Command, args []string) error {
moduleVersion, err := viperSemanticVersionString("version")
if err != nil {
return errors.New("failed to parse semantic version")
}
return registryModuleVersionDelete(
getTfxClientContext(),
*viperString("name"),
*viperString("provider"),
moduleVersion)
},
}
// `tfx registry module version download` command
registryModuleVersionDownloadCmd = &cobra.Command{
Use: "download",
Short: "Download a Module Version",
Long: "Download the Terraform code of Module Version in the Private Module Registry for a TFx Organization.",
RunE: func(cmd *cobra.Command, args []string) error {
moduleVersion, err := viperSemanticVersionString("version")
if err != nil {
return errors.New("failed to parse semantic version")
}
directory, err := getDirectory(*viperString("directory"),
*viperString("name"), *viperString("provider"), *viperString("version"))
if err != nil {
return err
}
return registryModuleVersionDownload(
getTfxClientContext(),
*viperString("name"),
*viperString("provider"),
moduleVersion,
directory)
},
}
)
func init() {
// `tfx registry module version list` arguments
registryModuleVersionListCmd.Flags().StringP("name", "n", "", "Name of the Module (no spaces)")
registryModuleVersionListCmd.Flags().StringP("provider", "p", "", "Name of the provider (no spaces) (i.e. aws, azure, google)")
registryModuleVersionListCmd.MarkFlagRequired("name")
registryModuleVersionListCmd.MarkFlagRequired("provider")
// `tfx registry module version create` arguments
registryModuleVersionCreateCmd.Flags().StringP("name", "n", "", "Name of the Module (no spaces)")
registryModuleVersionCreateCmd.Flags().StringP("provider", "p", "", "Name of the provider (no spaces) (i.e. aws, azure, google)")
registryModuleVersionCreateCmd.Flags().StringP("version", "v", "", "Version of module (i.e. 0.0.1)")
registryModuleVersionCreateCmd.Flags().StringP("directory", "d", "./", "Directory of Terraform (optional, defaults to current directory)")
registryModuleVersionCreateCmd.MarkFlagRequired("name")
registryModuleVersionCreateCmd.MarkFlagRequired("provider")
registryModuleVersionCreateCmd.MarkFlagRequired("version")
// `tfx registry module version delete` arguments
registryModuleVersionDeleteCmd.Flags().StringP("name", "n", "", "Name of the Module (no spaces)")
registryModuleVersionDeleteCmd.Flags().StringP("provider", "p", "", "Name of the provider (no spaces) (i.e. aws, azure, google)")
registryModuleVersionDeleteCmd.Flags().StringP("version", "v", "", "Version of module (i.e. 0.0.1)")
registryModuleVersionDeleteCmd.MarkFlagRequired("name")
registryModuleVersionDeleteCmd.MarkFlagRequired("provider")
registryModuleVersionDeleteCmd.MarkFlagRequired("version")
// `tfx registry module version download` arguments
registryModuleVersionDownloadCmd.Flags().StringP("name", "n", "", "Name of the Module (no spaces)")
registryModuleVersionDownloadCmd.Flags().StringP("provider", "p", "", "Name of the provider (no spaces) (i.e. aws, azure, google)")
registryModuleVersionDownloadCmd.Flags().StringP("version", "v", "", "Version of module (i.e. 0.0.1)")
registryModuleVersionDownloadCmd.Flags().StringP("directory", "d", "", "Directory to download module to (optional, defaults to a temp directory)")
registryModuleVersionDownloadCmd.MarkFlagRequired("name")
registryModuleVersionDownloadCmd.MarkFlagRequired("provider")
registryModuleVersionDownloadCmd.MarkFlagRequired("version")
registryModuleCmd.AddCommand(registryModuleVersionCmd)
registryModuleVersionCmd.AddCommand(registryModuleVersionCreateCmd)
registryModuleVersionCmd.AddCommand(registryModuleVersionListCmd)
registryModuleVersionCmd.AddCommand(registryModuleVersionDeleteCmd)
registryModuleVersionCmd.AddCommand(registryModuleVersionDownloadCmd)
}
func registryModuleVersionList(c TfxClientContext, moduleName string, providerName string) error {
o.AddMessageUserProvided("List Module Versions for Organization:", c.OrganizationName)
module, err := c.Client.RegistryModules.Read(c.Context, tfe.RegistryModuleID{
Organization: c.OrganizationName,
Name: moduleName,
Provider: providerName,
Namespace: c.OrganizationName,
RegistryName: tfe.PrivateRegistry,
})
if err != nil {
return errors.Wrap(err, "failed to list module versions")
}
o.AddTableHeader("Version", "Status")
for _, i := range module.VersionStatuses {
o.AddTableRows(i.Version, i.Status)
}
return nil
}
func registryModuleVersionCreate(c TfxClientContext, moduleName string, providerName string,
moduleVersion string, directory string) error {
o.AddMessageUserProvided("Create Module Version for Organization:", c.OrganizationName)
module, err := c.Client.RegistryModules.CreateVersion(c.Context, tfe.RegistryModuleID{
Organization: c.OrganizationName,
Name: moduleName,
Provider: providerName,
Namespace: c.OrganizationName,
RegistryName: tfe.PrivateRegistry,
}, tfe.RegistryModuleCreateVersionOptions{
Version: &moduleVersion,
})
if err != nil {
return errors.Wrap(err, "failed to create module version")
}
o.AddMessageUserProvided("Module Version Created, Uploading...", "")
err = c.Client.RegistryModules.Upload(c.Context, *module, directory)
if err != nil {
errors.Wrap(err, "failed to upload module version")
}
o.AddMessageUserProvided("Module Created:", module.RegistryModule.Name)
o.AddDeferredMessageRead("ID", module.RegistryModule.ID)
o.AddDeferredMessageRead("Created", module.CreatedAt)
return nil
}
func registryModuleVersionDelete(c TfxClientContext, moduleName string, providerName string,
moduleVersion string) error {
o.AddMessageUserProvided("Delete Module Version for Organization:", c.OrganizationName)
err := c.Client.RegistryModules.DeleteVersion(c.Context, tfe.RegistryModuleID{
Organization: c.OrganizationName,
Name: moduleName,
Provider: providerName,
Namespace: c.OrganizationName,
RegistryName: tfe.PrivateRegistry,
}, moduleVersion)
if err != nil {
return errors.Wrap(err, "failed to delete module version")
}
o.AddMessageUserProvided("Module Version Deleted:", moduleName)
o.AddDeferredMessageRead("Status", "Success")
return nil
}
func registryModuleVersionDownload(c TfxClientContext, moduleName string, providerName string,
moduleVersion string, directory string) error {
o.AddMessageUserProvided("Downloading Module Version:", moduleName)
o.AddMessageUserProvided("Module Version Found, download started...", "")
_, err := DownloadModule(c.Token, c.Hostname, c.OrganizationName, moduleName, providerName, moduleVersion, directory)
if err != nil {
return errors.Wrap(err, "failed to download module")
}
o.AddDeferredMessageRead("Status", "Success")
o.AddDeferredMessageRead("Directory", directory)
return nil
}