Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use bundle for kafka #757

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 6.7.1 (future release)
### Refactor
- Use SDK Bundle instead of the Kafka SDK
## 6.7.0
### Fixes
- Fix [#735](https://github.com/ionos-cloud/terraform-provider-ionoscloud/issues/735) by reading all values for `api_subnet_allow_list`, not only non-nill values.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
github.com/iancoleman/strcase v0.3.0
github.com/ionos-cloud/sdk-go-api-gateway v1.0.0
github.com/ionos-cloud/sdk-go-bundle/products/cdn/v2 v2.1.0
github.com/ionos-cloud/sdk-go-bundle/products/kafka/v2 v2.0.0
github.com/ionos-cloud/sdk-go-bundle/products/logging/v2 v2.1.1
github.com/ionos-cloud/sdk-go-bundle/products/monitoring/v2 v2.0.0
github.com/ionos-cloud/sdk-go-bundle/products/vpn/v2 v2.0.2
Expand All @@ -29,7 +30,6 @@ require (
github.com/ionos-cloud/sdk-go-dbaas-mongo v1.3.1
github.com/ionos-cloud/sdk-go-dbaas-postgres v1.1.3
github.com/ionos-cloud/sdk-go-dns v1.2.1
github.com/ionos-cloud/sdk-go-kafka v1.0.0
github.com/ionos-cloud/sdk-go-nfs v1.1.1
github.com/ionos-cloud/sdk-go-object-storage v1.1.0
github.com/ionos-cloud/sdk-go-object-storage-management v1.0.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ github.com/ionos-cloud/sdk-go-api-gateway v1.0.0 h1:T+JzarV1Iw7NjWQfEuv5bESnHoCL
github.com/ionos-cloud/sdk-go-api-gateway v1.0.0/go.mod h1:TkSlamP+qKRT8cgBaZEN8rawanVluOR0dXbXXdmmwTY=
github.com/ionos-cloud/sdk-go-bundle/products/cdn/v2 v2.1.0 h1:8fRICa4i+GJHrCgnLe8t3uwfahkg7pcDDwoLkH3B2Xc=
github.com/ionos-cloud/sdk-go-bundle/products/cdn/v2 v2.1.0/go.mod h1:BF6R+j1GdJEMImxkeh32qjIVWqsBfgi6VgyfsY5ezIA=
github.com/ionos-cloud/sdk-go-bundle/products/kafka/v2 v2.0.0 h1:KIauLPtA21TTDioX2y83hOWYL/rVhFiXNCRnfHrnzDQ=
github.com/ionos-cloud/sdk-go-bundle/products/kafka/v2 v2.0.0/go.mod h1:FveJvw/9hh+eo42FEXrUFTLJWqNJvsm7pXeEDph3rJM=
github.com/ionos-cloud/sdk-go-bundle/products/logging/v2 v2.1.1 h1:GXxdNKl7haQnHO1QewZeXazur5+LDCpHqvP+nNObGaA=
github.com/ionos-cloud/sdk-go-bundle/products/logging/v2 v2.1.1/go.mod h1:N0ifRq3ENqGg6qht1WPrVgSkcavPS5SRWlNfkHUZbfQ=
github.com/ionos-cloud/sdk-go-bundle/products/monitoring/v2 v2.0.0 h1:gVh+jd0o8ROtJLWqUB6/VU8vSLdj/kRYAz/qbMO7z7I=
Expand All @@ -140,8 +142,6 @@ github.com/ionos-cloud/sdk-go-dbaas-postgres v1.1.3 h1:T05nbKqBIw2zmPwaV2pWyFcUV
github.com/ionos-cloud/sdk-go-dbaas-postgres v1.1.3/go.mod h1:5cKJUIcXfMbG5vVkz6cYjnToZvaQylkLsIuhOW7PhEI=
github.com/ionos-cloud/sdk-go-dns v1.2.1 h1:Sy7Jg5trzObad9VPvEh7BgeJrIhaxdGlBEvG2aHJZH8=
github.com/ionos-cloud/sdk-go-dns v1.2.1/go.mod h1:l9gYdwtUshlBOiIi4nHn3RCX81XlV3VoNvLJrO2VfHg=
github.com/ionos-cloud/sdk-go-kafka v1.0.0 h1:S0Tfy/KlYW7UJW6DoU43fF0/iNPc1YKC+AC56G9wfA4=
github.com/ionos-cloud/sdk-go-kafka v1.0.0/go.mod h1:yIq1FjRtdcbwmybb42puagpLjmD2wbWAew8UMHO5wqU=
github.com/ionos-cloud/sdk-go-nfs v1.1.1 h1:tMMUPGKRQm2z5FTIMFoE/DJ8Xw3522T0jVutCGmsyMw=
github.com/ionos-cloud/sdk-go-nfs v1.1.1/go.mod h1:jgNAKLmjzBP6RzPnjydleZzVlAL8KnYKPcBjWgLVxJk=
github.com/ionos-cloud/sdk-go-object-storage v1.1.0 h1:4JTsxrbzFltF9liAHZ2I9f53rpkrXm8642UwQoQ+pMc=
Expand Down
6 changes: 3 additions & 3 deletions ionoscloud/data_source_kafka_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
kafkaSdk "github.com/ionos-cloud/sdk-go-kafka"
kafkaSdk "github.com/ionos-cloud/sdk-go-bundle/products/kafka/v2"

"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services"
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services/kafka"
Expand Down Expand Up @@ -124,8 +124,8 @@ func dataSourceKafkaClusterRead(ctx context.Context, d *schema.ResourceData, met
return diag.FromErr(fmt.Errorf("an error occurred while fetching Kafka Cluster: %w", err))
}

for _, cluster := range *clusters.Items {
if cluster.Properties != nil && cluster.Properties.Name != nil && utils.NameMatches(*cluster.Properties.Name, name, partialMatch) {
for _, cluster := range clusters.Items {
if utils.NameMatches(cluster.Properties.Name, name, partialMatch) {
results = append(results, cluster)
}
}
Expand Down
6 changes: 3 additions & 3 deletions ionoscloud/data_source_kafka_topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
kafkaSdk "github.com/ionos-cloud/sdk-go-kafka"
kafkaSdk "github.com/ionos-cloud/sdk-go-bundle/products/kafka/v2"

"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services"
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services/kafka"
Expand Down Expand Up @@ -107,8 +107,8 @@ func dataSourceKafkaTopicRead(ctx context.Context, d *schema.ResourceData, meta
return diag.FromErr(fmt.Errorf("an error occurred while fetching Kafka Cluster Topics: %w", err))
}

for _, t := range *topics.Items {
if t.Properties != nil && t.Properties.Name != nil && utils.NameMatches(*t.Properties.Name, name, partialMatch) {
for _, t := range topics.Items {
if utils.NameMatches(t.Properties.Name, name, partialMatch) {
results = append(results, t)
}
}
Expand Down
2 changes: 1 addition & 1 deletion ionoscloud/resource_kafka_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func resourceKafkaClusterCreate(ctx context.Context, d *schema.ResourceData, met
return diags
}

d.SetId(*createdCluster.Id)
d.SetId(createdCluster.Id)
log.Printf("[INFO] Created Kafka Cluster: %s", d.Id())

// Sleep for 5 second to avoid 500 error from the API
Expand Down
2 changes: 1 addition & 1 deletion ionoscloud/resource_kafka_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func testAccCheckKafkaClusterExists(n string) resource.TestCheckFunc {
if err != nil {
return fmt.Errorf("an error occurred while fetching Kafka Cluster with ID: %v, error: %w", rs.Primary.ID, err)
}
if *foundCluster.Id != rs.Primary.ID {
if foundCluster.Id != rs.Primary.ID {
return fmt.Errorf("resource not found")
}

Expand Down
2 changes: 1 addition & 1 deletion ionoscloud/resource_kafka_topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func resourceKafkaTopicCreate(ctx context.Context, d *schema.ResourceData, meta
return diags
}

d.SetId(*createdTopic.Id)
d.SetId(createdTopic.Id)
log.Printf("[INFO] Created Kafka Cluster Topic: %s", d.Id())

// Sleep for 5 second to avoid 500 error from the API
Expand Down
2 changes: 1 addition & 1 deletion ionoscloud/resource_kafka_topic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func testAccCheckKafkaTopicExists(n string) resource.TestCheckFunc {
if err != nil {
return fmt.Errorf("an error occurred while fetching Kafka Cluster Topic with ID: %v, error: %w", rs.Primary.ID, err)
}
if *foundCluster.Id != rs.Primary.ID {
if foundCluster.Id != rs.Primary.ID {
return fmt.Errorf("resource not found")
}

Expand Down
19 changes: 10 additions & 9 deletions services/kafka/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package kafka
import (
"fmt"
"net/http"
"os"
"runtime"

"github.com/hashicorp/terraform-plugin-sdk/v2/meta"

kafka "github.com/ionos-cloud/sdk-go-kafka"
kafka "github.com/ionos-cloud/sdk-go-bundle/products/kafka/v2"
"github.com/ionos-cloud/sdk-go-bundle/shared"

"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/utils"
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/utils/constant"
Expand Down Expand Up @@ -39,25 +39,26 @@ var (

// NewClient returns a new Kafka API client
func NewClient(username, password, token, url, version, terraformVersion string, insecure bool) *Client {
config := kafka.NewConfiguration(username, password, token, url)
config := shared.NewConfiguration(username, password, token, url)

if os.Getenv(constant.IonosDebug) != "" {
config.Debug = true
}
config.MaxRetries = constant.MaxRetries
config.MaxWaitTime = constant.MaxWaitTime
config.HTTPClient = &http.Client{Transport: utils.CreateTransport(insecure)}
config.UserAgent = fmt.Sprintf(
"terraform-provider/%s_ionos-cloud-sdk-go-kafka/%s_hashicorp-terraform/%s_terraform-plugin-sdk/%s_os/%s_arch/%s",
version, kafka.Version, terraformVersion, meta.SDKVersionString(), runtime.GOOS, runtime.GOARCH, //nolint:staticcheck
)

return &Client{sdkClient: *kafka.NewAPIClient(config)}
client := &Client{
sdkClient: *kafka.NewAPIClient(config),
}
client.sdkClient.GetConfig().HTTPClient = &http.Client{Transport: utils.CreateTransport(insecure)}

return client
}

func (c *Client) changeConfigURL(location string) {
config := c.sdkClient.GetConfig()
config.Servers = kafka.ServerConfigurations{
config.Servers = shared.ServerConfigurations{
{
URL: locationToURL[location],
},
Expand Down
55 changes: 19 additions & 36 deletions services/kafka/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package kafka

import (
"context"
"fmt"
"log"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
kafka "github.com/ionos-cloud/sdk-go-kafka"
kafka "github.com/ionos-cloud/sdk-go-bundle/products/kafka/v2"
"github.com/ionos-cloud/sdk-go-bundle/shared"

"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/utils"
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/utils/constant"
Expand All @@ -33,11 +33,8 @@ func (c *Client) IsClusterAvailable(ctx context.Context, d *schema.ResourceData)
if err != nil {
return false, err
}
if cluster.Metadata == nil || cluster.Metadata.State == nil {
return false, fmt.Errorf("expected metadata, got empty for Cluster with ID: %s", clusterID)
}
log.Printf("[DEBUG] Cluster status: %s", *cluster.Metadata.State)
return strings.EqualFold(*cluster.Metadata.State, constant.Available), nil
log.Printf("[DEBUG] Cluster status: %s", cluster.Metadata.State)
return strings.EqualFold(cluster.Metadata.State, constant.Available), nil
}

// DeleteCluster deletes a Kafka Cluster
Expand Down Expand Up @@ -70,7 +67,7 @@ func (c *Client) GetClusterByID(ctx context.Context, id string, location string)
}

// ListClusters retrieves a list of Kafka Clusters
func (c *Client) ListClusters(ctx context.Context, location string) (kafka.ClusterReadList, *kafka.APIResponse, error) {
func (c *Client) ListClusters(ctx context.Context, location string) (kafka.ClusterReadList, *shared.APIResponse, error) {
c.changeConfigURL(location)

Clusters, apiResponse, err := c.sdkClient.ClustersApi.ClustersGet(ctx).Execute()
Expand All @@ -92,44 +89,32 @@ func setClusterPostRequest(d *schema.ResourceData) *kafka.ClusterCreate {
}

connection := kafka.KafkaClusterConnection{
DatacenterId: &datacenterID,
LanId: &lanID,
BrokerAddresses: &brokerAddresses,
DatacenterId: datacenterID,
LanId: lanID,
BrokerAddresses: brokerAddresses,
}

return kafka.NewClusterCreate(*kafka.NewCluster(clusterName, version, size, []kafka.KafkaClusterConnection{connection}))
}

// SetKafkaClusterData sets the Kafka Cluster data to the Terraform Resource Data
func (c *Client) SetKafkaClusterData(d *schema.ResourceData, cluster *kafka.ClusterRead) error {
if cluster.Id != nil {
d.SetId(*cluster.Id)
}

if cluster.Properties == nil || cluster.Metadata == nil {
return fmt.Errorf("expected properties and metadata in the response for the Kafka Cluster with ID: %s, but received 'nil' instead", *cluster.Id)
}
d.SetId(cluster.Id)

if cluster.Properties.Name != nil {
if err := d.Set("name", *cluster.Properties.Name); err != nil {
return err
}
if err := d.Set("name", cluster.Properties.Name); err != nil {
return err
}
if cluster.Properties.Version != nil {
if err := d.Set("version", *cluster.Properties.Version); err != nil {
return err
}
if err := d.Set("version", cluster.Properties.Version); err != nil {
return err
}
if cluster.Properties.Size != nil {
if err := d.Set("size", *cluster.Properties.Size); err != nil {
return err
}
if err := d.Set("size", cluster.Properties.Size); err != nil {
return err
}

if cluster.Properties.Connections != nil && len(*cluster.Properties.Connections) > 0 {
if len(cluster.Properties.Connections) > 0 {
var connections []interface{}

for _, connection := range *cluster.Properties.Connections {
for _, connection := range cluster.Properties.Connections {
connectionEntry := c.setConnectionProperties(connection)
connections = append(connections, connectionEntry)
}
Expand All @@ -139,10 +124,8 @@ func (c *Client) SetKafkaClusterData(d *schema.ResourceData, cluster *kafka.Clus
}
}

if cluster.Metadata.BrokerAddresses != nil {
if err := d.Set("broker_addresses", *cluster.Metadata.BrokerAddresses); err != nil {
return err
}
if err := d.Set("broker_addresses", cluster.Metadata.BrokerAddresses); err != nil {
return err
}

return nil
Expand Down
26 changes: 7 additions & 19 deletions services/kafka/topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ package kafka

import (
"context"
"fmt"
"log"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

kafka "github.com/ionos-cloud/sdk-go-kafka"
kafka "github.com/ionos-cloud/sdk-go-bundle/products/kafka/v2"

"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/utils"
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/utils/constant"
Expand Down Expand Up @@ -73,11 +72,8 @@ func (c *Client) IsTopicAvailable(ctx context.Context, d *schema.ResourceData) (
if err != nil {
return false, err
}
if topic.Metadata == nil || topic.Metadata.State == nil {
return false, fmt.Errorf("expected metadata, got empty for Topic with ID: %s", clusterID)
}
log.Printf("[DEBUG] Topic status: %s", *topic.Metadata.State)
return strings.EqualFold(*topic.Metadata.State, constant.Available), nil
log.Printf("[DEBUG] Topic status: %s", topic.Metadata.State)
return strings.EqualFold(topic.Metadata.State, constant.Available), nil
}

// IsTopicDeleted checks if a Kafka Cluster Topic has been deleted
Expand All @@ -96,18 +92,10 @@ func (c *Client) IsTopicDeleted(ctx context.Context, d *schema.ResourceData) (bo

// SetKafkaTopicData sets the Kafka Cluster Topic data to the Terraform Resource Data
func (c *Client) SetKafkaTopicData(d *schema.ResourceData, topic *kafka.TopicRead) error {
if topic.Id != nil {
d.SetId(*topic.Id)
}

if topic.Properties == nil {
return fmt.Errorf("expected properties in the response for the Kafka Cluster Topic with ID: %s, but received 'nil' instead", *topic.Id)
}
d.SetId(topic.Id)

if topic.Properties.Name != nil {
if err := d.Set("name", *topic.Properties.Name); err != nil {
return err
}
if err := d.Set("name", topic.Properties.Name); err != nil {
return err
}
if topic.Properties.NumberOfPartitions != nil {
if err := d.Set("number_of_partitions", *topic.Properties.NumberOfPartitions); err != nil {
Expand Down Expand Up @@ -146,7 +134,7 @@ func setTopicPostRequest(d *schema.ResourceData) *kafka.TopicCreate {

return kafka.NewTopicCreate(
kafka.Topic{
Name: &topicName,
Name: topicName,
NumberOfPartitions: &partitionCount,
ReplicationFactor: &replicationFactor,
LogRetention: &kafka.TopicLogRetention{
Expand Down
Loading
Loading