Skip to content

Commit

Permalink
Implement linode_database_mysql_v2 data source (#1717)
Browse files Browse the repository at this point in the history
* Implement linode_database_mysql_v2 data source

* Add trailing newline to docs

* fix lint

* Address docs feedback

* Fix test

* oops

* g6-standard-1 -> g6-nanode-1

* Fix other test oversights
  • Loading branch information
lgarber-akamai authored Jan 23, 2025
1 parent 166ec90 commit 5369643
Show file tree
Hide file tree
Showing 10 changed files with 401 additions and 10 deletions.
102 changes: 102 additions & 0 deletions docs/data-sources/database_mysql_v2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
---
page_title: "Linode: linode_database_mysql_v2"
description: |-
Provides information about a Linode MySQL Database.
---

# Data Source: linode\_database\_mysql\_v2

Provides information about a Linode MySQL Database.
For more information, see the [Linode APIv4 docs](https://techdocs.akamai.com/linode-api/reference/get-databases-mysql-instance).

## Example Usage

Get information about a MySQL database:

```hcl
data "linode_database_mysql_v2" "my-db" {
id = 12345
}
```

## Argument Reference

The following arguments are supported:

* `id` - The ID of the MySQL database.

## Attributes Reference

The `linode_database_mysql_v2` data source exports the following attributes:

* `allow_list` - A list of IP addresses that can access the Managed Database. Each item can be a single IP address or a range in CIDR format. Use `linode_database_access_controls` to manage your allow list separately.

* `ca_cert` - The base64-encoded SSL CA certificate for the Managed Database.

* `cluster_size` - The number of Linode Instance nodes deployed to the Managed Database. (default `1`)

* `created` - When this Managed Database was created.

* `encrypted` - Whether the Managed Databases is encrypted.

* `engine` - The Managed Database engine. (e.g. `mysql`)

* `engine_id` - The Managed Database engine in engine/version format. (e.g. `mysql`)

* `fork_restore_time` - The database timestamp from which it was restored.

* `fork_source` - The ID of the database that was forked from.

* `host_primary` - The primary host for the Managed Database.

* `host_secondary` - The secondary/private host for the managed database.

* `label` - A unique, user-defined string referring to the Managed Database.

* [`pending_updates`](#pending_updates) - A set of pending updates.

* `platform` - The back-end platform for relational databases used by the service.

* `port` - The access port for this Managed Database.

* `region` - The region to use for the Managed Database.

* `root_password` - The randomly-generated root password for the Managed Database instance.

* `root_username` - The root username for the Managed Database instance.

* `ssl_connection` - Whether to require SSL credentials to establish a connection to the Managed Database.

* `status` - The operating status of the Managed Database.

* `type` - The Linode Instance type used for the nodes of the Managed Database.

* `updated` - When this Managed Database was last updated.

* [`updates`](#updates) - Configuration settings for automated patch update maintenance for the Managed Database.

* `version` - The Managed Database engine version. (e.g. `13.2`)

## pending_updates

The following arguments are exposed by each entry in the `pending_updates` attribute:

* `deadline` - The time when a mandatory update needs to be applied.

* `description` - A description of the update.

* `planned_for` - The date and time a maintenance update will be applied.

## updates

The following arguments are supported in the `updates` specification block:

* `day_of_week` - The day to perform maintenance. (`monday`, `tuesday`, ...)

* `duration` - The maximum maintenance window time in hours. (`1`..`3`)

* `frequency` - Whether maintenance occurs on a weekly or monthly basis. (`weekly`, `monthly`)

* `hour_of_day` - The hour to begin maintenance based in UTC time. (`0`..`23`)

* `week_of_month` - The week of the month to perform monthly frequency updates. Required for `monthly` frequency updates. (`1`..`4`)
2 changes: 1 addition & 1 deletion docs/data-sources/database_postgresql_v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ The `linode_database_postgresql_v2` data source exports the following attributes

* `label` - A unique, user-defined string referring to the Managed Database.

* `pending_updates` - A set of pending updates.
* [`pending_updates`](#pending_updates) - A set of pending updates.

* `platform` - The back-end platform for relational databases used by the service.

Expand Down
77 changes: 77 additions & 0 deletions linode/databasemysqlv2/datasource_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//go:build integration || databasemysqlv2

package databasemysqlv2_test

import (
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/linode/terraform-provider-linode/v2/linode/acceptance"
"github.com/linode/terraform-provider-linode/v2/linode/databasemysqlv2/tmpl"
)

func TestAccDataSource_basic(t *testing.T) {
t.Parallel()

label := acctest.RandomWithPrefix("tf_test")
dataSourceName := "data.linode_database_mysql_v2.foobar"

resource.Test(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
ProtoV5ProviderFactories: acceptance.ProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
Config: tmpl.Data(t, tmpl.TemplateData{
Label: label,
Region: testRegion,
EngineID: testEngine,
Type: "g6-nanode-1",
AllowedIP: "10.0.0.3/32",
ClusterSize: 1,
Updates: tmpl.TemplateDataUpdates{
HourOfDay: 3,
DayOfWeek: 2,
Duration: 4,
Frequency: "weekly",
},
}),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(dataSourceName, "id"),

resource.TestCheckResourceAttrSet(dataSourceName, "ca_cert"),
resource.TestCheckResourceAttr(dataSourceName, "cluster_size", "1"),
resource.TestCheckResourceAttrSet(dataSourceName, "created"),
resource.TestCheckResourceAttr(dataSourceName, "encrypted", "true"),
resource.TestCheckResourceAttr(dataSourceName, "engine", "mysql"),
resource.TestCheckResourceAttr(dataSourceName, "engine_id", testEngine),
resource.TestCheckNoResourceAttr(dataSourceName, "fork_restore_time"),
resource.TestCheckNoResourceAttr(dataSourceName, "fork_source"),
resource.TestCheckResourceAttrSet(dataSourceName, "host_primary"),
resource.TestCheckResourceAttr(dataSourceName, "label", label),
resource.TestCheckResourceAttrSet(dataSourceName, "members.%"),
resource.TestCheckResourceAttrSet(dataSourceName, "root_password"),
resource.TestCheckResourceAttrSet(dataSourceName, "root_username"),
resource.TestCheckResourceAttr(dataSourceName, "platform", "rdbms-default"),
resource.TestCheckResourceAttrSet(dataSourceName, "port"),
resource.TestCheckResourceAttr(dataSourceName, "region", testRegion),
resource.TestCheckResourceAttr(dataSourceName, "ssl_connection", "true"),
resource.TestCheckResourceAttr(dataSourceName, "status", "active"),
resource.TestCheckResourceAttr(dataSourceName, "type", "g6-nanode-1"),
resource.TestCheckResourceAttrSet(dataSourceName, "updated"),
resource.TestCheckResourceAttrSet(dataSourceName, "version"),

resource.TestCheckResourceAttr(dataSourceName, "allow_list.#", "1"),
resource.TestCheckResourceAttr(dataSourceName, "allow_list.0", "10.0.0.3/32"),

resource.TestCheckResourceAttr(dataSourceName, "updates.hour_of_day", "3"),
resource.TestCheckResourceAttr(dataSourceName, "updates.day_of_week", "2"),
resource.TestCheckResourceAttr(dataSourceName, "updates.duration", "4"),
resource.TestCheckResourceAttr(dataSourceName, "updates.frequency", "weekly"),

resource.TestCheckResourceAttr(dataSourceName, "pending_updates.#", "0"),
),
},
},
})
}
54 changes: 54 additions & 0 deletions linode/databasemysqlv2/framework_datasource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package databasemysqlv2

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/linode/terraform-provider-linode/v2/linode/helper"
)

type DataSource struct {
helper.BaseDataSource
}

func NewDataSource() datasource.DataSource {
return &DataSource{
BaseDataSource: helper.NewBaseDataSource(
helper.BaseDataSourceConfig{
Name: "linode_database_mysql_v2",
Schema: &frameworkDatasourceSchema,
},
),
}
}

func (d *DataSource) Read(
ctx context.Context,
req datasource.ReadRequest,
resp *datasource.ReadResponse,
) {
tflog.Debug(ctx, "Read data.linode_database_mysql_v2")

client := d.Meta.Client
var data Model

resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}

id := helper.FrameworkSafeStringToInt(data.ID.ValueString(), &resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
}

ctx = tflog.SetField(ctx, "id", id)

resp.Diagnostics.Append(data.Refresh(ctx, client, id, false)...)
if resp.Diagnostics.HasError() {
return
}

resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
135 changes: 135 additions & 0 deletions linode/databasemysqlv2/framework_datasource_schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package databasemysqlv2

import (
"github.com/hashicorp/terraform-plugin-framework-timetypes/timetypes"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
)

var frameworkDatasourceSchema = schema.Schema{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Description: "The id of the VPC.",
Required: true,
},

"engine_id": schema.StringAttribute{
Description: "The unique ID of the database engine and version to use. (e.g. mysql/8)",
Computed: true,
},
"label": schema.StringAttribute{
Description: "A unique, user-defined string referring to the Managed Database.",
Computed: true,
},
"region": schema.StringAttribute{
Description: "The Region ID for the Managed Database.",
Computed: true,
},
"type": schema.StringAttribute{
Description: "The Linode Instance type used by the Managed Database for its nodes.",
Computed: true,
},

"allow_list": schema.SetAttribute{
ElementType: types.StringType,
Computed: true,
Description: "A list of IP addresses that can access the Managed Database. " +
"Each item can be a single IP address or a range in CIDR format.",
},
"ca_cert": schema.StringAttribute{
Description: "The base64-encoded SSL CA certificate for the Managed Database.",
Computed: true,
Sensitive: true,
},
"cluster_size": schema.Int64Attribute{
Computed: true,
Description: "The number of Linode instance nodes deployed to the Managed Database.",
},
"fork_restore_time": schema.StringAttribute{
Description: "The database timestamp from which it was restored.",
Computed: true,
CustomType: timetypes.RFC3339Type{},
},
"fork_source": schema.Int64Attribute{
Description: "The ID of the database that was forked from.",
Computed: true,
},
"updates": schema.ObjectAttribute{
Description: "Configuration settings for automated patch update maintenance for the Managed Database.",
AttributeTypes: updatesAttributes,
Computed: true,
},

"created": schema.StringAttribute{
Description: "When this Managed Database was created.",
Computed: true,
CustomType: timetypes.RFC3339Type{},
},
"encrypted": schema.BoolAttribute{
Description: "Whether the Managed Databases is encrypted.",
Computed: true,
},
"engine": schema.StringAttribute{
Description: "The Managed Database engine in engine/version format.",
Computed: true,
},
"host_primary": schema.StringAttribute{
Description: "The primary host for the Managed Database.",
Computed: true,
},
"host_secondary": schema.StringAttribute{
Description: "The secondary/private host for the Managed Database.",
Computed: true,
},
"members": schema.MapAttribute{
ElementType: types.StringType,
Computed: true,
Description: "A mapping between IP addresses and strings designating them as primary or failover.",
},
"oldest_restore_time": schema.StringAttribute{
Description: "The oldest time to which a database can be restored.",
Computed: true,
CustomType: timetypes.RFC3339Type{},
},
"pending_updates": schema.SetAttribute{
Description: "A set of pending updates.",
Computed: true,
ElementType: types.ObjectType{AttrTypes: pendingUpdateAttributes},
},
"platform": schema.StringAttribute{
Computed: true,
Description: "The back-end platform for relational databases used by the service.",
},
"port": schema.Int64Attribute{
Description: "The access port for this Managed Database.",
Computed: true,
},
"root_password": schema.StringAttribute{
Description: "The randomly generated root password for the Managed Database instance.",
Computed: true,
Sensitive: true,
},
"root_username": schema.StringAttribute{
Description: "The root username for the Managed Database instance.",
Computed: true,
Sensitive: true,
},
"ssl_connection": schema.BoolAttribute{
Computed: true,
Description: "Whether to require SSL credentials to establish a connection to the Managed Database.",
},
"status": schema.StringAttribute{
Computed: true,
Description: "The operating status of the Managed Database.",
},
"updated": schema.StringAttribute{
Description: "When this Managed Database was last updated.",
Computed: true,
CustomType: timetypes.RFC3339Type{},
},
"version": schema.StringAttribute{
Description: "The Managed Database engine version.",
Computed: true,
},
},
}
5 changes: 4 additions & 1 deletion linode/databasemysqlv2/framework_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@ type ModelPendingUpdate struct {
PlannedFor timetypes.RFC3339 `tfsdk:"planned_for"`
}

type Model struct {
type ResourceModel struct {
Model
Timeouts timeouts.Value `tfsdk:"timeouts"`
}

type Model struct {
ID types.String `tfsdk:"id"`

AllowList types.Set `tfsdk:"allow_list"`
Expand Down
Loading

0 comments on commit 5369643

Please sign in to comment.