Skip to content

Commit 7c34d59

Browse files
Adrian Nackovaeter
authored andcommitted
STACKITRCO-187 - Add option iaas API param agent
Adds a terraform `stackit_server` option for the iaas ( _create server_ ) API param: `"agent": {"provisioned": true}` ref STACKITRCO-187 ref: stackitcloud/stackit-cli#1213 review url: #1113 --- Tests: * ran `make fmt`, `make generate-docs`, `make lint` * ran unit tests ``` [~/terraform-provider-stackit] go test stackit/internal/services/iaas/server/* ok command-line-arguments 15.005s [~/terraform-provider-stackit] make test ... ok github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/server 15.006s coverage: 33.0% of statements ... [~/terraform-provider-stackit] ``` * Tested: with a locally-configured terraform, by adding, changing, deleting `agent`-related parts of a tf config. Signed-off-by: Adrian Nackov <adrian.nackov@digits.schwarz>
1 parent cb9405d commit 7c34d59

File tree

8 files changed

+251
-18
lines changed

8 files changed

+251
-18
lines changed

docs/data-sources/server.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ data "stackit_server" "example" {
3434
### Read-Only
3535

3636
- `affinity_group` (String) The affinity group the server is assigned to.
37+
- `agent` (Attributes) STACKIT Server Agent as setup on the server (see [below for nested schema](#nestedatt--agent))
3738
- `availability_zone` (String) The availability zone of the server.
3839
- `boot_volume` (Attributes) The boot volume for the server (see [below for nested schema](#nestedatt--boot_volume))
3940
- `created_at` (String) Date-time when the server was created
@@ -48,6 +49,15 @@ data "stackit_server" "example" {
4849
- `updated_at` (String) Date-time when the server was updated
4950
- `user_data` (String) User data that is passed via cloud-init to the server.
5051

52+
<a id="nestedatt--agent"></a>
53+
### Nested Schema for `agent`
54+
55+
Read-Only:
56+
57+
- `provisioned` (Boolean) Whether a STACKIT Server Agent is provisioned at the server
58+
- `provisioning_policy` (String) The provisioning policy. In a data source, this defaults to "INHERIT" as the API does not provide this value.
59+
60+
5161
<a id="nestedatt--boot_volume"></a>
5262
### Nested Schema for `boot_volume`
5363

docs/resources/server.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@ import {
404404
### Optional
405405

406406
- `affinity_group` (String) The affinity group the server is assigned to.
407+
- `agent` (Attributes) The STACKIT Server Agent configured for the server (see [below for nested schema](#nestedatt--agent))
407408
- `availability_zone` (String) The availability zone of the server.
408409
- `boot_volume` (Attributes) The boot volume for the server (see [below for nested schema](#nestedatt--boot_volume))
409410
- `desired_status` (String) The desired status of the server resource. Possible values are: `active`, `inactive`, `deallocated`.
@@ -422,6 +423,18 @@ import {
422423
- `server_id` (String) The server ID.
423424
- `updated_at` (String) Date-time when the server was updated
424425

426+
<a id="nestedatt--agent"></a>
427+
### Nested Schema for `agent`
428+
429+
Optional:
430+
431+
- `provisioning_policy` (String) Agent provisioning policy: "ALWAYS", "NEVER", or "INHERIT". "INHERIT" follows the image default value.
432+
433+
Read-Only:
434+
435+
- `provisioned` (Boolean) Whether a STACKIT Server Agent should be provisioned at the server
436+
437+
425438
<a id="nestedatt--boot_volume"></a>
426439
### Nested Schema for `boot_volume`
427440

stackit/internal/services/iaas/iaas_acc_test.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2223,6 +2223,8 @@ func TestAccServerMin(t *testing.T) {
22232223
resource.TestCheckResourceAttrSet("stackit_server.server", "created_at"),
22242224
resource.TestCheckResourceAttrSet("stackit_server.server", "launched_at"),
22252225
resource.TestCheckResourceAttrSet("stackit_server.server", "updated_at"),
2226+
resource.TestCheckResourceAttr("stackit_server.server", "agent.provisioning_policy", "INHERIT"),
2227+
resource.TestCheckNoResourceAttr("stackit_server.server", "agent.provisioned"),
22262228
),
22272229
},
22282230
// Data source
@@ -2275,6 +2277,8 @@ func TestAccServerMin(t *testing.T) {
22752277
resource.TestCheckResourceAttrSet("data.stackit_server.server", "created_at"),
22762278
resource.TestCheckResourceAttrSet("data.stackit_server.server", "launched_at"),
22772279
resource.TestCheckResourceAttrSet("data.stackit_server.server", "updated_at"),
2280+
resource.TestCheckResourceAttr("data.stackit_server.server", "agent.provisioning_policy", "INHERIT"),
2281+
resource.TestCheckNoResourceAttr("data.stackit_server.server", "agent.provisioned"),
22782282
),
22792283
},
22802284
// Import
@@ -2294,7 +2298,7 @@ func TestAccServerMin(t *testing.T) {
22942298
},
22952299
ImportState: true,
22962300
ImportStateVerify: true,
2297-
ImportStateVerifyIgnore: []string{"boot_volume", "network_interfaces"}, // Field is not mapped as it is only relevant on creation
2301+
ImportStateVerifyIgnore: []string{"boot_volume", "network_interfaces", "agent"}, // Field is not mapped as it is only relevant on creation
22982302
},
22992303
// Update
23002304
{
@@ -2328,6 +2332,8 @@ func TestAccServerMin(t *testing.T) {
23282332
resource.TestCheckResourceAttrSet("stackit_server.server", "created_at"),
23292333
resource.TestCheckResourceAttrSet("stackit_server.server", "launched_at"),
23302334
resource.TestCheckResourceAttrSet("stackit_server.server", "updated_at"),
2335+
resource.TestCheckResourceAttr("stackit_server.server", "agent.provisioning_policy", "INHERIT"),
2336+
resource.TestCheckNoResourceAttr("stackit_server.server", "agent.provisioned"),
23312337
),
23322338
},
23332339
// Deletion is done by the framework implicitly
@@ -2425,6 +2431,10 @@ func TestAccServerMax(t *testing.T) {
24252431
resource.TestCheckResourceAttr("stackit_key_pair.key_pair", "name", testutil.ConvertConfigVariable(testConfigServerVarsMax["name_not_updated"])),
24262432
resource.TestCheckResourceAttr("stackit_key_pair.key_pair", "public_key", testutil.ConvertConfigVariable(testConfigServerVarsMax["public_key"])),
24272433

2434+
// Agent
2435+
resource.TestCheckResourceAttr("stackit_server.server", "agent.provisioning_policy", "ALWAYS"),
2436+
resource.TestCheckResourceAttr("stackit_server.server", "agent.provisioned", "true"),
2437+
24282438
// Service account attachment
24292439
resource.TestCheckResourceAttrPair(
24302440
"stackit_server_service_account_attach.attached_service_account", "project_id",
@@ -2500,6 +2510,8 @@ func TestAccServerMax(t *testing.T) {
25002510
"stackit_key_pair.key_pair", "name",
25012511
"data.stackit_server.server", "keypair_name",
25022512
),
2513+
resource.TestCheckResourceAttr("data.stackit_server.server", "agent.provisioned", "true"),
2514+
resource.TestCheckResourceAttr("data.stackit_server.server", "agent.provisioning_policy", "INHERIT"),
25032515
// All network interface which was are attached appear here
25042516
resource.TestCheckResourceAttr("data.stackit_server.server", "network_interfaces.#", "2"),
25052517
resource.TestCheckTypeSetElemAttrPair(
@@ -2787,6 +2799,11 @@ func TestAccServerMax(t *testing.T) {
27872799
resource.TestCheckResourceAttr("stackit_key_pair.key_pair", "name", testutil.ConvertConfigVariable(testConfigServerVarsMaxUpdated["name_not_updated"])),
27882800
resource.TestCheckResourceAttr("stackit_key_pair.key_pair", "public_key", testutil.ConvertConfigVariable(testConfigServerVarsMaxUpdated["public_key"])),
27892801

2802+
// Agent
2803+
resource.TestCheckResourceAttr("stackit_server.server", "agent.provisioning_policy", "NEVER"),
2804+
resource.TestCheckResourceAttr("stackit_server.server", "agent.provisioned", "false"),
2805+
resource.TestCheckResourceAttrSet("stackit_server.server", "server_id"),
2806+
27902807
// Service account attachment
27912808
resource.TestCheckResourceAttrPair(
27922809
"stackit_server_service_account_attach.attached_service_account", "project_id",

stackit/internal/services/iaas/server/datasource.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type DataSourceModel struct {
3535
ServerId types.String `tfsdk:"server_id"`
3636
MachineType types.String `tfsdk:"machine_type"`
3737
Name types.String `tfsdk:"name"`
38+
Agent types.Object `tfsdk:"agent"`
3839
AvailabilityZone types.String `tfsdk:"availability_zone"`
3940
BootVolume types.Object `tfsdk:"boot_volume"`
4041
ImageId types.String `tfsdk:"image_id"`
@@ -53,6 +54,11 @@ var bootVolumeDataTypes = map[string]attr.Type{
5354
"delete_on_termination": basetypes.BoolType{},
5455
}
5556

57+
var agentDataTypes = map[string]attr.Type{
58+
"provisioned": basetypes.BoolType{},
59+
"provisioning_policy": basetypes.StringType{},
60+
}
61+
5662
// NewServerDataSource is a helper function to simplify the provider implementation.
5763
func NewServerDataSource() datasource.DataSource {
5864
return &serverDataSource{}
@@ -124,6 +130,20 @@ func (d *serverDataSource) Schema(_ context.Context, _ datasource.SchemaRequest,
124130
MarkdownDescription: "Name of the type of the machine for the server. Possible values are documented in [Virtual machine flavors](https://docs.stackit.cloud/products/compute-engine/server/basics/machine-types/)",
125131
Computed: true,
126132
},
133+
"agent": schema.SingleNestedAttribute{
134+
Description: "STACKIT Server Agent as setup on the server",
135+
Computed: true,
136+
Attributes: map[string]schema.Attribute{
137+
"provisioned": schema.BoolAttribute{
138+
Description: "Whether a STACKIT Server Agent is provisioned at the server",
139+
Computed: true,
140+
},
141+
"provisioning_policy": schema.StringAttribute{
142+
Description: "The provisioning policy. In a data source, this defaults to \"INHERIT\" as the API does not provide this value.",
143+
Computed: true,
144+
},
145+
},
146+
},
127147
"availability_zone": schema.StringAttribute{
128148
Description: "The availability zone of the server.",
129149
Computed: true,
@@ -305,6 +325,27 @@ func mapDataSourceFields(ctx context.Context, serverResp *iaas.Server, model *Da
305325
model.BootVolume = types.ObjectNull(bootVolumeDataTypes)
306326
}
307327

328+
if serverResp.Agent != nil && serverResp.Agent.Provisioned != nil {
329+
agent, diags := types.ObjectValue(agentDataTypes, map[string]attr.Value{
330+
"provisioned": types.BoolPointerValue(serverResp.Agent.Provisioned),
331+
"provisioning_policy": types.StringValue("INHERIT"),
332+
})
333+
if diags.HasError() {
334+
return fmt.Errorf("failed to map agent: %w", core.DiagsToError(diags))
335+
}
336+
model.Agent = agent
337+
} else {
338+
// return a null object so user's code like 'agent.provisioned' doesn't crash
339+
agent, diags := types.ObjectValue(agentDataTypes, map[string]attr.Value{
340+
"provisioned": types.BoolNull(),
341+
"provisioning_policy": types.StringValue("INHERIT"),
342+
})
343+
if diags.HasError() {
344+
return fmt.Errorf("failed to map agent (null case): %w", core.DiagsToError(diags))
345+
}
346+
model.Agent = agent
347+
}
348+
308349
if serverResp.UserData != nil && len(*serverResp.UserData) > 0 {
309350
model.UserData = types.StringValue(string(*serverResp.UserData))
310351
}

stackit/internal/services/iaas/server/datasource_test.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,15 @@ import (
77
"github.com/google/go-cmp/cmp"
88
"github.com/hashicorp/terraform-plugin-framework/attr"
99
"github.com/hashicorp/terraform-plugin-framework/types"
10+
"github.com/stackitcloud/stackit-sdk-go/core/utils"
1011
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
1112
)
1213

14+
var expectedNullAgent, _ = types.ObjectValue(agentDataTypes, map[string]attr.Value{
15+
"provisioned": types.BoolNull(),
16+
"provisioning_policy": types.StringValue("INHERIT"),
17+
})
18+
1319
func TestMapDataSourceFields(t *testing.T) {
1420
type args struct {
1521
state DataSourceModel
@@ -40,6 +46,7 @@ func TestMapDataSourceFields(t *testing.T) {
4046
ServerId: types.StringValue("sid"),
4147
Name: types.StringNull(),
4248
AvailabilityZone: types.StringNull(),
49+
Agent: expectedNullAgent,
4350
Labels: types.MapNull(types.StringType),
4451
ImageId: types.StringNull(),
4552
NetworkInterfaces: types.ListNull(types.StringType),
@@ -77,12 +84,15 @@ func TestMapDataSourceFields(t *testing.T) {
7784
NicId: new("nic2"),
7885
},
7986
},
80-
KeypairName: new("keypair_name"),
81-
AffinityGroup: new("group_id"),
82-
CreatedAt: new(testTimestamp()),
83-
UpdatedAt: new(testTimestamp()),
84-
LaunchedAt: new(testTimestamp()),
85-
Status: new("active"),
87+
KeypairName: utils.Ptr("keypair_name"),
88+
Agent: &iaas.ServerAgent{
89+
Provisioned: utils.Ptr(true),
90+
},
91+
AffinityGroup: utils.Ptr("group_id"),
92+
CreatedAt: utils.Ptr(testTimestamp()),
93+
UpdatedAt: utils.Ptr(testTimestamp()),
94+
LaunchedAt: utils.Ptr(testTimestamp()),
95+
Status: utils.Ptr("active"),
8696
},
8797
region: "eu02",
8898
},
@@ -100,7 +110,11 @@ func TestMapDataSourceFields(t *testing.T) {
100110
types.StringValue("nic1"),
101111
types.StringValue("nic2"),
102112
}),
103-
KeypairName: types.StringValue("keypair_name"),
113+
KeypairName: types.StringValue("keypair_name"),
114+
Agent: types.ObjectValueMust(agentTypes, map[string]attr.Value{
115+
"provisioned": types.BoolValue(true),
116+
"provisioning_policy": types.StringValue("INHERIT"),
117+
}),
104118
AffinityGroup: types.StringValue("group_id"),
105119
CreatedAt: types.StringValue(testTimestampValue),
106120
UpdatedAt: types.StringValue(testTimestampValue),
@@ -131,6 +145,7 @@ func TestMapDataSourceFields(t *testing.T) {
131145
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{}),
132146
ImageId: types.StringNull(),
133147
NetworkInterfaces: types.ListNull(types.StringType),
148+
Agent: expectedNullAgent,
134149
KeypairName: types.StringNull(),
135150
AffinityGroup: types.StringNull(),
136151
UserData: types.StringNull(),

0 commit comments

Comments
 (0)