-
Notifications
You must be signed in to change notification settings - Fork 21
/
aws_args.go
181 lines (160 loc) · 5.87 KB
/
aws_args.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
// Copyright 2020 Google Inc. 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 importer
import (
"fmt"
"regexp"
"strings"
daisy "github.com/GoogleCloudPlatform/compute-daisy"
"github.com/GoogleCloudPlatform/compute-image-import/cli_tools/common/utils/param"
"github.com/GoogleCloudPlatform/compute-image-import/cli_tools/common/utils/validation"
)
// awsImportArguments holds the structured results of parsing CLI arguments,
// and optionally allows for validating and populating the arguments.
type awsImportArguments struct {
// Passed in by user
accessKeyID string
amiID string
clientID string
executablePath string
exportLocation string
sourceFilePath string
gcsComputeEndpoint string
gcsProjectPtr *string
gcsZone string
gcsRegion string
gcsNetwork string
gcsSubnet string
gcsScratchBucket string
gcsStorageLocation string
region string
secretAccessKey string
sessionToken string
workerMachineSeries []string
// Internal generated
exportBucket string
exportFolder string
exportKey string
exportFileSize int64
}
// Flags
const (
awsAMIIDFlag = "aws_ami_id"
awsAMIExportLocationFlag = "aws_ami_export_location"
awsAccessKeyIDFlag = "aws_access_key_id"
awsSecretAccessKeyFlag = "aws_secret_access_key"
awsSessionTokenFlag = "aws_session_token"
awsRegionFlag = "aws_region"
awsSourceAMIFilePathFlag = "aws_source_ami_file_path"
)
var (
bucketNameRegex = `[a-z0-9][-_.a-z0-9]*`
s3PathRegex = regexp.MustCompile(fmt.Sprintf(`^s3://(%s)(\/.*)?$`, bucketNameRegex))
)
// newAWSImportArguments creates a new AWSImportArgument instance.
func newAWSImportArguments(args *OneStepImportArguments) *awsImportArguments {
return &awsImportArguments{
accessKeyID: args.AWSAccessKeyID,
amiID: args.AWSAMIID,
clientID: args.ClientID,
executablePath: args.ExecutablePath,
exportLocation: args.AWSAMIExportLocation,
sourceFilePath: args.AWSSourceAMIFilePath,
gcsComputeEndpoint: args.ComputeEndpoint,
gcsProjectPtr: args.ProjectPtr,
gcsZone: args.Zone,
gcsRegion: args.Region,
gcsNetwork: args.Network,
gcsSubnet: args.Subnet,
gcsScratchBucket: args.ScratchBucketGcsPath,
gcsStorageLocation: args.StorageLocation,
region: args.AWSRegion,
secretAccessKey: args.AWSSecretAccessKey,
sessionToken: args.AWSSessionToken,
}
}
// ValidateAndPopulate validates args related to import from AWS, and populates
// any missing parameters.
func (args *awsImportArguments) validateAndPopulate(populator param.Populator) error {
err := args.validate()
if err != nil {
return err
}
err = populator.PopulateMissingParameters(args.gcsProjectPtr, args.clientID, &args.gcsZone,
&args.gcsRegion, &args.gcsScratchBucket, "", &args.gcsStorageLocation, &args.gcsNetwork, &args.gcsSubnet,
&args.workerMachineSeries)
if err != nil {
return err
}
return args.generateS3PathElements()
}
func (args *awsImportArguments) validate() error {
if err := validation.ValidateStringFlagNotEmpty(args.accessKeyID, awsAccessKeyIDFlag); err != nil {
return err
}
if err := validation.ValidateStringFlagNotEmpty(args.secretAccessKey, awsSecretAccessKeyFlag); err != nil {
return err
}
if err := validation.ValidateStringFlagNotEmpty(args.region, awsRegionFlag); err != nil {
return err
}
if err := validation.ValidateStringFlagNotEmpty(args.sessionToken, awsSessionTokenFlag); err != nil {
return err
}
needsExport := args.amiID != "" && args.exportLocation != "" && args.sourceFilePath == ""
isResumeExported := args.amiID == "" && args.exportLocation == "" && args.sourceFilePath != ""
if !(needsExport || isResumeExported) {
return daisy.Errf("specify -%v to import from "+
"exported image file, or both -%v and -%v to "+
"import from AMI", awsSourceAMIFilePathFlag, awsAMIIDFlag, awsAMIExportLocationFlag)
}
return nil
}
// isExportRequired returns true if AMI needs to be exported, false otherwise.
func (args *awsImportArguments) isExportRequired() bool {
return args.sourceFilePath == ""
}
// generateS3PathElements gets bucket name, and folder or object key depending on if
// AMI has been exported, for a valid object path. Error is returned otherwise.
func (args *awsImportArguments) generateS3PathElements() error {
var err error
if args.isExportRequired() {
// Export required, get metadata from provided export location.
args.exportBucket, args.exportFolder, err = splitS3Path(args.exportLocation)
if err != nil {
return err
}
if args.exportFolder != "" && !strings.HasSuffix(args.exportFolder, "/") {
args.exportFolder += "/"
}
} else {
// AMI already exported, get metadata from provide object path.
args.exportBucket, args.exportKey, err = splitS3Path(args.sourceFilePath)
if err != nil {
return err
}
if args.exportKey == "" {
return daisy.Errf("%v is not a valid S3 file path", args.sourceFilePath)
}
}
return nil
}
// splitS3Path splits S3 path into bucket and object path portions
func splitS3Path(path string) (string, string, error) {
matches := s3PathRegex.FindStringSubmatch(path)
if matches != nil {
return matches[1], strings.TrimLeft(matches[2], "/"), nil
}
return "", "", daisy.Errf("%v is not a valid AWS S3 path", path)
}