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

Prometheus for database #787

Merged
merged 4 commits into from
Jan 24, 2025
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
66 changes: 66 additions & 0 deletions ovh/data_cloud_project_database_mongodb_prometheus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
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 dataSourceCloudProjectDatabaseMongodbPrometheus() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceCloudProjectDatabaseMongodbPrometheusRead,
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
"username": {
Type: schema.TypeString,
Description: "Name of the user",
Computed: true,
},
"srv_domain": {
Type: schema.TypeString,
Description: "Name of the srv domain endpoint",
Computed: true,
},
},
}
}

func dataSourceCloudProjectDatabaseMongodbPrometheusRead(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/mongodb/%s/prometheus",
url.PathEscape(serviceName),
url.PathEscape(clusterID),
)
res := &CloudProjectDatabaseMongodbPrometheusEndpointResponse{}

log.Printf("[DEBUG] Will read database %s from project: %s", d.Id(), serviceName)
if err := config.OVHClient.GetWithContext(ctx, endpoint, res); err != nil {
return diag.FromErr(helpers.CheckDeleted(d, err, endpoint))
}

for k, v := range res.toMap() {
d.Set(k, v)
}
d.SetId(clusterID)

return nil
}
78 changes: 78 additions & 0 deletions ovh/data_cloud_project_database_mongodb_prometheus_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package ovh

import (
"fmt"
"os"
"testing"

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

const testAccCloudProjectDatabaseMongodbPrometheusDatasourceConfig_Basic = `
resource "ovh_cloud_project_database" "db" {
service_name = "%s"
description = "%s"
engine = "mongodb"
version = "%s"
plan = "production"
nodes {
region = "%s"
}
nodes {
region = "%s"
}
nodes {
region = "%s"
}
flavor = "%s"
}

resource "ovh_cloud_project_database_mongodb_prometheus" "prometheus" {
service_name = ovh_cloud_project_database.db.service_name
cluster_id = ovh_cloud_project_database.db.id
}

data "ovh_cloud_project_database_mongodb_prometheus" "prometheus" {
service_name = ovh_cloud_project_database_mongodb_prometheus.prometheus.service_name
cluster_id = ovh_cloud_project_database_mongodb_prometheus.prometheus.cluster_id
}
`

func TestAccCloudProjectDatabaseMongodbPrometheusDataSource_basic(t *testing.T) {
serviceName := os.Getenv("OVH_CLOUD_PROJECT_SERVICE_TEST")
version := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_MONGODB_VERSION_TEST")
if version == "" {
version = os.Getenv("OVH_CLOUD_PROJECT_DATABASE_VERSION_TEST")
}
region := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_MONGODB_REGION_TEST")
flavor := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_MONGODB_FLAVOR_TEST")
description := acctest.RandomWithPrefix(test_prefix)

config := fmt.Sprintf(
testAccCloudProjectDatabaseMongodbPrometheusDatasourceConfig_Basic,
serviceName,
description,
version,
region,
region,
region,
flavor,
)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheckCloudDatabase(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"data.ovh_cloud_project_database_prometheus.prometheus", "username", "prometheus"),
resource.TestCheckResourceAttrSet(
"data.ovh_cloud_project_database_mongodb_prometheus.prometheus", "srv_domain"),
),
},
},
})
}
91 changes: 91 additions & 0 deletions ovh/data_cloud_project_database_prometheus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
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 dataSourceCloudProjectDatabasePrometheus() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceCloudProjectDatabasePrometheusRead,
Schema: map[string]*schema.Schema{
"service_name": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("OVH_CLOUD_PROJECT_SERVICE", nil),
},
"engine": {
Type: schema.TypeString,
Description: "Name of the engine of the service",
ForceNew: true,
Required: true,
ValidateDiagFunc: helpers.ValidateDiagEnum([]string{"cassandra", "kafka", "kafkaConnect", "kafkaMirrorMaker", "mysql", "opensearch", "postgresql", "redis"}),
},
"cluster_id": {
Type: schema.TypeString,
Description: "Id of the database cluster",
ForceNew: true,
Required: true,
},

//Computed
"username": {
Type: schema.TypeString,
Description: "Name of the user",
Computed: true,
},
"targets": {
Type: schema.TypeList,
Description: "List of all endpoint targets",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"host": {
Type: schema.TypeString,
Description: "Host of the endpoint",
Computed: true,
},
"port": {
Type: schema.TypeInt,
Description: "Connection port for the endpoint",
Computed: true,
},
},
},
},
},
}
}

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

endpoint := fmt.Sprintf("/cloud/project/%s/database/%s/%s/prometheus",
url.PathEscape(serviceName),
url.PathEscape(engine),
url.PathEscape(clusterID),
)
res := &CloudProjectDatabasePrometheusEndpointResponse{}

log.Printf("[DEBUG] Will read database %s from project: %s", d.Id(), serviceName)
if err := config.OVHClient.GetWithContext(ctx, endpoint, res); err != nil {
return diag.FromErr(helpers.CheckDeleted(d, err, endpoint))
}

for k, v := range res.toMap() {
d.Set(k, v)
}
d.SetId(clusterID)

return nil
}
75 changes: 75 additions & 0 deletions ovh/data_cloud_project_database_prometheus_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package ovh

import (
"fmt"
"os"
"testing"

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

const testAccCloudProjectDatabasePrometheusDatasourceConfig_Basic = `
resource "ovh_cloud_project_database" "db" {
service_name = "%s"
description = "%s"
engine = "%s"
version = "%s"
plan = "essential"
nodes {
region = "%s"
}
flavor = "%s"
}

resource "ovh_cloud_project_database_prometheus" "prometheus" {
service_name = ovh_cloud_project_database.db.service_name
engine = ovh_cloud_project_database.db.engine
cluster_id = ovh_cloud_project_database.db.id
}

data "ovh_cloud_project_database_prometheus" "prometheus" {
service_name = ovh_cloud_project_database_prometheus.prometheus.service_name
engine = ovh_cloud_project_database_prometheus.prometheus.engine
cluster_id = ovh_cloud_project_database_prometheus.prometheus.cluster_id
}
`

func TestAccCloudProjectDatabasePrometheusDataSource_basic(t *testing.T) {
serviceName := os.Getenv("OVH_CLOUD_PROJECT_SERVICE_TEST")
engine := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_ENGINE_TEST")
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)

config := fmt.Sprintf(
testAccCloudProjectDatabasePrometheusDatasourceConfig_Basic,
serviceName,
description,
engine,
version,
region,
flavor,
)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheckCloudDatabase(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(
"data.ovh_cloud_project_database_prometheus.prometheus", "targets.#"),
resource.TestCheckResourceAttrSet(
"data.ovh_cloud_project_database_prometheus.prometheus", "targets.0.host"),
resource.TestCheckResourceAttrSet(
"data.ovh_cloud_project_database_prometheus.prometheus", "targets.0.port"),
resource.TestCheckResourceAttr(
"data.ovh_cloud_project_database_prometheus.prometheus", "username", "prometheus"),
),
},
},
})
}
64 changes: 64 additions & 0 deletions ovh/import_cloud_project_database_mongodb_prometheus_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package ovh

import (
"fmt"
"os"
"testing"

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

func TestAccCloudProjectDatabaseMongodbPrometheus_importBasic(t *testing.T) {
serviceName := os.Getenv("OVH_CLOUD_PROJECT_SERVICE_TEST")
version := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_MONGODB_VERSION_TEST")
if version == "" {
version = os.Getenv("OVH_CLOUD_PROJECT_DATABASE_VERSION_TEST")
}
region := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_MONGODB_REGION_TEST")
flavor := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_MONGODB_FLAVOR_TEST")
description := acctest.RandomWithPrefix(test_prefix)

config := fmt.Sprintf(
testAccCloudProjectDatabaseMongodbPrometheusConfig,
serviceName,
description,
version,
region,
region,
region,
flavor,
)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheckCloudDatabase(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: config,
},
{
ResourceName: "ovh_cloud_project_database_mongodb_prometheus.prometheus",
ImportState: true,
ImportStateVerify: true,
ImportStateIdFunc: testAccCloudProjectDatabaseMongodbPrometheusImportId("ovh_cloud_project_database_mongodb_prometheus.prometheus"),
ImportStateVerifyIgnore: []string{"password"},
},
},
})
}

func testAccCloudProjectDatabaseMongodbPrometheusImportId(resourceName string) resource.ImportStateIdFunc {
return func(s *terraform.State) (string, error) {
testUser, ok := s.RootModule().Resources[resourceName]
if !ok {
return "", fmt.Errorf("ovh_cloud_project_database_mongodb_prometheus not found: %s", resourceName)
}
return fmt.Sprintf(
"%s/%s",
testUser.Primary.Attributes["service_name"],
testUser.Primary.Attributes["cluster_id"],
), nil
}
}
Loading