2020-02-26 02:45:36 +01:00
|
|
|
// Copyright 2016-2018, Pulumi Corporation.
|
|
|
|
//
|
|
|
|
// 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 config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2020-02-26 00:03:16 +01:00
|
|
|
"fmt"
|
2020-02-26 02:45:36 +01:00
|
|
|
|
|
|
|
"github.com/spf13/cast"
|
|
|
|
|
2021-03-17 14:20:05 +01:00
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
|
2020-02-26 02:45:36 +01:00
|
|
|
)
|
|
|
|
|
2021-05-19 00:02:43 +02:00
|
|
|
func try(ctx *pulumi.Context, key, use, insteadOf string) (string, error) {
|
|
|
|
v, ok := get(ctx, key, use, insteadOf)
|
2020-02-26 02:45:36 +01:00
|
|
|
if !ok {
|
|
|
|
return "",
|
2020-02-26 00:03:16 +01:00
|
|
|
fmt.Errorf("missing required configuration variable '%s'; run `pulumi config` to set", key)
|
2020-02-26 02:45:36 +01:00
|
|
|
}
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
|
2021-05-19 00:02:43 +02:00
|
|
|
// Try loads a configuration value by its key, returning a non-nil error if it doesn't exist.
|
|
|
|
func Try(ctx *pulumi.Context, key string) (string, error) {
|
|
|
|
return try(ctx, key, "TrySecret", "Try")
|
|
|
|
}
|
|
|
|
|
|
|
|
func tryObject(ctx *pulumi.Context, key string, output interface{}, use, insteadOf string) error {
|
|
|
|
v, err := try(ctx, key, use, insteadOf)
|
2020-02-26 02:45:36 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return json.Unmarshal([]byte(v), output)
|
|
|
|
}
|
|
|
|
|
2021-05-19 00:02:43 +02:00
|
|
|
// TryObject loads an optional configuration value by its key into the output variable,
|
|
|
|
// or returns an error if unable to do so.
|
|
|
|
func TryObject(ctx *pulumi.Context, key string, output interface{}) error {
|
|
|
|
return tryObject(ctx, key, output, "TrySecretObject", "TryObject")
|
|
|
|
}
|
|
|
|
|
2020-02-26 02:45:36 +01:00
|
|
|
{{range .Builtins}}
|
|
|
|
{{if .GenerateConfig}}
|
2021-05-19 00:02:43 +02:00
|
|
|
func try{{.Name}}(ctx *pulumi.Context, key, use, insteadOf string) ({{.Type}}, error) {
|
|
|
|
v, err := try(ctx, key, use, insteadOf)
|
2020-02-26 02:45:36 +01:00
|
|
|
if err != nil {
|
|
|
|
return {{.DefaultConfig}}, err
|
|
|
|
}
|
|
|
|
return cast.To{{.Name}}(v), nil
|
|
|
|
}
|
|
|
|
|
2021-05-19 00:02:43 +02:00
|
|
|
// Try{{.Name}} loads an optional configuration value by its key, as a {{.Type}}, or returns an error if it doesn't exist.
|
|
|
|
func Try{{.Name}}(ctx *pulumi.Context, key string) ({{.Type}}, error) {
|
|
|
|
return try{{.Name}}(ctx, key, "TrySecret{{.Name}}", "Try{{.Name}}")
|
|
|
|
}
|
|
|
|
|
2020-02-26 02:45:36 +01:00
|
|
|
{{end}}
|
|
|
|
{{end}}
|
|
|
|
|
|
|
|
// TrySecret loads a configuration value by its key, returning a non-nil error if it doesn't exist.
|
2020-06-11 03:24:05 +02:00
|
|
|
func TrySecret(ctx *pulumi.Context, key string) (pulumi.StringOutput, error) {
|
2021-05-19 00:02:43 +02:00
|
|
|
v, err := try(ctx, key, "", "")
|
2020-02-26 02:45:36 +01:00
|
|
|
if err != nil {
|
2020-06-11 03:24:05 +02:00
|
|
|
var empty pulumi.StringOutput
|
|
|
|
return empty, err
|
2020-02-26 02:45:36 +01:00
|
|
|
}
|
2020-06-11 03:24:05 +02:00
|
|
|
return pulumi.ToSecret(pulumi.String(v)).(pulumi.StringOutput), nil
|
2020-02-26 02:45:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// TrySecretObject loads a configuration value by its key into the output variable,
|
|
|
|
// or returns an error if unable to do so.
|
|
|
|
func TrySecretObject(ctx *pulumi.Context, key string, output interface{}) (pulumi.Output, error) {
|
2021-05-19 00:02:43 +02:00
|
|
|
err := tryObject(ctx, key, output, "", "")
|
2020-02-26 02:45:36 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return pulumi.ToSecret(output), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
{{range .Builtins}}
|
|
|
|
{{if .GenerateConfig}}
|
|
|
|
// TrySecret{{.Name}} loads an optional configuration value by its key, as a {{.Type}},
|
|
|
|
// or returns an error if it doesn't exist.
|
2020-06-11 03:24:05 +02:00
|
|
|
func TrySecret{{.Name}}(ctx *pulumi.Context, key string) (pulumi.{{.Name}}Output, error) {
|
2021-05-19 00:02:43 +02:00
|
|
|
v, err := try{{.Name}}(ctx, key, "", "")
|
2020-02-26 02:45:36 +01:00
|
|
|
if err != nil {
|
2020-06-11 03:24:05 +02:00
|
|
|
var empty pulumi.{{.Name}}Output
|
|
|
|
return empty, err
|
2020-02-26 02:45:36 +01:00
|
|
|
}
|
2020-06-11 03:24:05 +02:00
|
|
|
return pulumi.ToSecret(pulumi.{{.Name}}(v)).(pulumi.{{.Name}}Output), nil
|
2020-02-26 02:45:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
{{end}}
|
|
|
|
{{end}}
|