-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
artifactregistry: accept all valid durations (#12667)
Co-authored-by: Chris Hawk <chrishawk+github@gmail.com>
- Loading branch information
1 parent
f4a1b93
commit 9449992
Showing
4 changed files
with
145 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
mmv1/templates/terraform/custom_expand/duration_to_seconds.go.tmpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{{/* | ||
The license inside this block applies to this file | ||
Copyright 2024 Google Inc. | ||
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. | ||
*/ -}} | ||
func expand{{$.GetPrefix}}{{$.TitlelizeProperty}}(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { | ||
if v == nil { | ||
return nil, nil | ||
} | ||
val, ok := v.(string) | ||
if !ok { | ||
return nil, fmt.Errorf("unexpected value is not string: %v", v) | ||
} | ||
if len(val) == 0 { | ||
return nil, nil | ||
} | ||
n, err := strconv.Atoi(val[:len(val)-1]) | ||
if err != nil { | ||
return nil, fmt.Errorf("unexpected value is not duration: %v", v) | ||
} | ||
// time.ParseDuration does not support 'd' | ||
var seconds int | ||
switch val[len(val)-1] { | ||
case 's': | ||
seconds = n | ||
case 'm': | ||
seconds = n * 60 | ||
case 'h': | ||
seconds = n * 3600 | ||
case 'd': | ||
seconds = n * 86400 | ||
default: | ||
return nil, fmt.Errorf("unexpected duration has unknown unit: %c", val[len(val)-1]) | ||
} | ||
return fmt.Sprintf("%ds", seconds), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters