Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion docs/data-sources/rabbitmq_credential.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@ data "stackit_rabbitmq_credential" "example" {
- `instance_id` (String) ID of the RabbitMQ instance.
- `project_id` (String) STACKIT project ID to which the instance is associated.

### Optional

- `region` (String) The resource region. If not defined, the provider region is used.

### Read-Only

- `host` (String)
- `hosts` (List of String)
- `http_api_uri` (String)
- `http_api_uris` (List of String)
- `id` (String) Terraform's internal data source. identifier. It is structured as "`project_id`,`instance_id`,`credential_id`".
- `id` (String) Terraform's internal data source. identifier. It is structured as "`project_id`,`region`,`instance_id`,`credential_id`".
- `management` (String)
- `password` (String, Sensitive)
- `port` (Number)
Expand Down
6 changes: 5 additions & 1 deletion docs/data-sources/rabbitmq_instance.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@ data "stackit_rabbitmq_instance" "example" {
- `instance_id` (String) ID of the RabbitMQ instance.
- `project_id` (String) STACKIT Project ID to which the instance is associated.

### Optional

- `region` (String) The resource region. If not defined, the provider region is used.

### Read-Only

- `cf_guid` (String)
- `cf_organization_guid` (String)
- `cf_space_guid` (String)
- `dashboard_url` (String)
- `id` (String) Terraform's internal data source. identifier. It is structured as "`project_id`,`instance_id`".
- `id` (String) Terraform's internal data source. identifier. It is structured as "`project_id`,`region`,`instance_id`".
- `image_url` (String)
- `name` (String) Instance name.
- `parameters` (Attributes) (see [below for nested schema](#nestedatt--parameters))
Expand Down
3 changes: 2 additions & 1 deletion docs/resources/rabbitmq_credential.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {

### Optional

- `region` (String) The resource region. If not defined, the provider region is used.
- `rotate_when_changed` (Map of String) A map of arbitrary key/value pairs that will force recreation of the resource when they change, enabling resource rotation based on external conditions such as a rotating timestamp. Changing this forces a new resource to be created.

### Read-Only
Expand All @@ -57,7 +58,7 @@ import {
- `hosts` (List of String)
- `http_api_uri` (String)
- `http_api_uris` (List of String)
- `id` (String) Terraform's internal resource identifier. It is structured as "`project_id`,`instance_id`,`credential_id`".
- `id` (String) Terraform's internal resource identifier. It is structured as "`project_id`,`region`,`instance_id`,`credential_id`".
- `management` (String)
- `password` (String, Sensitive)
- `port` (Number)
Expand Down
3 changes: 2 additions & 1 deletion docs/resources/rabbitmq_instance.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,15 @@ import {
### Optional

- `parameters` (Attributes) Configuration parameters. Please note that removing a previously configured field from your Terraform configuration won't replace its value in the API. To update a previously configured field, explicitly set a new value for it. (see [below for nested schema](#nestedatt--parameters))
- `region` (String) The resource region. If not defined, the provider region is used.

### Read-Only

- `cf_guid` (String)
- `cf_organization_guid` (String)
- `cf_space_guid` (String)
- `dashboard_url` (String)
- `id` (String) Terraform's internal resource ID. It is structured as "`project_id`,`instance_id`".
- `id` (String) Terraform's internal resource ID. It is structured as "`project_id`,`region`,`instance_id`".
- `image_url` (String)
- `instance_id` (String) ID of the RabbitMQ instance.
- `plan_id` (String) The selected plan ID.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ require (
github.com/stackitcloud/stackit-sdk-go/services/observability v0.24.0
github.com/stackitcloud/stackit-sdk-go/services/opensearch v1.0.0
github.com/stackitcloud/stackit-sdk-go/services/postgresflex v1.10.0
github.com/stackitcloud/stackit-sdk-go/services/rabbitmq v1.0.0
github.com/stackitcloud/stackit-sdk-go/services/rabbitmq v1.1.0
github.com/stackitcloud/stackit-sdk-go/services/redis v1.0.0
github.com/stackitcloud/stackit-sdk-go/services/resourcemanager v0.24.0
github.com/stackitcloud/stackit-sdk-go/services/scf v0.9.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -712,8 +712,8 @@ github.com/stackitcloud/stackit-sdk-go/services/opensearch v1.0.0 h1:xJ/rhcMTV2p
github.com/stackitcloud/stackit-sdk-go/services/opensearch v1.0.0/go.mod h1:L+NlfC1hilLOqlLLukCj/UDnxlnNrc/oMikcw3Ansyw=
github.com/stackitcloud/stackit-sdk-go/services/postgresflex v1.10.0 h1:Ia8FWqG14hkl7rWAUDjSKwd+8NPvrPGOpUC0jLB1sR0=
github.com/stackitcloud/stackit-sdk-go/services/postgresflex v1.10.0/go.mod h1:yzlakB+f8ur4yAHR6lyCABO+HcEtZG3G2Faj6m5/uW8=
github.com/stackitcloud/stackit-sdk-go/services/rabbitmq v1.0.0 h1:kHdXzMcYPLRwidoQuNu1U12PmqCBNqpTS+U82rB9HtY=
github.com/stackitcloud/stackit-sdk-go/services/rabbitmq v1.0.0/go.mod h1:TwfVVynB/+AKbccSOLk2qZpPL1tdK43BBAiACP6EtSg=
github.com/stackitcloud/stackit-sdk-go/services/rabbitmq v1.1.0 h1:HRJwodJX4aOn/487zaqJIKw13yIj4T6dn7/kEHLxeTg=
github.com/stackitcloud/stackit-sdk-go/services/rabbitmq v1.1.0/go.mod h1:TwfVVynB/+AKbccSOLk2qZpPL1tdK43BBAiACP6EtSg=
github.com/stackitcloud/stackit-sdk-go/services/redis v1.0.0 h1:JsHchCTVpCVUB0KQyjzlFEwJEdorql6pBpXtqQDjtqU=
github.com/stackitcloud/stackit-sdk-go/services/redis v1.0.0/go.mod h1:yjej6QfYoYdRIyKXlmbVz8fZYxbuUdl+QBkvLDPgA4k=
github.com/stackitcloud/stackit-sdk-go/services/resourcemanager v0.24.0 h1:JPP6a0ME1tZXr4iB69d/LtJsCAr58ENBadFaK9f48/c=
Expand Down
31 changes: 22 additions & 9 deletions stackit/internal/services/rabbitmq/credential/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
rabbitmq "github.com/stackitcloud/stackit-sdk-go/services/rabbitmq/v1api"
rabbitmq "github.com/stackitcloud/stackit-sdk-go/services/rabbitmq/v2api"
)

// Ensure the implementation satisfies the expected interfaces.
Expand All @@ -31,6 +31,7 @@ type DataSourceModel struct {
CredentialId types.String `tfsdk:"credential_id"`
InstanceId types.String `tfsdk:"instance_id"`
ProjectId types.String `tfsdk:"project_id"`
Region types.String `tfsdk:"region"`
Host types.String `tfsdk:"host"`
Hosts types.List `tfsdk:"hosts"`
HttpAPIURI types.String `tfsdk:"http_api_uri"`
Expand All @@ -50,7 +51,8 @@ func NewCredentialDataSource() datasource.DataSource {

// credentialDataSource is the data source implementation.
type credentialDataSource struct {
client *rabbitmq.APIClient
client *rabbitmq.APIClient
providerData core.ProviderData
}

// Metadata returns the data source type name.
Expand All @@ -60,12 +62,13 @@ func (r *credentialDataSource) Metadata(_ context.Context, req datasource.Metada

// Configure adds the provider configured client to the data source.
func (r *credentialDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
providerData, ok := conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics)
var ok bool
r.providerData, ok = conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics)
if !ok {
return
}

apiClient := rabbitmqUtils.ConfigureClient(ctx, &providerData, &resp.Diagnostics)
apiClient := rabbitmqUtils.ConfigureClient(ctx, &r.providerData, &resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
}
Expand All @@ -77,10 +80,11 @@ func (r *credentialDataSource) Configure(ctx context.Context, req datasource.Con
func (r *credentialDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
descriptions := map[string]string{ //nolint:gosec // description for credential id
"main": "RabbitMQ credential data source schema. Must have a `region` specified in the provider configuration.",
"id": "Terraform's internal data source. identifier. It is structured as \"`project_id`,`instance_id`,`credential_id`\".",
"id": "Terraform's internal data source. identifier. It is structured as \"`project_id`,`region`,`instance_id`,`credential_id`\".",
"credential_id": "The credential's ID.",
"instance_id": "ID of the RabbitMQ instance.",
"project_id": "STACKIT project ID to which the instance is associated.",
"region": "The resource region. If not defined, the provider region is used.",
}

resp.Schema = schema.Schema{
Expand Down Expand Up @@ -149,6 +153,12 @@ func (r *credentialDataSource) Schema(_ context.Context, _ datasource.SchemaRequ
"username": schema.StringAttribute{
Computed: true,
},
"region": schema.StringAttribute{
Optional: true,
// must be computed to allow for storing the override value from the provider
Computed: true,
Description: descriptions["region"],
},
},
}
}
Expand All @@ -165,13 +175,15 @@ func (r *credentialDataSource) Read(ctx context.Context, req datasource.ReadRequ
ctx = core.InitProviderContext(ctx)

projectId := model.ProjectId.ValueString()
region := r.providerData.GetRegionWithOverride(model.Region)
instanceId := model.InstanceId.ValueString()
credentialId := model.CredentialId.ValueString()
ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "region", region)
ctx = tflog.SetField(ctx, "instance_id", instanceId)
ctx = tflog.SetField(ctx, "credential_id", credentialId)

recordSetResp, err := r.client.DefaultAPI.GetCredentials(ctx, projectId, instanceId, credentialId).Execute()
recordSetResp, err := r.client.DefaultAPI.GetCredentials(ctx, projectId, region, instanceId, credentialId).Execute()
if err != nil {
utils.LogError(
ctx,
Expand All @@ -190,7 +202,7 @@ func (r *credentialDataSource) Read(ctx context.Context, req datasource.ReadRequ
ctx = core.LogResponse(ctx)

// Map response body to schema
err = mapDataSourceFields(ctx, recordSetResp, &model)
err = mapDataSourceFields(ctx, recordSetResp, &model, region)
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading credential", fmt.Sprintf("Processing API payload: %v", err))
return
Expand All @@ -205,7 +217,7 @@ func (r *credentialDataSource) Read(ctx context.Context, req datasource.ReadRequ
tflog.Info(ctx, "RabbitMQ credential read")
}

func mapDataSourceFields(ctx context.Context, credentialsResp *rabbitmq.CredentialsResponse, model *DataSourceModel) error {
func mapDataSourceFields(ctx context.Context, credentialsResp *rabbitmq.CredentialsResponse, model *DataSourceModel, region string) error {
if credentialsResp == nil {
return fmt.Errorf("response input is nil")
}
Expand All @@ -226,8 +238,9 @@ func mapDataSourceFields(ctx context.Context, credentialsResp *rabbitmq.Credenti
return fmt.Errorf("credentials id not present")
}

model.Region = types.StringValue(region)
model.Id = utils.BuildInternalTerraformId(
model.ProjectId.ValueString(), model.InstanceId.ValueString(), credentialId,
model.ProjectId.ValueString(), model.Region.ValueString(), model.InstanceId.ValueString(), credentialId,
)
model.CredentialId = types.StringValue(credentialId)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ package rabbitmq

import (
"context"
"fmt"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/types"
rabbitmq "github.com/stackitcloud/stackit-sdk-go/services/rabbitmq/v1api"
rabbitmq "github.com/stackitcloud/stackit-sdk-go/services/rabbitmq/v2api"
)

func TestMapDataSourceFields(t *testing.T) {
const testRegion = "eu01"
tests := []struct {
description string
state DataSourceModel
Expand All @@ -29,10 +31,11 @@ func TestMapDataSourceFields(t *testing.T) {
Raw: &rabbitmq.RawCredentials{},
},
DataSourceModel{
Id: types.StringValue("pid,iid,cid"),
Id: types.StringValue(fmt.Sprintf("pid,%s,iid,cid", testRegion)),
CredentialId: types.StringValue("cid"),
InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"),
Region: types.StringValue(testRegion),
Host: types.StringValue(""),
Hosts: types.ListNull(types.StringType),
HttpAPIURI: types.StringNull(),
Expand Down Expand Up @@ -79,10 +82,11 @@ func TestMapDataSourceFields(t *testing.T) {
},
},
DataSourceModel{
Id: types.StringValue("pid,iid,cid"),
Id: types.StringValue(fmt.Sprintf("pid,%s,iid,cid", testRegion)),
CredentialId: types.StringValue("cid"),
InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"),
Region: types.StringValue(testRegion),
Host: types.StringValue("host"),
Hosts: types.ListValueMust(types.StringType, []attr.Value{
types.StringValue("host_1"),
Expand Down Expand Up @@ -156,10 +160,11 @@ func TestMapDataSourceFields(t *testing.T) {
},
},
DataSourceModel{
Id: types.StringValue("pid,iid,cid"),
Id: types.StringValue(fmt.Sprintf("pid,%s,iid,cid", testRegion)),
CredentialId: types.StringValue("cid"),
InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"),
Region: types.StringValue(testRegion),
Host: types.StringValue("host"),
Hosts: types.ListValueMust(types.StringType, []attr.Value{
types.StringValue("host_2"),
Expand Down Expand Up @@ -209,10 +214,11 @@ func TestMapDataSourceFields(t *testing.T) {
},
},
DataSourceModel{
Id: types.StringValue("pid,iid,cid"),
Id: types.StringValue(fmt.Sprintf("pid,%s,iid,cid", testRegion)),
CredentialId: types.StringValue("cid"),
InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"),
Region: types.StringValue(testRegion),
Host: types.StringValue(""),
Hosts: types.ListValueMust(types.StringType, []attr.Value{}),
HttpAPIURI: types.StringNull(),
Expand Down Expand Up @@ -261,7 +267,7 @@ func TestMapDataSourceFields(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
err := mapDataSourceFields(context.Background(), tt.input, &tt.state)
err := mapDataSourceFields(context.Background(), tt.input, &tt.state, testRegion)
if !tt.isValid && err == nil {
t.Fatalf("Should have failed")
}
Expand Down
Loading
Loading