Rename credentials_id field to credential_id (#80)

* Rename credentials_id field to credential_id

* Address review comments
This commit is contained in:
João Palet 2023-10-12 15:57:05 +02:00 committed by GitHub
parent b02db190bf
commit 5a5ac6640c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
43 changed files with 722 additions and 722 deletions

View file

@ -97,7 +97,7 @@ import {
} }
import { import {
id = "project_id,instance_id,credentials_id" id = "project_id,instance_id,credential_id"
to = stackit_logme_credential.example-credential to = stackit_logme_credential.example-credential
} }
``` ```
@ -119,7 +119,7 @@ resource "stackit_logme_instance" "example-instance" {
version = null version = null
} }
# __generated__ by Terraform from "project_id,instance_id,credentials_id" # __generated__ by Terraform from "project_id,instance_id,credential_id"
resource "stackit_logme_credential" "example-credential" { resource "stackit_logme_credential" "example-credential" {
instance_id = "instance_id" instance_id = "instance_id"
project_id = "project_id" project_id = "project_id"

View file

@ -14,9 +14,9 @@ LogMe credential data source schema.
```terraform ```terraform
data "stackit_logme_credential" "example" { data "stackit_logme_credential" "example" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
instance_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" instance_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
credentials_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" credential_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
} }
``` ```
@ -25,7 +25,7 @@ data "stackit_logme_credential" "example" {
### Required ### Required
- `credentials_id` (String) The credentials ID. - `credential_id` (String) The credential's ID.
- `instance_id` (String) ID of the LogMe instance. - `instance_id` (String) ID of the LogMe instance.
- `project_id` (String) STACKIT project ID to which the instance is associated. - `project_id` (String) STACKIT project ID to which the instance is associated.
@ -34,7 +34,7 @@ data "stackit_logme_credential" "example" {
- `host` (String) - `host` (String)
- `hosts` (List of String) - `hosts` (List of String)
- `http_api_uri` (String) - `http_api_uri` (String)
- `id` (String) Terraform's internal data source. identifier. It is structured as "`project_id`,`instance_id`,`credentials_id`". - `id` (String) Terraform's internal data source. identifier. It is structured as "`project_id`,`instance_id`,`credential_id`".
- `name` (String) - `name` (String)
- `password` (String, Sensitive) - `password` (String, Sensitive)
- `port` (Number) - `port` (Number)

View file

@ -14,9 +14,9 @@ MariaDB credential data source schema.
```terraform ```terraform
data "stackit_mariadb_credential" "example" { data "stackit_mariadb_credential" "example" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
instance_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" instance_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
credentials_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" credential_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
} }
``` ```
@ -25,7 +25,7 @@ data "stackit_mariadb_credential" "example" {
### Required ### Required
- `credentials_id` (String) The credentials ID. - `credential_id` (String) The credential's ID.
- `instance_id` (String) ID of the MariaDB instance. - `instance_id` (String) ID of the MariaDB instance.
- `project_id` (String) STACKIT project ID to which the instance is associated. - `project_id` (String) STACKIT project ID to which the instance is associated.
@ -34,7 +34,7 @@ data "stackit_mariadb_credential" "example" {
- `host` (String) - `host` (String)
- `hosts` (List of String) - `hosts` (List of String)
- `http_api_uri` (String) - `http_api_uri` (String)
- `id` (String) Terraform's internal data source. identifier. It is structured as "`project_id`,`instance_id`,`credentials_id`". - `id` (String) Terraform's internal data source. identifier. It is structured as "`project_id`,`instance_id`,`credential_id`".
- `name` (String) - `name` (String)
- `password` (String, Sensitive) - `password` (String, Sensitive)
- `port` (Number) - `port` (Number)

View file

@ -14,9 +14,9 @@ OpenSearch credential data source schema.
```terraform ```terraform
data "stackit_opensearch_credential" "example" { data "stackit_opensearch_credential" "example" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
instance_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" instance_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
credentials_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" credential_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
} }
``` ```
@ -25,7 +25,7 @@ data "stackit_opensearch_credential" "example" {
### Required ### Required
- `credentials_id` (String) The credentials ID. - `credential_id` (String) The credential's ID.
- `instance_id` (String) ID of the OpenSearch instance. - `instance_id` (String) ID of the OpenSearch instance.
- `project_id` (String) STACKIT project ID to which the instance is associated. - `project_id` (String) STACKIT project ID to which the instance is associated.
@ -34,7 +34,7 @@ data "stackit_opensearch_credential" "example" {
- `host` (String) - `host` (String)
- `hosts` (List of String) - `hosts` (List of String)
- `http_api_uri` (String) - `http_api_uri` (String)
- `id` (String) Terraform's internal data source. identifier. It is structured as "`project_id`,`instance_id`,`credentials_id`". - `id` (String) Terraform's internal data source. identifier. It is structured as "`project_id`,`instance_id`,`credential_id`".
- `name` (String) - `name` (String)
- `password` (String, Sensitive) - `password` (String, Sensitive)
- `port` (Number) - `port` (Number)

View file

@ -14,9 +14,9 @@ PostgreSQL credential data source schema.
```terraform ```terraform
data "stackit_postgresql_credential" "example" { data "stackit_postgresql_credential" "example" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
instance_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" instance_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
credentials_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" credential_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
} }
``` ```
@ -25,7 +25,7 @@ data "stackit_postgresql_credential" "example" {
### Required ### Required
- `credentials_id` (String) The credentials ID. - `credential_id` (String) The credential's ID.
- `instance_id` (String) ID of the PostgreSQL instance. - `instance_id` (String) ID of the PostgreSQL instance.
- `project_id` (String) STACKIT project ID to which the instance is associated. - `project_id` (String) STACKIT project ID to which the instance is associated.
@ -34,7 +34,7 @@ data "stackit_postgresql_credential" "example" {
- `host` (String) - `host` (String)
- `hosts` (List of String) - `hosts` (List of String)
- `http_api_uri` (String) - `http_api_uri` (String)
- `id` (String) Terraform's internal data source. identifier. It is structured as "`project_id`,`instance_id`,`credentials_id`". - `id` (String) Terraform's internal data source. identifier. It is structured as "`project_id`,`instance_id`,`credential_id`".
- `name` (String) - `name` (String)
- `password` (String, Sensitive) - `password` (String, Sensitive)
- `port` (Number) - `port` (Number)

View file

@ -14,9 +14,9 @@ RabbitMQ credential data source schema.
```terraform ```terraform
data "stackit_rabbitmq_credential" "example" { data "stackit_rabbitmq_credential" "example" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
instance_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" instance_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
credentials_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" credential_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
} }
``` ```
@ -25,7 +25,7 @@ data "stackit_rabbitmq_credential" "example" {
### Required ### Required
- `credentials_id` (String) The credentials ID. - `credential_id` (String) The credential's ID.
- `instance_id` (String) ID of the RabbitMQ instance. - `instance_id` (String) ID of the RabbitMQ instance.
- `project_id` (String) STACKIT project ID to which the instance is associated. - `project_id` (String) STACKIT project ID to which the instance is associated.
@ -34,7 +34,7 @@ data "stackit_rabbitmq_credential" "example" {
- `host` (String) - `host` (String)
- `hosts` (List of String) - `hosts` (List of String)
- `http_api_uri` (String) - `http_api_uri` (String)
- `id` (String) Terraform's internal data source. identifier. It is structured as "`project_id`,`instance_id`,`credentials_id`". - `id` (String) Terraform's internal data source. identifier. It is structured as "`project_id`,`instance_id`,`credential_id`".
- `name` (String) - `name` (String)
- `password` (String, Sensitive) - `password` (String, Sensitive)
- `port` (Number) - `port` (Number)

View file

@ -14,9 +14,9 @@ Redis credential data source schema.
```terraform ```terraform
data "stackit_redis_credential" "example" { data "stackit_redis_credential" "example" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
instance_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" instance_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
credentials_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" credential_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
} }
``` ```
@ -25,7 +25,7 @@ data "stackit_redis_credential" "example" {
### Required ### Required
- `credentials_id` (String) The credentials ID. - `credential_id` (String) The credential's ID.
- `instance_id` (String) ID of the Redis instance. - `instance_id` (String) ID of the Redis instance.
- `project_id` (String) STACKIT project ID to which the instance is associated. - `project_id` (String) STACKIT project ID to which the instance is associated.
@ -34,7 +34,7 @@ data "stackit_redis_credential" "example" {
- `host` (String) - `host` (String)
- `hosts` (List of String) - `hosts` (List of String)
- `http_api_uri` (String) - `http_api_uri` (String)
- `id` (String) Terraform's internal data source. identifier. It is structured as "`project_id`,`instance_id`,`credentials_id`". - `id` (String) Terraform's internal data source. identifier. It is structured as "`project_id`,`instance_id`,`credential_id`".
- `name` (String) - `name` (String)
- `password` (String, Sensitive) - `password` (String, Sensitive)
- `port` (Number) - `port` (Number)

View file

@ -29,11 +29,11 @@ resource "stackit_logme_credential" "example" {
### Read-Only ### Read-Only
- `credentials_id` (String) The credentials ID. - `credential_id` (String) The credential's ID.
- `host` (String) - `host` (String)
- `hosts` (List of String) - `hosts` (List of String)
- `http_api_uri` (String) - `http_api_uri` (String)
- `id` (String) Terraform's internal resource identifier. It is structured as "`project_id`,`instance_id`,`credentials_id`". - `id` (String) Terraform's internal resource identifier. It is structured as "`project_id`,`instance_id`,`credential_id`".
- `name` (String) - `name` (String)
- `password` (String, Sensitive) - `password` (String, Sensitive)
- `port` (Number) - `port` (Number)

View file

@ -29,11 +29,11 @@ resource "stackit_mariadb_credential" "example" {
### Read-Only ### Read-Only
- `credentials_id` (String) The credentials ID. - `credential_id` (String) The credential's ID.
- `host` (String) - `host` (String)
- `hosts` (List of String) - `hosts` (List of String)
- `http_api_uri` (String) - `http_api_uri` (String)
- `id` (String) Terraform's internal resource identifier. It is structured as "`project_id`,`instance_id`,`credentials_id`". - `id` (String) Terraform's internal resource identifier. It is structured as "`project_id`,`instance_id`,`credential_id`".
- `name` (String) - `name` (String)
- `password` (String, Sensitive) - `password` (String, Sensitive)
- `port` (Number) - `port` (Number)

View file

@ -29,11 +29,11 @@ resource "stackit_opensearch_credential" "example" {
### Read-Only ### Read-Only
- `credentials_id` (String) The credentials ID. - `credential_id` (String) The credential's ID.
- `host` (String) - `host` (String)
- `hosts` (List of String) - `hosts` (List of String)
- `http_api_uri` (String) - `http_api_uri` (String)
- `id` (String) Terraform's internal resource identifier. It is structured as "`project_id`,`instance_id`,`credentials_id`". - `id` (String) Terraform's internal resource identifier. It is structured as "`project_id`,`instance_id`,`credential_id`".
- `name` (String) - `name` (String)
- `password` (String, Sensitive) - `password` (String, Sensitive)
- `port` (Number) - `port` (Number)

View file

@ -29,11 +29,11 @@ resource "stackit_postgresql_credential" "example" {
### Read-Only ### Read-Only
- `credentials_id` (String) The credentials ID. - `credential_id` (String) The credential's ID.
- `host` (String) - `host` (String)
- `hosts` (List of String) - `hosts` (List of String)
- `http_api_uri` (String) - `http_api_uri` (String)
- `id` (String) Terraform's internal resource identifier. It is structured as "`project_id`,`instance_id`,`credentials_id`". - `id` (String) Terraform's internal resource identifier. It is structured as "`project_id`,`instance_id`,`credential_id`".
- `name` (String) - `name` (String)
- `password` (String, Sensitive) - `password` (String, Sensitive)
- `port` (Number) - `port` (Number)

View file

@ -29,11 +29,11 @@ resource "stackit_rabbitmq_credential" "example" {
### Read-Only ### Read-Only
- `credentials_id` (String) The credentials ID. - `credential_id` (String) The credential's ID.
- `host` (String) - `host` (String)
- `hosts` (List of String) - `hosts` (List of String)
- `http_api_uri` (String) - `http_api_uri` (String)
- `id` (String) Terraform's internal resource identifier. It is structured as "`project_id`,`instance_id`,`credentials_id`". - `id` (String) Terraform's internal resource identifier. It is structured as "`project_id`,`instance_id`,`credential_id`".
- `name` (String) - `name` (String)
- `password` (String, Sensitive) - `password` (String, Sensitive)
- `port` (Number) - `port` (Number)

View file

@ -29,11 +29,11 @@ resource "stackit_redis_credential" "example" {
### Read-Only ### Read-Only
- `credentials_id` (String) The credentials ID. - `credential_id` (String) The credential's ID.
- `host` (String) - `host` (String)
- `hosts` (List of String) - `hosts` (List of String)
- `http_api_uri` (String) - `http_api_uri` (String)
- `id` (String) Terraform's internal resource identifier. It is structured as "`project_id`,`instance_id`,`credentials_id`". - `id` (String) Terraform's internal resource identifier. It is structured as "`project_id`,`instance_id`,`credential_id`".
- `name` (String) - `name` (String)
- `password` (String, Sensitive) - `password` (String, Sensitive)
- `port` (Number) - `port` (Number)

View file

@ -1,5 +1,5 @@
data "stackit_logme_credential" "example" { data "stackit_logme_credential" "example" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
instance_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" instance_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
credentials_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" credential_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
} }

View file

@ -1,5 +1,5 @@
data "stackit_mariadb_credential" "example" { data "stackit_mariadb_credential" "example" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
instance_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" instance_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
credentials_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" credential_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
} }

View file

@ -1,5 +1,5 @@
data "stackit_opensearch_credential" "example" { data "stackit_opensearch_credential" "example" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
instance_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" instance_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
credentials_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" credential_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
} }

View file

@ -1,5 +1,5 @@
data "stackit_postgresql_credential" "example" { data "stackit_postgresql_credential" "example" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
instance_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" instance_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
credentials_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" credential_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
} }

View file

@ -1,5 +1,5 @@
data "stackit_rabbitmq_credential" "example" { data "stackit_rabbitmq_credential" "example" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
instance_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" instance_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
credentials_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" credential_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
} }

View file

@ -1,5 +1,5 @@
data "stackit_redis_credential" "example" { data "stackit_redis_credential" "example" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
instance_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" instance_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
credentials_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" credential_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
} }

View file

@ -69,17 +69,17 @@ func (r *credentialDataSource) Configure(ctx context.Context, req datasource.Con
} }
r.client = apiClient r.client = apiClient
tflog.Info(ctx, "LogMe credentials client configured") tflog.Info(ctx, "LogMe credential client configured")
} }
// Schema defines the schema for the data source. // Schema defines the schema for the data source.
func (r *credentialDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { func (r *credentialDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
descriptions := map[string]string{ descriptions := map[string]string{
"main": "LogMe credential data source schema.", "main": "LogMe credential data source schema.",
"id": "Terraform's internal data source. identifier. It is structured as \"`project_id`,`instance_id`,`credentials_id`\".", "id": "Terraform's internal data source. identifier. It is structured as \"`project_id`,`instance_id`,`credential_id`\".",
"credentials_id": "The credentials ID.", "credential_id": "The credential's ID.",
"instance_id": "ID of the LogMe instance.", "instance_id": "ID of the LogMe instance.",
"project_id": "STACKIT project ID to which the instance is associated.", "project_id": "STACKIT project ID to which the instance is associated.",
} }
resp.Schema = schema.Schema{ resp.Schema = schema.Schema{
@ -89,8 +89,8 @@ func (r *credentialDataSource) Schema(_ context.Context, _ datasource.SchemaRequ
Description: descriptions["id"], Description: descriptions["id"],
Computed: true, Computed: true,
}, },
"credentials_id": schema.StringAttribute{ "credential_id": schema.StringAttribute{
Description: descriptions["credentials_id"], Description: descriptions["credential_id"],
Required: true, Required: true,
Validators: []validator.String{ Validators: []validator.String{
validate.UUID(), validate.UUID(),
@ -153,12 +153,12 @@ func (r *credentialDataSource) Read(ctx context.Context, req datasource.ReadRequ
} }
projectId := model.ProjectId.ValueString() projectId := model.ProjectId.ValueString()
instanceId := model.InstanceId.ValueString() instanceId := model.InstanceId.ValueString()
credentialsId := model.CredentialsId.ValueString() credentialId := model.CredentialId.ValueString()
ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "instance_id", instanceId) ctx = tflog.SetField(ctx, "instance_id", instanceId)
ctx = tflog.SetField(ctx, "credentials_id", credentialsId) ctx = tflog.SetField(ctx, "credential_id", credentialId)
recordSetResp, err := r.client.GetCredentials(ctx, projectId, instanceId, credentialsId).Execute() recordSetResp, err := r.client.GetCredentials(ctx, projectId, instanceId, credentialId).Execute()
if err != nil { if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading credential", fmt.Sprintf("Calling API: %v", err)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading credential", fmt.Sprintf("Calling API: %v", err))
return return
@ -177,5 +177,5 @@ func (r *credentialDataSource) Read(ctx context.Context, req datasource.ReadRequ
if resp.Diagnostics.HasError() { if resp.Diagnostics.HasError() {
return return
} }
tflog.Info(ctx, "LogMe credentials read") tflog.Info(ctx, "LogMe credential read")
} }

View file

@ -32,18 +32,18 @@ var (
) )
type Model struct { type Model struct {
Id types.String `tfsdk:"id"` // needed by TF Id types.String `tfsdk:"id"` // needed by TF
CredentialsId types.String `tfsdk:"credentials_id"` CredentialId types.String `tfsdk:"credential_id"`
InstanceId types.String `tfsdk:"instance_id"` InstanceId types.String `tfsdk:"instance_id"`
ProjectId types.String `tfsdk:"project_id"` ProjectId types.String `tfsdk:"project_id"`
Host types.String `tfsdk:"host"` Host types.String `tfsdk:"host"`
Hosts types.List `tfsdk:"hosts"` Hosts types.List `tfsdk:"hosts"`
HttpAPIURI types.String `tfsdk:"http_api_uri"` HttpAPIURI types.String `tfsdk:"http_api_uri"`
Name types.String `tfsdk:"name"` Name types.String `tfsdk:"name"`
Password types.String `tfsdk:"password"` Password types.String `tfsdk:"password"`
Port types.Int64 `tfsdk:"port"` Port types.Int64 `tfsdk:"port"`
Uri types.String `tfsdk:"uri"` Uri types.String `tfsdk:"uri"`
Username types.String `tfsdk:"username"` Username types.String `tfsdk:"username"`
} }
// NewCredentialResource is a helper function to simplify the provider implementation. // NewCredentialResource is a helper function to simplify the provider implementation.
@ -94,17 +94,17 @@ func (r *credentialResource) Configure(ctx context.Context, req resource.Configu
} }
r.client = apiClient r.client = apiClient
tflog.Info(ctx, "LogMe credentials client configured") tflog.Info(ctx, "LogMe credential client configured")
} }
// Schema defines the schema for the resource. // Schema defines the schema for the resource.
func (r *credentialResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { func (r *credentialResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
descriptions := map[string]string{ descriptions := map[string]string{
"main": "LogMe credential resource schema.", "main": "LogMe credential resource schema.",
"id": "Terraform's internal resource identifier. It is structured as \"`project_id`,`instance_id`,`credentials_id`\".", "id": "Terraform's internal resource identifier. It is structured as \"`project_id`,`instance_id`,`credential_id`\".",
"credentials_id": "The credentials ID.", "credential_id": "The credential's ID.",
"instance_id": "ID of the LogMe instance.", "instance_id": "ID of the LogMe instance.",
"project_id": "STACKIT Project ID to which the instance is associated.", "project_id": "STACKIT Project ID to which the instance is associated.",
} }
resp.Schema = schema.Schema{ resp.Schema = schema.Schema{
@ -117,8 +117,8 @@ func (r *credentialResource) Schema(_ context.Context, _ resource.SchemaRequest,
stringplanmodifier.UseStateForUnknown(), stringplanmodifier.UseStateForUnknown(),
}, },
}, },
"credentials_id": schema.StringAttribute{ "credential_id": schema.StringAttribute{
Description: descriptions["credentials_id"], Description: descriptions["credential_id"],
Computed: true, Computed: true,
PlanModifiers: []planmodifier.String{ PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(), stringplanmodifier.UseStateForUnknown(),
@ -202,13 +202,13 @@ func (r *credentialResource) Create(ctx context.Context, req resource.CreateRequ
return return
} }
if credentialsResp.Id == nil { if credentialsResp.Id == nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating credential", "Got empty credentials id") core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating credential", "Got empty credential id")
return return
} }
credentialsId := *credentialsResp.Id credentialId := *credentialsResp.Id
ctx = tflog.SetField(ctx, "credentials_id", credentialsId) ctx = tflog.SetField(ctx, "credential_id", credentialId)
wr, err := wait.CreateCredentialsWaitHandler(ctx, r.client, projectId, instanceId, credentialsId).SetTimeout(1 * time.Minute).WaitWithContext(ctx) wr, err := wait.CreateCredentialsWaitHandler(ctx, r.client, projectId, instanceId, credentialId).SetTimeout(1 * time.Minute).WaitWithContext(ctx)
if err != nil { if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating credential", fmt.Sprintf("Instance creation waiting: %v", err)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating credential", fmt.Sprintf("Instance creation waiting: %v", err))
return return
@ -230,7 +230,7 @@ func (r *credentialResource) Create(ctx context.Context, req resource.CreateRequ
if resp.Diagnostics.HasError() { if resp.Diagnostics.HasError() {
return return
} }
tflog.Info(ctx, "LogMe credentials created") tflog.Info(ctx, "LogMe credential created")
} }
// Read refreshes the Terraform state with the latest data. // Read refreshes the Terraform state with the latest data.
@ -243,12 +243,12 @@ func (r *credentialResource) Read(ctx context.Context, req resource.ReadRequest,
} }
projectId := model.ProjectId.ValueString() projectId := model.ProjectId.ValueString()
instanceId := model.InstanceId.ValueString() instanceId := model.InstanceId.ValueString()
credentialsId := model.CredentialsId.ValueString() credentialId := model.CredentialId.ValueString()
ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "instance_id", instanceId) ctx = tflog.SetField(ctx, "instance_id", instanceId)
ctx = tflog.SetField(ctx, "credentials_id", credentialsId) ctx = tflog.SetField(ctx, "credential_id", credentialId)
recordSetResp, err := r.client.GetCredentials(ctx, projectId, instanceId, credentialsId).Execute() recordSetResp, err := r.client.GetCredentials(ctx, projectId, instanceId, credentialId).Execute()
if err != nil { if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading credential", fmt.Sprintf("Calling API: %v", err)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading credential", fmt.Sprintf("Calling API: %v", err))
return return
@ -267,7 +267,7 @@ func (r *credentialResource) Read(ctx context.Context, req resource.ReadRequest,
if resp.Diagnostics.HasError() { if resp.Diagnostics.HasError() {
return return
} }
tflog.Info(ctx, "LogMe credentials read") tflog.Info(ctx, "LogMe credential read")
} }
// Update updates the resource and sets the updated Terraform state on success. // Update updates the resource and sets the updated Terraform state on success.
@ -287,40 +287,40 @@ func (r *credentialResource) Delete(ctx context.Context, req resource.DeleteRequ
projectId := model.ProjectId.ValueString() projectId := model.ProjectId.ValueString()
instanceId := model.InstanceId.ValueString() instanceId := model.InstanceId.ValueString()
credentialsId := model.CredentialsId.ValueString() credentialId := model.CredentialId.ValueString()
ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "instance_id", instanceId) ctx = tflog.SetField(ctx, "instance_id", instanceId)
ctx = tflog.SetField(ctx, "credentials_id", credentialsId) ctx = tflog.SetField(ctx, "credential_id", credentialId)
// Delete existing record set // Delete existing record set
err := r.client.DeleteCredentials(ctx, projectId, instanceId, credentialsId).Execute() err := r.client.DeleteCredentials(ctx, projectId, instanceId, credentialId).Execute()
if err != nil { if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting credential", fmt.Sprintf("Calling API: %v", err)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting credential", fmt.Sprintf("Calling API: %v", err))
} }
_, err = wait.DeleteCredentialsWaitHandler(ctx, r.client, projectId, instanceId, credentialsId).SetTimeout(1 * time.Minute).WaitWithContext(ctx) _, err = wait.DeleteCredentialsWaitHandler(ctx, r.client, projectId, instanceId, credentialId).SetTimeout(1 * time.Minute).WaitWithContext(ctx)
if err != nil { if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting credential", fmt.Sprintf("Instance deletion waiting: %v", err)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting credential", fmt.Sprintf("Instance deletion waiting: %v", err))
return return
} }
tflog.Info(ctx, "LogMe credentials deleted") tflog.Info(ctx, "LogMe credential deleted")
} }
// ImportState imports a resource into the Terraform state on success. // ImportState imports a resource into the Terraform state on success.
// The expected format of the resource import identifier is: project_id,instance_id,credentials_id // The expected format of the resource import identifier is: project_id,instance_id,credential_id
func (r *credentialResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { func (r *credentialResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
idParts := strings.Split(req.ID, core.Separator) idParts := strings.Split(req.ID, core.Separator)
if len(idParts) != 3 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" { if len(idParts) != 3 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" {
core.LogAndAddError(ctx, &resp.Diagnostics, core.LogAndAddError(ctx, &resp.Diagnostics,
"Error importing credential", "Error importing credential",
fmt.Sprintf("Expected import identifier with format [project_id],[instance_id],[credentials_id], got %q", req.ID), fmt.Sprintf("Expected import identifier with format [project_id],[instance_id],[credential_id], got %q", req.ID),
) )
return return
} }
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), idParts[0])...) resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), idParts[0])...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("instance_id"), idParts[1])...) resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("instance_id"), idParts[1])...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("credentials_id"), idParts[2])...) resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("credential_id"), idParts[2])...)
tflog.Info(ctx, "LogMe credentials state imported") tflog.Info(ctx, "LogMe credential state imported")
} }
func mapFields(credentialsResp *logme.CredentialsResponse, model *Model) error { func mapFields(credentialsResp *logme.CredentialsResponse, model *Model) error {
@ -335,11 +335,11 @@ func mapFields(credentialsResp *logme.CredentialsResponse, model *Model) error {
} }
credentials := credentialsResp.Raw.Credentials credentials := credentialsResp.Raw.Credentials
var credentialsId string var credentialId string
if model.CredentialsId.ValueString() != "" { if model.CredentialId.ValueString() != "" {
credentialsId = model.CredentialsId.ValueString() credentialId = model.CredentialId.ValueString()
} else if credentialsResp.Id != nil { } else if credentialsResp.Id != nil {
credentialsId = *credentialsResp.Id credentialId = *credentialsResp.Id
} else { } else {
return fmt.Errorf("credentials id not present") return fmt.Errorf("credentials id not present")
} }
@ -347,12 +347,12 @@ func mapFields(credentialsResp *logme.CredentialsResponse, model *Model) error {
idParts := []string{ idParts := []string{
model.ProjectId.ValueString(), model.ProjectId.ValueString(),
model.InstanceId.ValueString(), model.InstanceId.ValueString(),
credentialsId, credentialId,
} }
model.Id = types.StringValue( model.Id = types.StringValue(
strings.Join(idParts, core.Separator), strings.Join(idParts, core.Separator),
) )
model.CredentialsId = types.StringValue(credentialsId) model.CredentialId = types.StringValue(credentialId)
model.Hosts = types.ListNull(types.StringType) model.Hosts = types.ListNull(types.StringType)
if credentials != nil { if credentials != nil {
if credentials.Hosts != nil { if credentials.Hosts != nil {

View file

@ -24,18 +24,18 @@ func TestMapFields(t *testing.T) {
Raw: &logme.RawCredentials{}, Raw: &logme.RawCredentials{},
}, },
Model{ Model{
Id: types.StringValue("pid,iid,cid"), Id: types.StringValue("pid,iid,cid"),
CredentialsId: types.StringValue("cid"), CredentialId: types.StringValue("cid"),
InstanceId: types.StringValue("iid"), InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"), ProjectId: types.StringValue("pid"),
Host: types.StringNull(), Host: types.StringNull(),
Hosts: types.ListNull(types.StringType), Hosts: types.ListNull(types.StringType),
HttpAPIURI: types.StringNull(), HttpAPIURI: types.StringNull(),
Name: types.StringNull(), Name: types.StringNull(),
Password: types.StringNull(), Password: types.StringNull(),
Port: types.Int64Null(), Port: types.Int64Null(),
Uri: types.StringNull(), Uri: types.StringNull(),
Username: types.StringNull(), Username: types.StringNull(),
}, },
true, true,
}, },
@ -60,11 +60,11 @@ func TestMapFields(t *testing.T) {
}, },
}, },
Model{ Model{
Id: types.StringValue("pid,iid,cid"), Id: types.StringValue("pid,iid,cid"),
CredentialsId: types.StringValue("cid"), CredentialId: types.StringValue("cid"),
InstanceId: types.StringValue("iid"), InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"), ProjectId: types.StringValue("pid"),
Host: types.StringValue("host"), Host: types.StringValue("host"),
Hosts: types.ListValueMust(types.StringType, []attr.Value{ Hosts: types.ListValueMust(types.StringType, []attr.Value{
types.StringValue("host_1"), types.StringValue("host_1"),
types.StringValue(""), types.StringValue(""),
@ -96,18 +96,18 @@ func TestMapFields(t *testing.T) {
}, },
}, },
Model{ Model{
Id: types.StringValue("pid,iid,cid"), Id: types.StringValue("pid,iid,cid"),
CredentialsId: types.StringValue("cid"), CredentialId: types.StringValue("cid"),
InstanceId: types.StringValue("iid"), InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"), ProjectId: types.StringValue("pid"),
Host: types.StringValue(""), Host: types.StringValue(""),
Hosts: types.ListValueMust(types.StringType, []attr.Value{}), Hosts: types.ListValueMust(types.StringType, []attr.Value{}),
HttpAPIURI: types.StringNull(), HttpAPIURI: types.StringNull(),
Name: types.StringNull(), Name: types.StringNull(),
Password: types.StringValue(""), Password: types.StringValue(""),
Port: types.Int64Value(2123456789), Port: types.Int64Value(2123456789),
Uri: types.StringNull(), Uri: types.StringNull(),
Username: types.StringValue(""), Username: types.StringValue(""),
}, },
true, true,
}, },

View file

@ -41,7 +41,7 @@ func resourceConfig(acls string) string {
} }
} }
resource "stackit_logme_credential" "credentials" { resource "stackit_logme_credential" "credential" {
project_id = stackit_logme_instance.instance.project_id project_id = stackit_logme_instance.instance.project_id
instance_id = stackit_logme_instance.instance.instance_id instance_id = stackit_logme_instance.instance.instance_id
} }
@ -73,17 +73,17 @@ func TestAccLogMeResource(t *testing.T) {
resource.TestCheckResourceAttr("stackit_logme_instance.instance", "name", instanceResource["name"]), resource.TestCheckResourceAttr("stackit_logme_instance.instance", "name", instanceResource["name"]),
resource.TestCheckResourceAttr("stackit_logme_instance.instance", "parameters.sgw_acl", instanceResource["sgw_acl-1"]), resource.TestCheckResourceAttr("stackit_logme_instance.instance", "parameters.sgw_acl", instanceResource["sgw_acl-1"]),
// Credentials data // Credential data
resource.TestCheckResourceAttrPair( resource.TestCheckResourceAttrPair(
"stackit_logme_credential.credentials", "project_id", "stackit_logme_credential.credential", "project_id",
"stackit_logme_instance.instance", "project_id", "stackit_logme_instance.instance", "project_id",
), ),
resource.TestCheckResourceAttrPair( resource.TestCheckResourceAttrPair(
"stackit_logme_credential.credentials", "instance_id", "stackit_logme_credential.credential", "instance_id",
"stackit_logme_instance.instance", "instance_id", "stackit_logme_instance.instance", "instance_id",
), ),
resource.TestCheckResourceAttrSet("stackit_logme_credential.credentials", "credentials_id"), resource.TestCheckResourceAttrSet("stackit_logme_credential.credential", "credential_id"),
resource.TestCheckResourceAttrSet("stackit_logme_credential.credentials", "host"), resource.TestCheckResourceAttrSet("stackit_logme_credential.credential", "host"),
), ),
}, },
// Data source // Data source
@ -96,10 +96,10 @@ func TestAccLogMeResource(t *testing.T) {
instance_id = stackit_logme_instance.instance.instance_id instance_id = stackit_logme_instance.instance.instance_id
} }
data "stackit_logme_credential" "credentials" { data "stackit_logme_credential" "credential" {
project_id = stackit_logme_credential.credentials.project_id project_id = stackit_logme_credential.credential.project_id
instance_id = stackit_logme_credential.credentials.instance_id instance_id = stackit_logme_credential.credential.instance_id
credentials_id = stackit_logme_credential.credentials.credentials_id credential_id = stackit_logme_credential.credential.credential_id
}`, }`,
resourceConfig(instanceResource["sgw_acl-1"]), resourceConfig(instanceResource["sgw_acl-1"]),
), ),
@ -109,18 +109,18 @@ func TestAccLogMeResource(t *testing.T) {
resource.TestCheckResourceAttrPair("stackit_logme_instance.instance", "instance_id", resource.TestCheckResourceAttrPair("stackit_logme_instance.instance", "instance_id",
"data.stackit_logme_instance.instance", "instance_id"), "data.stackit_logme_instance.instance", "instance_id"),
resource.TestCheckResourceAttrPair("stackit_logme_credential.credentials", "credentials_id", resource.TestCheckResourceAttrPair("stackit_logme_credential.credential", "credential_id",
"data.stackit_logme_credential.credentials", "credentials_id"), "data.stackit_logme_credential.credential", "credential_id"),
resource.TestCheckResourceAttr("data.stackit_logme_instance.instance", "plan_id", instanceResource["plan_id"]), resource.TestCheckResourceAttr("data.stackit_logme_instance.instance", "plan_id", instanceResource["plan_id"]),
resource.TestCheckResourceAttr("data.stackit_logme_instance.instance", "name", instanceResource["name"]), resource.TestCheckResourceAttr("data.stackit_logme_instance.instance", "name", instanceResource["name"]),
resource.TestCheckResourceAttr("data.stackit_logme_instance.instance", "parameters.sgw_acl", instanceResource["sgw_acl-1"]), resource.TestCheckResourceAttr("data.stackit_logme_instance.instance", "parameters.sgw_acl", instanceResource["sgw_acl-1"]),
// Credentials data // Credential data
resource.TestCheckResourceAttr("data.stackit_logme_credential.credentials", "project_id", instanceResource["project_id"]), resource.TestCheckResourceAttr("data.stackit_logme_credential.credential", "project_id", instanceResource["project_id"]),
resource.TestCheckResourceAttrSet("data.stackit_logme_credential.credentials", "credentials_id"), resource.TestCheckResourceAttrSet("data.stackit_logme_credential.credential", "credential_id"),
resource.TestCheckResourceAttrSet("data.stackit_logme_credential.credentials", "host"), resource.TestCheckResourceAttrSet("data.stackit_logme_credential.credential", "host"),
resource.TestCheckResourceAttrSet("data.stackit_logme_credential.credentials", "port"), resource.TestCheckResourceAttrSet("data.stackit_logme_credential.credential", "port"),
resource.TestCheckResourceAttrSet("data.stackit_logme_credential.credentials", "uri"), resource.TestCheckResourceAttrSet("data.stackit_logme_credential.credential", "uri"),
), ),
}, },
// Import // Import
@ -141,21 +141,21 @@ func TestAccLogMeResource(t *testing.T) {
ImportStateVerify: true, ImportStateVerify: true,
}, },
{ {
ResourceName: "stackit_logme_credential.credentials", ResourceName: "stackit_logme_credential.credential",
ImportStateIdFunc: func(s *terraform.State) (string, error) { ImportStateIdFunc: func(s *terraform.State) (string, error) {
r, ok := s.RootModule().Resources["stackit_logme_credential.credentials"] r, ok := s.RootModule().Resources["stackit_logme_credential.credential"]
if !ok { if !ok {
return "", fmt.Errorf("couldn't find resource stackit_logme_credential.credentials") return "", fmt.Errorf("couldn't find resource stackit_logme_credential.credential")
} }
instanceId, ok := r.Primary.Attributes["instance_id"] instanceId, ok := r.Primary.Attributes["instance_id"]
if !ok { if !ok {
return "", fmt.Errorf("couldn't find attribute instance_id") return "", fmt.Errorf("couldn't find attribute instance_id")
} }
credentialsId, ok := r.Primary.Attributes["credentials_id"] credentialId, ok := r.Primary.Attributes["credential_id"]
if !ok { if !ok {
return "", fmt.Errorf("couldn't find attribute credentials_id") return "", fmt.Errorf("couldn't find attribute credential_id")
} }
return fmt.Sprintf("%s,%s,%s", testutil.ProjectId, instanceId, credentialsId), nil return fmt.Sprintf("%s,%s,%s", testutil.ProjectId, instanceId, credentialId), nil
}, },
ImportState: true, ImportState: true,
ImportStateVerify: true, ImportStateVerify: true,

View file

@ -69,17 +69,17 @@ func (r *credentialDataSource) Configure(ctx context.Context, req datasource.Con
} }
r.client = apiClient r.client = apiClient
tflog.Info(ctx, "mariadb credentials client configured") tflog.Info(ctx, "mariadb credential client configured")
} }
// Schema defines the schema for the data source. // Schema defines the schema for the data source.
func (r *credentialDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { func (r *credentialDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
descriptions := map[string]string{ descriptions := map[string]string{
"main": "MariaDB credential data source schema.", "main": "MariaDB credential data source schema.",
"id": "Terraform's internal data source. identifier. It is structured as \"`project_id`,`instance_id`,`credentials_id`\".", "id": "Terraform's internal data source. identifier. It is structured as \"`project_id`,`instance_id`,`credential_id`\".",
"credentials_id": "The credentials ID.", "credential_id": "The credential's ID.",
"instance_id": "ID of the MariaDB instance.", "instance_id": "ID of the MariaDB instance.",
"project_id": "STACKIT project ID to which the instance is associated.", "project_id": "STACKIT project ID to which the instance is associated.",
} }
resp.Schema = schema.Schema{ resp.Schema = schema.Schema{
@ -89,8 +89,8 @@ func (r *credentialDataSource) Schema(_ context.Context, _ datasource.SchemaRequ
Description: descriptions["id"], Description: descriptions["id"],
Computed: true, Computed: true,
}, },
"credentials_id": schema.StringAttribute{ "credential_id": schema.StringAttribute{
Description: descriptions["credentials_id"], Description: descriptions["credential_id"],
Required: true, Required: true,
Validators: []validator.String{ Validators: []validator.String{
validate.UUID(), validate.UUID(),
@ -153,12 +153,12 @@ func (r *credentialDataSource) Read(ctx context.Context, req datasource.ReadRequ
} }
projectId := model.ProjectId.ValueString() projectId := model.ProjectId.ValueString()
instanceId := model.InstanceId.ValueString() instanceId := model.InstanceId.ValueString()
credentialsId := model.CredentialsId.ValueString() credentialId := model.CredentialId.ValueString()
ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "instance_id", instanceId) ctx = tflog.SetField(ctx, "instance_id", instanceId)
ctx = tflog.SetField(ctx, "credentials_id", credentialsId) ctx = tflog.SetField(ctx, "credential_id", credentialId)
recordSetResp, err := r.client.GetCredentials(ctx, projectId, instanceId, credentialsId).Execute() recordSetResp, err := r.client.GetCredentials(ctx, projectId, instanceId, credentialId).Execute()
if err != nil { if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading credential", fmt.Sprintf("Calling API: %v", err)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading credential", fmt.Sprintf("Calling API: %v", err))
return return
@ -177,5 +177,5 @@ func (r *credentialDataSource) Read(ctx context.Context, req datasource.ReadRequ
if resp.Diagnostics.HasError() { if resp.Diagnostics.HasError() {
return return
} }
tflog.Info(ctx, "mariadb credentials read") tflog.Info(ctx, "mariadb credential read")
} }

View file

@ -32,18 +32,18 @@ var (
) )
type Model struct { type Model struct {
Id types.String `tfsdk:"id"` // needed by TF Id types.String `tfsdk:"id"` // needed by TF
CredentialsId types.String `tfsdk:"credentials_id"` CredentialId types.String `tfsdk:"credential_id"`
InstanceId types.String `tfsdk:"instance_id"` InstanceId types.String `tfsdk:"instance_id"`
ProjectId types.String `tfsdk:"project_id"` ProjectId types.String `tfsdk:"project_id"`
Host types.String `tfsdk:"host"` Host types.String `tfsdk:"host"`
Hosts types.List `tfsdk:"hosts"` Hosts types.List `tfsdk:"hosts"`
HttpAPIURI types.String `tfsdk:"http_api_uri"` HttpAPIURI types.String `tfsdk:"http_api_uri"`
Name types.String `tfsdk:"name"` Name types.String `tfsdk:"name"`
Password types.String `tfsdk:"password"` Password types.String `tfsdk:"password"`
Port types.Int64 `tfsdk:"port"` Port types.Int64 `tfsdk:"port"`
Uri types.String `tfsdk:"uri"` Uri types.String `tfsdk:"uri"`
Username types.String `tfsdk:"username"` Username types.String `tfsdk:"username"`
} }
// NewCredentialResource is a helper function to simplify the provider implementation. // NewCredentialResource is a helper function to simplify the provider implementation.
@ -94,17 +94,17 @@ func (r *credentialResource) Configure(ctx context.Context, req resource.Configu
} }
r.client = apiClient r.client = apiClient
tflog.Info(ctx, "MariaDB credentials client configured") tflog.Info(ctx, "MariaDB credential client configured")
} }
// Schema defines the schema for the resource. // Schema defines the schema for the resource.
func (r *credentialResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { func (r *credentialResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
descriptions := map[string]string{ descriptions := map[string]string{
"main": "MariaDB credential resource schema.", "main": "MariaDB credential resource schema.",
"id": "Terraform's internal resource identifier. It is structured as \"`project_id`,`instance_id`,`credentials_id`\".", "id": "Terraform's internal resource identifier. It is structured as \"`project_id`,`instance_id`,`credential_id`\".",
"credentials_id": "The credentials ID.", "credential_id": "The credential's ID.",
"instance_id": "ID of the MariaDB instance.", "instance_id": "ID of the MariaDB instance.",
"project_id": "STACKIT Project ID to which the instance is associated.", "project_id": "STACKIT Project ID to which the instance is associated.",
} }
resp.Schema = schema.Schema{ resp.Schema = schema.Schema{
@ -117,8 +117,8 @@ func (r *credentialResource) Schema(_ context.Context, _ resource.SchemaRequest,
stringplanmodifier.UseStateForUnknown(), stringplanmodifier.UseStateForUnknown(),
}, },
}, },
"credentials_id": schema.StringAttribute{ "credential_id": schema.StringAttribute{
Description: descriptions["credentials_id"], Description: descriptions["credential_id"],
Computed: true, Computed: true,
PlanModifiers: []planmodifier.String{ PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(), stringplanmodifier.UseStateForUnknown(),
@ -202,13 +202,13 @@ func (r *credentialResource) Create(ctx context.Context, req resource.CreateRequ
return return
} }
if credentialsResp.Id == nil { if credentialsResp.Id == nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating credential", "Got empty credentials id") core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating credential", "Got empty credential id")
return return
} }
credentialsId := *credentialsResp.Id credentialId := *credentialsResp.Id
ctx = tflog.SetField(ctx, "credentials_id", credentialsId) ctx = tflog.SetField(ctx, "credential_id", credentialId)
wr, err := wait.CreateCredentialsWaitHandler(ctx, r.client, projectId, instanceId, credentialsId).SetTimeout(1 * time.Minute).WaitWithContext(ctx) wr, err := wait.CreateCredentialsWaitHandler(ctx, r.client, projectId, instanceId, credentialId).SetTimeout(1 * time.Minute).WaitWithContext(ctx)
if err != nil { if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating credential", fmt.Sprintf("Instance creation waiting: %v", err)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating credential", fmt.Sprintf("Instance creation waiting: %v", err))
return return
@ -230,7 +230,7 @@ func (r *credentialResource) Create(ctx context.Context, req resource.CreateRequ
if resp.Diagnostics.HasError() { if resp.Diagnostics.HasError() {
return return
} }
tflog.Info(ctx, "MariaDB credentials created") tflog.Info(ctx, "MariaDB credential created")
} }
// Read refreshes the Terraform state with the latest data. // Read refreshes the Terraform state with the latest data.
@ -243,12 +243,12 @@ func (r *credentialResource) Read(ctx context.Context, req resource.ReadRequest,
} }
projectId := model.ProjectId.ValueString() projectId := model.ProjectId.ValueString()
instanceId := model.InstanceId.ValueString() instanceId := model.InstanceId.ValueString()
credentialsId := model.CredentialsId.ValueString() credentialId := model.CredentialId.ValueString()
ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "instance_id", instanceId) ctx = tflog.SetField(ctx, "instance_id", instanceId)
ctx = tflog.SetField(ctx, "credentials_id", credentialsId) ctx = tflog.SetField(ctx, "credential_id", credentialId)
recordSetResp, err := r.client.GetCredentials(ctx, projectId, instanceId, credentialsId).Execute() recordSetResp, err := r.client.GetCredentials(ctx, projectId, instanceId, credentialId).Execute()
if err != nil { if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading credential", fmt.Sprintf("Calling API: %v", err)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading credential", fmt.Sprintf("Calling API: %v", err))
return return
@ -267,7 +267,7 @@ func (r *credentialResource) Read(ctx context.Context, req resource.ReadRequest,
if resp.Diagnostics.HasError() { if resp.Diagnostics.HasError() {
return return
} }
tflog.Info(ctx, "MariaDB credentials read") tflog.Info(ctx, "MariaDB credential read")
} }
// Update updates the resource and sets the updated Terraform state on success. // Update updates the resource and sets the updated Terraform state on success.
@ -287,40 +287,40 @@ func (r *credentialResource) Delete(ctx context.Context, req resource.DeleteRequ
projectId := model.ProjectId.ValueString() projectId := model.ProjectId.ValueString()
instanceId := model.InstanceId.ValueString() instanceId := model.InstanceId.ValueString()
credentialsId := model.CredentialsId.ValueString() credentialId := model.CredentialId.ValueString()
ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "instance_id", instanceId) ctx = tflog.SetField(ctx, "instance_id", instanceId)
ctx = tflog.SetField(ctx, "credentials_id", credentialsId) ctx = tflog.SetField(ctx, "credential_id", credentialId)
// Delete existing record set // Delete existing record set
err := r.client.DeleteCredentials(ctx, projectId, instanceId, credentialsId).Execute() err := r.client.DeleteCredentials(ctx, projectId, instanceId, credentialId).Execute()
if err != nil { if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting credential", fmt.Sprintf("Calling API: %v", err)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting credential", fmt.Sprintf("Calling API: %v", err))
} }
_, err = wait.DeleteCredentialsWaitHandler(ctx, r.client, projectId, instanceId, credentialsId).SetTimeout(1 * time.Minute).WaitWithContext(ctx) _, err = wait.DeleteCredentialsWaitHandler(ctx, r.client, projectId, instanceId, credentialId).SetTimeout(1 * time.Minute).WaitWithContext(ctx)
if err != nil { if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting credential", fmt.Sprintf("Instance deletion waiting: %v", err)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting credential", fmt.Sprintf("Instance deletion waiting: %v", err))
return return
} }
tflog.Info(ctx, "MariaDB credentials deleted") tflog.Info(ctx, "MariaDB credential deleted")
} }
// ImportState imports a resource into the Terraform state on success. // ImportState imports a resource into the Terraform state on success.
// The expected format of the resource import identifier is: project_id,instance_id,credentials_id // The expected format of the resource import identifier is: project_id,instance_id,credential_id
func (r *credentialResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { func (r *credentialResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
idParts := strings.Split(req.ID, core.Separator) idParts := strings.Split(req.ID, core.Separator)
if len(idParts) != 3 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" { if len(idParts) != 3 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" {
core.LogAndAddError(ctx, &resp.Diagnostics, core.LogAndAddError(ctx, &resp.Diagnostics,
"Error importing credential", "Error importing credential",
fmt.Sprintf("Expected import identifier with format [project_id],[instance_id],[credentials_id], got %q", req.ID), fmt.Sprintf("Expected import identifier with format [project_id],[instance_id],[credential_id], got %q", req.ID),
) )
return return
} }
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), idParts[0])...) resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), idParts[0])...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("instance_id"), idParts[1])...) resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("instance_id"), idParts[1])...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("credentials_id"), idParts[2])...) resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("credential_id"), idParts[2])...)
tflog.Info(ctx, "MariaDB credentials state imported") tflog.Info(ctx, "MariaDB credential state imported")
} }
func mapFields(credentialsResp *mariadb.CredentialsResponse, model *Model) error { func mapFields(credentialsResp *mariadb.CredentialsResponse, model *Model) error {
@ -335,11 +335,11 @@ func mapFields(credentialsResp *mariadb.CredentialsResponse, model *Model) error
} }
credentials := credentialsResp.Raw.Credentials credentials := credentialsResp.Raw.Credentials
var credentialsId string var credentialId string
if model.CredentialsId.ValueString() != "" { if model.CredentialId.ValueString() != "" {
credentialsId = model.CredentialsId.ValueString() credentialId = model.CredentialId.ValueString()
} else if credentialsResp.Id != nil { } else if credentialsResp.Id != nil {
credentialsId = *credentialsResp.Id credentialId = *credentialsResp.Id
} else { } else {
return fmt.Errorf("credentials id not present") return fmt.Errorf("credentials id not present")
} }
@ -347,12 +347,12 @@ func mapFields(credentialsResp *mariadb.CredentialsResponse, model *Model) error
idParts := []string{ idParts := []string{
model.ProjectId.ValueString(), model.ProjectId.ValueString(),
model.InstanceId.ValueString(), model.InstanceId.ValueString(),
credentialsId, credentialId,
} }
model.Id = types.StringValue( model.Id = types.StringValue(
strings.Join(idParts, core.Separator), strings.Join(idParts, core.Separator),
) )
model.CredentialsId = types.StringValue(credentialsId) model.CredentialId = types.StringValue(credentialId)
model.Hosts = types.ListNull(types.StringType) model.Hosts = types.ListNull(types.StringType)
if credentials != nil { if credentials != nil {
if credentials.Hosts != nil { if credentials.Hosts != nil {

View file

@ -24,18 +24,18 @@ func TestMapFields(t *testing.T) {
Raw: &mariadb.RawCredentials{}, Raw: &mariadb.RawCredentials{},
}, },
Model{ Model{
Id: types.StringValue("pid,iid,cid"), Id: types.StringValue("pid,iid,cid"),
CredentialsId: types.StringValue("cid"), CredentialId: types.StringValue("cid"),
InstanceId: types.StringValue("iid"), InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"), ProjectId: types.StringValue("pid"),
Host: types.StringNull(), Host: types.StringNull(),
Hosts: types.ListNull(types.StringType), Hosts: types.ListNull(types.StringType),
HttpAPIURI: types.StringNull(), HttpAPIURI: types.StringNull(),
Name: types.StringNull(), Name: types.StringNull(),
Password: types.StringNull(), Password: types.StringNull(),
Port: types.Int64Null(), Port: types.Int64Null(),
Uri: types.StringNull(), Uri: types.StringNull(),
Username: types.StringNull(), Username: types.StringNull(),
}, },
true, true,
}, },
@ -60,11 +60,11 @@ func TestMapFields(t *testing.T) {
}, },
}, },
Model{ Model{
Id: types.StringValue("pid,iid,cid"), Id: types.StringValue("pid,iid,cid"),
CredentialsId: types.StringValue("cid"), CredentialId: types.StringValue("cid"),
InstanceId: types.StringValue("iid"), InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"), ProjectId: types.StringValue("pid"),
Host: types.StringValue("host"), Host: types.StringValue("host"),
Hosts: types.ListValueMust(types.StringType, []attr.Value{ Hosts: types.ListValueMust(types.StringType, []attr.Value{
types.StringValue("host_1"), types.StringValue("host_1"),
types.StringValue(""), types.StringValue(""),
@ -96,18 +96,18 @@ func TestMapFields(t *testing.T) {
}, },
}, },
Model{ Model{
Id: types.StringValue("pid,iid,cid"), Id: types.StringValue("pid,iid,cid"),
CredentialsId: types.StringValue("cid"), CredentialId: types.StringValue("cid"),
InstanceId: types.StringValue("iid"), InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"), ProjectId: types.StringValue("pid"),
Host: types.StringValue(""), Host: types.StringValue(""),
Hosts: types.ListValueMust(types.StringType, []attr.Value{}), Hosts: types.ListValueMust(types.StringType, []attr.Value{}),
HttpAPIURI: types.StringNull(), HttpAPIURI: types.StringNull(),
Name: types.StringNull(), Name: types.StringNull(),
Password: types.StringValue(""), Password: types.StringValue(""),
Port: types.Int64Value(2123456789), Port: types.Int64Value(2123456789),
Uri: types.StringNull(), Uri: types.StringNull(),
Username: types.StringValue(""), Username: types.StringValue(""),
}, },
true, true,
}, },

View file

@ -41,7 +41,7 @@ func resourceConfig(acls string) string {
} }
} }
resource "stackit_mariadb_credential" "credentials" { resource "stackit_mariadb_credential" "credential" {
project_id = stackit_mariadb_instance.instance.project_id project_id = stackit_mariadb_instance.instance.project_id
instance_id = stackit_mariadb_instance.instance.instance_id instance_id = stackit_mariadb_instance.instance.instance_id
} }
@ -73,17 +73,17 @@ func TestAccMariaDBResource(t *testing.T) {
resource.TestCheckResourceAttr("stackit_mariadb_instance.instance", "name", instanceResource["name"]), resource.TestCheckResourceAttr("stackit_mariadb_instance.instance", "name", instanceResource["name"]),
resource.TestCheckResourceAttr("stackit_mariadb_instance.instance", "parameters.sgw_acl", instanceResource["sgw_acl-1"]), resource.TestCheckResourceAttr("stackit_mariadb_instance.instance", "parameters.sgw_acl", instanceResource["sgw_acl-1"]),
// Credentials data // Credential data
resource.TestCheckResourceAttrPair( resource.TestCheckResourceAttrPair(
"stackit_mariadb_credential.credentials", "project_id", "stackit_mariadb_credential.credential", "project_id",
"stackit_mariadb_instance.instance", "project_id", "stackit_mariadb_instance.instance", "project_id",
), ),
resource.TestCheckResourceAttrPair( resource.TestCheckResourceAttrPair(
"stackit_mariadb_credential.credentials", "instance_id", "stackit_mariadb_credential.credential", "instance_id",
"stackit_mariadb_instance.instance", "instance_id", "stackit_mariadb_instance.instance", "instance_id",
), ),
resource.TestCheckResourceAttrSet("stackit_mariadb_credential.credentials", "credentials_id"), resource.TestCheckResourceAttrSet("stackit_mariadb_credential.credential", "credential_id"),
resource.TestCheckResourceAttrSet("stackit_mariadb_credential.credentials", "host"), resource.TestCheckResourceAttrSet("stackit_mariadb_credential.credential", "host"),
), ),
}, },
// Data source // Data source
@ -96,10 +96,10 @@ func TestAccMariaDBResource(t *testing.T) {
instance_id = stackit_mariadb_instance.instance.instance_id instance_id = stackit_mariadb_instance.instance.instance_id
} }
data "stackit_mariadb_credential" "credentials" { data "stackit_mariadb_credential" "credential" {
project_id = stackit_mariadb_credential.credentials.project_id project_id = stackit_mariadb_credential.credential.project_id
instance_id = stackit_mariadb_credential.credentials.instance_id instance_id = stackit_mariadb_credential.credential.instance_id
credentials_id = stackit_mariadb_credential.credentials.credentials_id credential_id = stackit_mariadb_credential.credential.credential_id
}`, }`,
resourceConfig(instanceResource["sgw_acl-1"]), resourceConfig(instanceResource["sgw_acl-1"]),
), ),
@ -108,18 +108,18 @@ func TestAccMariaDBResource(t *testing.T) {
resource.TestCheckResourceAttr("data.stackit_mariadb_instance.instance", "project_id", instanceResource["project_id"]), resource.TestCheckResourceAttr("data.stackit_mariadb_instance.instance", "project_id", instanceResource["project_id"]),
resource.TestCheckResourceAttrPair("stackit_mariadb_instance.instance", "instance_id", resource.TestCheckResourceAttrPair("stackit_mariadb_instance.instance", "instance_id",
"data.stackit_mariadb_instance.instance", "instance_id"), "data.stackit_mariadb_instance.instance", "instance_id"),
resource.TestCheckResourceAttrPair("stackit_mariadb_credential.credentials", "credentials_id", resource.TestCheckResourceAttrPair("stackit_mariadb_credential.credential", "credential_id",
"data.stackit_mariadb_credential.credentials", "credentials_id"), "data.stackit_mariadb_credential.credential", "credential_id"),
resource.TestCheckResourceAttr("data.stackit_mariadb_instance.instance", "plan_id", instanceResource["plan_id"]), resource.TestCheckResourceAttr("data.stackit_mariadb_instance.instance", "plan_id", instanceResource["plan_id"]),
resource.TestCheckResourceAttr("data.stackit_mariadb_instance.instance", "name", instanceResource["name"]), resource.TestCheckResourceAttr("data.stackit_mariadb_instance.instance", "name", instanceResource["name"]),
resource.TestCheckResourceAttr("data.stackit_mariadb_instance.instance", "parameters.sgw_acl", instanceResource["sgw_acl-1"]), resource.TestCheckResourceAttr("data.stackit_mariadb_instance.instance", "parameters.sgw_acl", instanceResource["sgw_acl-1"]),
// Credentials data // Credential data
resource.TestCheckResourceAttr("data.stackit_mariadb_credential.credentials", "project_id", instanceResource["project_id"]), resource.TestCheckResourceAttr("data.stackit_mariadb_credential.credential", "project_id", instanceResource["project_id"]),
resource.TestCheckResourceAttrSet("data.stackit_mariadb_credential.credentials", "credentials_id"), resource.TestCheckResourceAttrSet("data.stackit_mariadb_credential.credential", "credential_id"),
resource.TestCheckResourceAttrSet("data.stackit_mariadb_credential.credentials", "host"), resource.TestCheckResourceAttrSet("data.stackit_mariadb_credential.credential", "host"),
resource.TestCheckResourceAttrSet("data.stackit_mariadb_credential.credentials", "port"), resource.TestCheckResourceAttrSet("data.stackit_mariadb_credential.credential", "port"),
resource.TestCheckResourceAttrSet("data.stackit_mariadb_credential.credentials", "uri"), resource.TestCheckResourceAttrSet("data.stackit_mariadb_credential.credential", "uri"),
), ),
}, },
// Import // Import
@ -140,21 +140,21 @@ func TestAccMariaDBResource(t *testing.T) {
ImportStateVerify: true, ImportStateVerify: true,
}, },
{ {
ResourceName: "stackit_mariadb_credential.credentials", ResourceName: "stackit_mariadb_credential.credential",
ImportStateIdFunc: func(s *terraform.State) (string, error) { ImportStateIdFunc: func(s *terraform.State) (string, error) {
r, ok := s.RootModule().Resources["stackit_mariadb_credential.credentials"] r, ok := s.RootModule().Resources["stackit_mariadb_credential.credential"]
if !ok { if !ok {
return "", fmt.Errorf("couldn't find resource stackit_mariadb_credential.credentials") return "", fmt.Errorf("couldn't find resource stackit_mariadb_credential.credential")
} }
instanceId, ok := r.Primary.Attributes["instance_id"] instanceId, ok := r.Primary.Attributes["instance_id"]
if !ok { if !ok {
return "", fmt.Errorf("couldn't find attribute instance_id") return "", fmt.Errorf("couldn't find attribute instance_id")
} }
credentialsId, ok := r.Primary.Attributes["credentials_id"] credentialId, ok := r.Primary.Attributes["credential_id"]
if !ok { if !ok {
return "", fmt.Errorf("couldn't find attribute credentials_id") return "", fmt.Errorf("couldn't find attribute credential_id")
} }
return fmt.Sprintf("%s,%s,%s", testutil.ProjectId, instanceId, credentialsId), nil return fmt.Sprintf("%s,%s,%s", testutil.ProjectId, instanceId, credentialId), nil
}, },
ImportState: true, ImportState: true,
ImportStateVerify: true, ImportStateVerify: true,

View file

@ -69,17 +69,17 @@ func (r *credentialDataSource) Configure(ctx context.Context, req datasource.Con
} }
r.client = apiClient r.client = apiClient
tflog.Info(ctx, "OpenSearch credentials client configured") tflog.Info(ctx, "OpenSearch credential client configured")
} }
// Schema defines the schema for the data source. // Schema defines the schema for the data source.
func (r *credentialDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { func (r *credentialDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
descriptions := map[string]string{ descriptions := map[string]string{
"main": "OpenSearch credential data source schema.", "main": "OpenSearch credential data source schema.",
"id": "Terraform's internal data source. identifier. It is structured as \"`project_id`,`instance_id`,`credentials_id`\".", "id": "Terraform's internal data source. identifier. It is structured as \"`project_id`,`instance_id`,`credential_id`\".",
"credentials_id": "The credentials ID.", "credential_id": "The credential's ID.",
"instance_id": "ID of the OpenSearch instance.", "instance_id": "ID of the OpenSearch instance.",
"project_id": "STACKIT project ID to which the instance is associated.", "project_id": "STACKIT project ID to which the instance is associated.",
} }
resp.Schema = schema.Schema{ resp.Schema = schema.Schema{
@ -89,8 +89,8 @@ func (r *credentialDataSource) Schema(_ context.Context, _ datasource.SchemaRequ
Description: descriptions["id"], Description: descriptions["id"],
Computed: true, Computed: true,
}, },
"credentials_id": schema.StringAttribute{ "credential_id": schema.StringAttribute{
Description: descriptions["credentials_id"], Description: descriptions["credential_id"],
Required: true, Required: true,
Validators: []validator.String{ Validators: []validator.String{
validate.UUID(), validate.UUID(),
@ -153,12 +153,12 @@ func (r *credentialDataSource) Read(ctx context.Context, req datasource.ReadRequ
} }
projectId := model.ProjectId.ValueString() projectId := model.ProjectId.ValueString()
instanceId := model.InstanceId.ValueString() instanceId := model.InstanceId.ValueString()
credentialsId := model.CredentialsId.ValueString() credentialId := model.CredentialId.ValueString()
ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "instance_id", instanceId) ctx = tflog.SetField(ctx, "instance_id", instanceId)
ctx = tflog.SetField(ctx, "credentials_id", credentialsId) ctx = tflog.SetField(ctx, "credential_id", credentialId)
recordSetResp, err := r.client.GetCredentials(ctx, projectId, instanceId, credentialsId).Execute() recordSetResp, err := r.client.GetCredentials(ctx, projectId, instanceId, credentialId).Execute()
if err != nil { if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading credential", fmt.Sprintf("Calling API: %v", err)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading credential", fmt.Sprintf("Calling API: %v", err))
return return
@ -177,5 +177,5 @@ func (r *credentialDataSource) Read(ctx context.Context, req datasource.ReadRequ
if resp.Diagnostics.HasError() { if resp.Diagnostics.HasError() {
return return
} }
tflog.Info(ctx, "OpenSearch credentials read") tflog.Info(ctx, "OpenSearch credential read")
} }

View file

@ -32,18 +32,18 @@ var (
) )
type Model struct { type Model struct {
Id types.String `tfsdk:"id"` // needed by TF Id types.String `tfsdk:"id"` // needed by TF
CredentialsId types.String `tfsdk:"credentials_id"` CredentialId types.String `tfsdk:"credential_id"`
InstanceId types.String `tfsdk:"instance_id"` InstanceId types.String `tfsdk:"instance_id"`
ProjectId types.String `tfsdk:"project_id"` ProjectId types.String `tfsdk:"project_id"`
Host types.String `tfsdk:"host"` Host types.String `tfsdk:"host"`
Hosts types.List `tfsdk:"hosts"` Hosts types.List `tfsdk:"hosts"`
HttpAPIURI types.String `tfsdk:"http_api_uri"` HttpAPIURI types.String `tfsdk:"http_api_uri"`
Name types.String `tfsdk:"name"` Name types.String `tfsdk:"name"`
Password types.String `tfsdk:"password"` Password types.String `tfsdk:"password"`
Port types.Int64 `tfsdk:"port"` Port types.Int64 `tfsdk:"port"`
Uri types.String `tfsdk:"uri"` Uri types.String `tfsdk:"uri"`
Username types.String `tfsdk:"username"` Username types.String `tfsdk:"username"`
} }
// NewCredentialResource is a helper function to simplify the provider implementation. // NewCredentialResource is a helper function to simplify the provider implementation.
@ -94,17 +94,17 @@ func (r *credentialResource) Configure(ctx context.Context, req resource.Configu
} }
r.client = apiClient r.client = apiClient
tflog.Info(ctx, "OpenSearch credentials client configured") tflog.Info(ctx, "OpenSearch credential client configured")
} }
// Schema defines the schema for the resource. // Schema defines the schema for the resource.
func (r *credentialResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { func (r *credentialResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
descriptions := map[string]string{ descriptions := map[string]string{
"main": "OpenSearch credential resource schema.", "main": "OpenSearch credential resource schema.",
"id": "Terraform's internal resource identifier. It is structured as \"`project_id`,`instance_id`,`credentials_id`\".", "id": "Terraform's internal resource identifier. It is structured as \"`project_id`,`instance_id`,`credential_id`\".",
"credentials_id": "The credentials ID.", "credential_id": "The credential's ID.",
"instance_id": "ID of the OpenSearch instance.", "instance_id": "ID of the OpenSearch instance.",
"project_id": "STACKIT Project ID to which the instance is associated.", "project_id": "STACKIT Project ID to which the instance is associated.",
} }
resp.Schema = schema.Schema{ resp.Schema = schema.Schema{
@ -117,8 +117,8 @@ func (r *credentialResource) Schema(_ context.Context, _ resource.SchemaRequest,
stringplanmodifier.UseStateForUnknown(), stringplanmodifier.UseStateForUnknown(),
}, },
}, },
"credentials_id": schema.StringAttribute{ "credential_id": schema.StringAttribute{
Description: descriptions["credentials_id"], Description: descriptions["credential_id"],
Computed: true, Computed: true,
PlanModifiers: []planmodifier.String{ PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(), stringplanmodifier.UseStateForUnknown(),
@ -202,13 +202,13 @@ func (r *credentialResource) Create(ctx context.Context, req resource.CreateRequ
return return
} }
if credentialsResp.Id == nil { if credentialsResp.Id == nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating credential", "Got empty credentials id") core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating credential", "Got empty credential id")
return return
} }
credentialsId := *credentialsResp.Id credentialId := *credentialsResp.Id
ctx = tflog.SetField(ctx, "credentials_id", credentialsId) ctx = tflog.SetField(ctx, "credential_id", credentialId)
wr, err := wait.CreateCredentialsWaitHandler(ctx, r.client, projectId, instanceId, credentialsId).SetTimeout(1 * time.Minute).WaitWithContext(ctx) wr, err := wait.CreateCredentialsWaitHandler(ctx, r.client, projectId, instanceId, credentialId).SetTimeout(1 * time.Minute).WaitWithContext(ctx)
if err != nil { if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating credential", fmt.Sprintf("Instance creation waiting: %v", err)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating credential", fmt.Sprintf("Instance creation waiting: %v", err))
return return
@ -230,7 +230,7 @@ func (r *credentialResource) Create(ctx context.Context, req resource.CreateRequ
if resp.Diagnostics.HasError() { if resp.Diagnostics.HasError() {
return return
} }
tflog.Info(ctx, "OpenSearch credentials created") tflog.Info(ctx, "OpenSearch credential created")
} }
// Read refreshes the Terraform state with the latest data. // Read refreshes the Terraform state with the latest data.
@ -243,12 +243,12 @@ func (r *credentialResource) Read(ctx context.Context, req resource.ReadRequest,
} }
projectId := model.ProjectId.ValueString() projectId := model.ProjectId.ValueString()
instanceId := model.InstanceId.ValueString() instanceId := model.InstanceId.ValueString()
credentialsId := model.CredentialsId.ValueString() credentialId := model.CredentialId.ValueString()
ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "instance_id", instanceId) ctx = tflog.SetField(ctx, "instance_id", instanceId)
ctx = tflog.SetField(ctx, "credentials_id", credentialsId) ctx = tflog.SetField(ctx, "credential_id", credentialId)
recordSetResp, err := r.client.GetCredentials(ctx, projectId, instanceId, credentialsId).Execute() recordSetResp, err := r.client.GetCredentials(ctx, projectId, instanceId, credentialId).Execute()
if err != nil { if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading credential", fmt.Sprintf("Calling API: %v", err)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading credential", fmt.Sprintf("Calling API: %v", err))
return return
@ -267,7 +267,7 @@ func (r *credentialResource) Read(ctx context.Context, req resource.ReadRequest,
if resp.Diagnostics.HasError() { if resp.Diagnostics.HasError() {
return return
} }
tflog.Info(ctx, "OpenSearch credentials read") tflog.Info(ctx, "OpenSearch credential read")
} }
// Update updates the resource and sets the updated Terraform state on success. // Update updates the resource and sets the updated Terraform state on success.
@ -287,40 +287,40 @@ func (r *credentialResource) Delete(ctx context.Context, req resource.DeleteRequ
projectId := model.ProjectId.ValueString() projectId := model.ProjectId.ValueString()
instanceId := model.InstanceId.ValueString() instanceId := model.InstanceId.ValueString()
credentialsId := model.CredentialsId.ValueString() credentialId := model.CredentialId.ValueString()
ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "instance_id", instanceId) ctx = tflog.SetField(ctx, "instance_id", instanceId)
ctx = tflog.SetField(ctx, "credentials_id", credentialsId) ctx = tflog.SetField(ctx, "credential_id", credentialId)
// Delete existing record set // Delete existing record set
err := r.client.DeleteCredentials(ctx, projectId, instanceId, credentialsId).Execute() err := r.client.DeleteCredentials(ctx, projectId, instanceId, credentialId).Execute()
if err != nil { if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting credential", fmt.Sprintf("Calling API: %v", err)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting credential", fmt.Sprintf("Calling API: %v", err))
} }
_, err = wait.DeleteCredentialsWaitHandler(ctx, r.client, projectId, instanceId, credentialsId).SetTimeout(1 * time.Minute).WaitWithContext(ctx) _, err = wait.DeleteCredentialsWaitHandler(ctx, r.client, projectId, instanceId, credentialId).SetTimeout(1 * time.Minute).WaitWithContext(ctx)
if err != nil { if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting credential", fmt.Sprintf("Instance deletion waiting: %v", err)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting credential", fmt.Sprintf("Instance deletion waiting: %v", err))
return return
} }
tflog.Info(ctx, "OpenSearch credentials deleted") tflog.Info(ctx, "OpenSearch credential deleted")
} }
// ImportState imports a resource into the Terraform state on success. // ImportState imports a resource into the Terraform state on success.
// The expected format of the resource import identifier is: project_id,instance_id,credentials_id // The expected format of the resource import identifier is: project_id,instance_id,credential_id
func (r *credentialResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { func (r *credentialResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
idParts := strings.Split(req.ID, core.Separator) idParts := strings.Split(req.ID, core.Separator)
if len(idParts) != 3 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" { if len(idParts) != 3 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" {
core.LogAndAddError(ctx, &resp.Diagnostics, core.LogAndAddError(ctx, &resp.Diagnostics,
"Error importing credential", "Error importing credential",
fmt.Sprintf("Expected import identifier with format [project_id],[instance_id],[credentials_id], got %q", req.ID), fmt.Sprintf("Expected import identifier with format [project_id],[instance_id],[credential_id], got %q", req.ID),
) )
return return
} }
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), idParts[0])...) resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), idParts[0])...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("instance_id"), idParts[1])...) resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("instance_id"), idParts[1])...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("credentials_id"), idParts[2])...) resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("credential_id"), idParts[2])...)
tflog.Info(ctx, "OpenSearch credentials state imported") tflog.Info(ctx, "OpenSearch credential state imported")
} }
func mapFields(credentialsResp *opensearch.CredentialsResponse, model *Model) error { func mapFields(credentialsResp *opensearch.CredentialsResponse, model *Model) error {
@ -335,11 +335,11 @@ func mapFields(credentialsResp *opensearch.CredentialsResponse, model *Model) er
} }
credentials := credentialsResp.Raw.Credentials credentials := credentialsResp.Raw.Credentials
var credentialsId string var credentialId string
if model.CredentialsId.ValueString() != "" { if model.CredentialId.ValueString() != "" {
credentialsId = model.CredentialsId.ValueString() credentialId = model.CredentialId.ValueString()
} else if credentialsResp.Id != nil { } else if credentialsResp.Id != nil {
credentialsId = *credentialsResp.Id credentialId = *credentialsResp.Id
} else { } else {
return fmt.Errorf("credentials id not present") return fmt.Errorf("credentials id not present")
} }
@ -347,12 +347,12 @@ func mapFields(credentialsResp *opensearch.CredentialsResponse, model *Model) er
idParts := []string{ idParts := []string{
model.ProjectId.ValueString(), model.ProjectId.ValueString(),
model.InstanceId.ValueString(), model.InstanceId.ValueString(),
credentialsId, credentialId,
} }
model.Id = types.StringValue( model.Id = types.StringValue(
strings.Join(idParts, core.Separator), strings.Join(idParts, core.Separator),
) )
model.CredentialsId = types.StringValue(credentialsId) model.CredentialId = types.StringValue(credentialId)
model.Hosts = types.ListNull(types.StringType) model.Hosts = types.ListNull(types.StringType)
if credentials != nil { if credentials != nil {
if credentials.Hosts != nil { if credentials.Hosts != nil {

View file

@ -24,18 +24,18 @@ func TestMapFields(t *testing.T) {
Raw: &opensearch.RawCredentials{}, Raw: &opensearch.RawCredentials{},
}, },
Model{ Model{
Id: types.StringValue("pid,iid,cid"), Id: types.StringValue("pid,iid,cid"),
CredentialsId: types.StringValue("cid"), CredentialId: types.StringValue("cid"),
InstanceId: types.StringValue("iid"), InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"), ProjectId: types.StringValue("pid"),
Host: types.StringNull(), Host: types.StringNull(),
Hosts: types.ListNull(types.StringType), Hosts: types.ListNull(types.StringType),
HttpAPIURI: types.StringNull(), HttpAPIURI: types.StringNull(),
Name: types.StringNull(), Name: types.StringNull(),
Password: types.StringNull(), Password: types.StringNull(),
Port: types.Int64Null(), Port: types.Int64Null(),
Uri: types.StringNull(), Uri: types.StringNull(),
Username: types.StringNull(), Username: types.StringNull(),
}, },
true, true,
}, },
@ -60,11 +60,11 @@ func TestMapFields(t *testing.T) {
}, },
}, },
Model{ Model{
Id: types.StringValue("pid,iid,cid"), Id: types.StringValue("pid,iid,cid"),
CredentialsId: types.StringValue("cid"), CredentialId: types.StringValue("cid"),
InstanceId: types.StringValue("iid"), InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"), ProjectId: types.StringValue("pid"),
Host: types.StringValue("host"), Host: types.StringValue("host"),
Hosts: types.ListValueMust(types.StringType, []attr.Value{ Hosts: types.ListValueMust(types.StringType, []attr.Value{
types.StringValue("host_1"), types.StringValue("host_1"),
types.StringValue(""), types.StringValue(""),
@ -96,18 +96,18 @@ func TestMapFields(t *testing.T) {
}, },
}, },
Model{ Model{
Id: types.StringValue("pid,iid,cid"), Id: types.StringValue("pid,iid,cid"),
CredentialsId: types.StringValue("cid"), CredentialId: types.StringValue("cid"),
InstanceId: types.StringValue("iid"), InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"), ProjectId: types.StringValue("pid"),
Host: types.StringValue(""), Host: types.StringValue(""),
Hosts: types.ListValueMust(types.StringType, []attr.Value{}), Hosts: types.ListValueMust(types.StringType, []attr.Value{}),
HttpAPIURI: types.StringNull(), HttpAPIURI: types.StringNull(),
Name: types.StringNull(), Name: types.StringNull(),
Password: types.StringValue(""), Password: types.StringValue(""),
Port: types.Int64Value(2123456789), Port: types.Int64Value(2123456789),
Uri: types.StringNull(), Uri: types.StringNull(),
Username: types.StringValue(""), Username: types.StringValue(""),
}, },
true, true,
}, },

View file

@ -37,7 +37,7 @@ func resourceConfig() string {
version = "%s" version = "%s"
} }
resource "stackit_opensearch_credential" "credentials" { resource "stackit_opensearch_credential" "credential" {
project_id = stackit_opensearch_instance.instance.project_id project_id = stackit_opensearch_instance.instance.project_id
instance_id = stackit_opensearch_instance.instance.instance_id instance_id = stackit_opensearch_instance.instance.instance_id
} }
@ -64,7 +64,7 @@ func resourceConfigUpdate() string {
} }
} }
resource "stackit_opensearch_credential" "credentials" { resource "stackit_opensearch_credential" "credential" {
project_id = stackit_opensearch_instance.instance.project_id project_id = stackit_opensearch_instance.instance.project_id
instance_id = stackit_opensearch_instance.instance.instance_id instance_id = stackit_opensearch_instance.instance.instance_id
} }
@ -97,17 +97,17 @@ func TestAccOpenSearchResource(t *testing.T) {
resource.TestCheckResourceAttr("stackit_opensearch_instance.instance", "name", instanceResource["name"]), resource.TestCheckResourceAttr("stackit_opensearch_instance.instance", "name", instanceResource["name"]),
resource.TestCheckResourceAttrSet("stackit_opensearch_instance.instance", "parameters.sgw_acl"), resource.TestCheckResourceAttrSet("stackit_opensearch_instance.instance", "parameters.sgw_acl"),
// Credentials data // Credential data
resource.TestCheckResourceAttrPair( resource.TestCheckResourceAttrPair(
"stackit_opensearch_credential.credentials", "project_id", "stackit_opensearch_credential.credential", "project_id",
"stackit_opensearch_instance.instance", "project_id", "stackit_opensearch_instance.instance", "project_id",
), ),
resource.TestCheckResourceAttrPair( resource.TestCheckResourceAttrPair(
"stackit_opensearch_credential.credentials", "instance_id", "stackit_opensearch_credential.credential", "instance_id",
"stackit_opensearch_instance.instance", "instance_id", "stackit_opensearch_instance.instance", "instance_id",
), ),
resource.TestCheckResourceAttrSet("stackit_opensearch_credential.credentials", "credentials_id"), resource.TestCheckResourceAttrSet("stackit_opensearch_credential.credential", "credential_id"),
resource.TestCheckResourceAttrSet("stackit_opensearch_credential.credentials", "host"), resource.TestCheckResourceAttrSet("stackit_opensearch_credential.credential", "host"),
), ),
}, },
// Data source // Data source
@ -120,10 +120,10 @@ func TestAccOpenSearchResource(t *testing.T) {
instance_id = stackit_opensearch_instance.instance.instance_id instance_id = stackit_opensearch_instance.instance.instance_id
} }
data "stackit_opensearch_credential" "credentials" { data "stackit_opensearch_credential" "credential" {
project_id = stackit_opensearch_credential.credentials.project_id project_id = stackit_opensearch_credential.credential.project_id
instance_id = stackit_opensearch_credential.credentials.instance_id instance_id = stackit_opensearch_credential.credential.instance_id
credentials_id = stackit_opensearch_credential.credentials.credentials_id credential_id = stackit_opensearch_credential.credential.credential_id
}`, }`,
resourceConfig(), resourceConfig(),
), ),
@ -132,18 +132,18 @@ func TestAccOpenSearchResource(t *testing.T) {
resource.TestCheckResourceAttr("data.stackit_opensearch_instance.instance", "project_id", instanceResource["project_id"]), resource.TestCheckResourceAttr("data.stackit_opensearch_instance.instance", "project_id", instanceResource["project_id"]),
resource.TestCheckResourceAttrPair("stackit_opensearch_instance.instance", "instance_id", resource.TestCheckResourceAttrPair("stackit_opensearch_instance.instance", "instance_id",
"data.stackit_opensearch_instance.instance", "instance_id"), "data.stackit_opensearch_instance.instance", "instance_id"),
resource.TestCheckResourceAttrPair("stackit_opensearch_credential.credentials", "credentials_id", resource.TestCheckResourceAttrPair("stackit_opensearch_credential.credential", "credential_id",
"data.stackit_opensearch_credential.credentials", "credentials_id"), "data.stackit_opensearch_credential.credential", "credential_id"),
resource.TestCheckResourceAttr("data.stackit_opensearch_instance.instance", "plan_id", instanceResource["plan_id"]), resource.TestCheckResourceAttr("data.stackit_opensearch_instance.instance", "plan_id", instanceResource["plan_id"]),
resource.TestCheckResourceAttr("data.stackit_opensearch_instance.instance", "name", instanceResource["name"]), resource.TestCheckResourceAttr("data.stackit_opensearch_instance.instance", "name", instanceResource["name"]),
resource.TestCheckResourceAttrSet("data.stackit_opensearch_instance.instance", "parameters.sgw_acl"), resource.TestCheckResourceAttrSet("data.stackit_opensearch_instance.instance", "parameters.sgw_acl"),
// Credentials data // Credential data
resource.TestCheckResourceAttr("data.stackit_opensearch_credential.credentials", "project_id", instanceResource["project_id"]), resource.TestCheckResourceAttr("data.stackit_opensearch_credential.credential", "project_id", instanceResource["project_id"]),
resource.TestCheckResourceAttrSet("data.stackit_opensearch_credential.credentials", "credentials_id"), resource.TestCheckResourceAttrSet("data.stackit_opensearch_credential.credential", "credential_id"),
resource.TestCheckResourceAttrSet("data.stackit_opensearch_credential.credentials", "host"), resource.TestCheckResourceAttrSet("data.stackit_opensearch_credential.credential", "host"),
resource.TestCheckResourceAttrSet("data.stackit_opensearch_credential.credentials", "port"), resource.TestCheckResourceAttrSet("data.stackit_opensearch_credential.credential", "port"),
resource.TestCheckResourceAttrSet("data.stackit_opensearch_credential.credentials", "uri"), resource.TestCheckResourceAttrSet("data.stackit_opensearch_credential.credential", "uri"),
), ),
}, },
// Import // Import
@ -165,21 +165,21 @@ func TestAccOpenSearchResource(t *testing.T) {
ImportStateVerify: true, ImportStateVerify: true,
}, },
{ {
ResourceName: "stackit_opensearch_credential.credentials", ResourceName: "stackit_opensearch_credential.credential",
ImportStateIdFunc: func(s *terraform.State) (string, error) { ImportStateIdFunc: func(s *terraform.State) (string, error) {
r, ok := s.RootModule().Resources["stackit_opensearch_credential.credentials"] r, ok := s.RootModule().Resources["stackit_opensearch_credential.credential"]
if !ok { if !ok {
return "", fmt.Errorf("couldn't find resource stackit_opensearch_credential.credentials") return "", fmt.Errorf("couldn't find resource stackit_opensearch_credential.credential")
} }
instanceId, ok := r.Primary.Attributes["instance_id"] instanceId, ok := r.Primary.Attributes["instance_id"]
if !ok { if !ok {
return "", fmt.Errorf("couldn't find attribute instance_id") return "", fmt.Errorf("couldn't find attribute instance_id")
} }
credentialsId, ok := r.Primary.Attributes["credentials_id"] credentialId, ok := r.Primary.Attributes["credential_id"]
if !ok { if !ok {
return "", fmt.Errorf("couldn't find attribute credentials_id") return "", fmt.Errorf("couldn't find attribute credential_id")
} }
return fmt.Sprintf("%s,%s,%s", testutil.ProjectId, instanceId, credentialsId), nil return fmt.Sprintf("%s,%s,%s", testutil.ProjectId, instanceId, credentialId), nil
}, },
ImportState: true, ImportState: true,
ImportStateVerify: true, ImportStateVerify: true,

View file

@ -69,17 +69,17 @@ func (r *credentialDataSource) Configure(ctx context.Context, req datasource.Con
} }
r.client = apiClient r.client = apiClient
tflog.Info(ctx, "PostgreSQL credentials client configured") tflog.Info(ctx, "PostgreSQL credential client configured")
} }
// Schema defines the schema for the data source. // Schema defines the schema for the data source.
func (r *credentialDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { func (r *credentialDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
descriptions := map[string]string{ descriptions := map[string]string{
"main": "PostgreSQL credential data source schema.", "main": "PostgreSQL credential data source schema.",
"id": "Terraform's internal data source. identifier. It is structured as \"`project_id`,`instance_id`,`credentials_id`\".", "id": "Terraform's internal data source. identifier. It is structured as \"`project_id`,`instance_id`,`credential_id`\".",
"credentials_id": "The credentials ID.", "credential_id": "The credential's ID.",
"instance_id": "ID of the PostgreSQL instance.", "instance_id": "ID of the PostgreSQL instance.",
"project_id": "STACKIT project ID to which the instance is associated.", "project_id": "STACKIT project ID to which the instance is associated.",
} }
resp.Schema = schema.Schema{ resp.Schema = schema.Schema{
@ -89,8 +89,8 @@ func (r *credentialDataSource) Schema(_ context.Context, _ datasource.SchemaRequ
Description: descriptions["id"], Description: descriptions["id"],
Computed: true, Computed: true,
}, },
"credentials_id": schema.StringAttribute{ "credential_id": schema.StringAttribute{
Description: descriptions["credentials_id"], Description: descriptions["credential_id"],
Required: true, Required: true,
Validators: []validator.String{ Validators: []validator.String{
validate.UUID(), validate.UUID(),
@ -153,12 +153,12 @@ func (r *credentialDataSource) Read(ctx context.Context, req datasource.ReadRequ
} }
projectId := model.ProjectId.ValueString() projectId := model.ProjectId.ValueString()
instanceId := model.InstanceId.ValueString() instanceId := model.InstanceId.ValueString()
credentialsId := model.CredentialsId.ValueString() credentialId := model.CredentialId.ValueString()
ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "instance_id", instanceId) ctx = tflog.SetField(ctx, "instance_id", instanceId)
ctx = tflog.SetField(ctx, "credentials_id", credentialsId) ctx = tflog.SetField(ctx, "credential_id", credentialId)
recordSetResp, err := r.client.GetCredentials(ctx, projectId, instanceId, credentialsId).Execute() recordSetResp, err := r.client.GetCredentials(ctx, projectId, instanceId, credentialId).Execute()
if err != nil { if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading credential", fmt.Sprintf("Calling API: %v", err)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading credential", fmt.Sprintf("Calling API: %v", err))
return return
@ -177,5 +177,5 @@ func (r *credentialDataSource) Read(ctx context.Context, req datasource.ReadRequ
if resp.Diagnostics.HasError() { if resp.Diagnostics.HasError() {
return return
} }
tflog.Info(ctx, "PostgreSQL credentials read") tflog.Info(ctx, "PostgreSQL credential read")
} }

View file

@ -32,18 +32,18 @@ var (
) )
type Model struct { type Model struct {
Id types.String `tfsdk:"id"` // needed by TF Id types.String `tfsdk:"id"` // needed by TF
CredentialsId types.String `tfsdk:"credentials_id"` CredentialId types.String `tfsdk:"credential_id"`
InstanceId types.String `tfsdk:"instance_id"` InstanceId types.String `tfsdk:"instance_id"`
ProjectId types.String `tfsdk:"project_id"` ProjectId types.String `tfsdk:"project_id"`
Host types.String `tfsdk:"host"` Host types.String `tfsdk:"host"`
Hosts types.List `tfsdk:"hosts"` Hosts types.List `tfsdk:"hosts"`
HttpAPIURI types.String `tfsdk:"http_api_uri"` HttpAPIURI types.String `tfsdk:"http_api_uri"`
Name types.String `tfsdk:"name"` Name types.String `tfsdk:"name"`
Password types.String `tfsdk:"password"` Password types.String `tfsdk:"password"`
Port types.Int64 `tfsdk:"port"` Port types.Int64 `tfsdk:"port"`
Uri types.String `tfsdk:"uri"` Uri types.String `tfsdk:"uri"`
Username types.String `tfsdk:"username"` Username types.String `tfsdk:"username"`
} }
// NewCredentialResource is a helper function to simplify the provider implementation. // NewCredentialResource is a helper function to simplify the provider implementation.
@ -94,17 +94,17 @@ func (r *credentialResource) Configure(ctx context.Context, req resource.Configu
} }
r.client = apiClient r.client = apiClient
tflog.Info(ctx, "PostgreSQL credentials client configured") tflog.Info(ctx, "PostgreSQL credential client configured")
} }
// Schema defines the schema for the resource. // Schema defines the schema for the resource.
func (r *credentialResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { func (r *credentialResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
descriptions := map[string]string{ descriptions := map[string]string{
"main": "PostgreSQL credential resource schema.", "main": "PostgreSQL credential resource schema.",
"id": "Terraform's internal resource identifier. It is structured as \"`project_id`,`instance_id`,`credentials_id`\".", "id": "Terraform's internal resource identifier. It is structured as \"`project_id`,`instance_id`,`credential_id`\".",
"credentials_id": "The credentials ID.", "credential_id": "The credential's ID.",
"instance_id": "ID of the PostgreSQL instance.", "instance_id": "ID of the PostgreSQL instance.",
"project_id": "STACKIT Project ID to which the instance is associated.", "project_id": "STACKIT Project ID to which the instance is associated.",
} }
resp.Schema = schema.Schema{ resp.Schema = schema.Schema{
@ -117,8 +117,8 @@ func (r *credentialResource) Schema(_ context.Context, _ resource.SchemaRequest,
stringplanmodifier.UseStateForUnknown(), stringplanmodifier.UseStateForUnknown(),
}, },
}, },
"credentials_id": schema.StringAttribute{ "credential_id": schema.StringAttribute{
Description: descriptions["credentials_id"], Description: descriptions["credential_id"],
Computed: true, Computed: true,
PlanModifiers: []planmodifier.String{ PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(), stringplanmodifier.UseStateForUnknown(),
@ -202,13 +202,13 @@ func (r *credentialResource) Create(ctx context.Context, req resource.CreateRequ
return return
} }
if credentialsResp.Id == nil { if credentialsResp.Id == nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating credential", "Got empty credentials id") core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating credential", "Got empty credential id")
return return
} }
credentialsId := *credentialsResp.Id credentialId := *credentialsResp.Id
ctx = tflog.SetField(ctx, "credentials_id", credentialsId) ctx = tflog.SetField(ctx, "credential_id", credentialId)
wr, err := wait.CreateCredentialsWaitHandler(ctx, r.client, projectId, instanceId, credentialsId).SetTimeout(1 * time.Minute).WaitWithContext(ctx) wr, err := wait.CreateCredentialsWaitHandler(ctx, r.client, projectId, instanceId, credentialId).SetTimeout(1 * time.Minute).WaitWithContext(ctx)
if err != nil { if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating credential", fmt.Sprintf("Instance creation waiting: %v", err)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating credential", fmt.Sprintf("Instance creation waiting: %v", err))
return return
@ -230,7 +230,7 @@ func (r *credentialResource) Create(ctx context.Context, req resource.CreateRequ
if resp.Diagnostics.HasError() { if resp.Diagnostics.HasError() {
return return
} }
tflog.Info(ctx, "PostgreSQL credentials created") tflog.Info(ctx, "PostgreSQL credential created")
} }
// Read refreshes the Terraform state with the latest data. // Read refreshes the Terraform state with the latest data.
@ -243,12 +243,12 @@ func (r *credentialResource) Read(ctx context.Context, req resource.ReadRequest,
} }
projectId := model.ProjectId.ValueString() projectId := model.ProjectId.ValueString()
instanceId := model.InstanceId.ValueString() instanceId := model.InstanceId.ValueString()
credentialsId := model.CredentialsId.ValueString() credentialId := model.CredentialId.ValueString()
ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "instance_id", instanceId) ctx = tflog.SetField(ctx, "instance_id", instanceId)
ctx = tflog.SetField(ctx, "credentials_id", credentialsId) ctx = tflog.SetField(ctx, "credential_id", credentialId)
recordSetResp, err := r.client.GetCredentials(ctx, projectId, instanceId, credentialsId).Execute() recordSetResp, err := r.client.GetCredentials(ctx, projectId, instanceId, credentialId).Execute()
if err != nil { if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading credential", fmt.Sprintf("Calling API: %v", err)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading credential", fmt.Sprintf("Calling API: %v", err))
return return
@ -267,7 +267,7 @@ func (r *credentialResource) Read(ctx context.Context, req resource.ReadRequest,
if resp.Diagnostics.HasError() { if resp.Diagnostics.HasError() {
return return
} }
tflog.Info(ctx, "PostgreSQL credentials read") tflog.Info(ctx, "PostgreSQL credential read")
} }
// Update updates the resource and sets the updated Terraform state on success. // Update updates the resource and sets the updated Terraform state on success.
@ -287,40 +287,40 @@ func (r *credentialResource) Delete(ctx context.Context, req resource.DeleteRequ
projectId := model.ProjectId.ValueString() projectId := model.ProjectId.ValueString()
instanceId := model.InstanceId.ValueString() instanceId := model.InstanceId.ValueString()
credentialsId := model.CredentialsId.ValueString() credentialId := model.CredentialId.ValueString()
ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "instance_id", instanceId) ctx = tflog.SetField(ctx, "instance_id", instanceId)
ctx = tflog.SetField(ctx, "credentials_id", credentialsId) ctx = tflog.SetField(ctx, "credential_id", credentialId)
// Delete existing record set // Delete existing record set
err := r.client.DeleteCredentials(ctx, projectId, instanceId, credentialsId).Execute() err := r.client.DeleteCredentials(ctx, projectId, instanceId, credentialId).Execute()
if err != nil { if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting credential", fmt.Sprintf("Calling API: %v", err)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting credential", fmt.Sprintf("Calling API: %v", err))
} }
_, err = wait.DeleteCredentialsWaitHandler(ctx, r.client, projectId, instanceId, credentialsId).SetTimeout(1 * time.Minute).WaitWithContext(ctx) _, err = wait.DeleteCredentialsWaitHandler(ctx, r.client, projectId, instanceId, credentialId).SetTimeout(1 * time.Minute).WaitWithContext(ctx)
if err != nil { if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting credential", fmt.Sprintf("Instance deletion waiting: %v", err)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting credential", fmt.Sprintf("Instance deletion waiting: %v", err))
return return
} }
tflog.Info(ctx, "PostgreSQL credentials deleted") tflog.Info(ctx, "PostgreSQL credential deleted")
} }
// ImportState imports a resource into the Terraform state on success. // ImportState imports a resource into the Terraform state on success.
// The expected format of the resource import identifier is: project_id,instance_id,credentials_id // The expected format of the resource import identifier is: project_id,instance_id,credential_id
func (r *credentialResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { func (r *credentialResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
idParts := strings.Split(req.ID, core.Separator) idParts := strings.Split(req.ID, core.Separator)
if len(idParts) != 3 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" { if len(idParts) != 3 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" {
core.LogAndAddError(ctx, &resp.Diagnostics, core.LogAndAddError(ctx, &resp.Diagnostics,
"Error importing credential", "Error importing credential",
fmt.Sprintf("Expected import identifier with format [project_id],[instance_id],[credentials_id], got %q", req.ID), fmt.Sprintf("Expected import identifier with format [project_id],[instance_id],[credential_id], got %q", req.ID),
) )
return return
} }
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), idParts[0])...) resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), idParts[0])...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("instance_id"), idParts[1])...) resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("instance_id"), idParts[1])...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("credentials_id"), idParts[2])...) resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("credential_id"), idParts[2])...)
tflog.Info(ctx, "PostgreSQL credentials state imported") tflog.Info(ctx, "PostgreSQL credential state imported")
} }
func mapFields(credentialsResp *postgresql.CredentialsResponse, model *Model) error { func mapFields(credentialsResp *postgresql.CredentialsResponse, model *Model) error {
@ -335,11 +335,11 @@ func mapFields(credentialsResp *postgresql.CredentialsResponse, model *Model) er
} }
credentials := credentialsResp.Raw.Credentials credentials := credentialsResp.Raw.Credentials
var credentialsId string var credentialId string
if model.CredentialsId.ValueString() != "" { if model.CredentialId.ValueString() != "" {
credentialsId = model.CredentialsId.ValueString() credentialId = model.CredentialId.ValueString()
} else if credentialsResp.Id != nil { } else if credentialsResp.Id != nil {
credentialsId = *credentialsResp.Id credentialId = *credentialsResp.Id
} else { } else {
return fmt.Errorf("credentials id not present") return fmt.Errorf("credentials id not present")
} }
@ -347,12 +347,12 @@ func mapFields(credentialsResp *postgresql.CredentialsResponse, model *Model) er
idParts := []string{ idParts := []string{
model.ProjectId.ValueString(), model.ProjectId.ValueString(),
model.InstanceId.ValueString(), model.InstanceId.ValueString(),
credentialsId, credentialId,
} }
model.Id = types.StringValue( model.Id = types.StringValue(
strings.Join(idParts, core.Separator), strings.Join(idParts, core.Separator),
) )
model.CredentialsId = types.StringValue(credentialsId) model.CredentialId = types.StringValue(credentialId)
model.Hosts = types.ListNull(types.StringType) model.Hosts = types.ListNull(types.StringType)
if credentials != nil { if credentials != nil {
if credentials.Hosts != nil { if credentials.Hosts != nil {

View file

@ -24,18 +24,18 @@ func TestMapFields(t *testing.T) {
Raw: &postgresql.RawCredentials{}, Raw: &postgresql.RawCredentials{},
}, },
Model{ Model{
Id: types.StringValue("pid,iid,cid"), Id: types.StringValue("pid,iid,cid"),
CredentialsId: types.StringValue("cid"), CredentialId: types.StringValue("cid"),
InstanceId: types.StringValue("iid"), InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"), ProjectId: types.StringValue("pid"),
Host: types.StringNull(), Host: types.StringNull(),
Hosts: types.ListNull(types.StringType), Hosts: types.ListNull(types.StringType),
HttpAPIURI: types.StringNull(), HttpAPIURI: types.StringNull(),
Name: types.StringNull(), Name: types.StringNull(),
Password: types.StringNull(), Password: types.StringNull(),
Port: types.Int64Null(), Port: types.Int64Null(),
Uri: types.StringNull(), Uri: types.StringNull(),
Username: types.StringNull(), Username: types.StringNull(),
}, },
true, true,
}, },
@ -60,11 +60,11 @@ func TestMapFields(t *testing.T) {
}, },
}, },
Model{ Model{
Id: types.StringValue("pid,iid,cid"), Id: types.StringValue("pid,iid,cid"),
CredentialsId: types.StringValue("cid"), CredentialId: types.StringValue("cid"),
InstanceId: types.StringValue("iid"), InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"), ProjectId: types.StringValue("pid"),
Host: types.StringValue("host"), Host: types.StringValue("host"),
Hosts: types.ListValueMust(types.StringType, []attr.Value{ Hosts: types.ListValueMust(types.StringType, []attr.Value{
types.StringValue("host_1"), types.StringValue("host_1"),
types.StringValue(""), types.StringValue(""),
@ -96,18 +96,18 @@ func TestMapFields(t *testing.T) {
}, },
}, },
Model{ Model{
Id: types.StringValue("pid,iid,cid"), Id: types.StringValue("pid,iid,cid"),
CredentialsId: types.StringValue("cid"), CredentialId: types.StringValue("cid"),
InstanceId: types.StringValue("iid"), InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"), ProjectId: types.StringValue("pid"),
Host: types.StringValue(""), Host: types.StringValue(""),
Hosts: types.ListValueMust(types.StringType, []attr.Value{}), Hosts: types.ListValueMust(types.StringType, []attr.Value{}),
HttpAPIURI: types.StringNull(), HttpAPIURI: types.StringNull(),
Name: types.StringNull(), Name: types.StringNull(),
Password: types.StringNull(), Password: types.StringNull(),
Port: types.Int64Value(2123456789), Port: types.Int64Value(2123456789),
Uri: types.StringNull(), Uri: types.StringNull(),
Username: types.StringNull(), Username: types.StringNull(),
}, },
true, true,
}, },

View file

@ -47,7 +47,7 @@ func resourceConfig(acls, frequency, plugins string) string {
} }
} }
resource "stackit_postgresql_credential" "credentials" { resource "stackit_postgresql_credential" "credential" {
project_id = stackit_postgresql_instance.instance.project_id project_id = stackit_postgresql_instance.instance.project_id
instance_id = stackit_postgresql_instance.instance.instance_id instance_id = stackit_postgresql_instance.instance.instance_id
} }
@ -81,17 +81,17 @@ func TestAccPostgreSQLResource(t *testing.T) {
resource.TestCheckResourceAttr("stackit_postgresql_instance.instance", "name", instanceResource["name"]), resource.TestCheckResourceAttr("stackit_postgresql_instance.instance", "name", instanceResource["name"]),
resource.TestCheckResourceAttr("stackit_postgresql_instance.instance", "parameters.sgw_acl", instanceResource["sgw_acl"]), resource.TestCheckResourceAttr("stackit_postgresql_instance.instance", "parameters.sgw_acl", instanceResource["sgw_acl"]),
// Credentials data // Credential data
resource.TestCheckResourceAttrPair( resource.TestCheckResourceAttrPair(
"stackit_postgresql_credential.credentials", "project_id", "stackit_postgresql_credential.credential", "project_id",
"stackit_postgresql_instance.instance", "project_id", "stackit_postgresql_instance.instance", "project_id",
), ),
resource.TestCheckResourceAttrPair( resource.TestCheckResourceAttrPair(
"stackit_postgresql_credential.credentials", "instance_id", "stackit_postgresql_credential.credential", "instance_id",
"stackit_postgresql_instance.instance", "instance_id", "stackit_postgresql_instance.instance", "instance_id",
), ),
resource.TestCheckResourceAttrSet("stackit_postgresql_credential.credentials", "credentials_id"), resource.TestCheckResourceAttrSet("stackit_postgresql_credential.credential", "credential_id"),
resource.TestCheckResourceAttrSet("stackit_postgresql_credential.credentials", "host"), resource.TestCheckResourceAttrSet("stackit_postgresql_credential.credential", "host"),
), ),
}, },
{ // Data source { // Data source
@ -103,10 +103,10 @@ func TestAccPostgreSQLResource(t *testing.T) {
instance_id = stackit_postgresql_instance.instance.instance_id instance_id = stackit_postgresql_instance.instance.instance_id
} }
data "stackit_postgresql_credential" "credentials" { data "stackit_postgresql_credential" "credential" {
project_id = stackit_postgresql_credential.credentials.project_id project_id = stackit_postgresql_credential.credential.project_id
instance_id = stackit_postgresql_credential.credentials.instance_id instance_id = stackit_postgresql_credential.credential.instance_id
credentials_id = stackit_postgresql_credential.credentials.credentials_id credential_id = stackit_postgresql_credential.credential.credential_id
}`, }`,
resourceConfig(instanceResource["sgw_acl"], instanceResource["metrics_frequency"], instanceResource["plugins"]), resourceConfig(instanceResource["sgw_acl"], instanceResource["metrics_frequency"], instanceResource["plugins"]),
), ),
@ -115,20 +115,20 @@ func TestAccPostgreSQLResource(t *testing.T) {
resource.TestCheckResourceAttr("data.stackit_postgresql_instance.instance", "project_id", instanceResource["project_id"]), resource.TestCheckResourceAttr("data.stackit_postgresql_instance.instance", "project_id", instanceResource["project_id"]),
resource.TestCheckResourceAttrPair("stackit_postgresql_instance.instance", "instance_id", resource.TestCheckResourceAttrPair("stackit_postgresql_instance.instance", "instance_id",
"data.stackit_postgresql_instance.instance", "instance_id"), "data.stackit_postgresql_instance.instance", "instance_id"),
resource.TestCheckResourceAttrPair("stackit_postgresql_credential.credentials", "credentials_id", resource.TestCheckResourceAttrPair("stackit_postgresql_credential.credential", "credential_id",
"data.stackit_postgresql_credential.credentials", "credentials_id"), "data.stackit_postgresql_credential.credential", "credential_id"),
resource.TestCheckResourceAttr("data.stackit_postgresql_instance.instance", "plan_id", instanceResource["plan_id"]), resource.TestCheckResourceAttr("data.stackit_postgresql_instance.instance", "plan_id", instanceResource["plan_id"]),
resource.TestCheckResourceAttr("data.stackit_postgresql_instance.instance", "name", instanceResource["name"]), resource.TestCheckResourceAttr("data.stackit_postgresql_instance.instance", "name", instanceResource["name"]),
resource.TestCheckResourceAttr("data.stackit_postgresql_instance.instance", "parameters.sgw_acl", instanceResource["sgw_acl"]), resource.TestCheckResourceAttr("data.stackit_postgresql_instance.instance", "parameters.sgw_acl", instanceResource["sgw_acl"]),
resource.TestCheckResourceAttr("data.stackit_postgresql_instance.instance", "parameters.plugins.#", "1"), resource.TestCheckResourceAttr("data.stackit_postgresql_instance.instance", "parameters.plugins.#", "1"),
resource.TestCheckResourceAttr("data.stackit_postgresql_instance.instance", "parameters.plugins.0", instanceResource["plugins"]), resource.TestCheckResourceAttr("data.stackit_postgresql_instance.instance", "parameters.plugins.0", instanceResource["plugins"]),
// Credentials data // Credential data
resource.TestCheckResourceAttr("data.stackit_postgresql_credential.credentials", "project_id", instanceResource["project_id"]), resource.TestCheckResourceAttr("data.stackit_postgresql_credential.credential", "project_id", instanceResource["project_id"]),
resource.TestCheckResourceAttrSet("data.stackit_postgresql_credential.credentials", "credentials_id"), resource.TestCheckResourceAttrSet("data.stackit_postgresql_credential.credential", "credential_id"),
resource.TestCheckResourceAttrSet("data.stackit_postgresql_credential.credentials", "host"), resource.TestCheckResourceAttrSet("data.stackit_postgresql_credential.credential", "host"),
resource.TestCheckResourceAttrSet("data.stackit_postgresql_credential.credentials", "port"), resource.TestCheckResourceAttrSet("data.stackit_postgresql_credential.credential", "port"),
resource.TestCheckResourceAttrSet("data.stackit_postgresql_credential.credentials", "uri"), resource.TestCheckResourceAttrSet("data.stackit_postgresql_credential.credential", "uri"),
), ),
}, },
// Import // Import
@ -149,21 +149,21 @@ func TestAccPostgreSQLResource(t *testing.T) {
ImportStateVerify: true, ImportStateVerify: true,
}, },
{ {
ResourceName: "stackit_postgresql_credential.credentials", ResourceName: "stackit_postgresql_credential.credential",
ImportStateIdFunc: func(s *terraform.State) (string, error) { ImportStateIdFunc: func(s *terraform.State) (string, error) {
r, ok := s.RootModule().Resources["stackit_postgresql_credential.credentials"] r, ok := s.RootModule().Resources["stackit_postgresql_credential.credential"]
if !ok { if !ok {
return "", fmt.Errorf("couldn't find resource stackit_postgresql_credential.credentials") return "", fmt.Errorf("couldn't find resource stackit_postgresql_credential.credential")
} }
instanceId, ok := r.Primary.Attributes["instance_id"] instanceId, ok := r.Primary.Attributes["instance_id"]
if !ok { if !ok {
return "", fmt.Errorf("couldn't find attribute instance_id") return "", fmt.Errorf("couldn't find attribute instance_id")
} }
credentialsId, ok := r.Primary.Attributes["credentials_id"] credentialId, ok := r.Primary.Attributes["credential_id"]
if !ok { if !ok {
return "", fmt.Errorf("couldn't find attribute credentials_id") return "", fmt.Errorf("couldn't find attribute credential_id")
} }
return fmt.Sprintf("%s,%s,%s", testutil.ProjectId, instanceId, credentialsId), nil return fmt.Sprintf("%s,%s,%s", testutil.ProjectId, instanceId, credentialId), nil
}, },
ImportState: true, ImportState: true,
ImportStateVerify: true, ImportStateVerify: true,

View file

@ -69,17 +69,17 @@ func (r *credentialDataSource) Configure(ctx context.Context, req datasource.Con
} }
r.client = apiClient r.client = apiClient
tflog.Info(ctx, "RabbitMQ credentials client configured") tflog.Info(ctx, "RabbitMQ credential client configured")
} }
// Schema defines the schema for the data source. // Schema defines the schema for the data source.
func (r *credentialDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { func (r *credentialDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
descriptions := map[string]string{ descriptions := map[string]string{
"main": "RabbitMQ credential data source schema.", "main": "RabbitMQ credential data source schema.",
"id": "Terraform's internal data source. identifier. It is structured as \"`project_id`,`instance_id`,`credentials_id`\".", "id": "Terraform's internal data source. identifier. It is structured as \"`project_id`,`instance_id`,`credential_id`\".",
"credentials_id": "The credentials ID.", "credential_id": "The credential's ID.",
"instance_id": "ID of the RabbitMQ instance.", "instance_id": "ID of the RabbitMQ instance.",
"project_id": "STACKIT project ID to which the instance is associated.", "project_id": "STACKIT project ID to which the instance is associated.",
} }
resp.Schema = schema.Schema{ resp.Schema = schema.Schema{
@ -89,8 +89,8 @@ func (r *credentialDataSource) Schema(_ context.Context, _ datasource.SchemaRequ
Description: descriptions["id"], Description: descriptions["id"],
Computed: true, Computed: true,
}, },
"credentials_id": schema.StringAttribute{ "credential_id": schema.StringAttribute{
Description: descriptions["credentials_id"], Description: descriptions["credential_id"],
Required: true, Required: true,
Validators: []validator.String{ Validators: []validator.String{
validate.UUID(), validate.UUID(),
@ -153,12 +153,12 @@ func (r *credentialDataSource) Read(ctx context.Context, req datasource.ReadRequ
} }
projectId := model.ProjectId.ValueString() projectId := model.ProjectId.ValueString()
instanceId := model.InstanceId.ValueString() instanceId := model.InstanceId.ValueString()
credentialsId := model.CredentialsId.ValueString() credentialId := model.CredentialId.ValueString()
ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "instance_id", instanceId) ctx = tflog.SetField(ctx, "instance_id", instanceId)
ctx = tflog.SetField(ctx, "credentials_id", credentialsId) ctx = tflog.SetField(ctx, "credential_id", credentialId)
recordSetResp, err := r.client.GetCredentials(ctx, projectId, instanceId, credentialsId).Execute() recordSetResp, err := r.client.GetCredentials(ctx, projectId, instanceId, credentialId).Execute()
if err != nil { if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading credential", fmt.Sprintf("Calling API: %v", err)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading credential", fmt.Sprintf("Calling API: %v", err))
return return
@ -177,5 +177,5 @@ func (r *credentialDataSource) Read(ctx context.Context, req datasource.ReadRequ
if resp.Diagnostics.HasError() { if resp.Diagnostics.HasError() {
return return
} }
tflog.Info(ctx, "RabbitMQ credentials read") tflog.Info(ctx, "RabbitMQ credential read")
} }

View file

@ -32,18 +32,18 @@ var (
) )
type Model struct { type Model struct {
Id types.String `tfsdk:"id"` // needed by TF Id types.String `tfsdk:"id"` // needed by TF
CredentialsId types.String `tfsdk:"credentials_id"` CredentialId types.String `tfsdk:"credential_id"`
InstanceId types.String `tfsdk:"instance_id"` InstanceId types.String `tfsdk:"instance_id"`
ProjectId types.String `tfsdk:"project_id"` ProjectId types.String `tfsdk:"project_id"`
Host types.String `tfsdk:"host"` Host types.String `tfsdk:"host"`
Hosts types.List `tfsdk:"hosts"` Hosts types.List `tfsdk:"hosts"`
HttpAPIURI types.String `tfsdk:"http_api_uri"` HttpAPIURI types.String `tfsdk:"http_api_uri"`
Name types.String `tfsdk:"name"` Name types.String `tfsdk:"name"`
Password types.String `tfsdk:"password"` Password types.String `tfsdk:"password"`
Port types.Int64 `tfsdk:"port"` Port types.Int64 `tfsdk:"port"`
Uri types.String `tfsdk:"uri"` Uri types.String `tfsdk:"uri"`
Username types.String `tfsdk:"username"` Username types.String `tfsdk:"username"`
} }
// NewCredentialResource is a helper function to simplify the provider implementation. // NewCredentialResource is a helper function to simplify the provider implementation.
@ -94,17 +94,17 @@ func (r *credentialResource) Configure(ctx context.Context, req resource.Configu
} }
r.client = apiClient r.client = apiClient
tflog.Info(ctx, "RabbitMQ credentials client configured") tflog.Info(ctx, "RabbitMQ credential client configured")
} }
// Schema defines the schema for the resource. // Schema defines the schema for the resource.
func (r *credentialResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { func (r *credentialResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
descriptions := map[string]string{ descriptions := map[string]string{
"main": "RabbitMQ credential resource schema.", "main": "RabbitMQ credential resource schema.",
"id": "Terraform's internal resource identifier. It is structured as \"`project_id`,`instance_id`,`credentials_id`\".", "id": "Terraform's internal resource identifier. It is structured as \"`project_id`,`instance_id`,`credential_id`\".",
"credentials_id": "The credentials ID.", "credential_id": "The credential's ID.",
"instance_id": "ID of the RabbitMQ instance.", "instance_id": "ID of the RabbitMQ instance.",
"project_id": "STACKIT Project ID to which the instance is associated.", "project_id": "STACKIT Project ID to which the instance is associated.",
} }
resp.Schema = schema.Schema{ resp.Schema = schema.Schema{
@ -117,8 +117,8 @@ func (r *credentialResource) Schema(_ context.Context, _ resource.SchemaRequest,
stringplanmodifier.UseStateForUnknown(), stringplanmodifier.UseStateForUnknown(),
}, },
}, },
"credentials_id": schema.StringAttribute{ "credential_id": schema.StringAttribute{
Description: descriptions["credentials_id"], Description: descriptions["credential_id"],
Computed: true, Computed: true,
PlanModifiers: []planmodifier.String{ PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(), stringplanmodifier.UseStateForUnknown(),
@ -202,13 +202,13 @@ func (r *credentialResource) Create(ctx context.Context, req resource.CreateRequ
return return
} }
if credentialsResp.Id == nil { if credentialsResp.Id == nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating credential", "Got empty credentials id") core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating credential", "Got empty credential id")
return return
} }
credentialsId := *credentialsResp.Id credentialId := *credentialsResp.Id
ctx = tflog.SetField(ctx, "credentials_id", credentialsId) ctx = tflog.SetField(ctx, "credential_id", credentialId)
wr, err := wait.CreateCredentialsWaitHandler(ctx, r.client, projectId, instanceId, credentialsId).SetTimeout(1 * time.Minute).WaitWithContext(ctx) wr, err := wait.CreateCredentialsWaitHandler(ctx, r.client, projectId, instanceId, credentialId).SetTimeout(1 * time.Minute).WaitWithContext(ctx)
if err != nil { if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating credential", fmt.Sprintf("Instance creation waiting: %v", err)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating credential", fmt.Sprintf("Instance creation waiting: %v", err))
return return
@ -230,7 +230,7 @@ func (r *credentialResource) Create(ctx context.Context, req resource.CreateRequ
if resp.Diagnostics.HasError() { if resp.Diagnostics.HasError() {
return return
} }
tflog.Info(ctx, "RabbitMQ credentials created") tflog.Info(ctx, "RabbitMQ credential created")
} }
// Read refreshes the Terraform state with the latest data. // Read refreshes the Terraform state with the latest data.
@ -243,12 +243,12 @@ func (r *credentialResource) Read(ctx context.Context, req resource.ReadRequest,
} }
projectId := model.ProjectId.ValueString() projectId := model.ProjectId.ValueString()
instanceId := model.InstanceId.ValueString() instanceId := model.InstanceId.ValueString()
credentialsId := model.CredentialsId.ValueString() credentialId := model.CredentialId.ValueString()
ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "instance_id", instanceId) ctx = tflog.SetField(ctx, "instance_id", instanceId)
ctx = tflog.SetField(ctx, "credentials_id", credentialsId) ctx = tflog.SetField(ctx, "credential_id", credentialId)
recordSetResp, err := r.client.GetCredentials(ctx, projectId, instanceId, credentialsId).Execute() recordSetResp, err := r.client.GetCredentials(ctx, projectId, instanceId, credentialId).Execute()
if err != nil { if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading credential", fmt.Sprintf("Calling API: %v", err)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading credential", fmt.Sprintf("Calling API: %v", err))
return return
@ -267,7 +267,7 @@ func (r *credentialResource) Read(ctx context.Context, req resource.ReadRequest,
if resp.Diagnostics.HasError() { if resp.Diagnostics.HasError() {
return return
} }
tflog.Info(ctx, "RabbitMQ credentials read") tflog.Info(ctx, "RabbitMQ credential read")
} }
// Update updates the resource and sets the updated Terraform state on success. // Update updates the resource and sets the updated Terraform state on success.
@ -287,40 +287,40 @@ func (r *credentialResource) Delete(ctx context.Context, req resource.DeleteRequ
projectId := model.ProjectId.ValueString() projectId := model.ProjectId.ValueString()
instanceId := model.InstanceId.ValueString() instanceId := model.InstanceId.ValueString()
credentialsId := model.CredentialsId.ValueString() credentialId := model.CredentialId.ValueString()
ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "instance_id", instanceId) ctx = tflog.SetField(ctx, "instance_id", instanceId)
ctx = tflog.SetField(ctx, "credentials_id", credentialsId) ctx = tflog.SetField(ctx, "credential_id", credentialId)
// Delete existing record set // Delete existing record set
err := r.client.DeleteCredentials(ctx, projectId, instanceId, credentialsId).Execute() err := r.client.DeleteCredentials(ctx, projectId, instanceId, credentialId).Execute()
if err != nil { if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting credential", fmt.Sprintf("Calling API: %v", err)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting credential", fmt.Sprintf("Calling API: %v", err))
} }
_, err = wait.DeleteCredentialsWaitHandler(ctx, r.client, projectId, instanceId, credentialsId).SetTimeout(1 * time.Minute).WaitWithContext(ctx) _, err = wait.DeleteCredentialsWaitHandler(ctx, r.client, projectId, instanceId, credentialId).SetTimeout(1 * time.Minute).WaitWithContext(ctx)
if err != nil { if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting credential", fmt.Sprintf("Instance deletion waiting: %v", err)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting credential", fmt.Sprintf("Instance deletion waiting: %v", err))
return return
} }
tflog.Info(ctx, "RabbitMQ credentials deleted") tflog.Info(ctx, "RabbitMQ credential deleted")
} }
// ImportState imports a resource into the Terraform state on success. // ImportState imports a resource into the Terraform state on success.
// The expected format of the resource import identifier is: project_id,instance_id,credentials_id // The expected format of the resource import identifier is: project_id,instance_id,credential_id
func (r *credentialResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { func (r *credentialResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
idParts := strings.Split(req.ID, core.Separator) idParts := strings.Split(req.ID, core.Separator)
if len(idParts) != 3 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" { if len(idParts) != 3 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" {
core.LogAndAddError(ctx, &resp.Diagnostics, core.LogAndAddError(ctx, &resp.Diagnostics,
"Error importing credential", "Error importing credential",
fmt.Sprintf("Expected import identifier with format [project_id],[instance_id],[credentials_id], got %q", req.ID), fmt.Sprintf("Expected import identifier with format [project_id],[instance_id],[credential_id], got %q", req.ID),
) )
return return
} }
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), idParts[0])...) resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), idParts[0])...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("instance_id"), idParts[1])...) resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("instance_id"), idParts[1])...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("credentials_id"), idParts[2])...) resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("credential_id"), idParts[2])...)
tflog.Info(ctx, "RabbitMQ credentials state imported") tflog.Info(ctx, "RabbitMQ credential state imported")
} }
func mapFields(credentialsResp *rabbitmq.CredentialsResponse, model *Model) error { func mapFields(credentialsResp *rabbitmq.CredentialsResponse, model *Model) error {
@ -335,11 +335,11 @@ func mapFields(credentialsResp *rabbitmq.CredentialsResponse, model *Model) erro
} }
credentials := credentialsResp.Raw.Credentials credentials := credentialsResp.Raw.Credentials
var credentialsId string var credentialId string
if model.CredentialsId.ValueString() != "" { if model.CredentialId.ValueString() != "" {
credentialsId = model.CredentialsId.ValueString() credentialId = model.CredentialId.ValueString()
} else if credentialsResp.Id != nil { } else if credentialsResp.Id != nil {
credentialsId = *credentialsResp.Id credentialId = *credentialsResp.Id
} else { } else {
return fmt.Errorf("credentials id not present") return fmt.Errorf("credentials id not present")
} }
@ -347,12 +347,12 @@ func mapFields(credentialsResp *rabbitmq.CredentialsResponse, model *Model) erro
idParts := []string{ idParts := []string{
model.ProjectId.ValueString(), model.ProjectId.ValueString(),
model.InstanceId.ValueString(), model.InstanceId.ValueString(),
credentialsId, credentialId,
} }
model.Id = types.StringValue( model.Id = types.StringValue(
strings.Join(idParts, core.Separator), strings.Join(idParts, core.Separator),
) )
model.CredentialsId = types.StringValue(credentialsId) model.CredentialId = types.StringValue(credentialId)
model.Hosts = types.ListNull(types.StringType) model.Hosts = types.ListNull(types.StringType)
if credentials != nil { if credentials != nil {
if credentials.Hosts != nil { if credentials.Hosts != nil {

View file

@ -24,18 +24,18 @@ func TestMapFields(t *testing.T) {
Raw: &rabbitmq.RawCredentials{}, Raw: &rabbitmq.RawCredentials{},
}, },
Model{ Model{
Id: types.StringValue("pid,iid,cid"), Id: types.StringValue("pid,iid,cid"),
CredentialsId: types.StringValue("cid"), CredentialId: types.StringValue("cid"),
InstanceId: types.StringValue("iid"), InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"), ProjectId: types.StringValue("pid"),
Host: types.StringNull(), Host: types.StringNull(),
Hosts: types.ListNull(types.StringType), Hosts: types.ListNull(types.StringType),
HttpAPIURI: types.StringNull(), HttpAPIURI: types.StringNull(),
Name: types.StringNull(), Name: types.StringNull(),
Password: types.StringNull(), Password: types.StringNull(),
Port: types.Int64Null(), Port: types.Int64Null(),
Uri: types.StringNull(), Uri: types.StringNull(),
Username: types.StringNull(), Username: types.StringNull(),
}, },
true, true,
}, },
@ -60,11 +60,11 @@ func TestMapFields(t *testing.T) {
}, },
}, },
Model{ Model{
Id: types.StringValue("pid,iid,cid"), Id: types.StringValue("pid,iid,cid"),
CredentialsId: types.StringValue("cid"), CredentialId: types.StringValue("cid"),
InstanceId: types.StringValue("iid"), InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"), ProjectId: types.StringValue("pid"),
Host: types.StringValue("host"), Host: types.StringValue("host"),
Hosts: types.ListValueMust(types.StringType, []attr.Value{ Hosts: types.ListValueMust(types.StringType, []attr.Value{
types.StringValue("host_1"), types.StringValue("host_1"),
types.StringValue(""), types.StringValue(""),
@ -96,18 +96,18 @@ func TestMapFields(t *testing.T) {
}, },
}, },
Model{ Model{
Id: types.StringValue("pid,iid,cid"), Id: types.StringValue("pid,iid,cid"),
CredentialsId: types.StringValue("cid"), CredentialId: types.StringValue("cid"),
InstanceId: types.StringValue("iid"), InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"), ProjectId: types.StringValue("pid"),
Host: types.StringValue(""), Host: types.StringValue(""),
Hosts: types.ListValueMust(types.StringType, []attr.Value{}), Hosts: types.ListValueMust(types.StringType, []attr.Value{}),
HttpAPIURI: types.StringNull(), HttpAPIURI: types.StringNull(),
Name: types.StringNull(), Name: types.StringNull(),
Password: types.StringValue(""), Password: types.StringValue(""),
Port: types.Int64Value(2123456789), Port: types.Int64Value(2123456789),
Uri: types.StringNull(), Uri: types.StringNull(),
Username: types.StringValue(""), Username: types.StringValue(""),
}, },
true, true,
}, },

View file

@ -56,7 +56,7 @@ func resourceConfig(acls *string) string {
instanceResource["version"], instanceResource["version"],
aclsLine, aclsLine,
instanceResource["metrics_frequency"], instanceResource["metrics_frequency"],
resourceConfigCredentials(), resourceConfigCredential(),
) )
} }
@ -82,13 +82,13 @@ func resourceConfigWithUpdate() string {
instanceResource["plan_name"], instanceResource["plan_name"],
instanceResource["version"], instanceResource["version"],
instanceResource["sgw_acl_valid"], instanceResource["sgw_acl_valid"],
resourceConfigCredentials(), resourceConfigCredential(),
) )
} }
func resourceConfigCredentials() string { func resourceConfigCredential() string {
return ` return `
resource "stackit_rabbitmq_credential" "credentials" { resource "stackit_rabbitmq_credential" "credential" {
project_id = stackit_rabbitmq_instance.instance.project_id project_id = stackit_rabbitmq_instance.instance.project_id
instance_id = stackit_rabbitmq_instance.instance.instance_id instance_id = stackit_rabbitmq_instance.instance.instance_id
} }
@ -119,17 +119,17 @@ func TestAccRabbitMQResource(t *testing.T) {
resource.TestCheckResourceAttr("stackit_rabbitmq_instance.instance", "name", instanceResource["name"]), resource.TestCheckResourceAttr("stackit_rabbitmq_instance.instance", "name", instanceResource["name"]),
resource.TestCheckResourceAttrSet("stackit_rabbitmq_instance.instance", "parameters.sgw_acl"), resource.TestCheckResourceAttrSet("stackit_rabbitmq_instance.instance", "parameters.sgw_acl"),
// Credentials data // Credential data
resource.TestCheckResourceAttrPair( resource.TestCheckResourceAttrPair(
"stackit_rabbitmq_credential.credentials", "project_id", "stackit_rabbitmq_credential.credential", "project_id",
"stackit_rabbitmq_instance.instance", "project_id", "stackit_rabbitmq_instance.instance", "project_id",
), ),
resource.TestCheckResourceAttrPair( resource.TestCheckResourceAttrPair(
"stackit_rabbitmq_credential.credentials", "instance_id", "stackit_rabbitmq_credential.credential", "instance_id",
"stackit_rabbitmq_instance.instance", "instance_id", "stackit_rabbitmq_instance.instance", "instance_id",
), ),
resource.TestCheckResourceAttrSet("stackit_rabbitmq_credential.credentials", "credentials_id"), resource.TestCheckResourceAttrSet("stackit_rabbitmq_credential.credential", "credential_id"),
resource.TestCheckResourceAttrSet("stackit_rabbitmq_credential.credentials", "host"), resource.TestCheckResourceAttrSet("stackit_rabbitmq_credential.credential", "host"),
), ),
}, },
// data source // data source
@ -142,10 +142,10 @@ func TestAccRabbitMQResource(t *testing.T) {
instance_id = stackit_rabbitmq_instance.instance.instance_id instance_id = stackit_rabbitmq_instance.instance.instance_id
} }
data "stackit_rabbitmq_credential" "credentials" { data "stackit_rabbitmq_credential" "credential" {
project_id = stackit_rabbitmq_credential.credentials.project_id project_id = stackit_rabbitmq_credential.credential.project_id
instance_id = stackit_rabbitmq_credential.credentials.instance_id instance_id = stackit_rabbitmq_credential.credential.instance_id
credentials_id = stackit_rabbitmq_credential.credentials.credentials_id credential_id = stackit_rabbitmq_credential.credential.credential_id
}`, }`,
resourceConfig(nil), resourceConfig(nil),
), ),
@ -153,19 +153,19 @@ func TestAccRabbitMQResource(t *testing.T) {
// Instance data // Instance data
resource.TestCheckResourceAttr("data.stackit_rabbitmq_instance.instance", "project_id", instanceResource["project_id"]), resource.TestCheckResourceAttr("data.stackit_rabbitmq_instance.instance", "project_id", instanceResource["project_id"]),
resource.TestCheckResourceAttrPair("stackit_rabbitmq_instance.instance", "instance_id", resource.TestCheckResourceAttrPair("stackit_rabbitmq_instance.instance", "instance_id",
"data.stackit_rabbitmq_credential.credentials", "instance_id"), "data.stackit_rabbitmq_credential.credential", "instance_id"),
resource.TestCheckResourceAttrPair("data.stackit_rabbitmq_instance.instance", "instance_id", resource.TestCheckResourceAttrPair("data.stackit_rabbitmq_instance.instance", "instance_id",
"data.stackit_rabbitmq_credential.credentials", "instance_id"), "data.stackit_rabbitmq_credential.credential", "instance_id"),
resource.TestCheckResourceAttr("data.stackit_rabbitmq_instance.instance", "plan_id", instanceResource["plan_id"]), resource.TestCheckResourceAttr("data.stackit_rabbitmq_instance.instance", "plan_id", instanceResource["plan_id"]),
resource.TestCheckResourceAttr("data.stackit_rabbitmq_instance.instance", "name", instanceResource["name"]), resource.TestCheckResourceAttr("data.stackit_rabbitmq_instance.instance", "name", instanceResource["name"]),
resource.TestCheckResourceAttrSet("data.stackit_rabbitmq_instance.instance", "parameters.sgw_acl"), resource.TestCheckResourceAttrSet("data.stackit_rabbitmq_instance.instance", "parameters.sgw_acl"),
// Credentials data // Credential data
resource.TestCheckResourceAttr("data.stackit_rabbitmq_credential.credentials", "project_id", instanceResource["project_id"]), resource.TestCheckResourceAttr("data.stackit_rabbitmq_credential.credential", "project_id", instanceResource["project_id"]),
resource.TestCheckResourceAttrSet("data.stackit_rabbitmq_credential.credentials", "credentials_id"), resource.TestCheckResourceAttrSet("data.stackit_rabbitmq_credential.credential", "credential_id"),
resource.TestCheckResourceAttrSet("data.stackit_rabbitmq_credential.credentials", "host"), resource.TestCheckResourceAttrSet("data.stackit_rabbitmq_credential.credential", "host"),
resource.TestCheckResourceAttrSet("data.stackit_rabbitmq_credential.credentials", "port"), resource.TestCheckResourceAttrSet("data.stackit_rabbitmq_credential.credential", "port"),
resource.TestCheckResourceAttrSet("data.stackit_rabbitmq_credential.credentials", "uri"), resource.TestCheckResourceAttrSet("data.stackit_rabbitmq_credential.credential", "uri"),
), ),
}, },
// Import // Import
@ -186,21 +186,21 @@ func TestAccRabbitMQResource(t *testing.T) {
ImportStateVerify: true, ImportStateVerify: true,
}, },
{ {
ResourceName: "stackit_rabbitmq_credential.credentials", ResourceName: "stackit_rabbitmq_credential.credential",
ImportStateIdFunc: func(s *terraform.State) (string, error) { ImportStateIdFunc: func(s *terraform.State) (string, error) {
r, ok := s.RootModule().Resources["stackit_rabbitmq_credential.credentials"] r, ok := s.RootModule().Resources["stackit_rabbitmq_credential.credential"]
if !ok { if !ok {
return "", fmt.Errorf("couldn't find resource stackit_rabbitmq_credential.credentials") return "", fmt.Errorf("couldn't find resource stackit_rabbitmq_credential.credential")
} }
instanceId, ok := r.Primary.Attributes["instance_id"] instanceId, ok := r.Primary.Attributes["instance_id"]
if !ok { if !ok {
return "", fmt.Errorf("couldn't find attribute instance_id") return "", fmt.Errorf("couldn't find attribute instance_id")
} }
credentialsId, ok := r.Primary.Attributes["credentials_id"] credentialId, ok := r.Primary.Attributes["credential_id"]
if !ok { if !ok {
return "", fmt.Errorf("couldn't find attribute credentials_id") return "", fmt.Errorf("couldn't find attribute credential_id")
} }
return fmt.Sprintf("%s,%s,%s", testutil.ProjectId, instanceId, credentialsId), nil return fmt.Sprintf("%s,%s,%s", testutil.ProjectId, instanceId, credentialId), nil
}, },
ImportState: true, ImportState: true,
ImportStateVerify: true, ImportStateVerify: true,

View file

@ -69,17 +69,17 @@ func (r *credentialDataSource) Configure(ctx context.Context, req datasource.Con
} }
r.client = apiClient r.client = apiClient
tflog.Info(ctx, "Redis credentials client configured") tflog.Info(ctx, "Redis credential client configured")
} }
// Schema defines the schema for the data source. // Schema defines the schema for the data source.
func (r *credentialDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { func (r *credentialDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
descriptions := map[string]string{ descriptions := map[string]string{
"main": "Redis credential data source schema.", "main": "Redis credential data source schema.",
"id": "Terraform's internal data source. identifier. It is structured as \"`project_id`,`instance_id`,`credentials_id`\".", "id": "Terraform's internal data source. identifier. It is structured as \"`project_id`,`instance_id`,`credential_id`\".",
"credentials_id": "The credentials ID.", "credential_id": "The credential's ID.",
"instance_id": "ID of the Redis instance.", "instance_id": "ID of the Redis instance.",
"project_id": "STACKIT project ID to which the instance is associated.", "project_id": "STACKIT project ID to which the instance is associated.",
} }
resp.Schema = schema.Schema{ resp.Schema = schema.Schema{
@ -89,8 +89,8 @@ func (r *credentialDataSource) Schema(_ context.Context, _ datasource.SchemaRequ
Description: descriptions["id"], Description: descriptions["id"],
Computed: true, Computed: true,
}, },
"credentials_id": schema.StringAttribute{ "credential_id": schema.StringAttribute{
Description: descriptions["credentials_id"], Description: descriptions["credential_id"],
Required: true, Required: true,
Validators: []validator.String{ Validators: []validator.String{
validate.UUID(), validate.UUID(),
@ -153,12 +153,12 @@ func (r *credentialDataSource) Read(ctx context.Context, req datasource.ReadRequ
} }
projectId := model.ProjectId.ValueString() projectId := model.ProjectId.ValueString()
instanceId := model.InstanceId.ValueString() instanceId := model.InstanceId.ValueString()
credentialsId := model.CredentialsId.ValueString() credentialId := model.CredentialId.ValueString()
ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "instance_id", instanceId) ctx = tflog.SetField(ctx, "instance_id", instanceId)
ctx = tflog.SetField(ctx, "credentials_id", credentialsId) ctx = tflog.SetField(ctx, "credential_id", credentialId)
recordSetResp, err := r.client.GetCredentials(ctx, projectId, instanceId, credentialsId).Execute() recordSetResp, err := r.client.GetCredentials(ctx, projectId, instanceId, credentialId).Execute()
if err != nil { if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading credential", fmt.Sprintf("Calling API: %v", err)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading credential", fmt.Sprintf("Calling API: %v", err))
return return
@ -177,5 +177,5 @@ func (r *credentialDataSource) Read(ctx context.Context, req datasource.ReadRequ
if resp.Diagnostics.HasError() { if resp.Diagnostics.HasError() {
return return
} }
tflog.Info(ctx, "Redis credentials read") tflog.Info(ctx, "Redis credential read")
} }

View file

@ -32,18 +32,18 @@ var (
) )
type Model struct { type Model struct {
Id types.String `tfsdk:"id"` // needed by TF Id types.String `tfsdk:"id"` // needed by TF
CredentialsId types.String `tfsdk:"credentials_id"` CredentialId types.String `tfsdk:"credential_id"`
InstanceId types.String `tfsdk:"instance_id"` InstanceId types.String `tfsdk:"instance_id"`
ProjectId types.String `tfsdk:"project_id"` ProjectId types.String `tfsdk:"project_id"`
Host types.String `tfsdk:"host"` Host types.String `tfsdk:"host"`
Hosts types.List `tfsdk:"hosts"` Hosts types.List `tfsdk:"hosts"`
HttpAPIURI types.String `tfsdk:"http_api_uri"` HttpAPIURI types.String `tfsdk:"http_api_uri"`
Name types.String `tfsdk:"name"` Name types.String `tfsdk:"name"`
Password types.String `tfsdk:"password"` Password types.String `tfsdk:"password"`
Port types.Int64 `tfsdk:"port"` Port types.Int64 `tfsdk:"port"`
Uri types.String `tfsdk:"uri"` Uri types.String `tfsdk:"uri"`
Username types.String `tfsdk:"username"` Username types.String `tfsdk:"username"`
} }
// NewCredentialResource is a helper function to simplify the provider implementation. // NewCredentialResource is a helper function to simplify the provider implementation.
@ -94,17 +94,17 @@ func (r *credentialResource) Configure(ctx context.Context, req resource.Configu
} }
r.client = apiClient r.client = apiClient
tflog.Info(ctx, "Redis credentials client configured") tflog.Info(ctx, "Redis credential client configured")
} }
// Schema defines the schema for the resource. // Schema defines the schema for the resource.
func (r *credentialResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { func (r *credentialResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
descriptions := map[string]string{ descriptions := map[string]string{
"main": "Redis credential resource schema.", "main": "Redis credential resource schema.",
"id": "Terraform's internal resource identifier. It is structured as \"`project_id`,`instance_id`,`credentials_id`\".", "id": "Terraform's internal resource identifier. It is structured as \"`project_id`,`instance_id`,`credential_id`\".",
"credentials_id": "The credentials ID.", "credential_id": "The credential's ID.",
"instance_id": "ID of the Redis instance.", "instance_id": "ID of the Redis instance.",
"project_id": "STACKIT Project ID to which the instance is associated.", "project_id": "STACKIT Project ID to which the instance is associated.",
} }
resp.Schema = schema.Schema{ resp.Schema = schema.Schema{
@ -117,8 +117,8 @@ func (r *credentialResource) Schema(_ context.Context, _ resource.SchemaRequest,
stringplanmodifier.UseStateForUnknown(), stringplanmodifier.UseStateForUnknown(),
}, },
}, },
"credentials_id": schema.StringAttribute{ "credential_id": schema.StringAttribute{
Description: descriptions["credentials_id"], Description: descriptions["credential_id"],
Computed: true, Computed: true,
PlanModifiers: []planmodifier.String{ PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(), stringplanmodifier.UseStateForUnknown(),
@ -202,13 +202,13 @@ func (r *credentialResource) Create(ctx context.Context, req resource.CreateRequ
return return
} }
if credentialsResp.Id == nil { if credentialsResp.Id == nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating credential", "Got empty credentials id") core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating credential", "Got empty credential id")
return return
} }
credentialsId := *credentialsResp.Id credentialId := *credentialsResp.Id
ctx = tflog.SetField(ctx, "credentials_id", credentialsId) ctx = tflog.SetField(ctx, "credential_id", credentialId)
wr, err := wait.CreateCredentialsWaitHandler(ctx, r.client, projectId, instanceId, credentialsId).SetTimeout(1 * time.Minute).WaitWithContext(ctx) wr, err := wait.CreateCredentialsWaitHandler(ctx, r.client, projectId, instanceId, credentialId).SetTimeout(1 * time.Minute).WaitWithContext(ctx)
if err != nil { if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating credential", fmt.Sprintf("Instance creation waiting: %v", err)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating credential", fmt.Sprintf("Instance creation waiting: %v", err))
return return
@ -230,7 +230,7 @@ func (r *credentialResource) Create(ctx context.Context, req resource.CreateRequ
if resp.Diagnostics.HasError() { if resp.Diagnostics.HasError() {
return return
} }
tflog.Info(ctx, "Redis credentials created") tflog.Info(ctx, "Redis credential created")
} }
// Read refreshes the Terraform state with the latest data. // Read refreshes the Terraform state with the latest data.
@ -243,12 +243,12 @@ func (r *credentialResource) Read(ctx context.Context, req resource.ReadRequest,
} }
projectId := model.ProjectId.ValueString() projectId := model.ProjectId.ValueString()
instanceId := model.InstanceId.ValueString() instanceId := model.InstanceId.ValueString()
credentialsId := model.CredentialsId.ValueString() credentialId := model.CredentialId.ValueString()
ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "instance_id", instanceId) ctx = tflog.SetField(ctx, "instance_id", instanceId)
ctx = tflog.SetField(ctx, "credentials_id", credentialsId) ctx = tflog.SetField(ctx, "credential_id", credentialId)
recordSetResp, err := r.client.GetCredentials(ctx, projectId, instanceId, credentialsId).Execute() recordSetResp, err := r.client.GetCredentials(ctx, projectId, instanceId, credentialId).Execute()
if err != nil { if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading credential", fmt.Sprintf("Calling API: %v", err)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading credential", fmt.Sprintf("Calling API: %v", err))
return return
@ -267,7 +267,7 @@ func (r *credentialResource) Read(ctx context.Context, req resource.ReadRequest,
if resp.Diagnostics.HasError() { if resp.Diagnostics.HasError() {
return return
} }
tflog.Info(ctx, "Redis credentials read") tflog.Info(ctx, "Redis credential read")
} }
// Update updates the resource and sets the updated Terraform state on success. // Update updates the resource and sets the updated Terraform state on success.
@ -287,40 +287,40 @@ func (r *credentialResource) Delete(ctx context.Context, req resource.DeleteRequ
projectId := model.ProjectId.ValueString() projectId := model.ProjectId.ValueString()
instanceId := model.InstanceId.ValueString() instanceId := model.InstanceId.ValueString()
credentialsId := model.CredentialsId.ValueString() credentialId := model.CredentialId.ValueString()
ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "instance_id", instanceId) ctx = tflog.SetField(ctx, "instance_id", instanceId)
ctx = tflog.SetField(ctx, "credentials_id", credentialsId) ctx = tflog.SetField(ctx, "credential_id", credentialId)
// Delete existing record set // Delete existing record set
err := r.client.DeleteCredentials(ctx, projectId, instanceId, credentialsId).Execute() err := r.client.DeleteCredentials(ctx, projectId, instanceId, credentialId).Execute()
if err != nil { if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting credential", fmt.Sprintf("Calling API: %v", err)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting credential", fmt.Sprintf("Calling API: %v", err))
} }
_, err = wait.DeleteCredentialsWaitHandler(ctx, r.client, projectId, instanceId, credentialsId).SetTimeout(1 * time.Minute).WaitWithContext(ctx) _, err = wait.DeleteCredentialsWaitHandler(ctx, r.client, projectId, instanceId, credentialId).SetTimeout(1 * time.Minute).WaitWithContext(ctx)
if err != nil { if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting credential", fmt.Sprintf("Instance deletion waiting: %v", err)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting credential", fmt.Sprintf("Instance deletion waiting: %v", err))
return return
} }
tflog.Info(ctx, "Redis credentials deleted") tflog.Info(ctx, "Redis credential deleted")
} }
// ImportState imports a resource into the Terraform state on success. // ImportState imports a resource into the Terraform state on success.
// The expected format of the resource import identifier is: project_id,instance_id,credentials_id // The expected format of the resource import identifier is: project_id,instance_id,credential_id
func (r *credentialResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { func (r *credentialResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
idParts := strings.Split(req.ID, core.Separator) idParts := strings.Split(req.ID, core.Separator)
if len(idParts) != 3 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" { if len(idParts) != 3 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" {
core.LogAndAddError(ctx, &resp.Diagnostics, core.LogAndAddError(ctx, &resp.Diagnostics,
"Error importing credential", "Error importing credential",
fmt.Sprintf("Expected import identifier with format [project_id],[instance_id],[credentials_id], got %q", req.ID), fmt.Sprintf("Expected import identifier with format [project_id],[instance_id],[credential_id], got %q", req.ID),
) )
return return
} }
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), idParts[0])...) resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), idParts[0])...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("instance_id"), idParts[1])...) resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("instance_id"), idParts[1])...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("credentials_id"), idParts[2])...) resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("credential_id"), idParts[2])...)
tflog.Info(ctx, "Redis credentials state imported") tflog.Info(ctx, "Redis credential state imported")
} }
func mapFields(credentialsResp *redis.CredentialsResponse, model *Model) error { func mapFields(credentialsResp *redis.CredentialsResponse, model *Model) error {
@ -335,11 +335,11 @@ func mapFields(credentialsResp *redis.CredentialsResponse, model *Model) error {
} }
credentials := credentialsResp.Raw.Credentials credentials := credentialsResp.Raw.Credentials
var credentialsId string var credentialId string
if model.CredentialsId.ValueString() != "" { if model.CredentialId.ValueString() != "" {
credentialsId = model.CredentialsId.ValueString() credentialId = model.CredentialId.ValueString()
} else if credentialsResp.Id != nil { } else if credentialsResp.Id != nil {
credentialsId = *credentialsResp.Id credentialId = *credentialsResp.Id
} else { } else {
return fmt.Errorf("credentials id not present") return fmt.Errorf("credentials id not present")
} }
@ -347,12 +347,12 @@ func mapFields(credentialsResp *redis.CredentialsResponse, model *Model) error {
idParts := []string{ idParts := []string{
model.ProjectId.ValueString(), model.ProjectId.ValueString(),
model.InstanceId.ValueString(), model.InstanceId.ValueString(),
credentialsId, credentialId,
} }
model.Id = types.StringValue( model.Id = types.StringValue(
strings.Join(idParts, core.Separator), strings.Join(idParts, core.Separator),
) )
model.CredentialsId = types.StringValue(credentialsId) model.CredentialId = types.StringValue(credentialId)
model.Hosts = types.ListNull(types.StringType) model.Hosts = types.ListNull(types.StringType)
if credentials != nil { if credentials != nil {
if credentials.Hosts != nil { if credentials.Hosts != nil {

View file

@ -24,18 +24,18 @@ func TestMapFields(t *testing.T) {
Raw: &redis.RawCredentials{}, Raw: &redis.RawCredentials{},
}, },
Model{ Model{
Id: types.StringValue("pid,iid,cid"), Id: types.StringValue("pid,iid,cid"),
CredentialsId: types.StringValue("cid"), CredentialId: types.StringValue("cid"),
InstanceId: types.StringValue("iid"), InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"), ProjectId: types.StringValue("pid"),
Host: types.StringNull(), Host: types.StringNull(),
Hosts: types.ListNull(types.StringType), Hosts: types.ListNull(types.StringType),
HttpAPIURI: types.StringNull(), HttpAPIURI: types.StringNull(),
Name: types.StringNull(), Name: types.StringNull(),
Password: types.StringNull(), Password: types.StringNull(),
Port: types.Int64Null(), Port: types.Int64Null(),
Uri: types.StringNull(), Uri: types.StringNull(),
Username: types.StringNull(), Username: types.StringNull(),
}, },
true, true,
}, },
@ -60,11 +60,11 @@ func TestMapFields(t *testing.T) {
}, },
}, },
Model{ Model{
Id: types.StringValue("pid,iid,cid"), Id: types.StringValue("pid,iid,cid"),
CredentialsId: types.StringValue("cid"), CredentialId: types.StringValue("cid"),
InstanceId: types.StringValue("iid"), InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"), ProjectId: types.StringValue("pid"),
Host: types.StringValue("host"), Host: types.StringValue("host"),
Hosts: types.ListValueMust(types.StringType, []attr.Value{ Hosts: types.ListValueMust(types.StringType, []attr.Value{
types.StringValue("host_1"), types.StringValue("host_1"),
types.StringValue(""), types.StringValue(""),
@ -96,18 +96,18 @@ func TestMapFields(t *testing.T) {
}, },
}, },
Model{ Model{
Id: types.StringValue("pid,iid,cid"), Id: types.StringValue("pid,iid,cid"),
CredentialsId: types.StringValue("cid"), CredentialId: types.StringValue("cid"),
InstanceId: types.StringValue("iid"), InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"), ProjectId: types.StringValue("pid"),
Host: types.StringValue(""), Host: types.StringValue(""),
Hosts: types.ListValueMust(types.StringType, []attr.Value{}), Hosts: types.ListValueMust(types.StringType, []attr.Value{}),
HttpAPIURI: types.StringNull(), HttpAPIURI: types.StringNull(),
Name: types.StringNull(), Name: types.StringNull(),
Password: types.StringValue(""), Password: types.StringValue(""),
Port: types.Int64Value(2123456789), Port: types.Int64Value(2123456789),
Uri: types.StringNull(), Uri: types.StringNull(),
Username: types.StringValue(""), Username: types.StringValue(""),
}, },
true, true,
}, },

View file

@ -56,7 +56,7 @@ func resourceConfig(acls *string) string {
instanceResource["version"], instanceResource["version"],
aclsLine, aclsLine,
instanceResource["metrics_frequency"], instanceResource["metrics_frequency"],
resourceConfigCredentials(), resourceConfigCredential(),
) )
} }
@ -82,13 +82,13 @@ func resourceConfigWithUpdate() string {
instanceResource["plan_name"], instanceResource["plan_name"],
instanceResource["version"], instanceResource["version"],
instanceResource["sgw_acl_valid"], instanceResource["sgw_acl_valid"],
resourceConfigCredentials(), resourceConfigCredential(),
) )
} }
func resourceConfigCredentials() string { func resourceConfigCredential() string {
return ` return `
resource "stackit_redis_credential" "credentials" { resource "stackit_redis_credential" "credential" {
project_id = stackit_redis_instance.instance.project_id project_id = stackit_redis_instance.instance.project_id
instance_id = stackit_redis_instance.instance.instance_id instance_id = stackit_redis_instance.instance.instance_id
} }
@ -119,17 +119,17 @@ func TestAccRedisResource(t *testing.T) {
resource.TestCheckResourceAttr("stackit_redis_instance.instance", "name", instanceResource["name"]), resource.TestCheckResourceAttr("stackit_redis_instance.instance", "name", instanceResource["name"]),
resource.TestCheckResourceAttrSet("stackit_redis_instance.instance", "parameters.sgw_acl"), resource.TestCheckResourceAttrSet("stackit_redis_instance.instance", "parameters.sgw_acl"),
// Credentials data // Credential data
resource.TestCheckResourceAttrPair( resource.TestCheckResourceAttrPair(
"stackit_redis_credential.credentials", "project_id", "stackit_redis_credential.credential", "project_id",
"stackit_redis_instance.instance", "project_id", "stackit_redis_instance.instance", "project_id",
), ),
resource.TestCheckResourceAttrPair( resource.TestCheckResourceAttrPair(
"stackit_redis_credential.credentials", "instance_id", "stackit_redis_credential.credential", "instance_id",
"stackit_redis_instance.instance", "instance_id", "stackit_redis_instance.instance", "instance_id",
), ),
resource.TestCheckResourceAttrSet("stackit_redis_credential.credentials", "credentials_id"), resource.TestCheckResourceAttrSet("stackit_redis_credential.credential", "credential_id"),
resource.TestCheckResourceAttrSet("stackit_redis_credential.credentials", "host"), resource.TestCheckResourceAttrSet("stackit_redis_credential.credential", "host"),
), ),
}, },
// data source // data source
@ -142,10 +142,10 @@ func TestAccRedisResource(t *testing.T) {
instance_id = stackit_redis_instance.instance.instance_id instance_id = stackit_redis_instance.instance.instance_id
} }
data "stackit_redis_credential" "credentials" { data "stackit_redis_credential" "credential" {
project_id = stackit_redis_credential.credentials.project_id project_id = stackit_redis_credential.credential.project_id
instance_id = stackit_redis_credential.credentials.instance_id instance_id = stackit_redis_credential.credential.instance_id
credentials_id = stackit_redis_credential.credentials.credentials_id credential_id = stackit_redis_credential.credential.credential_id
}`, }`,
resourceConfig(nil), resourceConfig(nil),
), ),
@ -153,19 +153,19 @@ func TestAccRedisResource(t *testing.T) {
// Instance data // Instance data
resource.TestCheckResourceAttr("data.stackit_redis_instance.instance", "project_id", instanceResource["project_id"]), resource.TestCheckResourceAttr("data.stackit_redis_instance.instance", "project_id", instanceResource["project_id"]),
resource.TestCheckResourceAttrPair("stackit_redis_instance.instance", "instance_id", resource.TestCheckResourceAttrPair("stackit_redis_instance.instance", "instance_id",
"data.stackit_redis_credential.credentials", "instance_id"), "data.stackit_redis_credential.credential", "instance_id"),
resource.TestCheckResourceAttrPair("data.stackit_redis_instance.instance", "instance_id", resource.TestCheckResourceAttrPair("data.stackit_redis_instance.instance", "instance_id",
"data.stackit_redis_credential.credentials", "instance_id"), "data.stackit_redis_credential.credential", "instance_id"),
resource.TestCheckResourceAttr("data.stackit_redis_instance.instance", "plan_id", instanceResource["plan_id"]), resource.TestCheckResourceAttr("data.stackit_redis_instance.instance", "plan_id", instanceResource["plan_id"]),
resource.TestCheckResourceAttr("data.stackit_redis_instance.instance", "name", instanceResource["name"]), resource.TestCheckResourceAttr("data.stackit_redis_instance.instance", "name", instanceResource["name"]),
resource.TestCheckResourceAttrSet("data.stackit_redis_instance.instance", "parameters.sgw_acl"), resource.TestCheckResourceAttrSet("data.stackit_redis_instance.instance", "parameters.sgw_acl"),
// Credentials data // Credentials data
resource.TestCheckResourceAttr("data.stackit_redis_credential.credentials", "project_id", instanceResource["project_id"]), resource.TestCheckResourceAttr("data.stackit_redis_credential.credential", "project_id", instanceResource["project_id"]),
resource.TestCheckResourceAttrSet("data.stackit_redis_credential.credentials", "credentials_id"), resource.TestCheckResourceAttrSet("data.stackit_redis_credential.credential", "credential_id"),
resource.TestCheckResourceAttrSet("data.stackit_redis_credential.credentials", "host"), resource.TestCheckResourceAttrSet("data.stackit_redis_credential.credential", "host"),
resource.TestCheckResourceAttrSet("data.stackit_redis_credential.credentials", "port"), resource.TestCheckResourceAttrSet("data.stackit_redis_credential.credential", "port"),
resource.TestCheckResourceAttrSet("data.stackit_redis_credential.credentials", "uri"), resource.TestCheckResourceAttrSet("data.stackit_redis_credential.credential", "uri"),
), ),
}, },
// Import // Import
@ -186,21 +186,21 @@ func TestAccRedisResource(t *testing.T) {
ImportStateVerify: true, ImportStateVerify: true,
}, },
{ {
ResourceName: "stackit_redis_credential.credentials", ResourceName: "stackit_redis_credential.credential",
ImportStateIdFunc: func(s *terraform.State) (string, error) { ImportStateIdFunc: func(s *terraform.State) (string, error) {
r, ok := s.RootModule().Resources["stackit_redis_credential.credentials"] r, ok := s.RootModule().Resources["stackit_redis_credential.credential"]
if !ok { if !ok {
return "", fmt.Errorf("couldn't find resource stackit_redis_credential.credentials") return "", fmt.Errorf("couldn't find resource stackit_redis_credential.credential")
} }
instanceId, ok := r.Primary.Attributes["instance_id"] instanceId, ok := r.Primary.Attributes["instance_id"]
if !ok { if !ok {
return "", fmt.Errorf("couldn't find attribute instance_id") return "", fmt.Errorf("couldn't find attribute instance_id")
} }
credentialsId, ok := r.Primary.Attributes["credentials_id"] credentialId, ok := r.Primary.Attributes["credential_id"]
if !ok { if !ok {
return "", fmt.Errorf("couldn't find attribute credentials_id") return "", fmt.Errorf("couldn't find attribute credential_id")
} }
return fmt.Sprintf("%s,%s,%s", testutil.ProjectId, instanceId, credentialsId), nil return fmt.Sprintf("%s,%s,%s", testutil.ProjectId, instanceId, credentialId), nil
}, },
ImportState: true, ImportState: true,
ImportStateVerify: true, ImportStateVerify: true,