github.com/opentofu/opentofu@v1.7.1/internal/encryption/config/config_parse.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 config 7 8 import ( 9 "fmt" 10 11 "github.com/hashicorp/hcl/v2" 12 "github.com/hashicorp/hcl/v2/gohcl" 13 ) 14 15 // DecodeConfig takes a hcl.Body and decodes it into a Config struct. 16 // This method is here as an example for how someone using this library might want to decode a configuration. 17 // if they were not using gohcl directly. 18 // Right now for real world use this is only intended to be used in tests, until we publish this publicly. 19 func DecodeConfig(body hcl.Body, rng hcl.Range) (*EncryptionConfig, hcl.Diagnostics) { 20 cfg := &EncryptionConfig{DeclRange: rng} 21 22 diags := gohcl.DecodeBody(body, nil, cfg) 23 if diags.HasErrors() { 24 return nil, diags 25 } 26 27 for i, kp := range cfg.KeyProviderConfigs { 28 for j, okp := range cfg.KeyProviderConfigs { 29 if i != j && kp.Type == okp.Type && kp.Name == okp.Name { 30 diags = append(diags, &hcl.Diagnostic{ 31 Severity: hcl.DiagError, 32 Summary: "Duplicate key_provider", 33 Detail: fmt.Sprintf("Found multiple instances of key_provider.%s.%s", kp.Type, kp.Name), 34 Subject: rng.Ptr(), 35 }) 36 break 37 } 38 } 39 } 40 41 for i, m := range cfg.MethodConfigs { 42 for j, om := range cfg.MethodConfigs { 43 if i != j && m.Type == om.Type && m.Name == om.Name { 44 diags = append(diags, &hcl.Diagnostic{ 45 Severity: hcl.DiagError, 46 Summary: "Duplicate method", 47 Detail: fmt.Sprintf("Found multiple instances of method.%s.%s", m.Type, m.Name), 48 Subject: rng.Ptr(), 49 }) 50 break 51 } 52 } 53 } 54 55 if cfg.Remote != nil { 56 for i, t := range cfg.Remote.Targets { 57 for j, ot := range cfg.Remote.Targets { 58 if i != j && t.Name == ot.Name { 59 diags = append(diags, &hcl.Diagnostic{ 60 Severity: hcl.DiagError, 61 Summary: "Duplicate remote_data_source", 62 Detail: fmt.Sprintf("Found multiple instances of remote_data_source.%s", t.Name), 63 Subject: rng.Ptr(), 64 }) 65 break 66 } 67 } 68 } 69 } 70 71 if diags.HasErrors() { 72 return nil, diags 73 } 74 75 return cfg, diags 76 }