-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_workspace.go
149 lines (137 loc) · 4.65 KB
/
data_workspace.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
146
147
148
149
package provider
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)
type dataWorkspace struct {
p *provider
}
func newDataWorkspace(p *provider) (*dataWorkspace, error) {
if p == nil {
return nil, fmt.Errorf("a provider is required")
}
return &dataWorkspace{p: p}, nil
}
func (*dataWorkspace) Schema(ctx context.Context) *tfprotov5.Schema {
attrs := []*tfprotov5.SchemaAttribute{
deprecatedIDAttribute(),
{
Name: "stack_id",
Type: tftypes.Number,
Description: "The ID of the stack in nullstone that owns this workspace.",
DescriptionKind: tfprotov5.StringKindMarkdown,
Computed: true,
},
{
Name: "stack_name",
Type: tftypes.String,
Description: "The name of the stack in nullstone that owns this workspace.",
DescriptionKind: tfprotov5.StringKindMarkdown,
Computed: true,
},
{
Name: "block_id",
Type: tftypes.Number,
Description: "The ID of the block in nullstone associated with this workspace.",
DescriptionKind: tfprotov5.StringKindMarkdown,
Computed: true,
},
{
Name: "block_name",
Type: tftypes.String,
Description: "The name of the block in nullstone that owns this workspace.",
DescriptionKind: tfprotov5.StringKindMarkdown,
Computed: true,
},
{
Name: "block_ref",
Type: tftypes.String,
Description: `The reference of the block in nullstone that owns this workspace.
This is typically used to construct unique resource names. See unique_name.`,
DescriptionKind: tfprotov5.StringKindMarkdown,
Computed: true,
},
{
Name: "env_id",
Type: tftypes.Number,
Description: "The ID of the environment in nullstone associated with this workspace.",
DescriptionKind: tfprotov5.StringKindMarkdown,
Computed: true,
},
{
Name: "env_name",
Type: tftypes.String,
Description: "The name of the block in nullstone that owns this workspace.",
DescriptionKind: tfprotov5.StringKindMarkdown,
Computed: true,
},
{
Name: "tags",
Type: tftypes.Map{ElementType: tftypes.String},
Computed: true,
Description: "A default list of tags including all nullstone configuration for this workspace.",
DescriptionKind: tfprotov5.StringKindMarkdown,
},
}
return &tfprotov5.Schema{
Version: 1,
Block: &tfprotov5.SchemaBlock{
Description: "Data source to configure module based on current nullstone workspace.",
DescriptionKind: tfprotov5.StringKindMarkdown,
Attributes: attrs,
},
}
}
func (d *dataWorkspace) Validate(ctx context.Context, config map[string]tftypes.Value) ([]*tfprotov5.Diagnostic, error) {
return nil, nil
}
func (d *dataWorkspace) Read(ctx context.Context, config map[string]tftypes.Value) (map[string]tftypes.Value, []*tfprotov5.Diagnostic, error) {
planConfig := d.p.PlanConfig
stackId := extractInt64FromConfig(config, "stack_id")
if stackId <= 0 {
stackId = planConfig.StackId
}
stackName := extractStringFromConfig(config, "stack_name")
if stackName == "" {
stackName = planConfig.StackName
}
blockId := extractInt64FromConfig(config, "block_id")
if blockId <= 0 {
blockId = planConfig.BlockId
}
blockName := extractStringFromConfig(config, "block_name")
if blockName == "" {
blockName = planConfig.BlockName
}
blockRef := extractStringFromConfig(config, "block_ref")
if blockRef == "" {
blockRef = planConfig.BlockRef
}
envId := extractInt64FromConfig(config, "env_id")
if envId <= 0 {
envId = planConfig.EnvId
}
envName := extractStringFromConfig(config, "env_name")
if envName == "" {
envName = planConfig.EnvName
}
id := fmt.Sprintf("%s/%s/%s/%s", planConfig.OrgName, stackName, blockName, envName)
tags := map[string]tftypes.Value{
"Stack": tftypes.NewValue(tftypes.String, stackName),
"Env": tftypes.NewValue(tftypes.String, envName),
"Block": tftypes.NewValue(tftypes.String, blockName),
}
return map[string]tftypes.Value{
"id": tftypes.NewValue(tftypes.String, id),
"stack_id": tftypes.NewValue(tftypes.Number, &stackId),
"stack_name": tftypes.NewValue(tftypes.String, stackName),
"block_id": tftypes.NewValue(tftypes.Number, &blockId),
"block_name": tftypes.NewValue(tftypes.String, blockName),
"block_ref": tftypes.NewValue(tftypes.String, blockRef),
"env_id": tftypes.NewValue(tftypes.Number, &envId),
"env_name": tftypes.NewValue(tftypes.String, envName),
"tags": tftypes.NewValue(tftypes.Map{ElementType: tftypes.String}, tags),
}, nil, nil
}