/
data_source_component_config.go
145 lines (130 loc) · 3.64 KB
/
data_source_component_config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package provider
import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"gopkg.in/yaml.v2"
p "github.com/cloudposse/atmos/pkg/component"
c "github.com/cloudposse/atmos/pkg/convert"
)
func dataSourceComponentConfig() *schema.Resource {
return &schema.Resource{
Description: "The `component_config` data source accepts a component and a stack name " +
"and returns the component configuration in the stack",
ReadContext: dataSourceComponentConfigRead,
Schema: map[string]*schema.Schema{
"component": {
Description: "Component name.",
Type: schema.TypeString,
Required: true,
},
"stack": {
Description: "Stack name.",
Type: schema.TypeString,
Optional: true,
Default: "",
},
"tenant": {
Description: "Tenant.",
Type: schema.TypeString,
Optional: true,
Default: "",
},
"namespace": {
Description: "Namespace.",
Type: schema.TypeString,
Optional: true,
Default: "",
},
"environment": {
Description: "Environment.",
Type: schema.TypeString,
Optional: true,
Default: "",
},
"stage": {
Description: "Stage.",
Type: schema.TypeString,
Optional: true,
Default: "",
},
"ignore_errors": {
Description: "Flag to ignore errors if the component is not found in the stack.",
Type: schema.TypeBool,
Optional: true,
Default: false,
},
// https://www.terraform.io/plugin/sdkv2/schemas/schema-types#typemap
"env": {
Description: "Map of ENV vars in the format 'key=value'. These ENV vars will be set before executing the data source",
Type: schema.TypeMap,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
Default: nil,
},
"atmos_cli_config_path": {
Description: "atmos CLI config path.",
Type: schema.TypeString,
Optional: true,
Default: "",
},
"atmos_base_path": {
Description: "atmos base path to components and stacks.",
Type: schema.TypeString,
Optional: true,
Default: "",
},
"output": {
Description: "Component configuration.",
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceComponentConfigRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
component := d.Get("component").(string)
stack := d.Get("stack").(string)
namespace := d.Get("namespace").(string)
tenant := d.Get("tenant").(string)
environment := d.Get("environment").(string)
stage := d.Get("stage").(string)
ignoreErrors := d.Get("ignore_errors").(bool)
env := d.Get("env").(map[string]any)
atmosCliConfigPath := d.Get("atmos_cli_config_path").(string)
atmosBasePath := d.Get("atmos_base_path").(string)
var result map[string]any
var err error
var yamlConfig []byte
err = setEnv(env)
if err != nil {
return diag.FromErr(err)
}
if len(stack) > 0 {
result, err = p.ProcessComponentInStack(component, stack, atmosCliConfigPath, atmosBasePath)
if err != nil && !ignoreErrors {
return diag.FromErr(err)
}
} else {
result, err = p.ProcessComponentFromContext(component, namespace, tenant, environment, stage, atmosCliConfigPath, atmosBasePath)
if err != nil && !ignoreErrors {
return diag.FromErr(err)
}
}
if err != nil {
result = map[string]any{}
}
yamlConfig, err = yaml.Marshal(result)
if err != nil {
return diag.FromErr(err)
}
err = d.Set("output", string(yamlConfig))
if err != nil {
return diag.FromErr(err)
}
id := c.MakeId(yamlConfig)
d.SetId(id)
return nil
}