-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(alloydbomni_database): add resource and datasource
- Loading branch information
Showing
9 changed files
with
431 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "aiven_alloydbomni_database Data Source - terraform-provider-aiven" | ||
subcategory: "" | ||
description: |- | ||
Gets information about a database in an Aiven for AlloyDB Omni service. | ||
This resource is in the beta stage and may change without notice. Set | ||
the PROVIDER_AIVEN_ENABLE_BETA environment variable to use the resource. | ||
--- | ||
|
||
# aiven_alloydbomni_database (Data Source) | ||
|
||
Gets information about a database in an Aiven for AlloyDB Omni service. | ||
|
||
**This resource is in the beta stage and may change without notice.** Set | ||
the `PROVIDER_AIVEN_ENABLE_BETA` environment variable to use the resource. | ||
|
||
|
||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `database_name` (String) The name of the service database. Changing this property forces recreation of the resource. | ||
- `project` (String) The name of the project this resource belongs to. To set up proper dependencies please refer to this variable as a reference. Changing this property forces recreation of the resource. | ||
- `service_name` (String) The name of the service that this resource belongs to. To set up proper dependencies please refer to this variable as a reference. Changing this property forces recreation of the resource. | ||
|
||
### Read-Only | ||
|
||
- `id` (String) The ID of this resource. | ||
- `lc_collate` (String) Default string sort order (`LC_COLLATE`) of the database. The default value is `en_US.UTF-8`. Changing this property forces recreation of the resource. | ||
- `lc_ctype` (String) Default character classification (`LC_CTYPE`) of the database. The default value is `en_US.UTF-8`. Changing this property forces recreation of the resource. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "aiven_alloydbomni_database Resource - terraform-provider-aiven" | ||
subcategory: "" | ||
description: |- | ||
Creates and manages a database in an Aiven for AlloyDB Omni service. | ||
This resource is in the beta stage and may change without notice. Set | ||
the PROVIDER_AIVEN_ENABLE_BETA environment variable to use the resource. | ||
--- | ||
|
||
# aiven_alloydbomni_database (Resource) | ||
|
||
Creates and manages a database in an Aiven for AlloyDB Omni service. | ||
|
||
**This resource is in the beta stage and may change without notice.** Set | ||
the `PROVIDER_AIVEN_ENABLE_BETA` environment variable to use the resource. | ||
|
||
|
||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `database_name` (String) The name of the service database. Changing this property forces recreation of the resource. | ||
- `project` (String) The name of the project this resource belongs to. To set up proper dependencies please refer to this variable as a reference. Changing this property forces recreation of the resource. | ||
- `service_name` (String) The name of the service that this resource belongs to. To set up proper dependencies please refer to this variable as a reference. Changing this property forces recreation of the resource. | ||
|
||
### Optional | ||
|
||
- `lc_collate` (String) Default string sort order (`LC_COLLATE`) of the database. The default value is `en_US.UTF-8`. Changing this property forces recreation of the resource. | ||
- `lc_ctype` (String) Default character classification (`LC_CTYPE`) of the database. The default value is `en_US.UTF-8`. Changing this property forces recreation of the resource. | ||
- `timeouts` (Block, Optional) (see [below for nested schema](#nestedblock--timeouts)) | ||
|
||
### Read-Only | ||
|
||
- `id` (String) The ID of this resource. | ||
|
||
<a id="nestedblock--timeouts"></a> | ||
### Nested Schema for `timeouts` | ||
|
||
Optional: | ||
|
||
- `create` (String) | ||
- `default` (String) | ||
- `delete` (String) | ||
- `read` (String) | ||
- `update` (String) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
internal/sdkprovider/service/alloydbomni/alloydbomni_database.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package alloydbomni | ||
|
||
import ( | ||
"context" | ||
|
||
avngen "github.com/aiven/go-client-codegen" | ||
"github.com/aiven/go-client-codegen/handler/service" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
|
||
"github.com/aiven/terraform-provider-aiven/internal/common" | ||
"github.com/aiven/terraform-provider-aiven/internal/schemautil" | ||
"github.com/aiven/terraform-provider-aiven/internal/schemautil/userconfig" | ||
) | ||
|
||
const defaultLC = "en_US.UTF-8" | ||
|
||
var aivenAlloyDBOmniDatabaseSchema = map[string]*schema.Schema{ | ||
"project": schemautil.CommonSchemaProjectReference, | ||
"service_name": schemautil.CommonSchemaServiceNameReference, | ||
"database_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
Description: userconfig.Desc("The name of the service database.").ForceNew().Build(), | ||
}, | ||
"lc_ctype": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
Default: defaultLC, | ||
Description: userconfig.Desc("Default character classification (`LC_CTYPE`) of the database.").DefaultValue(defaultLC).ForceNew().Build(), | ||
}, | ||
"lc_collate": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
Default: defaultLC, | ||
Description: userconfig.Desc("Default string sort order (`LC_COLLATE`) of the database.").DefaultValue(defaultLC).ForceNew().Build(), | ||
}, | ||
} | ||
|
||
func ResourceAlloyDBOmniDatabase() *schema.Resource { | ||
return &schema.Resource{ | ||
Description: "Creates and manages a database in an Aiven for AlloyDB Omni service.", | ||
CreateContext: common.WithGenClient(resourceAlloyDBOmniDatabaseCreate), | ||
ReadContext: common.WithGenClient(resourceAlloyDBOmniDatabaseRead), | ||
DeleteContext: common.WithGenClient(resourceAlloyDBOmniDatabaseDelete), | ||
Importer: &schema.ResourceImporter{ | ||
StateContext: schema.ImportStatePassthroughContext, | ||
}, | ||
Timeouts: schemautil.DefaultResourceTimeouts(), | ||
Schema: aivenAlloyDBOmniDatabaseSchema, | ||
} | ||
} | ||
|
||
func resourceAlloyDBOmniDatabaseCreate(ctx context.Context, d *schema.ResourceData, client avngen.Client) error { | ||
projectName := d.Get("project").(string) | ||
serviceName := d.Get("service_name").(string) | ||
|
||
req := new(service.ServiceDatabaseCreateIn) | ||
err := schemautil.ResourceDataGet(d, req, schemautil.RenameAlias("database_name", "database")) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = client.ServiceDatabaseCreate(ctx, projectName, serviceName, req) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d.SetId(schemautil.BuildResourceID(projectName, serviceName, req.Database)) | ||
return resourceAlloyDBOmniDatabaseRead(ctx, d, client) | ||
} | ||
|
||
func resourceAlloyDBOmniDatabaseRead(ctx context.Context, d *schema.ResourceData, client avngen.Client) error { | ||
projectName, serviceName, dbName, err := schemautil.SplitResourceID3(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
db, err := getDatabase(ctx, client, projectName, serviceName, dbName) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := d.Set("project", projectName); err != nil { | ||
return err | ||
} | ||
|
||
if err := d.Set("service_name", serviceName); err != nil { | ||
return err | ||
} | ||
|
||
return schemautil.ResourceDataSet(aivenAlloyDBOmniDatabaseSchema, d, db) | ||
} | ||
|
||
func resourceAlloyDBOmniDatabaseDelete(ctx context.Context, d *schema.ResourceData, client avngen.Client) error { | ||
projectName, serviceName, dbName, err := schemautil.SplitResourceID3(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = client.ServiceDatabaseDelete(ctx, projectName, serviceName, dbName) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Waits until database is deleted | ||
return schemautil.WaitUntilNotFound(ctx, func() error { | ||
_, err = getDatabase(ctx, client, projectName, serviceName, dbName) | ||
return err | ||
}) | ||
} | ||
|
||
func getDatabase(ctx context.Context, client avngen.Client, projectName, serviceName, dbName string) (*service.DatabaseOut, error) { | ||
list, err := client.ServiceDatabaseList(ctx, projectName, serviceName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, db := range list { | ||
if db.DatabaseName == dbName { | ||
return &db, nil | ||
} | ||
} | ||
|
||
return nil, schemautil.NewNotFound("service database %q not found", dbName) | ||
} |
29 changes: 29 additions & 0 deletions
29
internal/sdkprovider/service/alloydbomni/alloydbomni_database_data_source.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package alloydbomni | ||
|
||
import ( | ||
"context" | ||
|
||
avngen "github.com/aiven/go-client-codegen" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
|
||
"github.com/aiven/terraform-provider-aiven/internal/common" | ||
"github.com/aiven/terraform-provider-aiven/internal/schemautil" | ||
) | ||
|
||
func DatasourceAlloyDBOmniDatabase() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: common.WithGenClient(datasourceDatabaseRead), | ||
Description: "Gets information about a database in an Aiven for AlloyDB Omni service.", | ||
Schema: schemautil.ResourceSchemaAsDatasourceSchema(aivenAlloyDBOmniDatabaseSchema, | ||
"project", "service_name", "database_name"), | ||
} | ||
} | ||
|
||
func datasourceDatabaseRead(ctx context.Context, d *schema.ResourceData, client avngen.Client) error { | ||
projectName := d.Get("project").(string) | ||
serviceName := d.Get("service_name").(string) | ||
databaseName := d.Get("database_name").(string) | ||
|
||
d.SetId(schemautil.BuildResourceID(projectName, serviceName, databaseName)) | ||
return resourceAlloyDBOmniDatabaseRead(ctx, d, client) | ||
} |
Oops, something went wrong.