Skip to content

Commit

Permalink
Topic data source now supports AVRO schemas (#49)
Browse files Browse the repository at this point in the history
* 1. Topic data source now supports AVRO schemas
2. Previously if someone wanted to run all tests they had to change 19 different files for instance name and user's group name. Now they only have to change 1 file `test_config.yaml`.

* 1. refactored common logic into helper function

---------

Co-authored-by: Kaspar-Metsa <[email protected]>
  • Loading branch information
kasparAxual and Kaspar-Metsa authored Dec 6, 2024
1 parent 25c47ab commit 7f801c8
Show file tree
Hide file tree
Showing 35 changed files with 107 additions and 137 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ All notable changes to this project will be documented in this file.
## [master](https://github.com/Axual/terraform-provider-axual/blob/master) - TBR
* Added `Instance` data source.
* Added error handling for environment, group and topic datasources.
* Added AVRO schema support to Topic data source.
* Refactored running acceptance tests to be simpler.

## [2.4.1](https://github.com/Axual/terraform-provider-axual/releases/tag/v2.4.1) - 2024-09-23
* Documentation improvements: Concise front page and improved Connector guide.
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,16 @@ go generate
- For IntelliJ IDEA click edit configuration -> Go tool arguments
- Make sure to turn off test caching, because then we can run the same tests multiple times to test stability without having to change the test.
- - Use this go tool argument: `-count 1`
- In all test .tf files replace the instance name inside the data block `data "axual_instance" "testInstance"` with the existing real instance name in your platform
- In the file `test_config.yaml`:
- Replace the value of the `instanceName` with the existing real instance name in your platform.
- Replace the value of the `userGroup` with any group name that the logged-in user is a member of.
- Make sure the certs in the tests match the CA for the Instance, replace them if not.
- Easiest way to do this is to change Instance CA to `axual-dummy-intermediate` using the UI.
- For Axual employees download it from here: https://gitlab.com/axual/qa/local-development/-/blob/main/governance/files/axual-dummy-intermediate
- Make sure OAUTHBEARER auth method is turned on: in PM conf(`api.available.auth.methods`), Tenant auth method, Instance auth method
- Needed for testing OAUTHBEARER Application Principal
- Make sure Granular Stream Browse Permissions are turned on for the instance
- Needed for testing Topic Browse Permissions
- Make sure you replace `data "axual_group" "root_user_group"` with a group name that your logged-in user is a member of in files `axual_topic_browse_permissions_initial.tf` and `axual_topic_browse_permissions_updated.tf`.
- First try to run one acceptance test, before trying to run all the tests. It might happen that if a test fails, you have to manually delete resources using UI.
- We recommend to try to run in this order:
- user_resource_test.go
Expand Down
8 changes: 6 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ The following example demonstrates the basic functionality of Axual Self-Service
# This TerraForm file shows the basic capabilities of the TerraForm provider for Axual
#
# - When trying out this example:
# - replace `instance` UID
# - replace `instance` name from `Dev Test Acceptance` to the name of your instance.
# - make sure that the user, group, topic and other resource names already do not exist in the environment
Expand All @@ -71,14 +71,18 @@ resource "axual_group" "tenant_admin_group" {
]
}
data "axual_instance" "testInstance"{
name = "Dev Test Acceptance" # Replace with the name of your instance
}
resource "axual_environment" "development" {
name = "development"
short_name = "dev"
description = "This is the development environment"
color = "#19b9be"
visibility = "Public"
authorization_issuer = "Auto"
instance = "51be2a6a5eee481198787dc346ab6608"
instance = data.axual_instance.testInstance.id
owners = axual_group.tenant_admin_group.id
}
Expand Down
4 changes: 2 additions & 2 deletions examples/axual/avro-schemas/gitops_test_v1.avsc
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"type": "record",
"name": "GitOpsTest",
"name": "GitOpsTest1",
"namespace": "io.axual.qa.general",
"doc": "Object type that is supposed to be filled with a gitops test value. This should be used when the Key is irrelevant.",
"fields": [
{
"name": "gitops",
"name": "gitops1",
"type": "string",
"doc": "The gitops test value. v 1.0.0"
}
Expand Down
8 changes: 6 additions & 2 deletions examples/axual/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# This TerraForm file shows the basic capabilities of the TerraForm provider for Axual
#
# - When trying out this example:
# - replace `instance` UID
# - replace `instance` name from `Dev Test Acceptance` to the name of your instance.
# - make sure that the user, group, topic and other resource names already do not exist in the environment


Expand All @@ -30,14 +30,18 @@ resource "axual_group" "tenant_admin_group" {
]
}

data "axual_instance" "testInstance"{
name = "Dev Test Acceptance" # Replace with the name of your instance
}

resource "axual_environment" "development" {
name = "development"
short_name = "dev"
description = "This is the development environment"
color = "#19b9be"
visibility = "Public"
authorization_issuer = "Auto"
instance = "51be2a6a5eee481198787dc346ab6608"
instance = data.axual_instance.testInstance.id
owners = axual_group.tenant_admin_group.id
}

Expand Down
23 changes: 23 additions & 0 deletions internal/provider/data_source_topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package provider

import (
webclient "axual-webclient"
"axual.com/terraform-provider-axual/internal/provider/utils"
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
Expand Down Expand Up @@ -32,7 +33,9 @@ type topicDataSourceData struct {
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
KeyType types.String `tfsdk:"key_type"`
KeySchema types.String `tfsdk:"key_schema"`
ValueType types.String `tfsdk:"value_type"`
ValueSchema types.String `tfsdk:"value_schema"`
Owners types.String `tfsdk:"owners"`
RetentionPolicy types.String `tfsdk:"retention_policy"`
Id types.String `tfsdk:"id"`
Expand Down Expand Up @@ -69,6 +72,14 @@ func (d *topicDataSource) Schema(ctx context.Context, req datasource.SchemaReque
MarkdownDescription: "The value type and reference to the schema (if applicable). Read more: https://docs.axual.io/axual/2024.2/self-service/topic-management.html#value-type",
Computed: true,
},
"key_schema": schema.StringAttribute{
MarkdownDescription: "The key schema UID if `key_type` is 'AVRO'.",
Computed: true,
},
"value_schema": schema.StringAttribute{
MarkdownDescription: "The value schema UID if `value_type` is 'AVRO'.",
Computed: true,
},
"owners": schema.StringAttribute{
MarkdownDescription: "The team owning this topic. Read more: https://docs.axual.io/axual/2024.2/self-service/topic-management.html#topic-owner",
Computed: true,
Expand Down Expand Up @@ -156,4 +167,16 @@ func mapTopicDataSourceResponseToData(ctx context.Context, data *topicDataSource
} else {
data.Description = types.StringValue(topic.Description.(string))
}

if data.KeyType.ValueString() == "AVRO" {
data.KeySchema = utils.SetStringValue(topic.Embedded.KeySchema.Uid)
} else {
data.KeySchema = types.StringNull()
}

if data.ValueType.ValueString() == "AVRO" {
data.ValueSchema = utils.SetStringValue(topic.Embedded.ValueSchema.Uid)
} else {
data.ValueSchema = types.StringNull()
}
}
13 changes: 13 additions & 0 deletions internal/provider/resource_topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package provider
import (
webclient "axual-webclient"
custom_validator "axual.com/terraform-provider-axual/internal/custom-validator"
"axual.com/terraform-provider-axual/internal/provider/utils"
"context"
"errors"
"fmt"
Expand Down Expand Up @@ -370,4 +371,16 @@ func mapTopicResponseToData(ctx context.Context, data *topicResourceData, topic
tflog.Error(ctx, "Error creating viewers set", errorDetails)
}
}

if data.KeyType.ValueString() == "AVRO" {
data.KeySchema = utils.SetStringValue(topic.Embedded.KeySchema.Uid)
} else {
data.KeySchema = types.StringNull()
}

if data.ValueType.ValueString() == "AVRO" {
data.ValueSchema = utils.SetStringValue(topic.Embedded.ValueSchema.Uid)
} else {
data.ValueSchema = types.StringNull()
}
}
11 changes: 11 additions & 0 deletions internal/provider/utils/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package utils

import "github.com/hashicorp/terraform-plugin-framework/types"

// Helper function to set a Terraform string value or null based on input
func SetStringValue(input string) types.String {
if input != "" {
return types.StringValue(input)
}
return types.StringNull()
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ resource "axual_topic" "topic-test" {
description = "Demo of deploying a topic config via Terraform"
}

data "axual_instance" "testInstance"{
name = "testInstance"
}

resource "axual_environment" "tf-test-env" {
name = "tf-development"
short_name = "tfdev"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,6 @@ resource "axual_topic_config" "tf-topic-config" {
properties = {"segment.ms"="600012", "retention.bytes"="-1"}
}

data "axual_instance" "testInstance"{
name = "testInstance"
}

resource "axual_environment" "tf-test-env" {
name = "tf-development"
short_name = "tfdev"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@ resource "axual_topic_config" "tf-topic-config" {
environment = axual_environment.tf-test-env.id
properties = {"segment.ms"="600012", "retention.bytes"="-1"}
}

data "axual_instance" "testInstance"{
name = "testInstance"
}

resource "axual_environment" "tf-test-env" {
name = "tf-development"
short_name = "tfdev"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,6 @@ resource "axual_topic_config" "tf-topic-config" {
properties = {"segment.ms"="600012", "retention.bytes"="-1"}
}

data "axual_instance" "testInstance"{
name = "testInstance"
}

resource "axual_environment" "tf-test-env" {
name = "tf-development"
short_name = "tfdev"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ resource "axual_topic_config" "tf-topic-config" {
properties = {"segment.ms"="600012", "retention.bytes"="-1"}
}

data "axual_instance" "testInstance"{
name = "testInstance"
}

resource "axual_environment" "tf-test-env" {
name = "tf-development"
short_name = "tfdev"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,6 @@ resource "axual_topic_config" "tf-topic-config" {
properties = {"segment.ms"="600012", "retention.bytes"="-1"}
}


data "axual_instance" "testInstance"{
name = "testInstance"
}

resource "axual_environment" "tf-test-env" {
name = "tf-development"
short_name = "tfdev"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ resource "axual_group" "team-integrations" {
]
}

data "axual_instance" "testInstance"{
name = "testInstance"
}

resource "axual_environment" "tf-test-env" {
name = "tf-development"
short_name = "tfdev"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ resource "axual_group" "team-integrations" {
]
}

data "axual_instance" "testInstance"{
name = "testInstance"
}

resource "axual_environment" "tf-test-env" {
name = "tf-development"
short_name = "tfdev"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ resource "axual_group" "team-integrations" {
]
}

data "axual_instance" "testInstance"{
name = "testInstance"
}

resource "axual_environment" "tf-test-env" {
name = "tf-development"
short_name = "tfdev"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ resource "axual_group" "team-integrations" {
]
}

data "axual_instance" "testInstance"{
name = "testInstance"
}

resource "axual_environment" "tf-test-env" {
name = "tf-development"
short_name = "tfdev"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ resource "axual_group" "team-integrations" {
]
}

data "axual_instance" "testInstance"{
name = "testInstance"
}

resource "axual_environment" "tf-test-env" {
name = "tf-development"
short_name = "tfdev"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ resource "axual_group" "team-integrations" {
]
}

data "axual_instance" "testInstance"{
name = "testInstance"
}

resource "axual_environment" "tf-test-env" {
name = "tf-development"
short_name = "tfdev"
Expand Down
4 changes: 0 additions & 4 deletions internal/tests/EnvironmentDataSource/axual_environment.tf
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ resource "axual_group" "team-integrations" {
]
}

data "axual_instance" "testInstance"{
name = "testInstance"
}

resource "axual_environment" "tf-test-env" {
name = "tf-development"
short_name = "tfdev"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ resource "axual_group" "team-integrations" {
]
}

data "axual_instance" "testInstance"{
name = "testInstance"
}

resource "axual_environment" "tf-test-env" {
name = "tf-development"
short_name = "tfdev"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ resource "axual_group" "team-integrations3" {
]
}

data "axual_instance" "testInstance"{
name = "testInstance"
}

resource "axual_environment" "tf-test-env" {
name = "tf-development1"
short_name = "tfdev"
Expand Down
3 changes: 0 additions & 3 deletions internal/tests/InstanceDataSource/axual_instance_initial.tf

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
data "axual_instance" "testInstance" {
data "axual_instance" "wrong_name" {
name = "dt"
}
10 changes: 3 additions & 7 deletions internal/tests/InstanceDataSource/instance_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@ func TestInstanceDataSource(t *testing.T) {
ExternalProviders: GetProviderConfig(t).ExternalProviders,
Steps: []resource.TestStep{
{
Config: GetProvider() + GetFile("axual_instance_initial.tf"),
Config: GetProvider(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.axual_instance.dta", "name", "Dev Test Acceptance"),
resource.TestCheckResourceAttr("data.axual_instance.dta", "description", "Dev Test Acceptance"),
resource.TestCheckResourceAttr("data.axual_instance.dta", "short_name", "dta"),
resource.TestCheckResourceAttr("data.axual_instance.dta", "id", "4b0f204ede6542dfae6bf836f8185c5e"),
resource.TestCheckResourceAttr("data.axual_instance.testInstance", "name", "Dev Test Acceptance"),
),
},
{
Expand All @@ -33,9 +30,8 @@ func TestInstanceDataSource(t *testing.T) {
ExpectError: regexp.MustCompile("Attribute name string length must be between 3 and 50, got: 2"),
},
{
// To ensure cleanup if one of the test cases had an error
Destroy: true,
Config: GetProvider() + GetFile("axual_instance_initial.tf"),
Config: GetProvider(),
},
},
})
Expand Down
Loading

0 comments on commit 7f801c8

Please sign in to comment.