// 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"

	"github.com/spf13/cast"

	"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
	"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)

func require(ctx *pulumi.Context, key, use, insteadOf string) string {
	v, ok := get(ctx, key, use, insteadOf)
	if !ok {
		contract.Failf("missing required configuration variable '%s'; run `pulumi config` to set", key)
	}
	return v
}

// Require loads a configuration value by its key, or panics if it doesn't exist.
func Require(ctx *pulumi.Context, key string) string {
	return require(ctx, key, "RequireSecret", "Require")
}

func requireObject(ctx *pulumi.Context, key string, output interface{}, use, insteadOf string) {
	v := require(ctx, key, use, insteadOf)
	if err := json.Unmarshal([]byte(v), output); err != nil {
		contract.Failf("unable to unmarshall required configuration variable '%s'; %s", key, err.Error())
	}
}

// RequireObject loads an optional configuration value by its key into the output variable,
// or panics if unable to do so.
func RequireObject(ctx *pulumi.Context, key string, output interface{}) {
	requireObject(ctx, key, output, "RequireSecretObject", "RequireObject")
}

{{range .Builtins}}
{{if .GenerateConfig}}
func require{{.Name}}(ctx *pulumi.Context, key, use, insteadOf string) {{.Type}} {
	v := require(ctx, key, use, insteadOf)
	return cast.To{{.Name}}(v)
}

// Require{{.Name}} loads an optional configuration value by its key, as a {{.Type}}, or panics if it doesn't exist.
func Require{{.Name}}(ctx *pulumi.Context, key string) {{.Type}} {
	return require{{.Name}}(ctx, key, "RequireSecret{{.Name}}", "Require{{.Name}}")
}

{{end}}
{{end}}
// RequireSecret loads a configuration value by its key returning it wrapped in a secret Output,
// or panics if it doesn't exist.
func RequireSecret(ctx *pulumi.Context, key string) pulumi.StringOutput {
	return pulumi.ToSecret(require(ctx, key, "", "")).(pulumi.StringOutput)
}

// RequireSecretObject loads an optional configuration value by its key into the output variable,
// returning it wrapped in a secret Output, or panics if unable to do so.
func RequireSecretObject(ctx *pulumi.Context, key string, output interface{}) pulumi.Output {
	requireObject(ctx, key, output, "", "")
	return pulumi.ToSecret(output)
}

{{range .Builtins}}
{{if .GenerateConfig}}
// RequireSecret{{.Name}} loads an optional configuration value by its key,
// as a {{.Type}} wrapped in a secret Output, or panics if it doesn't exist.
func RequireSecret{{.Name}}(ctx *pulumi.Context, key string) pulumi.{{.Name}}Output {
	return pulumi.ToSecret(require{{.Name}}(ctx, key, "", "")).(pulumi.{{.Name}}Output)
}
{{end}}
{{end}}
