github.com/opentofu/opentofu@v1.7.1/internal/encryption/keyprovider/addr.go (about) 1 // Copyright (c) The OpenTofu Authors 2 // SPDX-License-Identifier: MPL-2.0 3 // Copyright (c) 2023 HashiCorp, Inc. 4 // SPDX-License-Identifier: MPL-2.0 5 6 package keyprovider 7 8 import ( 9 "fmt" 10 11 "github.com/hashicorp/hcl/v2" 12 ) 13 14 // Addr is a type-alias for key provider address strings that identify a specific key provider configuration. 15 // The Addr is an opaque value. Do not perform string manipulation on it outside the functions supplied by the 16 // keyprovider package. 17 type Addr string 18 19 // Validate validates the Addr for formal naming conformance, but does not check if the referenced key provider actually 20 // exists in the configuration. 21 func (a Addr) Validate() hcl.Diagnostics { 22 if !addrRe.MatchString(string(a)) { 23 return hcl.Diagnostics{ 24 &hcl.Diagnostic{ 25 Severity: hcl.DiagError, 26 Summary: "Invalid key provider address", 27 Detail: fmt.Sprintf( 28 "The supplied key provider address does not match the required form of %s", 29 addrRe.String(), 30 ), 31 }, 32 } 33 } 34 return nil 35 } 36 37 // NewAddr creates a new Addr type from the provider and name supplied. The Addr is a type-alias for key provider 38 // address strings that identify a specific key provider configuration. You should treat the value as opaque and not 39 // perform string manipulation on it outside the functions supplied by the keyprovider package. 40 func NewAddr(provider string, name string) (addr Addr, err hcl.Diagnostics) { 41 if !nameRe.MatchString(provider) { 42 err = err.Append( 43 &hcl.Diagnostic{ 44 Severity: hcl.DiagError, 45 Summary: "The provided key provider type is invalid", 46 Detail: fmt.Sprintf( 47 "The supplied key provider type (%s) does not match the required form of %s.", 48 provider, 49 nameRe.String(), 50 ), 51 }, 52 ) 53 } 54 if !nameRe.MatchString(name) { 55 err = err.Append( 56 &hcl.Diagnostic{ 57 Severity: hcl.DiagError, 58 Summary: "The provided key provider name is invalid", 59 Detail: fmt.Sprintf( 60 "The supplied key provider name (%s) does not match the required form of %s.", 61 name, 62 nameRe.String(), 63 ), 64 }, 65 ) 66 } 67 return Addr(fmt.Sprintf("key_provider.%s.%s", provider, name)), err 68 }