Skip to content

Commit 8942b1e

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: stackitcloud#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 8942b1e

8 files changed

Lines changed: 244 additions & 18 deletions

File tree

docs/data-sources/server.md

Lines changed: 9 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,14 @@ 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+
59+
5160
<a id="nestedatt--boot_volume"></a>
5261
### Nested Schema for `boot_volume`
5362

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: 16 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,7 @@ 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.TestCheckNoResourceAttr("data.stackit_server.server", "agent.provisioned"),
22782281
),
22792282
},
22802283
// Import
@@ -2294,7 +2297,7 @@ func TestAccServerMin(t *testing.T) {
22942297
},
22952298
ImportState: true,
22962299
ImportStateVerify: true,
2297-
ImportStateVerifyIgnore: []string{"boot_volume", "network_interfaces"}, // Field is not mapped as it is only relevant on creation
2300+
ImportStateVerifyIgnore: []string{"boot_volume", "network_interfaces", "agent"}, // Field is not mapped as it is only relevant on creation
22982301
},
22992302
// Update
23002303
{
@@ -2328,6 +2331,8 @@ func TestAccServerMin(t *testing.T) {
23282331
resource.TestCheckResourceAttrSet("stackit_server.server", "created_at"),
23292332
resource.TestCheckResourceAttrSet("stackit_server.server", "launched_at"),
23302333
resource.TestCheckResourceAttrSet("stackit_server.server", "updated_at"),
2334+
resource.TestCheckResourceAttr("stackit_server.server", "agent.provisioning_policy", "INHERIT"),
2335+
resource.TestCheckNoResourceAttr("stackit_server.server", "agent.provisioned"),
23312336
),
23322337
},
23332338
// Deletion is done by the framework implicitly
@@ -2425,6 +2430,10 @@ func TestAccServerMax(t *testing.T) {
24252430
resource.TestCheckResourceAttr("stackit_key_pair.key_pair", "name", testutil.ConvertConfigVariable(testConfigServerVarsMax["name_not_updated"])),
24262431
resource.TestCheckResourceAttr("stackit_key_pair.key_pair", "public_key", testutil.ConvertConfigVariable(testConfigServerVarsMax["public_key"])),
24272432

2433+
// Agent
2434+
resource.TestCheckResourceAttr("stackit_server.server", "agent.provisioning_policy", "ALWAYS"),
2435+
resource.TestCheckResourceAttr("stackit_server.server", "agent.provisioned", "true"),
2436+
24282437
// Service account attachment
24292438
resource.TestCheckResourceAttrPair(
24302439
"stackit_server_service_account_attach.attached_service_account", "project_id",
@@ -2500,6 +2509,7 @@ func TestAccServerMax(t *testing.T) {
25002509
"stackit_key_pair.key_pair", "name",
25012510
"data.stackit_server.server", "keypair_name",
25022511
),
2512+
resource.TestCheckResourceAttr("data.stackit_server.server", "agent.provisioned", "true"),
25032513
// All network interface which was are attached appear here
25042514
resource.TestCheckResourceAttr("data.stackit_server.server", "network_interfaces.#", "2"),
25052515
resource.TestCheckTypeSetElemAttrPair(
@@ -2787,6 +2797,11 @@ func TestAccServerMax(t *testing.T) {
27872797
resource.TestCheckResourceAttr("stackit_key_pair.key_pair", "name", testutil.ConvertConfigVariable(testConfigServerVarsMaxUpdated["name_not_updated"])),
27882798
resource.TestCheckResourceAttr("stackit_key_pair.key_pair", "public_key", testutil.ConvertConfigVariable(testConfigServerVarsMaxUpdated["public_key"])),
27892799

2800+
// Agent
2801+
resource.TestCheckResourceAttr("stackit_server.server", "agent.provisioning_policy", "NEVER"),
2802+
resource.TestCheckResourceAttr("stackit_server.server", "agent.provisioned", "false"),
2803+
resource.TestCheckResourceAttrSet("stackit_server.server", "server_id"),
2804+
27902805
// Service account attachment
27912806
resource.TestCheckResourceAttrPair(
27922807
"stackit_server_service_account_attach.attached_service_account", "project_id",

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

Lines changed: 34 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,10 @@ 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+
}
60+
5661
// NewServerDataSource is a helper function to simplify the provider implementation.
5762
func NewServerDataSource() datasource.DataSource {
5863
return &serverDataSource{}
@@ -124,6 +129,16 @@ func (d *serverDataSource) Schema(_ context.Context, _ datasource.SchemaRequest,
124129
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/)",
125130
Computed: true,
126131
},
132+
"agent": schema.SingleNestedAttribute{
133+
Description: "STACKIT Server Agent as setup on the server",
134+
Computed: true,
135+
Attributes: map[string]schema.Attribute{
136+
"provisioned": schema.BoolAttribute{
137+
Description: "Whether a STACKIT Server Agent is provisioned at the server",
138+
Computed: true,
139+
},
140+
},
141+
},
127142
"availability_zone": schema.StringAttribute{
128143
Description: "The availability zone of the server.",
129144
Computed: true,
@@ -305,6 +320,25 @@ func mapDataSourceFields(ctx context.Context, serverResp *iaas.Server, model *Da
305320
model.BootVolume = types.ObjectNull(bootVolumeDataTypes)
306321
}
307322

323+
if serverResp.Agent != nil && serverResp.Agent.Provisioned != nil {
324+
agent, diags := types.ObjectValue(agentDataTypes, map[string]attr.Value{
325+
"provisioned": types.BoolPointerValue(serverResp.Agent.Provisioned),
326+
})
327+
if diags.HasError() {
328+
return fmt.Errorf("failed to map agent: %w", core.DiagsToError(diags))
329+
}
330+
model.Agent = agent
331+
} else {
332+
// return a null object so user's code like 'agent.provisioned' doesn't crash
333+
agent, diags := types.ObjectValue(agentDataTypes, map[string]attr.Value{
334+
"provisioned": types.BoolNull(),
335+
})
336+
if diags.HasError() {
337+
return fmt.Errorf("failed to map agent (null case): %w", core.DiagsToError(diags))
338+
}
339+
model.Agent = agent
340+
}
341+
308342
if serverResp.UserData != nil && len(*serverResp.UserData) > 0 {
309343
model.UserData = types.StringValue(string(*serverResp.UserData))
310344
}

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

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@ 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 expectedNullAgentData, _ = types.ObjectValue(agentDataTypes, map[string]attr.Value{
15+
"provisioned": types.BoolNull(),
16+
})
17+
1318
func TestMapDataSourceFields(t *testing.T) {
1419
type args struct {
1520
state DataSourceModel
@@ -40,6 +45,7 @@ func TestMapDataSourceFields(t *testing.T) {
4045
ServerId: types.StringValue("sid"),
4146
Name: types.StringNull(),
4247
AvailabilityZone: types.StringNull(),
48+
Agent: expectedNullAgentData,
4349
Labels: types.MapNull(types.StringType),
4450
ImageId: types.StringNull(),
4551
NetworkInterfaces: types.ListNull(types.StringType),
@@ -77,12 +83,15 @@ func TestMapDataSourceFields(t *testing.T) {
7783
NicId: new("nic2"),
7884
},
7985
},
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"),
86+
KeypairName: utils.Ptr("keypair_name"),
87+
Agent: &iaas.ServerAgent{
88+
Provisioned: utils.Ptr(true),
89+
},
90+
AffinityGroup: utils.Ptr("group_id"),
91+
CreatedAt: utils.Ptr(testTimestamp()),
92+
UpdatedAt: utils.Ptr(testTimestamp()),
93+
LaunchedAt: utils.Ptr(testTimestamp()),
94+
Status: utils.Ptr("active"),
8695
},
8796
region: "eu02",
8897
},
@@ -100,7 +109,10 @@ func TestMapDataSourceFields(t *testing.T) {
100109
types.StringValue("nic1"),
101110
types.StringValue("nic2"),
102111
}),
103-
KeypairName: types.StringValue("keypair_name"),
112+
KeypairName: types.StringValue("keypair_name"),
113+
Agent: types.ObjectValueMust(agentDataTypes, map[string]attr.Value{
114+
"provisioned": types.BoolValue(true),
115+
}),
104116
AffinityGroup: types.StringValue("group_id"),
105117
CreatedAt: types.StringValue(testTimestampValue),
106118
UpdatedAt: types.StringValue(testTimestampValue),
@@ -131,6 +143,7 @@ func TestMapDataSourceFields(t *testing.T) {
131143
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{}),
132144
ImageId: types.StringNull(),
133145
NetworkInterfaces: types.ListNull(types.StringType),
146+
Agent: expectedNullAgentData,
134147
KeypairName: types.StringNull(),
135148
AffinityGroup: types.StringNull(),
136149
UserData: types.StringNull(),

0 commit comments

Comments
 (0)