• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Scalr / terraform-provider-scalr / 14217303880

02 Apr 2025 10:22AM UTC coverage: 67.264%. First build
14217303880

Pull #409

github

petroprotsakh
SCALRCORE-32300 Update changelog
Pull Request #409: SCALRCORE-32300 BE > Provider > `scalr_variable` resource doesn't offer any way to expose/use variable value

175 of 520 new or added lines in 5 files covered. (33.65%)

7773 of 11556 relevant lines covered (67.26%)

224.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

47.3
/internal/provider/variable_resource.go
1
package provider
2

3
import (
4
        "context"
5
        "errors"
6

7
        "github.com/hashicorp/terraform-plugin-framework/path"
8
        "github.com/hashicorp/terraform-plugin-framework/resource"
9
        "github.com/scalr/go-scalr"
10

11
        "github.com/scalr/terraform-provider-scalr/internal/framework"
12
)
13

14
// Compile-time interface checks
15
var (
16
        _ resource.Resource                     = &variableResource{}
17
        _ resource.ResourceWithConfigure        = &variableResource{}
18
        _ resource.ResourceWithConfigValidators = &variableResource{}
19
        _ resource.ResourceWithImportState      = &variableResource{}
20
        _ resource.ResourceWithUpgradeState     = &variableResource{}
21
)
22

23
func newVariableResource() resource.Resource {
2,319✔
24
        return &variableResource{}
2,319✔
25
}
2,319✔
26

27
// variableResource defines the resource implementation.
28
type variableResource struct {
29
        framework.ResourceWithScalrClient
30
}
31

32
func (r *variableResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
147✔
33
        resp.TypeName = req.ProviderTypeName + "_variable"
147✔
34
}
147✔
35

36
func (r *variableResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
1,531✔
37
        resp.Schema = *variableResourceSchema()
1,531✔
38
}
1,531✔
39

40
func (r *variableResource) ConfigValidators(_ context.Context) []resource.ConfigValidator {
261✔
41
        // If needed, add config validation logic here,
261✔
42
        // or remove this method if no additional validation is needed.
261✔
43
        return []resource.ConfigValidator{}
261✔
44
}
261✔
45

46
func (r *variableResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
17✔
47
        var plan variableResourceModel
17✔
48

17✔
49
        // Read plan data
17✔
50
        resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
17✔
51
        if resp.Diagnostics.HasError() {
17✔
NEW
52
                return
×
NEW
53
        }
×
54

55
        opts := scalr.VariableCreateOptions{
17✔
56
                Key:         plan.Key.ValueStringPointer(),
17✔
57
                Value:       plan.Value.ValueStringPointer(),
17✔
58
                Description: plan.Description.ValueStringPointer(),
17✔
59
                Category:    ptr(scalr.CategoryType(plan.Category.ValueString())),
17✔
60
                HCL:         plan.HCL.ValueBoolPointer(),
17✔
61
                Sensitive:   plan.Sensitive.ValueBoolPointer(),
17✔
62
                Final:       plan.Final.ValueBoolPointer(),
17✔
63
                Account:     &scalr.Account{ID: plan.AccountID.ValueString()},
17✔
64
                QueryOptions: &scalr.VariableWriteQueryOptions{
17✔
65
                        Force:   plan.Force.ValueBoolPointer(),
17✔
66
                        Include: ptr("updated-by"),
17✔
67
                },
17✔
68
        }
17✔
69

17✔
70
        if !plan.WorkspaceID.IsUnknown() && !plan.WorkspaceID.IsNull() {
24✔
71
                opts.Workspace = &scalr.Workspace{ID: plan.WorkspaceID.ValueString()}
7✔
72
        }
7✔
73

74
        if !plan.EnvironmentID.IsUnknown() && !plan.EnvironmentID.IsNull() {
20✔
75
                opts.Environment = &scalr.Environment{ID: plan.EnvironmentID.ValueString()}
3✔
76
        }
3✔
77

78
        variable, err := r.Client.Variables.Create(ctx, opts)
17✔
79
        if err != nil {
17✔
NEW
80
                resp.Diagnostics.AddError("Error creating variable", err.Error())
×
NEW
81
                return
×
NEW
82
        }
×
83

84
        result, diags := variableResourceModelFromAPI(ctx, variable, &plan)
17✔
85
        resp.Diagnostics.Append(diags...)
17✔
86
        if resp.Diagnostics.HasError() {
17✔
NEW
87
                return
×
NEW
88
        }
×
89

90
        // Set state to fully populated data
91
        resp.Diagnostics.Append(resp.State.Set(ctx, result)...)
17✔
92
        if resp.Diagnostics.HasError() {
17✔
NEW
93
                return
×
NEW
94
        }
×
95
}
96

97
func (r *variableResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
62✔
98
        // Get current state
62✔
99
        var state variableResourceModel
62✔
100
        resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
62✔
101
        if resp.Diagnostics.HasError() {
62✔
NEW
102
                return
×
NEW
103
        }
×
104

105
        // Get refreshed resource state from API
106
        variable, err := r.Client.Variables.Read(ctx, state.Id.ValueString())
62✔
107
        if err != nil {
62✔
NEW
108
                if errors.Is(err, scalr.ErrResourceNotFound) {
×
NEW
109
                        resp.State.RemoveResource(ctx)
×
NEW
110
                        return
×
NEW
111
                }
×
NEW
112
                resp.Diagnostics.AddError("Error retrieving variable", err.Error())
×
NEW
113
                return
×
114
        }
115

116
        result, diags := variableResourceModelFromAPI(ctx, variable, &state)
62✔
117
        resp.Diagnostics.Append(diags...)
62✔
118
        if resp.Diagnostics.HasError() {
62✔
NEW
119
                return
×
NEW
120
        }
×
121

122
        // Set state to fully populated data
123
        resp.Diagnostics.Append(resp.State.Set(ctx, result)...)
62✔
124
        if resp.Diagnostics.HasError() {
62✔
NEW
125
                return
×
NEW
126
        }
×
127
}
128

NEW
129
func (r *variableResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
×
NEW
130
        // Read plan & state data
×
NEW
131
        var plan, state variableResourceModel
×
NEW
132
        resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
×
NEW
133
        resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
×
NEW
134
        if resp.Diagnostics.HasError() {
×
NEW
135
                return
×
NEW
136
        }
×
137

NEW
138
        opts := scalr.VariableUpdateOptions{
×
NEW
139
                Key:         plan.Key.ValueStringPointer(),
×
NEW
140
                Value:       plan.Value.ValueStringPointer(),
×
NEW
141
                HCL:         plan.HCL.ValueBoolPointer(),
×
NEW
142
                Sensitive:   plan.Sensitive.ValueBoolPointer(),
×
NEW
143
                Description: plan.Description.ValueStringPointer(),
×
NEW
144
                Final:       plan.Final.ValueBoolPointer(),
×
NEW
145
                QueryOptions: &scalr.VariableWriteQueryOptions{
×
NEW
146
                        Force:   plan.Force.ValueBoolPointer(),
×
NEW
147
                        Include: ptr("updated-by"),
×
NEW
148
                },
×
NEW
149
        }
×
NEW
150

×
NEW
151
        // Update existing resource
×
NEW
152
        variable, err := r.Client.Variables.Update(ctx, plan.Id.ValueString(), opts)
×
NEW
153
        if err != nil {
×
NEW
154
                resp.Diagnostics.AddError("Error updating variable", err.Error())
×
NEW
155
                return
×
NEW
156
        }
×
157

NEW
158
        result, diags := variableResourceModelFromAPI(ctx, variable, &state)
×
NEW
159
        resp.Diagnostics.Append(diags...)
×
NEW
160
        if resp.Diagnostics.HasError() {
×
NEW
161
                return
×
NEW
162
        }
×
163

164
        // Set state to fully populated data
NEW
165
        resp.Diagnostics.Append(resp.State.Set(ctx, result)...)
×
NEW
166
        if resp.Diagnostics.HasError() {
×
NEW
167
                return
×
NEW
168
        }
×
169
}
170

171
func (r *variableResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
18✔
172
        // Get current state
18✔
173
        var state variableResourceModel
18✔
174
        resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
18✔
175
        if resp.Diagnostics.HasError() {
18✔
NEW
176
                return
×
NEW
177
        }
×
178

179
        err := r.Client.Variables.Delete(ctx, state.Id.ValueString())
18✔
180
        if err != nil && !errors.Is(err, scalr.ErrResourceNotFound) {
18✔
NEW
181
                resp.Diagnostics.AddError("Error deleting variable", err.Error())
×
NEW
182
                return
×
NEW
183
        }
×
184
}
185

186
func (r *variableResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
1✔
187
        resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
1✔
188
}
1✔
189

NEW
190
func (r *variableResource) UpgradeState(_ context.Context) map[int64]resource.StateUpgrader {
×
NEW
191
        return map[int64]resource.StateUpgrader{
×
NEW
192
                0: {
×
NEW
193
                        PriorSchema:   variableResourceSchemaV0(),
×
NEW
194
                        StateUpgrader: upgradeVariableResourceStateV0toV3(r.Client),
×
NEW
195
                },
×
NEW
196
                1: {
×
NEW
197
                        PriorSchema:   variableResourceSchemaV1(),
×
NEW
198
                        StateUpgrader: upgradeVariableResourceStateV1toV3(r.Client),
×
199
                },
×
200
                2: {
×
201
                        PriorSchema:   variableResourceSchemaV2(),
×
202
                        StateUpgrader: upgradeVariableResourceStateV2toV3(r.Client),
×
203
                },
×
204
        }
×
205
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc