-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Schema resource & data sources (#83)
* Schema resource * Resource import test fix * Schema data source * Schemas list data source * Resource import test fix * Resource import test fix * Docs and validation fixes
- Loading branch information
1 parent
a88219e
commit 1495c24
Showing
26 changed files
with
1,115 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "mssql_schema Data Source - terraform-provider-mssql" | ||
subcategory: "" | ||
description: |- | ||
Retrieves information about DB schema. | ||
--- | ||
|
||
# mssql_schema (Data Source) | ||
|
||
Retrieves information about DB schema. | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
data "mssql_database" "example" { | ||
name = "example" | ||
} | ||
data "mssql_schema" "by_name" { | ||
database_id = data.mssql_database.example.id | ||
name = "dbo" | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Optional | ||
|
||
- `database_id` (String) ID of database. Can be retrieved using `mssql_database` or `SELECT DB_ID('<db_name>')`. | ||
- `id` (String) `<database_id>/<schema_id>`. Schema ID can be retrieved using `SELECT SCHEMA_ID('<schema_name>')`. Either `id` or `name` must be provided. | ||
- `name` (String) Schema name. Either `id` or `name` must be provided. | ||
|
||
### Read-Only | ||
|
||
- `owner_id` (String) ID of database role or user owning this schema. Can be retrieved using `mssql_database_role`, `mssql_sql_user`, `mssql_azuread_user` or `mssql_azuread_service_principal` | ||
|
||
|
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,51 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "mssql_schemas Data Source - terraform-provider-mssql" | ||
subcategory: "" | ||
description: |- | ||
Obtains information about all schemas found in SQL database. | ||
--- | ||
|
||
# mssql_schemas (Data Source) | ||
|
||
Obtains information about all schemas found in SQL database. | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
data "mssql_database" "example" { | ||
name = "example" | ||
} | ||
data "mssql_schemas" "all" { | ||
database_id = data.mssql_database.example.id | ||
} | ||
output "all_schema_names" { | ||
value = data.mssql_schemas.all.schemas[*].name | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Optional | ||
|
||
- `database_id` (String) ID of database. Can be retrieved using `mssql_database` or `SELECT DB_ID('<db_name>')`. Defaults to ID of `master`. | ||
|
||
### Read-Only | ||
|
||
- `id` (String) ID of the data source, equals to database ID | ||
- `schemas` (Attributes Set) Set of schemas found in the DB. (see [below for nested schema](#nestedatt--schemas)) | ||
|
||
<a id="nestedatt--schemas"></a> | ||
### Nested Schema for `schemas` | ||
|
||
Read-Only: | ||
|
||
- `database_id` (String) ID of database. Can be retrieved using `mssql_database` or `SELECT DB_ID('<db_name>')`. | ||
- `id` (String) `<database_id>/<schema_id>`. Schema ID can be retrieved using `SELECT SCHEMA_ID('<schema_name>')`. | ||
- `name` (String) Schema name. | ||
- `owner_id` (String) ID of database role or user owning this schema. Can be retrieved using `mssql_database_role`, `mssql_sql_user`, `mssql_azuread_user` or `mssql_azuread_service_principal` | ||
|
||
|
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,54 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "mssql_schema Resource - terraform-provider-mssql" | ||
subcategory: "" | ||
description: |- | ||
Manages single DB schema. | ||
--- | ||
|
||
# mssql_schema (Resource) | ||
|
||
Manages single DB schema. | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
data "mssql_database" "example" { | ||
name = "example" | ||
} | ||
data "mssql_sql_user" "owner" { | ||
name = "example_user" | ||
} | ||
resource "mssql_schema" "example" { | ||
name = "example" | ||
database_id = data.mssql_database.example.id | ||
owner_id = data.mssql_sql_user.owner.id | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `name` (String) Schema name. | ||
|
||
### Optional | ||
|
||
- `database_id` (String) ID of database. Can be retrieved using `mssql_database` or `SELECT DB_ID('<db_name>')`. Defaults to ID of `master`. | ||
- `owner_id` (String) ID of database role or user owning this schema. Can be retrieved using `mssql_database_role`, `mssql_sql_user`, `mssql_azuread_user` or `mssql_azuread_service_principal` | ||
|
||
### Read-Only | ||
|
||
- `id` (String) `<database_id>/<schema_id>`. Schema ID can be retrieved using `SELECT SCHEMA_ID('<schema_name>')`. | ||
|
||
## Import | ||
|
||
Import is supported using the following syntax: | ||
|
||
```shell | ||
# import using <db_id>/<schema_id> - can be retrieved using `SELECT CONCAT(DB_ID(), '/', SCHEMA_ID('<schema_name>'))` | ||
terraform import mssql_schema.example '7/5' | ||
``` |
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,8 @@ | ||
data "mssql_database" "example" { | ||
name = "example" | ||
} | ||
|
||
data "mssql_schema" "by_name" { | ||
database_id = data.mssql_database.example.id | ||
name = "dbo" | ||
} |
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,11 @@ | ||
data "mssql_database" "example" { | ||
name = "example" | ||
} | ||
|
||
data "mssql_schemas" "all" { | ||
database_id = data.mssql_database.example.id | ||
} | ||
|
||
output "all_schema_names" { | ||
value = data.mssql_schemas.all.schemas[*].name | ||
} |
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,2 @@ | ||
# import using <db_id>/<schema_id> - can be retrieved using `SELECT CONCAT(DB_ID(), '/', SCHEMA_ID('<schema_name>'))` | ||
terraform import mssql_schema.example '7/5' |
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,13 @@ | ||
data "mssql_database" "example" { | ||
name = "example" | ||
} | ||
|
||
data "mssql_sql_user" "owner" { | ||
name = "example_user" | ||
} | ||
|
||
resource "mssql_schema" "example" { | ||
name = "example" | ||
database_id = data.mssql_database.example.id | ||
owner_id = data.mssql_sql_user.owner.id | ||
} |
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
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,46 @@ | ||
package schema | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/PGSSoft/terraform-provider-mssql/internal/services/common" | ||
"github.com/PGSSoft/terraform-provider-mssql/internal/sql" | ||
"github.com/PGSSoft/terraform-provider-mssql/internal/validators" | ||
"github.com/hashicorp/terraform-plugin-framework/tfsdk" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
var attributes = map[string]tfsdk.Attribute{ | ||
"id": { | ||
MarkdownDescription: "`<database_id>/<schema_id>`. Schema ID can be retrieved using `SELECT SCHEMA_ID('<schema_name>')`.", | ||
Type: types.StringType, | ||
}, | ||
"database_id": common.DatabaseIdAttribute, | ||
"name": { | ||
MarkdownDescription: "Schema name.", | ||
Type: types.StringType, | ||
Validators: validators.SchemaNameValidators, | ||
}, | ||
"owner_id": { | ||
MarkdownDescription: "ID of database role or user owning this schema. Can be retrieved using `mssql_database_role`, `mssql_sql_user`, `mssql_azuread_user` or `mssql_azuread_service_principal`", | ||
Type: types.StringType, | ||
}, | ||
} | ||
|
||
type resourceData struct { | ||
Id types.String `tfsdk:"id"` | ||
DatabaseId types.String `tfsdk:"database_id"` | ||
Name types.String `tfsdk:"name"` | ||
OwnerId types.String `tfsdk:"owner_id"` | ||
} | ||
|
||
func (d resourceData) withSchemaData(ctx context.Context, schema sql.Schema) resourceData { | ||
dbId := schema.GetDb(ctx).GetId(ctx) | ||
|
||
return resourceData{ | ||
Id: types.String{Value: common.DbObjectId[sql.SchemaId]{DbId: dbId, ObjectId: schema.GetId(ctx)}.String()}, | ||
Name: types.String{Value: schema.GetName(ctx)}, | ||
DatabaseId: types.String{Value: fmt.Sprint(dbId)}, | ||
OwnerId: types.String{Value: common.DbObjectId[sql.GenericDatabasePrincipalId]{DbId: dbId, ObjectId: schema.GetOwnerId(ctx)}.String()}, | ||
} | ||
} |
Oops, something went wrong.