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

Add Database kafka schema registry #449

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions ovh/data_cloud_project_database_kafka_schemaregistryacl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package ovh

import (
"context"
"fmt"
"log"
"net/url"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
)

func dataSourceCloudProjectDatabaseKafkaSchemaRegistryAcl() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceCloudProjectDatabaseKafkaSchemaregistryaclRead,
Schema: map[string]*schema.Schema{
"service_name": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("OVH_CLOUD_PROJECT_SERVICE", nil),
},
"cluster_id": {
Type: schema.TypeString,
Description: "Id of the database cluster",
Required: true,
},
"id": {
Type: schema.TypeString,
Description: "Shema registry ACL ID",
Required: true,
},

// Computed
"permission": {
Type: schema.TypeString,
Description: "Permission to give to this username on this resource",
Computed: true,
},
"resource": {
Type: schema.TypeString,
Description: "Resource affected by this ACL",
Computed: true,
},
"username": {
Type: schema.TypeString,
Description: "Username affected by this ACL",
Computed: true,
},
},
}
}

func dataSourceCloudProjectDatabaseKafkaSchemaregistryaclRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
clusterID := d.Get("cluster_id").(string)
id := d.Get("id").(string)

endpoint := fmt.Sprintf("/cloud/project/%s/database/kafka/%s/schemaRegistryAcl/%s",
url.PathEscape(serviceName),
url.PathEscape(clusterID),
url.PathEscape(id),
)
res := &CloudProjectDatabaseKafkaAclResponse{}

log.Printf("[DEBUG] Will read schema registry ACL %s from cluster %s from project %s", id, clusterID, serviceName)
if err := config.OVHClient.Get(endpoint, res); err != nil {
return diag.FromErr(helpers.CheckDeleted(d, err, endpoint))
}

for k, v := range res.ToMap() {
if k != "id" {
d.Set(k, v)
} else {
d.SetId(fmt.Sprint(v))
}
}

log.Printf("[DEBUG] Read ACL %+v", res)
return nil
}
64 changes: 64 additions & 0 deletions ovh/data_cloud_project_database_kafka_schemaregistryacls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package ovh

import (
"context"
"fmt"
"log"
"net/url"
"sort"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
"github.com/ovh/terraform-provider-ovh/ovh/helpers/hashcode"
)

func dataSourceCloudProjectDatabaseKafkaSchemaRegistryAcls() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceCloudProjectDatabaseKafkaSchemaregistryclsRead,
Schema: map[string]*schema.Schema{
"service_name": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("OVH_CLOUD_PROJECT_SERVICE", nil),
},
"cluster_id": {
Type: schema.TypeString,
Description: "Id of the database cluster",
Required: true,
},

// Computed
"acl_ids": {
Type: schema.TypeList,
Description: "List of schema registry acl ids",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Description: "List of schema registry acl ids",
Description: "List of schema registry ACL IDs",

Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}

func dataSourceCloudProjectDatabaseKafkaSchemaregistryclsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
clusterId := d.Get("cluster_id").(string)

endpoint := fmt.Sprintf("/cloud/project/%s/database/kafka/%s/schemaRegistryAcl",
url.PathEscape(serviceName),
url.PathEscape(clusterId),
)
res := make([]string, 0)

log.Printf("[DEBUG] Will read schema registry ACLs from cluster %s from project %s", clusterId, serviceName)
if err := config.OVHClient.Get(endpoint, &res); err != nil {
return diag.FromErr(helpers.CheckDeleted(d, err, endpoint))
}

// sort.Strings sorts in place, returns nothing
sort.Strings(res)

d.SetId(hashcode.Strings(res))
d.Set("acl_ids", res)
return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package ovh

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

const testAccCloudProjectDatabaseKafkaSchemaregistryaclsDatasourceConfig_Basic = `
resource "ovh_cloud_project_database" "db" {
service_name = "%s"
description = "%s"
engine = "kafka"
version = "%s"
plan = "business"
nodes {
region = "%s"
}
nodes {
region = "%s"
}
nodes {
region = "%s"
}
flavor = "%s"
}

resource "ovh_cloud_project_database_kafka_schemaregistryacl" "schemaRegistryAcl" {
service_name = ovh_cloud_project_database.db.service_name
cluster_id = ovh_cloud_project_database.db.id
permission = "%s"
resource = "%s"
username = "%s"
}

data "ovh_cloud_project_database_kafka_schemaregistryacls" "schemaRegistryAcls" {
service_name = ovh_cloud_project_database_kafka_acl.acl.service_name
cluster_id = ovh_cloud_project_database_kafka_acl.acl.cluster_id
}
`

func TestAccCloudProjectDatabaseKafkaSchemaregistryaclsDataSource_basic(t *testing.T) {
serviceName := os.Getenv("OVH_CLOUD_PROJECT_SERVICE_TEST")
version := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_KAFKA_VERSION_TEST")
if version == "" {
version = os.Getenv("OVH_CLOUD_PROJECT_DATABASE_VERSION_TEST")
}
region := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_REGION_TEST")
flavor := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_FLAVOR_TEST")
description := acctest.RandomWithPrefix(test_prefix)
permission := "schema_registry_read"
aclResource := "Subject:myResource"
username := "johnDoe"

config := fmt.Sprintf(
testAccCloudProjectDatabaseKafkaAclsDatasourceConfig_Basic,
serviceName,
description,
version,
region,
region,
region,
flavor,
permission,
aclResource,
username,
)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheckCloudDatabaseNoEngine(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(
"data.ovh_cloud_project_database_kafka_schemaregistryacls.schemaRegistryAcls",
"acl_ids.#",
),
),
},
},
})
}
3 changes: 3 additions & 0 deletions ovh/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ func Provider() *schema.Provider {
"ovh_cloud_project_database_ip_restrictions": dataSourceCloudProjectDatabaseIPRestrictions(),
"ovh_cloud_project_database_kafka_acl": dataSourceCloudProjectDatabaseKafkaACL(),
"ovh_cloud_project_database_kafka_acls": dataSourceCloudProjectDatabaseKafkaAcls(),
"ovh_cloud_project_database_kafka_schemaregistryacl": dataSourceCloudProjectDatabaseKafkaSchemaRegistryAcl(),
"ovh_cloud_project_database_kafka_schemaregistryacls": dataSourceCloudProjectDatabaseKafkaSchemaRegistryAcls(),
"ovh_cloud_project_database_kafka_topic": dataSourceCloudProjectDatabaseKafkaTopic(),
"ovh_cloud_project_database_kafka_topics": dataSourceCloudProjectDatabaseKafkaTopics(),
"ovh_cloud_project_database_kafka_user_access": dataSourceCloudProjectDatabaseKafkaUserAccess(),
Expand Down Expand Up @@ -146,6 +148,7 @@ func Provider() *schema.Provider {
"ovh_cloud_project_database_integration": resourceCloudProjectDatabaseIntegration(),
"ovh_cloud_project_database_ip_restriction": resourceCloudProjectDatabaseIpRestriction(),
"ovh_cloud_project_database_kafka_acl": resourceCloudProjectDatabaseKafkaAcl(),
"ovh_cloud_project_database_kafka_schemaregistryacl": resourceCloudProjectDatabaseKafkaSchemaregistryacl(),
"ovh_cloud_project_database_kafka_topic": resourceCloudProjectDatabaseKafkaTopic(),
"ovh_cloud_project_database_m3db_namespace": resourceCloudProjectDatabaseM3dbNamespace(),
"ovh_cloud_project_database_m3db_user": resourceCloudProjectDatabaseM3dbUser(),
Expand Down
Loading