From b12b56bc9c469fedbfb34ce658e36c502e47913b Mon Sep 17 00:00:00 2001 From: Stefan Benz Date: Tue, 9 Jan 2024 18:19:35 +0100 Subject: [PATCH 1/8] fix: add projects datasource for filter with name --- docs/data-sources/orgs.md | 28 +++++++++-- docs/data-sources/projects.md | 52 +++++++++++++++++++ docs/index.md | 3 +- examples/provider/data-sources/projects.tf | 15 ++++++ templates/data-sources/orgs.md.tmpl | 16 ++++++ templates/data-sources/projects.md.tmpl | 16 ++++++ zitadel/project/const.go | 2 + zitadel/project/datasource.go | 58 ++++++++++++++++++++++ zitadel/project/datasource_test.go | 51 +++++++++++++++++++ zitadel/project/funcs.go | 39 +++++++++++++++ zitadel/provider.go | 1 + 11 files changed, 275 insertions(+), 6 deletions(-) create mode 100644 docs/data-sources/projects.md create mode 100644 examples/provider/data-sources/projects.tf create mode 100644 templates/data-sources/orgs.md.tmpl create mode 100644 templates/data-sources/projects.md.tmpl create mode 100644 zitadel/project/datasource_test.go diff --git a/docs/data-sources/orgs.md b/docs/data-sources/orgs.md index 22bc0cac..edcebb9f 100644 --- a/docs/data-sources/orgs.md +++ b/docs/data-sources/orgs.md @@ -1,5 +1,4 @@ --- -# generated by https://github.com/hashicorp/terraform-plugin-docs page_title: "zitadel_orgs Data Source - terraform-provider-zitadel" subcategory: "" description: |- @@ -10,7 +9,28 @@ description: |- Datasource representing an organization in ZITADEL, which is the highest level after the instance and contains several other resource including policies if the configuration differs to the default policies on the instance. - +## Example Usage + +```terraform +data "zitadel_orgs" "default" { + name = "example-name" + name_method = "TEXT_QUERY_METHOD_CONTAINS_IGNORE_CASE" + domain = "example.com" + domain_method = "TEXT_QUERY_METHOD_CONTAINS_IGNORE_CASE" + state = "ORG_STATE_ACTIVE" +} + +data "zitadel_org" "default" { + for_each = toset(data.zitadel_orgs.default.ids) + id = each.value +} + +output "org_names" { + value = toset([ + for org in data.zitadel_org.default : org.name + ]) +} +``` ## Schema @@ -27,6 +47,4 @@ Datasource representing an organization in ZITADEL, which is the highest level a - `id` (String) The ID of this resource. - `ids` (List of String) A set of all organization IDs. -- `primary_domain` (String) Primary domain of the org - - +- `primary_domain` (String) Primary domain of the org \ No newline at end of file diff --git a/docs/data-sources/projects.md b/docs/data-sources/projects.md new file mode 100644 index 00000000..45ec4f67 --- /dev/null +++ b/docs/data-sources/projects.md @@ -0,0 +1,52 @@ +--- +page_title: "zitadel_projects Data Source - terraform-provider-zitadel" +subcategory: "" +description: |- + Datasource representing the project, which can then be granted to different organizations or users directly, containing different applications. +--- + +# zitadel_projects (Data Source) + +Datasource representing the project, which can then be granted to different organizations or users directly, containing different applications. + +## Example Usage + +```terraform +data "zitadel_projects" "default" { + name = "example-name" + name_method = "TEXT_QUERY_METHOD_CONTAINS_IGNORE_CASE" +} + +data "zitadel_project" "default" { + for_each = toset(data.zitadel_projects.default.project_ids) + id = each.value +} + +output "project_names" { + value = toset([ + for project in data.zitadel_project.default : project.name + ]) +} +``` + + +## Schema + +### Required + +- `name` (String) Name of the project + +### Optional + +- `name_method` (String) Method for querying projects by name, supported values: TEXT_QUERY_METHOD_EQUALS, TEXT_QUERY_METHOD_EQUALS_IGNORE_CASE, TEXT_QUERY_METHOD_STARTS_WITH, TEXT_QUERY_METHOD_STARTS_WITH_IGNORE_CASE, TEXT_QUERY_METHOD_CONTAINS, TEXT_QUERY_METHOD_CONTAINS_IGNORE_CASE, TEXT_QUERY_METHOD_ENDS_WITH, TEXT_QUERY_METHOD_ENDS_WITH_IGNORE_CASE +- `org_id` (String) ID of the organization + +### Read-Only + +- `has_project_check` (Boolean) ZITADEL checks if the org of the user has permission to this project +- `id` (String) The ID of this resource. +- `private_labeling_setting` (String) Defines from where the private labeling should be triggered +- `project_ids` (List of String) A set of all project IDs. +- `project_role_assertion` (Boolean) describes if roles of user should be added in token +- `project_role_check` (Boolean) ZITADEL checks if the user has at least one on this project +- `state` (String) State of the project \ No newline at end of file diff --git a/docs/index.md b/docs/index.md index 6a5f8a75..5a360a7e 100644 --- a/docs/index.md +++ b/docs/index.md @@ -31,7 +31,7 @@ terraform { required_providers { zitadel = { source = "zitadel/zitadel" - version = "1.0.3" + version = "1.0.0-alpha.16" } } } @@ -40,6 +40,7 @@ provider "zitadel" { domain = "localhost" insecure = "true" port = "8080" + project = "170832731415117995" jwt_profile_file = "local-token" } ``` diff --git a/examples/provider/data-sources/projects.tf b/examples/provider/data-sources/projects.tf new file mode 100644 index 00000000..98ccdd69 --- /dev/null +++ b/examples/provider/data-sources/projects.tf @@ -0,0 +1,15 @@ +data "zitadel_projects" "default" { + name = "example-name" + name_method = "TEXT_QUERY_METHOD_CONTAINS_IGNORE_CASE" +} + +data "zitadel_project" "default" { + for_each = toset(data.zitadel_projects.default.project_ids) + id = each.value +} + +output "project_names" { + value = toset([ + for project in data.zitadel_project.default : project.name + ]) +} diff --git a/templates/data-sources/orgs.md.tmpl b/templates/data-sources/orgs.md.tmpl new file mode 100644 index 00000000..1cb37d5c --- /dev/null +++ b/templates/data-sources/orgs.md.tmpl @@ -0,0 +1,16 @@ +--- +page_title: "{{.Name}} {{.Type}} - {{.ProviderName}}" +subcategory: "" +description: |- +{{ .Description | plainmarkdown | trimspace | prefixlines " " }} +--- + +# {{.Name}} ({{.Type}}) + +{{ .Description | trimspace }} + +## Example Usage + +{{ tffile "examples/provider/data-sources/orgs.tf" }} + +{{ .SchemaMarkdown | trimspace }} \ No newline at end of file diff --git a/templates/data-sources/projects.md.tmpl b/templates/data-sources/projects.md.tmpl new file mode 100644 index 00000000..f11bdd10 --- /dev/null +++ b/templates/data-sources/projects.md.tmpl @@ -0,0 +1,16 @@ +--- +page_title: "{{.Name}} {{.Type}} - {{.ProviderName}}" +subcategory: "" +description: |- +{{ .Description | plainmarkdown | trimspace | prefixlines " " }} +--- + +# {{.Name}} ({{.Type}}) + +{{ .Description | trimspace }} + +## Example Usage + +{{ tffile "examples/provider/data-sources/projects.tf" }} + +{{ .SchemaMarkdown | trimspace }} \ No newline at end of file diff --git a/zitadel/project/const.go b/zitadel/project/const.go index 403e0ea9..7c758cfe 100644 --- a/zitadel/project/const.go +++ b/zitadel/project/const.go @@ -4,7 +4,9 @@ import "github.com/zitadel/zitadel-go/v2/pkg/client/zitadel/project" const ( ProjectIDVar = "project_id" + projectIDsVar = "project_ids" NameVar = "name" + nameMethodVar = "name_method" stateVar = "state" roleAssertionVar = "project_role_assertion" roleCheckVar = "project_role_check" diff --git a/zitadel/project/datasource.go b/zitadel/project/datasource.go index 1ffa655b..830fb88a 100644 --- a/zitadel/project/datasource.go +++ b/zitadel/project/datasource.go @@ -1,7 +1,10 @@ package project import ( + "github.com/hashicorp/go-cty/cty" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/zitadel/zitadel-go/v2/pkg/client/zitadel/object" "github.com/zitadel/terraform-provider-zitadel/zitadel/helper" ) @@ -50,3 +53,58 @@ func GetDatasource() *schema.Resource { ReadContext: read, } } + +func ListDatasources() *schema.Resource { + return &schema.Resource{ + Description: "Datasource representing the project, which can then be granted to different organizations or users directly, containing different applications.", + Schema: map[string]*schema.Schema{ + projectIDsVar: { + Type: schema.TypeList, + Computed: true, + Description: "A set of all project IDs.", + Elem: &schema.Schema{Type: schema.TypeString}, + }, + NameVar: { + Type: schema.TypeString, + Required: true, + Description: "Name of the project", + }, + nameMethodVar: { + Type: schema.TypeString, + Optional: true, + Description: "Method for querying projects by name" + helper.DescriptionEnumValuesList(object.TextQueryMethod_name), + ValidateDiagFunc: func(value interface{}, path cty.Path) diag.Diagnostics { + return helper.EnumValueValidation(nameMethodVar, value, object.TextQueryMethod_value) + }, + Default: object.TextQueryMethod_TEXT_QUERY_METHOD_EQUALS_IGNORE_CASE.String(), + }, + helper.OrgIDVar: helper.OrgIDResourceField, + stateVar: { + Type: schema.TypeString, + Computed: true, + Description: "State of the project", + }, + roleAssertionVar: { + Type: schema.TypeBool, + Computed: true, + Description: "describes if roles of user should be added in token", + }, + roleCheckVar: { + Type: schema.TypeBool, + Computed: true, + Description: "ZITADEL checks if the user has at least one on this project", + }, + hasProjectCheckVar: { + Type: schema.TypeBool, + Computed: true, + Description: "ZITADEL checks if the org of the user has permission to this project", + }, + privateLabelingSettingVar: { + Type: schema.TypeString, + Computed: true, + Description: "Defines from where the private labeling should be triggered", + }, + }, + ReadContext: list, + } +} diff --git a/zitadel/project/datasource_test.go b/zitadel/project/datasource_test.go new file mode 100644 index 00000000..91fd12be --- /dev/null +++ b/zitadel/project/datasource_test.go @@ -0,0 +1,51 @@ +package project_test + +import ( + "fmt" + "strings" + "testing" + + "github.com/zitadel/terraform-provider-zitadel/zitadel/helper/test_utils" + "github.com/zitadel/terraform-provider-zitadel/zitadel/project" + "github.com/zitadel/terraform-provider-zitadel/zitadel/project/project_test_dep" +) + +func TestAccProjectDatasource_ID(t *testing.T) { + frame := test_utils.NewOrgTestFrame(t, "zitadel_project") + projectName := "project_datasource_" + frame.UniqueResourcesID + projectDep, projectID := project_test_dep.Create(t, frame) + test_utils.RunDatasourceTest( + t, + frame.BaseTestFrame, + projectDep, + nil, + map[string]string{ + "org_id": frame.OrgID, + "project_id": projectID, + "name": projectName, + }, + ) +} + +func TestAccProjectsDatasources_ID_Name_Match(t *testing.T) { + datasourceName := "zitadel_projects" + frame := test_utils.NewOrgTestFrame(t, datasourceName) + config, attributes := test_utils.ReadExample(t, test_utils.Datasources, datasourceName) + exampleName := test_utils.AttributeValue(t, project.NameVar, attributes).AsString() + projectName := fmt.Sprintf("%s-%s", exampleName, frame.UniqueResourcesID) + // for-each is not supported in acceptance tests, so we cut the example down to the first block + // https://github.com/hashicorp/terraform-plugin-sdk/issues/536 + config = strings.Join(strings.Split(config, "\n")[0:7], "\n") + config = strings.Replace(config, exampleName, projectName, 1) + _, projectID := project_test_dep.Create(t, frame) + test_utils.RunDatasourceTest( + t, + frame.BaseTestFrame, + config, + checkRemoteProperty(frame)(projectName), + map[string]string{ + "project_ids.0": projectID, + "project_ids.#": "1", + }, + ) +} diff --git a/zitadel/project/funcs.go b/zitadel/project/funcs.go index 55d223b1..d92e29e1 100644 --- a/zitadel/project/funcs.go +++ b/zitadel/project/funcs.go @@ -7,6 +7,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/zitadel/zitadel-go/v2/pkg/client/zitadel/management" + "github.com/zitadel/zitadel-go/v2/pkg/client/zitadel/object" "github.com/zitadel/zitadel-go/v2/pkg/client/zitadel/project" "github.com/zitadel/terraform-provider-zitadel/zitadel/helper" @@ -131,3 +132,41 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn return nil } + +func list(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + tflog.Info(ctx, "started list") + name := d.Get(NameVar).(string) + nameMethod := d.Get(nameMethodVar).(string) + clientinfo, ok := m.(*helper.ClientInfo) + if !ok { + return diag.Errorf("failed to get client") + } + client, err := helper.GetManagementClient(clientinfo) + if err != nil { + return diag.FromErr(err) + } + req := &management.ListProjectsRequest{} + if name != "" { + req.Queries = append(req.Queries, + &project.ProjectQuery{ + Query: &project.ProjectQuery_NameQuery{ + NameQuery: &project.ProjectNameQuery{ + Name: name, + Method: object.TextQueryMethod(object.TextQueryMethod_value[nameMethod]), + }, + }, + }) + } + + resp, err := client.ListProjects(helper.CtxWithOrgID(ctx, d), req) + if err != nil { + return diag.Errorf("error while getting project by name %s: %v", name, err) + } + ids := make([]string, len(resp.Result)) + for i, res := range resp.Result { + ids[i] = res.Id + } + // If the ID is blank, the datasource is deleted and not usable. + d.SetId("-") + return diag.FromErr(d.Set(projectIDsVar, ids)) +} diff --git a/zitadel/provider.go b/zitadel/provider.go index 27404ef5..1a2596cd 100644 --- a/zitadel/provider.go +++ b/zitadel/provider.go @@ -209,6 +209,7 @@ func Provider() *schema.Provider { "zitadel_human_user": human_user.GetDatasource(), "zitadel_machine_user": machine_user.GetDatasource(), "zitadel_project": project.GetDatasource(), + "zitadel_projects": project.ListDatasources(), "zitadel_project_role": project_role.GetDatasource(), "zitadel_action": action.GetDatasource(), "zitadel_application_oidc": application_oidc.GetDatasource(), From 2ea73708fd1db3f251a18d832734a19f22929677 Mon Sep 17 00:00:00 2001 From: Stefan Benz Date: Wed, 10 Jan 2024 19:32:36 +0100 Subject: [PATCH 2/8] fix: add app oidc, app api and machine user datasource for filter with name --- docs/data-sources/application_api.md | 8 +- docs/data-sources/application_apis.md | 50 ++++++++ docs/data-sources/application_oidc.md | 8 +- docs/data-sources/application_oidcs.md | 50 ++++++++ docs/data-sources/machine_user.md | 6 +- docs/data-sources/machine_users.md | 48 ++++++++ docs/data-sources/project.md | 6 +- docs/data-sources/projects.md | 12 +- .../provider/data-sources/application_api.tf | 8 +- .../provider/data-sources/application_apis.tf | 17 +++ .../provider/data-sources/application_oidc.tf | 8 +- .../data-sources/application_oidcs.tf | 17 +++ .../provider/data-sources/machine_user.tf | 6 +- .../provider/data-sources/machine_users.tf | 16 +++ examples/provider/data-sources/project.tf | 6 +- examples/provider/data-sources/projects.tf | 5 +- .../data-sources/application_apis.md.tmpl | 16 +++ .../data-sources/application_oidcs.md.tmpl | 16 +++ templates/data-sources/machine_users.md.tmpl | 16 +++ .../application_api_test_dep/dependency.go | 6 +- zitadel/application_api/const.go | 2 + zitadel/application_api/datasource.go | 38 +++++++ zitadel/application_api/datasource_test.go | 107 ++++++++++++++++++ zitadel/application_api/funcs.go | 40 +++++++ zitadel/application_api/resource_test.go | 2 +- zitadel/application_key/resource_test.go | 4 +- .../application_oidc_test_dep/dependency.go | 20 ++++ zitadel/application_oidc/const.go | 4 +- zitadel/application_oidc/datasource.go | 40 ++++++- zitadel/application_oidc/datasource_test.go | 107 ++++++++++++++++++ zitadel/application_oidc/funcs.go | 42 ++++++- zitadel/application_oidc/resource.go | 2 +- zitadel/application_oidc/resource_test.go | 2 +- zitadel/helper/test_utils/base_frame.go | 6 +- zitadel/helper/test_utils/dependency.go | 27 +++++ zitadel/machine_key/resource_test.go | 2 +- zitadel/machine_user/const.go | 2 + zitadel/machine_user/datasource.go | 33 ++++++ zitadel/machine_user/datasource_test.go | 98 ++++++++++++++++ zitadel/machine_user/funcs.go | 38 +++++++ .../machine_user_test_dep/dependency.go | 6 +- zitadel/pat/resource_test.go | 2 +- zitadel/project/datasource.go | 27 +---- zitadel/project/datasource_test.go | 55 ++++++++- .../project/project_test_dep/dependency.go | 14 ++- zitadel/project_grant/resource_test.go | 2 +- zitadel/project_grant_member/resource_test.go | 2 +- zitadel/project_member/resource_test.go | 2 +- zitadel/project_role/resource_test.go | 2 +- zitadel/provider.go | 3 + zitadel/user_grant/resource_test.go | 2 +- 51 files changed, 947 insertions(+), 111 deletions(-) create mode 100644 docs/data-sources/application_apis.md create mode 100644 docs/data-sources/application_oidcs.md create mode 100644 docs/data-sources/machine_users.md create mode 100644 examples/provider/data-sources/application_apis.tf create mode 100644 examples/provider/data-sources/application_oidcs.tf create mode 100644 examples/provider/data-sources/machine_users.tf create mode 100644 templates/data-sources/application_apis.md.tmpl create mode 100644 templates/data-sources/application_oidcs.md.tmpl create mode 100644 templates/data-sources/machine_users.md.tmpl create mode 100644 zitadel/application_api/datasource_test.go create mode 100644 zitadel/application_oidc/application_oidc_test_dep/dependency.go create mode 100644 zitadel/application_oidc/datasource_test.go create mode 100644 zitadel/machine_user/datasource_test.go diff --git a/docs/data-sources/application_api.md b/docs/data-sources/application_api.md index 8b0a6cb3..acdebf38 100644 --- a/docs/data-sources/application_api.md +++ b/docs/data-sources/application_api.md @@ -13,14 +13,10 @@ Datasource representing an API application belonging to a project, with all conf ```terraform data "zitadel_application_api" "default" { - org_id = data.zitadel_org.default.id - project_id = data.zitadel_project.default.id + org_id = "345678901234567890" + project_id = "234567890123456789" app_id = "123456789012345678" } - -output "application_api" { - value = data.zitadel_application_api.default -} ``` diff --git a/docs/data-sources/application_apis.md b/docs/data-sources/application_apis.md new file mode 100644 index 00000000..7b4a9793 --- /dev/null +++ b/docs/data-sources/application_apis.md @@ -0,0 +1,50 @@ +--- +page_title: "zitadel_application_apis Data Source - terraform-provider-zitadel" +subcategory: "" +description: |- + Datasource representing an API application belonging to a project, with all configuration possibilities. +--- + +# zitadel_application_apis (Data Source) + +Datasource representing an API application belonging to a project, with all configuration possibilities. + +## Example Usage + +```terraform +data "zitadel_application_apis" "default" { + org_id = "123456789012345678" + project_id = "234567890123456789" + name = "example-name" + name_method = "TEXT_QUERY_METHOD_CONTAINS_IGNORE_CASE" +} + +data "zitadel_application_api" "default" { + for_each = toset(data.zitadel_application_apis.default.app_ids) + id = each.value +} + +output "app_api_names" { + value = toset([ + for app in data.zitadel_application_api.default : app.name + ]) +} +``` + + +## Schema + +### Required + +- `name` (String) Name of the application +- `project_id` (String) ID of the project + +### Optional + +- `name_method` (String) Method for querying applications by name, supported values: TEXT_QUERY_METHOD_EQUALS, TEXT_QUERY_METHOD_EQUALS_IGNORE_CASE, TEXT_QUERY_METHOD_STARTS_WITH, TEXT_QUERY_METHOD_STARTS_WITH_IGNORE_CASE, TEXT_QUERY_METHOD_CONTAINS, TEXT_QUERY_METHOD_CONTAINS_IGNORE_CASE, TEXT_QUERY_METHOD_ENDS_WITH, TEXT_QUERY_METHOD_ENDS_WITH_IGNORE_CASE +- `org_id` (String) ID of the organization + +### Read-Only + +- `app_ids` (List of String) A set of all IDs. +- `id` (String) The ID of this resource. \ No newline at end of file diff --git a/docs/data-sources/application_oidc.md b/docs/data-sources/application_oidc.md index 0053d9a1..bc63d9f0 100644 --- a/docs/data-sources/application_oidc.md +++ b/docs/data-sources/application_oidc.md @@ -13,14 +13,10 @@ Datasource representing an OIDC application belonging to a project, with all con ```terraform data "zitadel_application_oidc" "default" { - org_id = data.zitadel_org.default.id - project_id = data.zitadel_project.default.id + org_id = "345678901234567890" + project_id = "234567890123456789" app_id = "123456789012345678" } - -output "application_oidc" { - value = data.zitadel_application_oidc.default -} ``` diff --git a/docs/data-sources/application_oidcs.md b/docs/data-sources/application_oidcs.md new file mode 100644 index 00000000..fe1f8ba7 --- /dev/null +++ b/docs/data-sources/application_oidcs.md @@ -0,0 +1,50 @@ +--- +page_title: "zitadel_application_oidcs Data Source - terraform-provider-zitadel" +subcategory: "" +description: |- + Datasource representing an OIDC application belonging to a project, with all configuration possibilities. +--- + +# zitadel_application_oidcs (Data Source) + +Datasource representing an OIDC application belonging to a project, with all configuration possibilities. + +## Example Usage + +```terraform +data "zitadel_application_oidcs" "default" { + org_id = "123456789012345678" + project_id = "234567890123456789" + name = "example-name" + name_method = "TEXT_QUERY_METHOD_CONTAINS_IGNORE_CASE" +} + +data "zitadel_application_oidc" "default" { + for_each = toset(data.zitadel_application_oidcs.default.app_ids) + id = each.value +} + +output "app_oidc_names" { + value = toset([ + for app in data.zitadel_application_oidc.default : app.name + ]) +} +``` + + +## Schema + +### Required + +- `name` (String) Name of the application +- `project_id` (String) ID of the project + +### Optional + +- `name_method` (String) Method for querying applications by name, supported values: TEXT_QUERY_METHOD_EQUALS, TEXT_QUERY_METHOD_EQUALS_IGNORE_CASE, TEXT_QUERY_METHOD_STARTS_WITH, TEXT_QUERY_METHOD_STARTS_WITH_IGNORE_CASE, TEXT_QUERY_METHOD_CONTAINS, TEXT_QUERY_METHOD_CONTAINS_IGNORE_CASE, TEXT_QUERY_METHOD_ENDS_WITH, TEXT_QUERY_METHOD_ENDS_WITH_IGNORE_CASE +- `org_id` (String) ID of the organization + +### Read-Only + +- `app_ids` (List of String) A set of all IDs. +- `id` (String) The ID of this resource. \ No newline at end of file diff --git a/docs/data-sources/machine_user.md b/docs/data-sources/machine_user.md index 22439163..2528f28e 100644 --- a/docs/data-sources/machine_user.md +++ b/docs/data-sources/machine_user.md @@ -13,13 +13,9 @@ Datasource representing a serviceaccount situated under an organization, which t ```terraform data "zitadel_machine_user" "default" { - org_id = data.zitadel_org.default.id + org_id = "234567890123456789" user_id = "123456789012345678" } - -output "machine_user" { - value = data.zitadel_machine_user.default -} ``` diff --git a/docs/data-sources/machine_users.md b/docs/data-sources/machine_users.md new file mode 100644 index 00000000..fa4d8b66 --- /dev/null +++ b/docs/data-sources/machine_users.md @@ -0,0 +1,48 @@ +--- +page_title: "zitadel_machine_users Data Source - terraform-provider-zitadel" +subcategory: "" +description: |- + Datasource representing a serviceaccount situated under an organization, which then can be authorized through memberships or direct grants on other resources. +--- + +# zitadel_machine_users (Data Source) + +Datasource representing a serviceaccount situated under an organization, which then can be authorized through memberships or direct grants on other resources. + +## Example Usage + +```terraform +data "zitadel_machine_users" "default" { + org_id = "123456789012345678" + user_name = "example-name" + user_name_method = "TEXT_QUERY_METHOD_CONTAINS_IGNORE_CASE" +} + +data "zitadel_machine_user" "default" { + for_each = toset(data.zitadel_machine_users.default.user_ids) + id = each.value +} + +output "user_names" { + value = toset([ + for user in data.zitadel_machine_user.default : user.name + ]) +} +``` + + +## Schema + +### Required + +- `user_name` (String) Username + +### Optional + +- `org_id` (String) ID of the organization +- `user_name_method` (String) Method for querying machine users by username, supported values: TEXT_QUERY_METHOD_EQUALS, TEXT_QUERY_METHOD_EQUALS_IGNORE_CASE, TEXT_QUERY_METHOD_STARTS_WITH, TEXT_QUERY_METHOD_STARTS_WITH_IGNORE_CASE, TEXT_QUERY_METHOD_CONTAINS, TEXT_QUERY_METHOD_CONTAINS_IGNORE_CASE, TEXT_QUERY_METHOD_ENDS_WITH, TEXT_QUERY_METHOD_ENDS_WITH_IGNORE_CASE + +### Read-Only + +- `id` (String) The ID of this resource. +- `user_ids` (List of String) A set of all IDs. \ No newline at end of file diff --git a/docs/data-sources/project.md b/docs/data-sources/project.md index 83574494..65270dc7 100644 --- a/docs/data-sources/project.md +++ b/docs/data-sources/project.md @@ -13,13 +13,9 @@ Datasource representing the project, which can then be granted to different orga ```terraform data "zitadel_project" "default" { - org_id = data.zitadel_org.default.id + org_id = "234567890123456789" project_id = "123456789012345678" } - -output "project" { - value = data.zitadel_project.default -} ``` diff --git a/docs/data-sources/projects.md b/docs/data-sources/projects.md index 45ec4f67..6c869018 100644 --- a/docs/data-sources/projects.md +++ b/docs/data-sources/projects.md @@ -13,8 +13,9 @@ Datasource representing the project, which can then be granted to different orga ```terraform data "zitadel_projects" "default" { - name = "example-name" - name_method = "TEXT_QUERY_METHOD_CONTAINS_IGNORE_CASE" + org_id = "123456789012345678" + name = "example-name" + name_method = "TEXT_QUERY_METHOD_CONTAINS_IGNORE_CASE" } data "zitadel_project" "default" { @@ -43,10 +44,5 @@ output "project_names" { ### Read-Only -- `has_project_check` (Boolean) ZITADEL checks if the org of the user has permission to this project - `id` (String) The ID of this resource. -- `private_labeling_setting` (String) Defines from where the private labeling should be triggered -- `project_ids` (List of String) A set of all project IDs. -- `project_role_assertion` (Boolean) describes if roles of user should be added in token -- `project_role_check` (Boolean) ZITADEL checks if the user has at least one on this project -- `state` (String) State of the project \ No newline at end of file +- `project_ids` (List of String) A set of all project IDs. \ No newline at end of file diff --git a/examples/provider/data-sources/application_api.tf b/examples/provider/data-sources/application_api.tf index fc5f7d18..46460922 100644 --- a/examples/provider/data-sources/application_api.tf +++ b/examples/provider/data-sources/application_api.tf @@ -1,9 +1,5 @@ data "zitadel_application_api" "default" { - org_id = data.zitadel_org.default.id - project_id = data.zitadel_project.default.id + org_id = "345678901234567890" + project_id = "234567890123456789" app_id = "123456789012345678" } - -output "application_api" { - value = data.zitadel_application_api.default -} diff --git a/examples/provider/data-sources/application_apis.tf b/examples/provider/data-sources/application_apis.tf new file mode 100644 index 00000000..ad78793f --- /dev/null +++ b/examples/provider/data-sources/application_apis.tf @@ -0,0 +1,17 @@ +data "zitadel_application_apis" "default" { + org_id = "123456789012345678" + project_id = "234567890123456789" + name = "example-name" + name_method = "TEXT_QUERY_METHOD_CONTAINS_IGNORE_CASE" +} + +data "zitadel_application_api" "default" { + for_each = toset(data.zitadel_application_apis.default.app_ids) + id = each.value +} + +output "app_api_names" { + value = toset([ + for app in data.zitadel_application_api.default : app.name + ]) +} diff --git a/examples/provider/data-sources/application_oidc.tf b/examples/provider/data-sources/application_oidc.tf index 3b821841..f64ae032 100644 --- a/examples/provider/data-sources/application_oidc.tf +++ b/examples/provider/data-sources/application_oidc.tf @@ -1,9 +1,5 @@ data "zitadel_application_oidc" "default" { - org_id = data.zitadel_org.default.id - project_id = data.zitadel_project.default.id + org_id = "345678901234567890" + project_id = "234567890123456789" app_id = "123456789012345678" } - -output "application_oidc" { - value = data.zitadel_application_oidc.default -} diff --git a/examples/provider/data-sources/application_oidcs.tf b/examples/provider/data-sources/application_oidcs.tf new file mode 100644 index 00000000..e6a2ec19 --- /dev/null +++ b/examples/provider/data-sources/application_oidcs.tf @@ -0,0 +1,17 @@ +data "zitadel_application_oidcs" "default" { + org_id = "123456789012345678" + project_id = "234567890123456789" + name = "example-name" + name_method = "TEXT_QUERY_METHOD_CONTAINS_IGNORE_CASE" +} + +data "zitadel_application_oidc" "default" { + for_each = toset(data.zitadel_application_oidcs.default.app_ids) + id = each.value +} + +output "app_oidc_names" { + value = toset([ + for app in data.zitadel_application_oidc.default : app.name + ]) +} diff --git a/examples/provider/data-sources/machine_user.tf b/examples/provider/data-sources/machine_user.tf index c3b529f6..e1cf9fa4 100644 --- a/examples/provider/data-sources/machine_user.tf +++ b/examples/provider/data-sources/machine_user.tf @@ -1,8 +1,4 @@ data "zitadel_machine_user" "default" { - org_id = data.zitadel_org.default.id + org_id = "234567890123456789" user_id = "123456789012345678" } - -output "machine_user" { - value = data.zitadel_machine_user.default -} diff --git a/examples/provider/data-sources/machine_users.tf b/examples/provider/data-sources/machine_users.tf new file mode 100644 index 00000000..d91c29dd --- /dev/null +++ b/examples/provider/data-sources/machine_users.tf @@ -0,0 +1,16 @@ +data "zitadel_machine_users" "default" { + org_id = "123456789012345678" + user_name = "example-name" + user_name_method = "TEXT_QUERY_METHOD_CONTAINS_IGNORE_CASE" +} + +data "zitadel_machine_user" "default" { + for_each = toset(data.zitadel_machine_users.default.user_ids) + id = each.value +} + +output "user_names" { + value = toset([ + for user in data.zitadel_machine_user.default : user.name + ]) +} diff --git a/examples/provider/data-sources/project.tf b/examples/provider/data-sources/project.tf index 4650cd8a..44598dc0 100644 --- a/examples/provider/data-sources/project.tf +++ b/examples/provider/data-sources/project.tf @@ -1,8 +1,4 @@ data "zitadel_project" "default" { - org_id = data.zitadel_org.default.id + org_id = "234567890123456789" project_id = "123456789012345678" } - -output "project" { - value = data.zitadel_project.default -} diff --git a/examples/provider/data-sources/projects.tf b/examples/provider/data-sources/projects.tf index 98ccdd69..166a5364 100644 --- a/examples/provider/data-sources/projects.tf +++ b/examples/provider/data-sources/projects.tf @@ -1,6 +1,7 @@ data "zitadel_projects" "default" { - name = "example-name" - name_method = "TEXT_QUERY_METHOD_CONTAINS_IGNORE_CASE" + org_id = "123456789012345678" + name = "example-name" + name_method = "TEXT_QUERY_METHOD_CONTAINS_IGNORE_CASE" } data "zitadel_project" "default" { diff --git a/templates/data-sources/application_apis.md.tmpl b/templates/data-sources/application_apis.md.tmpl new file mode 100644 index 00000000..a026ce6c --- /dev/null +++ b/templates/data-sources/application_apis.md.tmpl @@ -0,0 +1,16 @@ +--- +page_title: "{{.Name}} {{.Type}} - {{.ProviderName}}" +subcategory: "" +description: |- +{{ .Description | plainmarkdown | trimspace | prefixlines " " }} +--- + +# {{.Name}} ({{.Type}}) + +{{ .Description | trimspace }} + +## Example Usage + +{{ tffile "examples/provider/data-sources/application_apis.tf" }} + +{{ .SchemaMarkdown | trimspace }} \ No newline at end of file diff --git a/templates/data-sources/application_oidcs.md.tmpl b/templates/data-sources/application_oidcs.md.tmpl new file mode 100644 index 00000000..3d217154 --- /dev/null +++ b/templates/data-sources/application_oidcs.md.tmpl @@ -0,0 +1,16 @@ +--- +page_title: "{{.Name}} {{.Type}} - {{.ProviderName}}" +subcategory: "" +description: |- +{{ .Description | plainmarkdown | trimspace | prefixlines " " }} +--- + +# {{.Name}} ({{.Type}}) + +{{ .Description | trimspace }} + +## Example Usage + +{{ tffile "examples/provider/data-sources/application_oidcs.tf" }} + +{{ .SchemaMarkdown | trimspace }} \ No newline at end of file diff --git a/templates/data-sources/machine_users.md.tmpl b/templates/data-sources/machine_users.md.tmpl new file mode 100644 index 00000000..7eb666e5 --- /dev/null +++ b/templates/data-sources/machine_users.md.tmpl @@ -0,0 +1,16 @@ +--- +page_title: "{{.Name}} {{.Type}} - {{.ProviderName}}" +subcategory: "" +description: |- +{{ .Description | plainmarkdown | trimspace | prefixlines " " }} +--- + +# {{.Name}} ({{.Type}}) + +{{ .Description | trimspace }} + +## Example Usage + +{{ tffile "examples/provider/data-sources/machine_users.tf" }} + +{{ .SchemaMarkdown | trimspace }} \ No newline at end of file diff --git a/zitadel/application_api/application_api_test_dep/dependency.go b/zitadel/application_api/application_api_test_dep/dependency.go index 4215885a..aad6a675 100644 --- a/zitadel/application_api/application_api_test_dep/dependency.go +++ b/zitadel/application_api/application_api_test_dep/dependency.go @@ -10,11 +10,11 @@ import ( "github.com/zitadel/terraform-provider-zitadel/zitadel/helper/test_utils" ) -func Create(t *testing.T, frame *test_utils.OrgTestFrame, projectID string) (string, string) { - return test_utils.CreateDefaultDependency(t, "zitadel_application_api", application_api.AppIDVar, func() (string, error) { +func Create(t *testing.T, frame *test_utils.OrgTestFrame, projectID, name string) (string, string) { + return test_utils.CreateProjectDefaultDependency(t, "zitadel_application_api", frame.OrgID, application_api.ProjectIDVar, projectID, application_api.AppIDVar, func() (string, error) { apiApp, err := frame.AddAPIApp(frame, &management.AddAPIAppRequest{ ProjectId: projectID, - Name: frame.UniqueResourcesID, + Name: name, AuthMethodType: app.APIAuthMethodType_API_AUTH_METHOD_TYPE_PRIVATE_KEY_JWT, }) return apiApp.GetAppId(), err diff --git a/zitadel/application_api/const.go b/zitadel/application_api/const.go index c1c211a4..a3359439 100644 --- a/zitadel/application_api/const.go +++ b/zitadel/application_api/const.go @@ -2,8 +2,10 @@ package application_api const ( AppIDVar = "app_id" + appIDsVar = "app_ids" ProjectIDVar = "project_id" NameVar = "name" + nameMethodVar = "name_method" authMethodTypeVar = "auth_method_type" ClientIDVar = "client_id" ClientSecretVar = "client_secret" diff --git a/zitadel/application_api/datasource.go b/zitadel/application_api/datasource.go index 08a91496..39b26a4b 100644 --- a/zitadel/application_api/datasource.go +++ b/zitadel/application_api/datasource.go @@ -1,7 +1,10 @@ package application_api import ( + "github.com/hashicorp/go-cty/cty" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/zitadel/zitadel-go/v2/pkg/client/zitadel/object" "github.com/zitadel/terraform-provider-zitadel/zitadel/helper" ) @@ -35,3 +38,38 @@ func GetDatasource() *schema.Resource { ReadContext: read, } } + +func ListDatasources() *schema.Resource { + return &schema.Resource{ + Description: "Datasource representing an API application belonging to a project, with all configuration possibilities.", + Schema: map[string]*schema.Schema{ + helper.OrgIDVar: helper.OrgIDDatasourceField, + appIDsVar: { + Type: schema.TypeList, + Computed: true, + Description: "A set of all IDs.", + Elem: &schema.Schema{Type: schema.TypeString}, + }, + ProjectIDVar: { + Type: schema.TypeString, + Required: true, + Description: "ID of the project", + }, + NameVar: { + Type: schema.TypeString, + Required: true, + Description: "Name of the application", + }, + nameMethodVar: { + Type: schema.TypeString, + Optional: true, + Description: "Method for querying applications by name" + helper.DescriptionEnumValuesList(object.TextQueryMethod_name), + ValidateDiagFunc: func(value interface{}, path cty.Path) diag.Diagnostics { + return helper.EnumValueValidation(nameMethodVar, value, object.TextQueryMethod_value) + }, + Default: object.TextQueryMethod_TEXT_QUERY_METHOD_EQUALS_IGNORE_CASE.String(), + }, + }, + ReadContext: list, + } +} diff --git a/zitadel/application_api/datasource_test.go b/zitadel/application_api/datasource_test.go new file mode 100644 index 00000000..ce358015 --- /dev/null +++ b/zitadel/application_api/datasource_test.go @@ -0,0 +1,107 @@ +package application_api_test + +import ( + "fmt" + "strings" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" + "github.com/zitadel/zitadel-go/v2/pkg/client/zitadel/management" + + "github.com/zitadel/terraform-provider-zitadel/zitadel/application_api" + "github.com/zitadel/terraform-provider-zitadel/zitadel/application_api/application_api_test_dep" + "github.com/zitadel/terraform-provider-zitadel/zitadel/helper" + "github.com/zitadel/terraform-provider-zitadel/zitadel/helper/test_utils" + "github.com/zitadel/terraform-provider-zitadel/zitadel/project/project_test_dep" +) + +func TestAccApplicationAPIDatasource_ID(t *testing.T) { + frame := test_utils.NewOrgTestFrame(t, "zitadel_application_api") + appName := "application_api_datasource_" + frame.UniqueResourcesID + _, projectID := project_test_dep.Create(t, frame, frame.UniqueResourcesID) + appDep, appID := application_api_test_dep.Create(t, frame, projectID, appName) + test_utils.RunDatasourceTest( + t, + frame.BaseTestFrame, + appDep, + nil, + map[string]string{ + "org_id": frame.OrgID, + "project_id": projectID, + "app_id": appID, + "name": appName, + }, + ) +} + +func TestAccApplicationAPIsDatasources_ID_Name_Match(t *testing.T) { + datasourceName := "zitadel_application_apis" + frame := test_utils.NewOrgTestFrame(t, datasourceName) + config, attributes := test_utils.ReadExample(t, test_utils.Datasources, datasourceName) + exampleName := test_utils.AttributeValue(t, application_api.NameVar, attributes).AsString() + exampleProject := test_utils.AttributeValue(t, application_api.ProjectIDVar, attributes).AsString() + exampleOrg := test_utils.AttributeValue(t, helper.OrgIDVar, attributes).AsString() + appName := fmt.Sprintf("%s-%s", exampleName, frame.UniqueResourcesID) + // for-each is not supported in acceptance tests, so we cut the example down to the first block + // https://github.com/hashicorp/terraform-plugin-sdk/issues/536 + config = strings.Join(strings.Split(config, "\n")[0:6], "\n") + config = strings.Replace(config, exampleName, appName, 1) + config = strings.Replace(config, exampleOrg, frame.OrgID, 1) + _, projectID := project_test_dep.Create(t, frame, frame.UniqueResourcesID) + config = strings.Replace(config, exampleProject, projectID, 1) + _, appID := application_api_test_dep.Create(t, frame, projectID, appName) + test_utils.RunDatasourceTest( + t, + frame.BaseTestFrame, + config, + checkRemoteDatasourceProperty(frame, projectID, appID)(appName), + map[string]string{ + "app_ids.0": appID, + "app_ids.#": "1", + }, + ) +} + +func TestAccApplicationAPIsDatasources_ID_Name_Mismatch(t *testing.T) { + datasourceName := "zitadel_application_apis" + frame := test_utils.NewOrgTestFrame(t, datasourceName) + config, attributes := test_utils.ReadExample(t, test_utils.Datasources, datasourceName) + exampleName := test_utils.AttributeValue(t, application_api.NameVar, attributes).AsString() + exampleProject := test_utils.AttributeValue(t, application_api.ProjectIDVar, attributes).AsString() + exampleOrg := test_utils.AttributeValue(t, helper.OrgIDVar, attributes).AsString() + appName := fmt.Sprintf("%s-%s", exampleName, frame.UniqueResourcesID) + // for-each is not supported in acceptance tests, so we cut the example down to the first block + // https://github.com/hashicorp/terraform-plugin-sdk/issues/536 + config = strings.Join(strings.Split(config, "\n")[0:6], "\n") + config = strings.Replace(config, exampleName, "mismatch", 1) + config = strings.Replace(config, exampleOrg, frame.OrgID, 1) + _, projectID := project_test_dep.Create(t, frame, frame.UniqueResourcesID) + config = strings.Replace(config, exampleProject, projectID, 1) + _, userID := application_api_test_dep.Create(t, frame, projectID, appName) + test_utils.RunDatasourceTest( + t, + frame.BaseTestFrame, + config, + checkRemoteDatasourceProperty(frame, projectID, userID)(appName), + map[string]string{ + "app_ids.#": "0", + }, + ) +} + +func checkRemoteDatasourceProperty(frame *test_utils.OrgTestFrame, projectId, id string) func(string) resource.TestCheckFunc { + return func(expect string) resource.TestCheckFunc { + return func(state *terraform.State) error { + remoteResource, err := frame.GetAppByID(frame, &management.GetAppByIDRequest{AppId: id, ProjectId: projectId}) + if err != nil { + return err + } + actual := remoteResource.GetApp().GetName() + if actual != expect { + return fmt.Errorf("expected %s, but got %s", expect, actual) + } + return nil + } + } +} diff --git a/zitadel/application_api/funcs.go b/zitadel/application_api/funcs.go index 0bde22d6..78cfbffd 100644 --- a/zitadel/application_api/funcs.go +++ b/zitadel/application_api/funcs.go @@ -8,6 +8,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/zitadel/zitadel-go/v2/pkg/client/zitadel/app" "github.com/zitadel/zitadel-go/v2/pkg/client/zitadel/management" + "github.com/zitadel/zitadel-go/v2/pkg/client/zitadel/object" "github.com/zitadel/terraform-provider-zitadel/zitadel/helper" ) @@ -145,3 +146,42 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn d.SetId(app.GetId()) return nil } + +func list(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + tflog.Info(ctx, "started list") + name := d.Get(NameVar).(string) + nameMethod := d.Get(nameMethodVar).(string) + clientinfo, ok := m.(*helper.ClientInfo) + if !ok { + return diag.Errorf("failed to get client") + } + client, err := helper.GetManagementClient(clientinfo) + if err != nil { + return diag.FromErr(err) + } + req := &management.ListAppsRequest{ + ProjectId: d.Get(ProjectIDVar).(string), + } + if name != "" { + req.Queries = append(req.Queries, + &app.AppQuery{ + Query: &app.AppQuery_NameQuery{ + NameQuery: &app.AppNameQuery{ + Name: name, + Method: object.TextQueryMethod(object.TextQueryMethod_value[nameMethod]), + }, + }, + }) + } + resp, err := client.ListApps(helper.CtxWithOrgID(ctx, d), req) + if err != nil { + return diag.Errorf("error while getting app by name %s: %v", name, err) + } + ids := make([]string, len(resp.Result)) + for i, res := range resp.Result { + ids[i] = res.Id + } + // If the ID is blank, the datasource is deleted and not usable. + d.SetId("-") + return diag.FromErr(d.Set(appIDsVar, ids)) +} diff --git a/zitadel/application_api/resource_test.go b/zitadel/application_api/resource_test.go index cd482699..9205538d 100644 --- a/zitadel/application_api/resource_test.go +++ b/zitadel/application_api/resource_test.go @@ -18,7 +18,7 @@ func TestAccAppAPI(t *testing.T) { frame := test_utils.NewOrgTestFrame(t, "zitadel_application_api") resourceExample, exampleAttributes := test_utils.ReadExample(t, test_utils.Resources, frame.ResourceType) exampleProperty := test_utils.AttributeValue(t, application_api.NameVar, exampleAttributes).AsString() - projectDep, projectID := project_test_dep.Create(t, frame) + projectDep, projectID := project_test_dep.Create(t, frame, frame.UniqueResourcesID) test_utils.RunLifecyleTest( t, frame.BaseTestFrame, diff --git a/zitadel/application_key/resource_test.go b/zitadel/application_key/resource_test.go index ead74857..e11517d2 100644 --- a/zitadel/application_key/resource_test.go +++ b/zitadel/application_key/resource_test.go @@ -20,8 +20,8 @@ func TestAccAppKey(t *testing.T) { resourceExample, exampleAttributes := test_utils.ReadExample(t, test_utils.Resources, frame.ResourceType) exampleProperty := test_utils.AttributeValue(t, application_key.ExpirationDateVar, exampleAttributes).AsString() updatedProperty := "2501-01-01T08:45:00Z" - projectDep, projectID := project_test_dep.Create(t, frame) - appDep, appID := application_api_test_dep.Create(t, frame, projectID) + projectDep, projectID := project_test_dep.Create(t, frame, frame.UniqueResourcesID) + appDep, appID := application_api_test_dep.Create(t, frame, projectID, frame.UniqueResourcesID) test_utils.RunLifecyleTest( t, frame.BaseTestFrame, diff --git a/zitadel/application_oidc/application_oidc_test_dep/dependency.go b/zitadel/application_oidc/application_oidc_test_dep/dependency.go new file mode 100644 index 00000000..4e0df1d3 --- /dev/null +++ b/zitadel/application_oidc/application_oidc_test_dep/dependency.go @@ -0,0 +1,20 @@ +package application_oidc_test_dep + +import ( + "testing" + + "github.com/zitadel/zitadel-go/v2/pkg/client/zitadel/management" + + "github.com/zitadel/terraform-provider-zitadel/zitadel/application_oidc" + "github.com/zitadel/terraform-provider-zitadel/zitadel/helper/test_utils" +) + +func Create(t *testing.T, frame *test_utils.OrgTestFrame, projectID, name string) (string, string) { + return test_utils.CreateProjectDefaultDependency(t, "zitadel_application_oidc", frame.OrgID, application_oidc.ProjectIDVar, projectID, application_oidc.AppIDVar, func() (string, error) { + oidcApp, err := frame.AddOIDCApp(frame, &management.AddOIDCAppRequest{ + ProjectId: projectID, + Name: name, + }) + return oidcApp.GetAppId(), err + }) +} diff --git a/zitadel/application_oidc/const.go b/zitadel/application_oidc/const.go index f06e41b0..6724e361 100644 --- a/zitadel/application_oidc/const.go +++ b/zitadel/application_oidc/const.go @@ -1,9 +1,11 @@ package application_oidc const ( - appIDVar = "app_id" + AppIDVar = "app_id" + appIDsVar = "app_ids" ProjectIDVar = "project_id" NameVar = "name" + nameMethodVar = "name_method" redirectURIsVar = "redirect_uris" responseTypesVar = "response_types" grantTypesVar = "grant_types" diff --git a/zitadel/application_oidc/datasource.go b/zitadel/application_oidc/datasource.go index 00cc3fc2..c1aea588 100644 --- a/zitadel/application_oidc/datasource.go +++ b/zitadel/application_oidc/datasource.go @@ -1,7 +1,10 @@ package application_oidc import ( + "github.com/hashicorp/go-cty/cty" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/zitadel/zitadel-go/v2/pkg/client/zitadel/object" "github.com/zitadel/terraform-provider-zitadel/zitadel/helper" ) @@ -10,7 +13,7 @@ func GetDatasource() *schema.Resource { return &schema.Resource{ Description: "Datasource representing an OIDC application belonging to a project, with all configuration possibilities.", Schema: map[string]*schema.Schema{ - appIDVar: { + AppIDVar: { Type: schema.TypeString, Required: true, Description: "The ID of this resource.", @@ -115,3 +118,38 @@ func GetDatasource() *schema.Resource { ReadContext: read, } } + +func ListDatasources() *schema.Resource { + return &schema.Resource{ + Description: "Datasource representing an OIDC application belonging to a project, with all configuration possibilities.", + Schema: map[string]*schema.Schema{ + helper.OrgIDVar: helper.OrgIDDatasourceField, + appIDsVar: { + Type: schema.TypeList, + Computed: true, + Description: "A set of all IDs.", + Elem: &schema.Schema{Type: schema.TypeString}, + }, + ProjectIDVar: { + Type: schema.TypeString, + Required: true, + Description: "ID of the project", + }, + NameVar: { + Type: schema.TypeString, + Required: true, + Description: "Name of the application", + }, + nameMethodVar: { + Type: schema.TypeString, + Optional: true, + Description: "Method for querying applications by name" + helper.DescriptionEnumValuesList(object.TextQueryMethod_name), + ValidateDiagFunc: func(value interface{}, path cty.Path) diag.Diagnostics { + return helper.EnumValueValidation(nameMethodVar, value, object.TextQueryMethod_value) + }, + Default: object.TextQueryMethod_TEXT_QUERY_METHOD_EQUALS_IGNORE_CASE.String(), + }, + }, + ReadContext: list, + } +} diff --git a/zitadel/application_oidc/datasource_test.go b/zitadel/application_oidc/datasource_test.go new file mode 100644 index 00000000..a1be8726 --- /dev/null +++ b/zitadel/application_oidc/datasource_test.go @@ -0,0 +1,107 @@ +package application_oidc_test + +import ( + "fmt" + "strings" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" + "github.com/zitadel/zitadel-go/v2/pkg/client/zitadel/management" + + "github.com/zitadel/terraform-provider-zitadel/zitadel/application_oidc" + "github.com/zitadel/terraform-provider-zitadel/zitadel/application_oidc/application_oidc_test_dep" + "github.com/zitadel/terraform-provider-zitadel/zitadel/helper" + "github.com/zitadel/terraform-provider-zitadel/zitadel/helper/test_utils" + "github.com/zitadel/terraform-provider-zitadel/zitadel/project/project_test_dep" +) + +func TestAccApplicationOIDCDatasource_ID(t *testing.T) { + frame := test_utils.NewOrgTestFrame(t, "zitadel_application_oidc") + appName := "application_oidc_datasource_" + frame.UniqueResourcesID + _, projectID := project_test_dep.Create(t, frame, frame.UniqueResourcesID) + appDep, appID := application_oidc_test_dep.Create(t, frame, projectID, appName) + test_utils.RunDatasourceTest( + t, + frame.BaseTestFrame, + appDep, + nil, + map[string]string{ + "org_id": frame.OrgID, + "project_id": projectID, + "app_id": appID, + "name": appName, + }, + ) +} + +func TestAccApplicationOIDCsDatasources_ID_Name_Match(t *testing.T) { + datasourceName := "zitadel_application_oidcs" + frame := test_utils.NewOrgTestFrame(t, datasourceName) + config, attributes := test_utils.ReadExample(t, test_utils.Datasources, datasourceName) + exampleName := test_utils.AttributeValue(t, application_oidc.NameVar, attributes).AsString() + exampleProject := test_utils.AttributeValue(t, application_oidc.ProjectIDVar, attributes).AsString() + exampleOrg := test_utils.AttributeValue(t, helper.OrgIDVar, attributes).AsString() + appName := fmt.Sprintf("%s-%s", exampleName, frame.UniqueResourcesID) + // for-each is not supported in acceptance tests, so we cut the example down to the first block + // https://github.com/hashicorp/terraform-plugin-sdk/issues/536 + config = strings.Join(strings.Split(config, "\n")[0:6], "\n") + config = strings.Replace(config, exampleName, appName, 1) + config = strings.Replace(config, exampleOrg, frame.OrgID, 1) + _, projectID := project_test_dep.Create(t, frame, frame.UniqueResourcesID) + config = strings.Replace(config, exampleProject, projectID, 1) + _, appID := application_oidc_test_dep.Create(t, frame, projectID, appName) + test_utils.RunDatasourceTest( + t, + frame.BaseTestFrame, + config, + checkRemoteDatasourceProperty(frame, projectID, appID)(appName), + map[string]string{ + "app_ids.0": appID, + "app_ids.#": "1", + }, + ) +} + +func TestAccApplicationOIDCsDatasources_ID_Name_Mismatch(t *testing.T) { + datasourceName := "zitadel_application_oidcs" + frame := test_utils.NewOrgTestFrame(t, datasourceName) + config, attributes := test_utils.ReadExample(t, test_utils.Datasources, datasourceName) + exampleName := test_utils.AttributeValue(t, application_oidc.NameVar, attributes).AsString() + exampleProject := test_utils.AttributeValue(t, application_oidc.ProjectIDVar, attributes).AsString() + exampleOrg := test_utils.AttributeValue(t, helper.OrgIDVar, attributes).AsString() + appName := fmt.Sprintf("%s-%s", exampleName, frame.UniqueResourcesID) + // for-each is not supported in acceptance tests, so we cut the example down to the first block + // https://github.com/hashicorp/terraform-plugin-sdk/issues/536 + config = strings.Join(strings.Split(config, "\n")[0:6], "\n") + config = strings.Replace(config, exampleName, "mismatch", 1) + config = strings.Replace(config, exampleOrg, frame.OrgID, 1) + _, projectID := project_test_dep.Create(t, frame, frame.UniqueResourcesID) + config = strings.Replace(config, exampleProject, projectID, 1) + _, userID := application_oidc_test_dep.Create(t, frame, projectID, appName) + test_utils.RunDatasourceTest( + t, + frame.BaseTestFrame, + config, + checkRemoteDatasourceProperty(frame, projectID, userID)(appName), + map[string]string{ + "app_ids.#": "0", + }, + ) +} + +func checkRemoteDatasourceProperty(frame *test_utils.OrgTestFrame, projectId, id string) func(string) resource.TestCheckFunc { + return func(expect string) resource.TestCheckFunc { + return func(state *terraform.State) error { + remoteResource, err := frame.GetAppByID(frame, &management.GetAppByIDRequest{AppId: id, ProjectId: projectId}) + if err != nil { + return err + } + actual := remoteResource.GetApp().GetName() + if actual != expect { + return fmt.Errorf("expected %s, but got %s", expect, actual) + } + return nil + } + } +} diff --git a/zitadel/application_oidc/funcs.go b/zitadel/application_oidc/funcs.go index d121c728..691069aa 100644 --- a/zitadel/application_oidc/funcs.go +++ b/zitadel/application_oidc/funcs.go @@ -9,6 +9,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/zitadel/zitadel-go/v2/pkg/client/zitadel/app" "github.com/zitadel/zitadel-go/v2/pkg/client/zitadel/management" + "github.com/zitadel/zitadel-go/v2/pkg/client/zitadel/object" "google.golang.org/protobuf/types/known/durationpb" "github.com/zitadel/terraform-provider-zitadel/zitadel/helper" @@ -191,7 +192,7 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn return diag.FromErr(err) } - resp, err := client.GetAppByID(helper.CtxWithOrgID(ctx, d), &management.GetAppByIDRequest{ProjectId: d.Get(ProjectIDVar).(string), AppId: helper.GetID(d, appIDVar)}) + resp, err := client.GetAppByID(helper.CtxWithOrgID(ctx, d), &management.GetAppByIDRequest{ProjectId: d.Get(ProjectIDVar).(string), AppId: helper.GetID(d, AppIDVar)}) if err != nil && helper.IgnoreIfNotFoundError(err) == nil { d.SetId("") return nil @@ -250,3 +251,42 @@ func interfaceToStringSlice(in interface{}) []string { } return ret } + +func list(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + tflog.Info(ctx, "started list") + name := d.Get(NameVar).(string) + nameMethod := d.Get(nameMethodVar).(string) + clientinfo, ok := m.(*helper.ClientInfo) + if !ok { + return diag.Errorf("failed to get client") + } + client, err := helper.GetManagementClient(clientinfo) + if err != nil { + return diag.FromErr(err) + } + req := &management.ListAppsRequest{ + ProjectId: d.Get(ProjectIDVar).(string), + } + if name != "" { + req.Queries = append(req.Queries, + &app.AppQuery{ + Query: &app.AppQuery_NameQuery{ + NameQuery: &app.AppNameQuery{ + Name: name, + Method: object.TextQueryMethod(object.TextQueryMethod_value[nameMethod]), + }, + }, + }) + } + resp, err := client.ListApps(helper.CtxWithOrgID(ctx, d), req) + if err != nil { + return diag.Errorf("error while getting app by name %s: %v", name, err) + } + ids := make([]string, len(resp.Result)) + for i, res := range resp.Result { + ids[i] = res.Id + } + // If the ID is blank, the datasource is deleted and not usable. + d.SetId("-") + return diag.FromErr(d.Set(appIDsVar, ids)) +} diff --git a/zitadel/application_oidc/resource.go b/zitadel/application_oidc/resource.go index d83f4603..1220cafb 100644 --- a/zitadel/application_oidc/resource.go +++ b/zitadel/application_oidc/resource.go @@ -151,7 +151,7 @@ func GetResource() *schema.Resource { UpdateContext: update, ReadContext: read, Importer: helper.ImportWithIDAndOptionalOrg( - appIDVar, + AppIDVar, helper.NewImportAttribute(ProjectIDVar, helper.ConvertID, false), helper.NewImportAttribute(ClientIDVar, helper.ConvertNonEmpty, true), helper.NewImportAttribute(ClientSecretVar, helper.ConvertNonEmpty, true), diff --git a/zitadel/application_oidc/resource_test.go b/zitadel/application_oidc/resource_test.go index 6b02d07b..87046d13 100644 --- a/zitadel/application_oidc/resource_test.go +++ b/zitadel/application_oidc/resource_test.go @@ -18,7 +18,7 @@ func TestAccAppOIDC(t *testing.T) { frame := test_utils.NewOrgTestFrame(t, "zitadel_application_oidc") resourceExample, exampleAttributes := test_utils.ReadExample(t, test_utils.Resources, frame.ResourceType) exampleProperty := test_utils.AttributeValue(t, application_oidc.NameVar, exampleAttributes).AsString() - projectDep, projectID := project_test_dep.Create(t, frame) + projectDep, projectID := project_test_dep.Create(t, frame, frame.UniqueResourcesID) test_utils.RunLifecyleTest( t, frame.BaseTestFrame, diff --git a/zitadel/helper/test_utils/base_frame.go b/zitadel/helper/test_utils/base_frame.go index 41b4cbad..2860b852 100644 --- a/zitadel/helper/test_utils/base_frame.go +++ b/zitadel/helper/test_utils/base_frame.go @@ -94,5 +94,9 @@ func (b *BaseTestFrame) State(state *terraform.State) *terraform.InstanceState { if resource != nil { return resource.Primary } - return resources["data."+b.TerraformName].Primary + resource = resources["data."+b.TerraformName] + if resource != nil { + return resource.Primary + } + return &terraform.InstanceState{} } diff --git a/zitadel/helper/test_utils/dependency.go b/zitadel/helper/test_utils/dependency.go index df934b66..c9b38e22 100644 --- a/zitadel/helper/test_utils/dependency.go +++ b/zitadel/helper/test_utils/dependency.go @@ -3,6 +3,8 @@ package test_utils import ( "strings" "testing" + + "github.com/zitadel/terraform-provider-zitadel/zitadel/helper" ) func CreateDefaultDependency(t *testing.T, datasourceType string, idField string, newDependencyID func() (string, error)) (string, string) { @@ -13,3 +15,28 @@ func CreateDefaultDependency(t *testing.T, datasourceType string, idField string } return strings.Replace(datasourceExample, AttributeValue(t, idField, datasourceExampleAttributes).AsString(), dependencyID, 1), dependencyID } + +func CreateOrgDefaultDependency(t *testing.T, datasourceType string, orgID string, idField string, newDependencyID func() (string, error)) (string, string) { + datasourceExample, datasourceExampleAttributes := ReadExample(t, Datasources, datasourceType) + dependencyID, err := newDependencyID() + if err != nil { + t.Fatalf("failed to create dependency for %s: %v", datasourceType, err) + } + // org exampleID does always have to be different then the idField value + dep := strings.Replace(datasourceExample, AttributeValue(t, idField, datasourceExampleAttributes).AsString(), dependencyID, 1) + // replace the exmaple OrgID with the given org + return strings.Replace(dep, AttributeValue(t, helper.OrgIDVar, datasourceExampleAttributes).AsString(), orgID, 1), dependencyID +} + +func CreateProjectDefaultDependency(t *testing.T, datasourceType string, orgID string, projectField string, projectID string, idField string, newDependencyID func() (string, error)) (string, string) { + datasourceExample, datasourceExampleAttributes := ReadExample(t, Datasources, datasourceType) + dependencyID, err := newDependencyID() + if err != nil { + t.Fatalf("failed to create dependency for %s: %v", datasourceType, err) + } + // org exampleID does always have to be different then the idField value + dep := strings.Replace(datasourceExample, AttributeValue(t, idField, datasourceExampleAttributes).AsString(), dependencyID, 1) + dep = strings.Replace(dep, AttributeValue(t, projectField, datasourceExampleAttributes).AsString(), projectID, 1) + // replace the exmaple OrgID with the given org + return strings.Replace(dep, AttributeValue(t, helper.OrgIDVar, datasourceExampleAttributes).AsString(), orgID, 1), dependencyID +} diff --git a/zitadel/machine_key/resource_test.go b/zitadel/machine_key/resource_test.go index ad2ebc04..2d75151a 100644 --- a/zitadel/machine_key/resource_test.go +++ b/zitadel/machine_key/resource_test.go @@ -16,7 +16,7 @@ import ( func TestAccMachineKey(t *testing.T) { frame := test_utils.NewOrgTestFrame(t, "zitadel_machine_key") - userDep, userID := machine_user_test_dep.Create(t, frame) + userDep, userID := machine_user_test_dep.Create(t, frame, frame.UniqueResourcesID) resourceExample, exampleAttributes := test_utils.ReadExample(t, test_utils.Resources, frame.ResourceType) exampleProperty := test_utils.AttributeValue(t, machine_key.ExpirationDateVar, exampleAttributes).AsString() test_utils.RunLifecyleTest( diff --git a/zitadel/machine_user/const.go b/zitadel/machine_user/const.go index 8233f9bc..154e42b6 100644 --- a/zitadel/machine_user/const.go +++ b/zitadel/machine_user/const.go @@ -6,8 +6,10 @@ import ( const ( UserIDVar = "user_id" + userIDsVar = "user_ids" userStateVar = "state" UserNameVar = "user_name" + userNameMethodVar = "user_name_method" loginNamesVar = "login_names" preferredLoginNameVar = "preferred_login_name" nameVar = "name" diff --git a/zitadel/machine_user/datasource.go b/zitadel/machine_user/datasource.go index 3b07ae86..f92a17e6 100644 --- a/zitadel/machine_user/datasource.go +++ b/zitadel/machine_user/datasource.go @@ -1,7 +1,10 @@ package machine_user import ( + "github.com/hashicorp/go-cty/cty" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/zitadel/zitadel-go/v2/pkg/client/zitadel/object" "github.com/zitadel/terraform-provider-zitadel/zitadel/helper" ) @@ -58,3 +61,33 @@ func GetDatasource() *schema.Resource { ReadContext: read, } } + +func ListDatasources() *schema.Resource { + return &schema.Resource{ + Description: "Datasource representing a serviceaccount situated under an organization, which then can be authorized through memberships or direct grants on other resources.", + Schema: map[string]*schema.Schema{ + helper.OrgIDVar: helper.OrgIDDatasourceField, + userIDsVar: { + Type: schema.TypeList, + Computed: true, + Description: "A set of all IDs.", + Elem: &schema.Schema{Type: schema.TypeString}, + }, + UserNameVar: { + Type: schema.TypeString, + Required: true, + Description: "Username", + }, + userNameMethodVar: { + Type: schema.TypeString, + Optional: true, + Description: "Method for querying machine users by username" + helper.DescriptionEnumValuesList(object.TextQueryMethod_name), + ValidateDiagFunc: func(value interface{}, path cty.Path) diag.Diagnostics { + return helper.EnumValueValidation(userNameMethodVar, value, object.TextQueryMethod_value) + }, + Default: object.TextQueryMethod_TEXT_QUERY_METHOD_EQUALS_IGNORE_CASE.String(), + }, + }, + ReadContext: list, + } +} diff --git a/zitadel/machine_user/datasource_test.go b/zitadel/machine_user/datasource_test.go new file mode 100644 index 00000000..80574b9e --- /dev/null +++ b/zitadel/machine_user/datasource_test.go @@ -0,0 +1,98 @@ +package machine_user_test + +import ( + "fmt" + "strings" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" + "github.com/zitadel/zitadel-go/v2/pkg/client/zitadel/management" + + "github.com/zitadel/terraform-provider-zitadel/zitadel/helper" + "github.com/zitadel/terraform-provider-zitadel/zitadel/helper/test_utils" + "github.com/zitadel/terraform-provider-zitadel/zitadel/machine_user" + "github.com/zitadel/terraform-provider-zitadel/zitadel/machine_user/machine_user_test_dep" +) + +func TestAccMachineUserDatasource_ID(t *testing.T) { + frame := test_utils.NewOrgTestFrame(t, "zitadel_machine_user") + userName := "machine_user_datasource_" + frame.UniqueResourcesID + userDep, userID := machine_user_test_dep.Create(t, frame, userName) + test_utils.RunDatasourceTest( + t, + frame.BaseTestFrame, + userDep, + nil, + map[string]string{ + "org_id": frame.OrgID, + "user_id": userID, + "user_name": userName, + }, + ) +} + +func TestAccMachineUsersDatasources_ID_Name_Match(t *testing.T) { + datasourceName := "zitadel_machine_users" + frame := test_utils.NewOrgTestFrame(t, datasourceName) + config, attributes := test_utils.ReadExample(t, test_utils.Datasources, datasourceName) + exampleName := test_utils.AttributeValue(t, machine_user.UserNameVar, attributes).AsString() + exampleOrg := test_utils.AttributeValue(t, helper.OrgIDVar, attributes).AsString() + userName := fmt.Sprintf("%s-%s", exampleName, frame.UniqueResourcesID) + // for-each is not supported in acceptance tests, so we cut the example down to the first block + // https://github.com/hashicorp/terraform-plugin-sdk/issues/536 + config = strings.Join(strings.Split(config, "\n")[0:5], "\n") + config = strings.Replace(config, exampleName, userName, 1) + config = strings.Replace(config, exampleOrg, frame.OrgID, 1) + _, userID := machine_user_test_dep.Create(t, frame, userName) + test_utils.RunDatasourceTest( + t, + frame.BaseTestFrame, + config, + checkRemoteDatasourceProperty(frame, userID)(userName), + map[string]string{ + "user_ids.0": userID, + "user_ids.#": "1", + }, + ) +} + +func TestAccMachineUsersDatasources_ID_Name_Mismatch(t *testing.T) { + datasourceName := "zitadel_machine_users" + frame := test_utils.NewOrgTestFrame(t, datasourceName) + config, attributes := test_utils.ReadExample(t, test_utils.Datasources, datasourceName) + exampleName := test_utils.AttributeValue(t, machine_user.UserNameVar, attributes).AsString() + exampleOrg := test_utils.AttributeValue(t, helper.OrgIDVar, attributes).AsString() + userName := fmt.Sprintf("%s-%s", exampleName, frame.UniqueResourcesID) + // for-each is not supported in acceptance tests, so we cut the example down to the first block + // https://github.com/hashicorp/terraform-plugin-sdk/issues/536 + config = strings.Join(strings.Split(config, "\n")[0:5], "\n") + config = strings.Replace(config, exampleName, "mismatch", 1) + config = strings.Replace(config, exampleOrg, frame.OrgID, 1) + _, userID := machine_user_test_dep.Create(t, frame, userName) + test_utils.RunDatasourceTest( + t, + frame.BaseTestFrame, + config, + checkRemoteDatasourceProperty(frame, userID)(userName), + map[string]string{ + "user_ids.#": "0", + }, + ) +} + +func checkRemoteDatasourceProperty(frame *test_utils.OrgTestFrame, id string) func(string) resource.TestCheckFunc { + return func(expect string) resource.TestCheckFunc { + return func(state *terraform.State) error { + remoteResource, err := frame.GetUserByID(frame, &management.GetUserByIDRequest{Id: id}) + if err != nil { + return err + } + actual := remoteResource.GetUser().GetUserName() + if actual != expect { + return fmt.Errorf("expected %s, but got %s", expect, actual) + } + return nil + } + } +} diff --git a/zitadel/machine_user/funcs.go b/zitadel/machine_user/funcs.go index f1ee06ca..a3720645 100644 --- a/zitadel/machine_user/funcs.go +++ b/zitadel/machine_user/funcs.go @@ -7,6 +7,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/zitadel/zitadel-go/v2/pkg/client/zitadel/management" + "github.com/zitadel/zitadel-go/v2/pkg/client/zitadel/object" "github.com/zitadel/zitadel-go/v2/pkg/client/zitadel/user" "github.com/zitadel/terraform-provider-zitadel/zitadel/helper" @@ -142,3 +143,40 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn d.SetId(user.GetId()) return nil } + +func list(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + tflog.Info(ctx, "started list") + userName := d.Get(UserNameVar).(string) + userNameMethod := d.Get(userNameMethodVar).(string) + clientinfo, ok := m.(*helper.ClientInfo) + if !ok { + return diag.Errorf("failed to get client") + } + client, err := helper.GetManagementClient(clientinfo) + if err != nil { + return diag.FromErr(err) + } + req := &management.ListUsersRequest{} + if userName != "" { + req.Queries = append(req.Queries, + &user.SearchQuery{ + Query: &user.SearchQuery_UserNameQuery{ + UserNameQuery: &user.UserNameQuery{ + UserName: userName, + Method: object.TextQueryMethod(object.TextQueryMethod_value[userNameMethod]), + }, + }, + }) + } + resp, err := client.ListUsers(helper.CtxWithOrgID(ctx, d), req) + if err != nil { + return diag.Errorf("error while getting user by username %s: %v", userName, err) + } + ids := make([]string, len(resp.Result)) + for i, res := range resp.Result { + ids[i] = res.Id + } + // If the ID is blank, the datasource is deleted and not usable. + d.SetId("-") + return diag.FromErr(d.Set(userIDsVar, ids)) +} diff --git a/zitadel/machine_user/machine_user_test_dep/dependency.go b/zitadel/machine_user/machine_user_test_dep/dependency.go index a32a2527..41433baf 100644 --- a/zitadel/machine_user/machine_user_test_dep/dependency.go +++ b/zitadel/machine_user/machine_user_test_dep/dependency.go @@ -9,10 +9,10 @@ import ( "github.com/zitadel/terraform-provider-zitadel/zitadel/machine_user" ) -func Create(t *testing.T, frame *test_utils.OrgTestFrame) (string, string) { - return test_utils.CreateDefaultDependency(t, "zitadel_machine_user", machine_user.UserIDVar, func() (string, error) { +func Create(t *testing.T, frame *test_utils.OrgTestFrame, username string) (string, string) { + return test_utils.CreateOrgDefaultDependency(t, "zitadel_machine_user", frame.OrgID, machine_user.UserIDVar, func() (string, error) { user, err := frame.AddMachineUser(frame, &management.AddMachineUserRequest{ - UserName: frame.UniqueResourcesID, + UserName: username, Name: "Don't care", }) userID := user.GetUserId() diff --git a/zitadel/pat/resource_test.go b/zitadel/pat/resource_test.go index dd24a877..a67d1b48 100644 --- a/zitadel/pat/resource_test.go +++ b/zitadel/pat/resource_test.go @@ -16,7 +16,7 @@ import ( func TestAccPersonalAccessToken(t *testing.T) { frame := test_utils.NewOrgTestFrame(t, "zitadel_personal_access_token") - userDep, userID := machine_user_test_dep.Create(t, frame) + userDep, userID := machine_user_test_dep.Create(t, frame, frame.UniqueResourcesID) resourceExample, exampleAttributes := test_utils.ReadExample(t, test_utils.Resources, frame.ResourceType) exampleProperty := test_utils.AttributeValue(t, pat.ExpirationDateVar, exampleAttributes).AsString() updatedProperty := "2051-01-01T00:00:00Z" diff --git a/zitadel/project/datasource.go b/zitadel/project/datasource.go index 830fb88a..d8264b86 100644 --- a/zitadel/project/datasource.go +++ b/zitadel/project/datasource.go @@ -58,6 +58,7 @@ func ListDatasources() *schema.Resource { return &schema.Resource{ Description: "Datasource representing the project, which can then be granted to different organizations or users directly, containing different applications.", Schema: map[string]*schema.Schema{ + helper.OrgIDVar: helper.OrgIDResourceField, projectIDsVar: { Type: schema.TypeList, Computed: true, @@ -78,32 +79,6 @@ func ListDatasources() *schema.Resource { }, Default: object.TextQueryMethod_TEXT_QUERY_METHOD_EQUALS_IGNORE_CASE.String(), }, - helper.OrgIDVar: helper.OrgIDResourceField, - stateVar: { - Type: schema.TypeString, - Computed: true, - Description: "State of the project", - }, - roleAssertionVar: { - Type: schema.TypeBool, - Computed: true, - Description: "describes if roles of user should be added in token", - }, - roleCheckVar: { - Type: schema.TypeBool, - Computed: true, - Description: "ZITADEL checks if the user has at least one on this project", - }, - hasProjectCheckVar: { - Type: schema.TypeBool, - Computed: true, - Description: "ZITADEL checks if the org of the user has permission to this project", - }, - privateLabelingSettingVar: { - Type: schema.TypeString, - Computed: true, - Description: "Defines from where the private labeling should be triggered", - }, }, ReadContext: list, } diff --git a/zitadel/project/datasource_test.go b/zitadel/project/datasource_test.go index 91fd12be..0d21d938 100644 --- a/zitadel/project/datasource_test.go +++ b/zitadel/project/datasource_test.go @@ -5,6 +5,11 @@ import ( "strings" "testing" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" + "github.com/zitadel/zitadel-go/v2/pkg/client/zitadel/management" + + "github.com/zitadel/terraform-provider-zitadel/zitadel/helper" "github.com/zitadel/terraform-provider-zitadel/zitadel/helper/test_utils" "github.com/zitadel/terraform-provider-zitadel/zitadel/project" "github.com/zitadel/terraform-provider-zitadel/zitadel/project/project_test_dep" @@ -13,7 +18,7 @@ import ( func TestAccProjectDatasource_ID(t *testing.T) { frame := test_utils.NewOrgTestFrame(t, "zitadel_project") projectName := "project_datasource_" + frame.UniqueResourcesID - projectDep, projectID := project_test_dep.Create(t, frame) + projectDep, projectID := project_test_dep.Create(t, frame, projectName) test_utils.RunDatasourceTest( t, frame.BaseTestFrame, @@ -32,20 +37,62 @@ func TestAccProjectsDatasources_ID_Name_Match(t *testing.T) { frame := test_utils.NewOrgTestFrame(t, datasourceName) config, attributes := test_utils.ReadExample(t, test_utils.Datasources, datasourceName) exampleName := test_utils.AttributeValue(t, project.NameVar, attributes).AsString() + exampleOrg := test_utils.AttributeValue(t, helper.OrgIDVar, attributes).AsString() projectName := fmt.Sprintf("%s-%s", exampleName, frame.UniqueResourcesID) // for-each is not supported in acceptance tests, so we cut the example down to the first block // https://github.com/hashicorp/terraform-plugin-sdk/issues/536 - config = strings.Join(strings.Split(config, "\n")[0:7], "\n") + config = strings.Join(strings.Split(config, "\n")[0:5], "\n") config = strings.Replace(config, exampleName, projectName, 1) - _, projectID := project_test_dep.Create(t, frame) + config = strings.Replace(config, exampleOrg, frame.OrgID, 1) + _, projectID := project_test_dep.Create(t, frame, projectName) test_utils.RunDatasourceTest( t, frame.BaseTestFrame, config, - checkRemoteProperty(frame)(projectName), + checkRemoteDatasourceProperty(frame, projectID)(projectName), map[string]string{ "project_ids.0": projectID, "project_ids.#": "1", }, ) } + +func TestAccProjectsDatasources_ID_Name_Mismatch(t *testing.T) { + datasourceName := "zitadel_projects" + frame := test_utils.NewOrgTestFrame(t, datasourceName) + config, attributes := test_utils.ReadExample(t, test_utils.Datasources, datasourceName) + exampleName := test_utils.AttributeValue(t, project.NameVar, attributes).AsString() + exampleOrg := test_utils.AttributeValue(t, helper.OrgIDVar, attributes).AsString() + projectName := fmt.Sprintf("%s-%s", exampleName, frame.UniqueResourcesID) + // for-each is not supported in acceptance tests, so we cut the example down to the first block + // https://github.com/hashicorp/terraform-plugin-sdk/issues/536 + config = strings.Join(strings.Split(config, "\n")[0:5], "\n") + config = strings.Replace(config, exampleName, "mismatch", 1) + config = strings.Replace(config, exampleOrg, frame.OrgID, 1) + _, projectID := project_test_dep.Create(t, frame, projectName) + test_utils.RunDatasourceTest( + t, + frame.BaseTestFrame, + config, + checkRemoteDatasourceProperty(frame, projectID)(projectName), + map[string]string{ + "project_ids.#": "0", + }, + ) +} + +func checkRemoteDatasourceProperty(frame *test_utils.OrgTestFrame, id string) func(string) resource.TestCheckFunc { + return func(expect string) resource.TestCheckFunc { + return func(state *terraform.State) error { + remoteResource, err := frame.GetProjectByID(frame, &management.GetProjectByIDRequest{Id: id}) + if err != nil { + return err + } + actual := remoteResource.GetProject().GetName() + if actual != expect { + return fmt.Errorf("expected %s, but got %s", expect, actual) + } + return nil + } + } +} diff --git a/zitadel/project/project_test_dep/dependency.go b/zitadel/project/project_test_dep/dependency.go index d255f873..47b15672 100644 --- a/zitadel/project/project_test_dep/dependency.go +++ b/zitadel/project/project_test_dep/dependency.go @@ -9,9 +9,13 @@ import ( "github.com/zitadel/terraform-provider-zitadel/zitadel/project" ) -func Create(t *testing.T, frame *test_utils.OrgTestFrame) (string, string) { - return test_utils.CreateDefaultDependency(t, "zitadel_project", project.ProjectIDVar, func() (string, error) { - p, err := frame.AddProject(frame, &management.AddProjectRequest{Name: frame.UniqueResourcesID}) - return p.GetId(), err - }) +func Create(t *testing.T, frame *test_utils.OrgTestFrame, name string) (string, string) { + return test_utils.CreateOrgDefaultDependency(t, + "zitadel_project", + frame.OrgID, + project.ProjectIDVar, + func() (string, error) { + p, err := frame.AddProject(frame, &management.AddProjectRequest{Name: name}) + return p.GetId(), err + }) } diff --git a/zitadel/project_grant/resource_test.go b/zitadel/project_grant/resource_test.go index 21401235..273d2521 100644 --- a/zitadel/project_grant/resource_test.go +++ b/zitadel/project_grant/resource_test.go @@ -21,7 +21,7 @@ func TestAccProjectGrant(t *testing.T) { resourceExample, exampleAttributes := test_utils.ReadExample(t, test_utils.Resources, frame.ResourceType) exampleProperty := test_utils.AttributeValue(t, project_grant.RoleKeysVar, exampleAttributes).AsValueSlice()[0].AsString() updatedProperty := "updatedproperty" - projectDep, projectID := project_test_dep.Create(t, frame) + projectDep, projectID := project_test_dep.Create(t, frame, frame.UniqueResourcesID) project_role_test_dep.Create(t, frame, projectID, exampleProperty, updatedProperty) grantedOrgDep, _, _ := org_test_dep.Create(t, frame, "granted_org") test_utils.RunLifecyleTest( diff --git a/zitadel/project_grant_member/resource_test.go b/zitadel/project_grant_member/resource_test.go index 2757b035..bf6777d9 100644 --- a/zitadel/project_grant_member/resource_test.go +++ b/zitadel/project_grant_member/resource_test.go @@ -25,7 +25,7 @@ func TestAccProjectGrantMember(t *testing.T) { resourceExample, exampleAttributes := test_utils.ReadExample(t, test_utils.Resources, frame.ResourceType) exampleProperty := test_utils.AttributeValue(t, project_grant_member.RolesVar, exampleAttributes).AsValueSlice()[0].AsString() grantIDProperty := test_utils.AttributeValue(t, project_grant_member.GrantIDVar, exampleAttributes).AsString() - projectDep, projectID := project_test_dep.Create(t, frame) + projectDep, projectID := project_test_dep.Create(t, frame, frame.UniqueResourcesID) userDep, userID := human_user_test_dep.Create(t, frame) _, grantedOrgID, _ := org_test_dep.Create(t, frame, "granting_org") grantID := project_grant_test_dep.Create(t, frame, projectID, grantedOrgID) diff --git a/zitadel/project_member/resource_test.go b/zitadel/project_member/resource_test.go index 2aaa04fc..627e2601 100644 --- a/zitadel/project_member/resource_test.go +++ b/zitadel/project_member/resource_test.go @@ -22,7 +22,7 @@ func TestAccProjectMember(t *testing.T) { frame := test_utils.NewOrgTestFrame(t, "zitadel_project_member") resourceExample, exampleAttributes := test_utils.ReadExample(t, test_utils.Resources, frame.ResourceType) exampleProperty := test_utils.AttributeValue(t, project_grant_member.RolesVar, exampleAttributes).AsValueSlice()[0].AsString() - projectDep, projectID := project_test_dep.Create(t, frame) + projectDep, projectID := project_test_dep.Create(t, frame, frame.UniqueResourcesID) userDep, userID := human_user_test_dep.Create(t, frame) test_utils.RunLifecyleTest( t, diff --git a/zitadel/project_role/resource_test.go b/zitadel/project_role/resource_test.go index 38366a20..944a54f8 100644 --- a/zitadel/project_role/resource_test.go +++ b/zitadel/project_role/resource_test.go @@ -21,7 +21,7 @@ func TestAccProjectRole(t *testing.T) { resourceExample, exampleAttributes := test_utils.ReadExample(t, test_utils.Resources, frame.ResourceType) exampleProperty := test_utils.AttributeValue(t, project_role.KeyVar, exampleAttributes).AsString() updatedProperty := "updatedProperty" - projectDep, projectID := project_test_dep.Create(t, frame) + projectDep, projectID := project_test_dep.Create(t, frame, frame.UniqueResourcesID) test_utils.RunLifecyleTest( t, frame.BaseTestFrame, diff --git a/zitadel/provider.go b/zitadel/provider.go index 1a2596cd..899e901e 100644 --- a/zitadel/provider.go +++ b/zitadel/provider.go @@ -208,12 +208,15 @@ func Provider() *schema.Provider { "zitadel_orgs": org.ListDatasources(), "zitadel_human_user": human_user.GetDatasource(), "zitadel_machine_user": machine_user.GetDatasource(), + "zitadel_machine_users": machine_user.ListDatasources(), "zitadel_project": project.GetDatasource(), "zitadel_projects": project.ListDatasources(), "zitadel_project_role": project_role.GetDatasource(), "zitadel_action": action.GetDatasource(), "zitadel_application_oidc": application_oidc.GetDatasource(), + "zitadel_application_oidcs": application_oidc.ListDatasources(), "zitadel_application_api": application_api.GetDatasource(), + "zitadel_application_apis": application_api.ListDatasources(), "zitadel_trigger_actions": trigger_actions.GetDatasource(), "zitadel_idp_github": idp_github.GetDatasource(), "zitadel_idp_github_es": idp_github_es.GetDatasource(), diff --git a/zitadel/user_grant/resource_test.go b/zitadel/user_grant/resource_test.go index 0d6a882e..547b0aac 100644 --- a/zitadel/user_grant/resource_test.go +++ b/zitadel/user_grant/resource_test.go @@ -21,7 +21,7 @@ func TestAccUserGrant(t *testing.T) { resourceExample, exampleAttributes := test_utils.ReadExample(t, test_utils.Resources, frame.ResourceType) exampleProperty := test_utils.AttributeValue(t, user_grant.RoleKeysVar, exampleAttributes).AsValueSlice()[0].AsString() updatedProperty := "updatedProperty" - projectDep, projectID := project_test_dep.Create(t, frame) + projectDep, projectID := project_test_dep.Create(t, frame, frame.UniqueResourcesID) project_role_test_dep.Create(t, frame, projectID, exampleProperty, updatedProperty) userDep, userID := human_user_test_dep.Create(t, frame) test_utils.RunLifecyleTest( From a86d20109cd208dc94531f7b95632f261f76ca62 Mon Sep 17 00:00:00 2001 From: Elio Bischof Date: Fri, 12 Jan 2024 23:53:15 +0100 Subject: [PATCH 3/8] test: update versions --- acceptance/docker-compose.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/acceptance/docker-compose.yaml b/acceptance/docker-compose.yaml index 05c4c956..7033d7bb 100644 --- a/acceptance/docker-compose.yaml +++ b/acceptance/docker-compose.yaml @@ -2,7 +2,7 @@ version: '3.8' services: db: - image: 'cockroachdb/cockroach:v22.2.2' + image: 'cockroachdb/cockroach:v23.1.13' command: 'start-single-node --insecure --http-addr :9090' healthcheck: test: ['CMD', 'curl', '-f', 'http://localhost:9090/health?ready=1'] @@ -15,7 +15,7 @@ services: zitadel: user: '${ZITADEL_DEV_UID}' - image: '${ZITADEL_IMAGE:-ghcr.io/zitadel/zitadel:v2.38.0}' + image: '${ZITADEL_IMAGE:-ghcr.io/zitadel/zitadel:v2.43.4}' command: 'start-from-init --masterkey "MasterkeyNeedsToHave32Characters" --tlsMode disabled --config /zitadel.yaml --steps /zitadel.yaml' ports: - "8080:8080" From 5b42932ff68bc8373a5199e8eab8fc436b2b4214 Mon Sep 17 00:00:00 2001 From: Elio Bischof Date: Sat, 13 Jan 2024 00:17:50 +0100 Subject: [PATCH 4/8] fix readiness check --- acceptance/docker-compose.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/acceptance/docker-compose.yaml b/acceptance/docker-compose.yaml index 7033d7bb..6900b1df 100644 --- a/acceptance/docker-compose.yaml +++ b/acceptance/docker-compose.yaml @@ -23,7 +23,7 @@ services: - ./keys:/keys - ./zitadel.yaml:/zitadel.yaml healthcheck: - test: ['CMD', '/app/zitadel', 'ready'] + test: ['CMD', '/app/zitadel', 'ready', "--config", "/zitadel.yaml"] interval: '2s' timeout: '30s' retries: 5 From 011371e6d6465e3e03ea54020a55e00ea760495f Mon Sep 17 00:00:00 2001 From: Elio Bischof Date: Sat, 13 Jan 2024 00:22:06 +0100 Subject: [PATCH 5/8] fix readiness check --- acceptance/docker-compose.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/acceptance/docker-compose.yaml b/acceptance/docker-compose.yaml index 05c4c956..6f93541f 100644 --- a/acceptance/docker-compose.yaml +++ b/acceptance/docker-compose.yaml @@ -23,7 +23,7 @@ services: - ./keys:/keys - ./zitadel.yaml:/zitadel.yaml healthcheck: - test: ['CMD', '/app/zitadel', 'ready'] + test: ['CMD', '/app/zitadel', 'ready', "--config", "/zitadel.yaml"] interval: '2s' timeout: '30s' retries: 5 From 10c368d3687a1285c722699e5da9d213f677bd52 Mon Sep 17 00:00:00 2001 From: Elio Bischof Date: Sat, 13 Jan 2024 01:29:25 +0100 Subject: [PATCH 6/8] test: reuse deps and read examples --- .../provider/data-sources/application_api.tf | 4 +-- .../provider/data-sources/application_apis.tf | 4 +-- .../provider/data-sources/application_oidc.tf | 4 +-- .../data-sources/application_oidcs.tf | 4 +-- .../provider/data-sources/machine_user.tf | 2 +- .../provider/data-sources/machine_users.tf | 2 +- examples/provider/data-sources/project.tf | 2 +- examples/provider/data-sources/projects.tf | 2 +- .../application_api_test_dep/dependency.go | 2 +- zitadel/application_api/datasource_test.go | 32 +++++++++---------- .../application_oidc_test_dep/dependency.go | 2 +- zitadel/application_oidc/datasource_test.go | 32 +++++++++---------- zitadel/helper/test_utils/datasourcetest.go | 4 ++- zitadel/helper/test_utils/dependency.go | 27 ---------------- zitadel/machine_user/datasource_test.go | 18 ++++++----- .../machine_user_test_dep/dependency.go | 2 +- zitadel/project/datasource_test.go | 18 ++++++----- .../project/project_test_dep/dependency.go | 3 +- 18 files changed, 69 insertions(+), 95 deletions(-) diff --git a/examples/provider/data-sources/application_api.tf b/examples/provider/data-sources/application_api.tf index 46460922..3daef683 100644 --- a/examples/provider/data-sources/application_api.tf +++ b/examples/provider/data-sources/application_api.tf @@ -1,5 +1,5 @@ data "zitadel_application_api" "default" { - org_id = "345678901234567890" - project_id = "234567890123456789" + org_id = data.zitadel_org.default.id + project_id = data.zitadel_project.default.id app_id = "123456789012345678" } diff --git a/examples/provider/data-sources/application_apis.tf b/examples/provider/data-sources/application_apis.tf index ad78793f..6b803703 100644 --- a/examples/provider/data-sources/application_apis.tf +++ b/examples/provider/data-sources/application_apis.tf @@ -1,6 +1,6 @@ data "zitadel_application_apis" "default" { - org_id = "123456789012345678" - project_id = "234567890123456789" + org_id = data.zitadel_org.default.id + project_id = data.zitadel_project.default.id name = "example-name" name_method = "TEXT_QUERY_METHOD_CONTAINS_IGNORE_CASE" } diff --git a/examples/provider/data-sources/application_oidc.tf b/examples/provider/data-sources/application_oidc.tf index f64ae032..63668035 100644 --- a/examples/provider/data-sources/application_oidc.tf +++ b/examples/provider/data-sources/application_oidc.tf @@ -1,5 +1,5 @@ data "zitadel_application_oidc" "default" { - org_id = "345678901234567890" - project_id = "234567890123456789" + org_id = data.zitadel_org.default.id + project_id = data.zitadel_project.default.id app_id = "123456789012345678" } diff --git a/examples/provider/data-sources/application_oidcs.tf b/examples/provider/data-sources/application_oidcs.tf index e6a2ec19..f1b7c3b3 100644 --- a/examples/provider/data-sources/application_oidcs.tf +++ b/examples/provider/data-sources/application_oidcs.tf @@ -1,6 +1,6 @@ data "zitadel_application_oidcs" "default" { - org_id = "123456789012345678" - project_id = "234567890123456789" + org_id = data.zitadel_org.default.id + project_id = data.zitadel_project.default.id name = "example-name" name_method = "TEXT_QUERY_METHOD_CONTAINS_IGNORE_CASE" } diff --git a/examples/provider/data-sources/machine_user.tf b/examples/provider/data-sources/machine_user.tf index e1cf9fa4..b7306aa7 100644 --- a/examples/provider/data-sources/machine_user.tf +++ b/examples/provider/data-sources/machine_user.tf @@ -1,4 +1,4 @@ data "zitadel_machine_user" "default" { - org_id = "234567890123456789" + org_id = data.zitadel_org.default.id user_id = "123456789012345678" } diff --git a/examples/provider/data-sources/machine_users.tf b/examples/provider/data-sources/machine_users.tf index d91c29dd..c6a76f98 100644 --- a/examples/provider/data-sources/machine_users.tf +++ b/examples/provider/data-sources/machine_users.tf @@ -1,5 +1,5 @@ data "zitadel_machine_users" "default" { - org_id = "123456789012345678" + org_id = data.zitadel_org.default.id user_name = "example-name" user_name_method = "TEXT_QUERY_METHOD_CONTAINS_IGNORE_CASE" } diff --git a/examples/provider/data-sources/project.tf b/examples/provider/data-sources/project.tf index 44598dc0..a1bee456 100644 --- a/examples/provider/data-sources/project.tf +++ b/examples/provider/data-sources/project.tf @@ -1,4 +1,4 @@ data "zitadel_project" "default" { - org_id = "234567890123456789" + org_id = data.zitadel_org.default.id project_id = "123456789012345678" } diff --git a/examples/provider/data-sources/projects.tf b/examples/provider/data-sources/projects.tf index 166a5364..e754488a 100644 --- a/examples/provider/data-sources/projects.tf +++ b/examples/provider/data-sources/projects.tf @@ -1,5 +1,5 @@ data "zitadel_projects" "default" { - org_id = "123456789012345678" + org_id = data.zitadel_org.default.id name = "example-name" name_method = "TEXT_QUERY_METHOD_CONTAINS_IGNORE_CASE" } diff --git a/zitadel/application_api/application_api_test_dep/dependency.go b/zitadel/application_api/application_api_test_dep/dependency.go index aad6a675..856f860f 100644 --- a/zitadel/application_api/application_api_test_dep/dependency.go +++ b/zitadel/application_api/application_api_test_dep/dependency.go @@ -11,7 +11,7 @@ import ( ) func Create(t *testing.T, frame *test_utils.OrgTestFrame, projectID, name string) (string, string) { - return test_utils.CreateProjectDefaultDependency(t, "zitadel_application_api", frame.OrgID, application_api.ProjectIDVar, projectID, application_api.AppIDVar, func() (string, error) { + return test_utils.CreateDefaultDependency(t, "zitadel_application_api", application_api.AppIDVar, func() (string, error) { apiApp, err := frame.AddAPIApp(frame, &management.AddAPIAppRequest{ ProjectId: projectID, Name: name, diff --git a/zitadel/application_api/datasource_test.go b/zitadel/application_api/datasource_test.go index ce358015..a32c6863 100644 --- a/zitadel/application_api/datasource_test.go +++ b/zitadel/application_api/datasource_test.go @@ -11,20 +11,24 @@ import ( "github.com/zitadel/terraform-provider-zitadel/zitadel/application_api" "github.com/zitadel/terraform-provider-zitadel/zitadel/application_api/application_api_test_dep" - "github.com/zitadel/terraform-provider-zitadel/zitadel/helper" "github.com/zitadel/terraform-provider-zitadel/zitadel/helper/test_utils" "github.com/zitadel/terraform-provider-zitadel/zitadel/project/project_test_dep" ) func TestAccApplicationAPIDatasource_ID(t *testing.T) { - frame := test_utils.NewOrgTestFrame(t, "zitadel_application_api") + datasourceName := "zitadel_application_api" + frame := test_utils.NewOrgTestFrame(t, datasourceName) + config, attributes := test_utils.ReadExample(t, test_utils.Datasources, datasourceName) + exampleID := test_utils.AttributeValue(t, application_api.AppIDVar, attributes).AsString() + projectDep, projectID := project_test_dep.Create(t, frame, frame.UniqueResourcesID) appName := "application_api_datasource_" + frame.UniqueResourcesID - _, projectID := project_test_dep.Create(t, frame, frame.UniqueResourcesID) - appDep, appID := application_api_test_dep.Create(t, frame, projectID, appName) + _, appID := application_api_test_dep.Create(t, frame, projectID, appName) + config = strings.Replace(config, exampleID, appID, 1) test_utils.RunDatasourceTest( t, frame.BaseTestFrame, - appDep, + config, + []string{frame.AsOrgDefaultDependency, projectDep}, nil, map[string]string{ "org_id": frame.OrgID, @@ -40,21 +44,18 @@ func TestAccApplicationAPIsDatasources_ID_Name_Match(t *testing.T) { frame := test_utils.NewOrgTestFrame(t, datasourceName) config, attributes := test_utils.ReadExample(t, test_utils.Datasources, datasourceName) exampleName := test_utils.AttributeValue(t, application_api.NameVar, attributes).AsString() - exampleProject := test_utils.AttributeValue(t, application_api.ProjectIDVar, attributes).AsString() - exampleOrg := test_utils.AttributeValue(t, helper.OrgIDVar, attributes).AsString() appName := fmt.Sprintf("%s-%s", exampleName, frame.UniqueResourcesID) // for-each is not supported in acceptance tests, so we cut the example down to the first block // https://github.com/hashicorp/terraform-plugin-sdk/issues/536 config = strings.Join(strings.Split(config, "\n")[0:6], "\n") config = strings.Replace(config, exampleName, appName, 1) - config = strings.Replace(config, exampleOrg, frame.OrgID, 1) - _, projectID := project_test_dep.Create(t, frame, frame.UniqueResourcesID) - config = strings.Replace(config, exampleProject, projectID, 1) + projectDep, projectID := project_test_dep.Create(t, frame, frame.UniqueResourcesID) _, appID := application_api_test_dep.Create(t, frame, projectID, appName) test_utils.RunDatasourceTest( t, frame.BaseTestFrame, config, + []string{frame.AsOrgDefaultDependency, projectDep}, checkRemoteDatasourceProperty(frame, projectID, appID)(appName), map[string]string{ "app_ids.0": appID, @@ -68,22 +69,19 @@ func TestAccApplicationAPIsDatasources_ID_Name_Mismatch(t *testing.T) { frame := test_utils.NewOrgTestFrame(t, datasourceName) config, attributes := test_utils.ReadExample(t, test_utils.Datasources, datasourceName) exampleName := test_utils.AttributeValue(t, application_api.NameVar, attributes).AsString() - exampleProject := test_utils.AttributeValue(t, application_api.ProjectIDVar, attributes).AsString() - exampleOrg := test_utils.AttributeValue(t, helper.OrgIDVar, attributes).AsString() appName := fmt.Sprintf("%s-%s", exampleName, frame.UniqueResourcesID) // for-each is not supported in acceptance tests, so we cut the example down to the first block // https://github.com/hashicorp/terraform-plugin-sdk/issues/536 config = strings.Join(strings.Split(config, "\n")[0:6], "\n") config = strings.Replace(config, exampleName, "mismatch", 1) - config = strings.Replace(config, exampleOrg, frame.OrgID, 1) - _, projectID := project_test_dep.Create(t, frame, frame.UniqueResourcesID) - config = strings.Replace(config, exampleProject, projectID, 1) - _, userID := application_api_test_dep.Create(t, frame, projectID, appName) + projectDep, projectID := project_test_dep.Create(t, frame, frame.UniqueResourcesID) + _, appID := application_api_test_dep.Create(t, frame, projectID, appName) test_utils.RunDatasourceTest( t, frame.BaseTestFrame, config, - checkRemoteDatasourceProperty(frame, projectID, userID)(appName), + []string{frame.AsOrgDefaultDependency, projectDep}, + checkRemoteDatasourceProperty(frame, projectID, appID)(appName), map[string]string{ "app_ids.#": "0", }, diff --git a/zitadel/application_oidc/application_oidc_test_dep/dependency.go b/zitadel/application_oidc/application_oidc_test_dep/dependency.go index 4e0df1d3..672410c8 100644 --- a/zitadel/application_oidc/application_oidc_test_dep/dependency.go +++ b/zitadel/application_oidc/application_oidc_test_dep/dependency.go @@ -10,7 +10,7 @@ import ( ) func Create(t *testing.T, frame *test_utils.OrgTestFrame, projectID, name string) (string, string) { - return test_utils.CreateProjectDefaultDependency(t, "zitadel_application_oidc", frame.OrgID, application_oidc.ProjectIDVar, projectID, application_oidc.AppIDVar, func() (string, error) { + return test_utils.CreateDefaultDependency(t, "zitadel_application_oidc", application_oidc.AppIDVar, func() (string, error) { oidcApp, err := frame.AddOIDCApp(frame, &management.AddOIDCAppRequest{ ProjectId: projectID, Name: name, diff --git a/zitadel/application_oidc/datasource_test.go b/zitadel/application_oidc/datasource_test.go index a1be8726..5e188acc 100644 --- a/zitadel/application_oidc/datasource_test.go +++ b/zitadel/application_oidc/datasource_test.go @@ -11,20 +11,24 @@ import ( "github.com/zitadel/terraform-provider-zitadel/zitadel/application_oidc" "github.com/zitadel/terraform-provider-zitadel/zitadel/application_oidc/application_oidc_test_dep" - "github.com/zitadel/terraform-provider-zitadel/zitadel/helper" "github.com/zitadel/terraform-provider-zitadel/zitadel/helper/test_utils" "github.com/zitadel/terraform-provider-zitadel/zitadel/project/project_test_dep" ) func TestAccApplicationOIDCDatasource_ID(t *testing.T) { - frame := test_utils.NewOrgTestFrame(t, "zitadel_application_oidc") + datasourceName := "zitadel_application_oidc" + frame := test_utils.NewOrgTestFrame(t, datasourceName) + config, attributes := test_utils.ReadExample(t, test_utils.Datasources, datasourceName) + exampleID := test_utils.AttributeValue(t, application_oidc.AppIDVar, attributes).AsString() + projectDep, projectID := project_test_dep.Create(t, frame, frame.UniqueResourcesID) appName := "application_oidc_datasource_" + frame.UniqueResourcesID - _, projectID := project_test_dep.Create(t, frame, frame.UniqueResourcesID) - appDep, appID := application_oidc_test_dep.Create(t, frame, projectID, appName) + _, appID := application_oidc_test_dep.Create(t, frame, projectID, appName) + config = strings.Replace(config, exampleID, appID, 1) test_utils.RunDatasourceTest( t, frame.BaseTestFrame, - appDep, + config, + []string{frame.AsOrgDefaultDependency, projectDep}, nil, map[string]string{ "org_id": frame.OrgID, @@ -40,21 +44,18 @@ func TestAccApplicationOIDCsDatasources_ID_Name_Match(t *testing.T) { frame := test_utils.NewOrgTestFrame(t, datasourceName) config, attributes := test_utils.ReadExample(t, test_utils.Datasources, datasourceName) exampleName := test_utils.AttributeValue(t, application_oidc.NameVar, attributes).AsString() - exampleProject := test_utils.AttributeValue(t, application_oidc.ProjectIDVar, attributes).AsString() - exampleOrg := test_utils.AttributeValue(t, helper.OrgIDVar, attributes).AsString() appName := fmt.Sprintf("%s-%s", exampleName, frame.UniqueResourcesID) // for-each is not supported in acceptance tests, so we cut the example down to the first block // https://github.com/hashicorp/terraform-plugin-sdk/issues/536 config = strings.Join(strings.Split(config, "\n")[0:6], "\n") config = strings.Replace(config, exampleName, appName, 1) - config = strings.Replace(config, exampleOrg, frame.OrgID, 1) - _, projectID := project_test_dep.Create(t, frame, frame.UniqueResourcesID) - config = strings.Replace(config, exampleProject, projectID, 1) + projectDep, projectID := project_test_dep.Create(t, frame, frame.UniqueResourcesID) _, appID := application_oidc_test_dep.Create(t, frame, projectID, appName) test_utils.RunDatasourceTest( t, frame.BaseTestFrame, config, + []string{frame.AsOrgDefaultDependency, projectDep}, checkRemoteDatasourceProperty(frame, projectID, appID)(appName), map[string]string{ "app_ids.0": appID, @@ -68,22 +69,19 @@ func TestAccApplicationOIDCsDatasources_ID_Name_Mismatch(t *testing.T) { frame := test_utils.NewOrgTestFrame(t, datasourceName) config, attributes := test_utils.ReadExample(t, test_utils.Datasources, datasourceName) exampleName := test_utils.AttributeValue(t, application_oidc.NameVar, attributes).AsString() - exampleProject := test_utils.AttributeValue(t, application_oidc.ProjectIDVar, attributes).AsString() - exampleOrg := test_utils.AttributeValue(t, helper.OrgIDVar, attributes).AsString() appName := fmt.Sprintf("%s-%s", exampleName, frame.UniqueResourcesID) // for-each is not supported in acceptance tests, so we cut the example down to the first block // https://github.com/hashicorp/terraform-plugin-sdk/issues/536 config = strings.Join(strings.Split(config, "\n")[0:6], "\n") config = strings.Replace(config, exampleName, "mismatch", 1) - config = strings.Replace(config, exampleOrg, frame.OrgID, 1) - _, projectID := project_test_dep.Create(t, frame, frame.UniqueResourcesID) - config = strings.Replace(config, exampleProject, projectID, 1) - _, userID := application_oidc_test_dep.Create(t, frame, projectID, appName) + projectDep, projectID := project_test_dep.Create(t, frame, frame.UniqueResourcesID) + _, appID := application_oidc_test_dep.Create(t, frame, projectID, appName) test_utils.RunDatasourceTest( t, frame.BaseTestFrame, config, - checkRemoteDatasourceProperty(frame, projectID, userID)(appName), + []string{frame.AsOrgDefaultDependency, projectDep}, + checkRemoteDatasourceProperty(frame, projectID, appID)(appName), map[string]string{ "app_ids.#": "0", }, diff --git a/zitadel/helper/test_utils/datasourcetest.go b/zitadel/helper/test_utils/datasourcetest.go index be356fc0..5ff8ca01 100644 --- a/zitadel/helper/test_utils/datasourcetest.go +++ b/zitadel/helper/test_utils/datasourcetest.go @@ -2,6 +2,7 @@ package test_utils import ( "fmt" + "strings" "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" @@ -11,6 +12,7 @@ func RunDatasourceTest( t *testing.T, frame BaseTestFrame, config string, + dependencies []string, awaitRemoteResource resource.TestCheckFunc, expectProperties map[string]string, ) { @@ -23,7 +25,7 @@ func RunDatasourceTest( } resource.ParallelTest(t, resource.TestCase{ Steps: []resource.TestStep{{ - Config: fmt.Sprintf("%s\n%s", frame.ProviderSnippet, config), + Config: fmt.Sprintf("%s\n%s\n%s", frame.ProviderSnippet, strings.Join(dependencies, "\n"), config), Check: resource.ComposeAggregateTestCheckFunc(checks...), }}, ProtoV6ProviderFactories: frame.v6ProviderFactories, diff --git a/zitadel/helper/test_utils/dependency.go b/zitadel/helper/test_utils/dependency.go index c9b38e22..df934b66 100644 --- a/zitadel/helper/test_utils/dependency.go +++ b/zitadel/helper/test_utils/dependency.go @@ -3,8 +3,6 @@ package test_utils import ( "strings" "testing" - - "github.com/zitadel/terraform-provider-zitadel/zitadel/helper" ) func CreateDefaultDependency(t *testing.T, datasourceType string, idField string, newDependencyID func() (string, error)) (string, string) { @@ -15,28 +13,3 @@ func CreateDefaultDependency(t *testing.T, datasourceType string, idField string } return strings.Replace(datasourceExample, AttributeValue(t, idField, datasourceExampleAttributes).AsString(), dependencyID, 1), dependencyID } - -func CreateOrgDefaultDependency(t *testing.T, datasourceType string, orgID string, idField string, newDependencyID func() (string, error)) (string, string) { - datasourceExample, datasourceExampleAttributes := ReadExample(t, Datasources, datasourceType) - dependencyID, err := newDependencyID() - if err != nil { - t.Fatalf("failed to create dependency for %s: %v", datasourceType, err) - } - // org exampleID does always have to be different then the idField value - dep := strings.Replace(datasourceExample, AttributeValue(t, idField, datasourceExampleAttributes).AsString(), dependencyID, 1) - // replace the exmaple OrgID with the given org - return strings.Replace(dep, AttributeValue(t, helper.OrgIDVar, datasourceExampleAttributes).AsString(), orgID, 1), dependencyID -} - -func CreateProjectDefaultDependency(t *testing.T, datasourceType string, orgID string, projectField string, projectID string, idField string, newDependencyID func() (string, error)) (string, string) { - datasourceExample, datasourceExampleAttributes := ReadExample(t, Datasources, datasourceType) - dependencyID, err := newDependencyID() - if err != nil { - t.Fatalf("failed to create dependency for %s: %v", datasourceType, err) - } - // org exampleID does always have to be different then the idField value - dep := strings.Replace(datasourceExample, AttributeValue(t, idField, datasourceExampleAttributes).AsString(), dependencyID, 1) - dep = strings.Replace(dep, AttributeValue(t, projectField, datasourceExampleAttributes).AsString(), projectID, 1) - // replace the exmaple OrgID with the given org - return strings.Replace(dep, AttributeValue(t, helper.OrgIDVar, datasourceExampleAttributes).AsString(), orgID, 1), dependencyID -} diff --git a/zitadel/machine_user/datasource_test.go b/zitadel/machine_user/datasource_test.go index 80574b9e..3a939a57 100644 --- a/zitadel/machine_user/datasource_test.go +++ b/zitadel/machine_user/datasource_test.go @@ -9,20 +9,24 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" "github.com/zitadel/zitadel-go/v2/pkg/client/zitadel/management" - "github.com/zitadel/terraform-provider-zitadel/zitadel/helper" "github.com/zitadel/terraform-provider-zitadel/zitadel/helper/test_utils" "github.com/zitadel/terraform-provider-zitadel/zitadel/machine_user" "github.com/zitadel/terraform-provider-zitadel/zitadel/machine_user/machine_user_test_dep" ) func TestAccMachineUserDatasource_ID(t *testing.T) { - frame := test_utils.NewOrgTestFrame(t, "zitadel_machine_user") + datasourceName := "zitadel_machine_user" + frame := test_utils.NewOrgTestFrame(t, datasourceName) + config, attributes := test_utils.ReadExample(t, test_utils.Datasources, datasourceName) + exampleID := test_utils.AttributeValue(t, machine_user.UserIDVar, attributes).AsString() userName := "machine_user_datasource_" + frame.UniqueResourcesID - userDep, userID := machine_user_test_dep.Create(t, frame, userName) + _, userID := machine_user_test_dep.Create(t, frame, userName) + config = strings.Replace(config, exampleID, userID, 1) test_utils.RunDatasourceTest( t, frame.BaseTestFrame, - userDep, + config, + []string{frame.AsOrgDefaultDependency}, nil, map[string]string{ "org_id": frame.OrgID, @@ -37,18 +41,17 @@ func TestAccMachineUsersDatasources_ID_Name_Match(t *testing.T) { frame := test_utils.NewOrgTestFrame(t, datasourceName) config, attributes := test_utils.ReadExample(t, test_utils.Datasources, datasourceName) exampleName := test_utils.AttributeValue(t, machine_user.UserNameVar, attributes).AsString() - exampleOrg := test_utils.AttributeValue(t, helper.OrgIDVar, attributes).AsString() userName := fmt.Sprintf("%s-%s", exampleName, frame.UniqueResourcesID) // for-each is not supported in acceptance tests, so we cut the example down to the first block // https://github.com/hashicorp/terraform-plugin-sdk/issues/536 config = strings.Join(strings.Split(config, "\n")[0:5], "\n") config = strings.Replace(config, exampleName, userName, 1) - config = strings.Replace(config, exampleOrg, frame.OrgID, 1) _, userID := machine_user_test_dep.Create(t, frame, userName) test_utils.RunDatasourceTest( t, frame.BaseTestFrame, config, + []string{frame.AsOrgDefaultDependency}, checkRemoteDatasourceProperty(frame, userID)(userName), map[string]string{ "user_ids.0": userID, @@ -62,18 +65,17 @@ func TestAccMachineUsersDatasources_ID_Name_Mismatch(t *testing.T) { frame := test_utils.NewOrgTestFrame(t, datasourceName) config, attributes := test_utils.ReadExample(t, test_utils.Datasources, datasourceName) exampleName := test_utils.AttributeValue(t, machine_user.UserNameVar, attributes).AsString() - exampleOrg := test_utils.AttributeValue(t, helper.OrgIDVar, attributes).AsString() userName := fmt.Sprintf("%s-%s", exampleName, frame.UniqueResourcesID) // for-each is not supported in acceptance tests, so we cut the example down to the first block // https://github.com/hashicorp/terraform-plugin-sdk/issues/536 config = strings.Join(strings.Split(config, "\n")[0:5], "\n") config = strings.Replace(config, exampleName, "mismatch", 1) - config = strings.Replace(config, exampleOrg, frame.OrgID, 1) _, userID := machine_user_test_dep.Create(t, frame, userName) test_utils.RunDatasourceTest( t, frame.BaseTestFrame, config, + []string{frame.AsOrgDefaultDependency}, checkRemoteDatasourceProperty(frame, userID)(userName), map[string]string{ "user_ids.#": "0", diff --git a/zitadel/machine_user/machine_user_test_dep/dependency.go b/zitadel/machine_user/machine_user_test_dep/dependency.go index 41433baf..f29d485e 100644 --- a/zitadel/machine_user/machine_user_test_dep/dependency.go +++ b/zitadel/machine_user/machine_user_test_dep/dependency.go @@ -10,7 +10,7 @@ import ( ) func Create(t *testing.T, frame *test_utils.OrgTestFrame, username string) (string, string) { - return test_utils.CreateOrgDefaultDependency(t, "zitadel_machine_user", frame.OrgID, machine_user.UserIDVar, func() (string, error) { + return test_utils.CreateDefaultDependency(t, "zitadel_machine_user", machine_user.UserIDVar, func() (string, error) { user, err := frame.AddMachineUser(frame, &management.AddMachineUserRequest{ UserName: username, Name: "Don't care", diff --git a/zitadel/project/datasource_test.go b/zitadel/project/datasource_test.go index 0d21d938..58932f6e 100644 --- a/zitadel/project/datasource_test.go +++ b/zitadel/project/datasource_test.go @@ -9,20 +9,24 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" "github.com/zitadel/zitadel-go/v2/pkg/client/zitadel/management" - "github.com/zitadel/terraform-provider-zitadel/zitadel/helper" "github.com/zitadel/terraform-provider-zitadel/zitadel/helper/test_utils" "github.com/zitadel/terraform-provider-zitadel/zitadel/project" "github.com/zitadel/terraform-provider-zitadel/zitadel/project/project_test_dep" ) func TestAccProjectDatasource_ID(t *testing.T) { - frame := test_utils.NewOrgTestFrame(t, "zitadel_project") + datasourceName := "zitadel_project" + frame := test_utils.NewOrgTestFrame(t, datasourceName) + config, attributes := test_utils.ReadExample(t, test_utils.Datasources, datasourceName) + exampleID := test_utils.AttributeValue(t, project.ProjectIDVar, attributes).AsString() projectName := "project_datasource_" + frame.UniqueResourcesID - projectDep, projectID := project_test_dep.Create(t, frame, projectName) + _, projectID := project_test_dep.Create(t, frame, projectName) + config = strings.Replace(config, exampleID, projectID, 1) test_utils.RunDatasourceTest( t, frame.BaseTestFrame, - projectDep, + config, + []string{frame.AsOrgDefaultDependency}, nil, map[string]string{ "org_id": frame.OrgID, @@ -37,18 +41,17 @@ func TestAccProjectsDatasources_ID_Name_Match(t *testing.T) { frame := test_utils.NewOrgTestFrame(t, datasourceName) config, attributes := test_utils.ReadExample(t, test_utils.Datasources, datasourceName) exampleName := test_utils.AttributeValue(t, project.NameVar, attributes).AsString() - exampleOrg := test_utils.AttributeValue(t, helper.OrgIDVar, attributes).AsString() projectName := fmt.Sprintf("%s-%s", exampleName, frame.UniqueResourcesID) // for-each is not supported in acceptance tests, so we cut the example down to the first block // https://github.com/hashicorp/terraform-plugin-sdk/issues/536 config = strings.Join(strings.Split(config, "\n")[0:5], "\n") config = strings.Replace(config, exampleName, projectName, 1) - config = strings.Replace(config, exampleOrg, frame.OrgID, 1) _, projectID := project_test_dep.Create(t, frame, projectName) test_utils.RunDatasourceTest( t, frame.BaseTestFrame, config, + []string{frame.AsOrgDefaultDependency}, checkRemoteDatasourceProperty(frame, projectID)(projectName), map[string]string{ "project_ids.0": projectID, @@ -62,18 +65,17 @@ func TestAccProjectsDatasources_ID_Name_Mismatch(t *testing.T) { frame := test_utils.NewOrgTestFrame(t, datasourceName) config, attributes := test_utils.ReadExample(t, test_utils.Datasources, datasourceName) exampleName := test_utils.AttributeValue(t, project.NameVar, attributes).AsString() - exampleOrg := test_utils.AttributeValue(t, helper.OrgIDVar, attributes).AsString() projectName := fmt.Sprintf("%s-%s", exampleName, frame.UniqueResourcesID) // for-each is not supported in acceptance tests, so we cut the example down to the first block // https://github.com/hashicorp/terraform-plugin-sdk/issues/536 config = strings.Join(strings.Split(config, "\n")[0:5], "\n") config = strings.Replace(config, exampleName, "mismatch", 1) - config = strings.Replace(config, exampleOrg, frame.OrgID, 1) _, projectID := project_test_dep.Create(t, frame, projectName) test_utils.RunDatasourceTest( t, frame.BaseTestFrame, config, + []string{frame.AsOrgDefaultDependency}, checkRemoteDatasourceProperty(frame, projectID)(projectName), map[string]string{ "project_ids.#": "0", diff --git a/zitadel/project/project_test_dep/dependency.go b/zitadel/project/project_test_dep/dependency.go index 47b15672..21155171 100644 --- a/zitadel/project/project_test_dep/dependency.go +++ b/zitadel/project/project_test_dep/dependency.go @@ -10,9 +10,8 @@ import ( ) func Create(t *testing.T, frame *test_utils.OrgTestFrame, name string) (string, string) { - return test_utils.CreateOrgDefaultDependency(t, + return test_utils.CreateDefaultDependency(t, "zitadel_project", - frame.OrgID, project.ProjectIDVar, func() (string, error) { p, err := frame.AddProject(frame, &management.AddProjectRequest{Name: name}) From d3f9249e8629b04b2446158fc653155116772245 Mon Sep 17 00:00:00 2001 From: Elio Bischof Date: Sat, 13 Jan 2024 13:18:58 +0100 Subject: [PATCH 7/8] test: fix org datasources tests --- zitadel/org/datasource_test.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/zitadel/org/datasource_test.go b/zitadel/org/datasource_test.go index d40092af..a9ccf5aa 100644 --- a/zitadel/org/datasource_test.go +++ b/zitadel/org/datasource_test.go @@ -12,13 +12,18 @@ import ( ) func TestAccOrgDatasource_ID(t *testing.T) { - frame := test_utils.NewOrgTestFrame(t, "zitadel_org") + datasourceName := "zitadel_org" + frame := test_utils.NewOrgTestFrame(t, datasourceName) + config, attributes := test_utils.ReadExample(t, test_utils.Datasources, datasourceName) + exampleID := test_utils.AttributeValue(t, org.OrgIDVar, attributes).AsString() orgName := "org_datasource_" + frame.UniqueResourcesID otherFrame := frame.AnotherOrg(t, orgName) + config = strings.Replace(config, exampleID, otherFrame.OrgID, 1) test_utils.RunDatasourceTest( t, otherFrame.BaseTestFrame, - otherFrame.AsOrgDefaultDependency, + config, + nil, nil, map[string]string{ "id": otherFrame.OrgID, @@ -45,6 +50,7 @@ func TestAccOrgsDatasources_ID_Name_Match(t *testing.T) { t, otherFrame.BaseTestFrame, config, + nil, checkRemoteProperty(otherFrame, idFromFrame(otherFrame))(orgName), map[string]string{ "ids.0": otherFrame.OrgID, @@ -63,6 +69,7 @@ func TestAccOrgsDatasources_ID_Name_Mismatch(t *testing.T) { t, otherFrame.BaseTestFrame, config, + nil, checkRemoteProperty(otherFrame, idFromFrame(otherFrame))(orgName), map[string]string{"ids.#": "0"}, ) From a81d5537c1e8af3407439a889b7e9c3ffc277045 Mon Sep 17 00:00:00 2001 From: Stefan Benz Date: Tue, 16 Jan 2024 11:26:42 +0100 Subject: [PATCH 8/8] docs: generate docs for examples --- docs/data-sources/application_api.md | 4 ++-- docs/data-sources/application_apis.md | 4 ++-- docs/data-sources/application_oidc.md | 4 ++-- docs/data-sources/application_oidcs.md | 4 ++-- docs/data-sources/machine_user.md | 2 +- docs/data-sources/machine_users.md | 2 +- docs/data-sources/project.md | 2 +- docs/data-sources/projects.md | 2 +- 8 files changed, 12 insertions(+), 12 deletions(-) diff --git a/docs/data-sources/application_api.md b/docs/data-sources/application_api.md index acdebf38..2656445a 100644 --- a/docs/data-sources/application_api.md +++ b/docs/data-sources/application_api.md @@ -13,8 +13,8 @@ Datasource representing an API application belonging to a project, with all conf ```terraform data "zitadel_application_api" "default" { - org_id = "345678901234567890" - project_id = "234567890123456789" + org_id = data.zitadel_org.default.id + project_id = data.zitadel_project.default.id app_id = "123456789012345678" } ``` diff --git a/docs/data-sources/application_apis.md b/docs/data-sources/application_apis.md index 7b4a9793..0d5c3c69 100644 --- a/docs/data-sources/application_apis.md +++ b/docs/data-sources/application_apis.md @@ -13,8 +13,8 @@ Datasource representing an API application belonging to a project, with all conf ```terraform data "zitadel_application_apis" "default" { - org_id = "123456789012345678" - project_id = "234567890123456789" + org_id = data.zitadel_org.default.id + project_id = data.zitadel_project.default.id name = "example-name" name_method = "TEXT_QUERY_METHOD_CONTAINS_IGNORE_CASE" } diff --git a/docs/data-sources/application_oidc.md b/docs/data-sources/application_oidc.md index bc63d9f0..f2fae7c1 100644 --- a/docs/data-sources/application_oidc.md +++ b/docs/data-sources/application_oidc.md @@ -13,8 +13,8 @@ Datasource representing an OIDC application belonging to a project, with all con ```terraform data "zitadel_application_oidc" "default" { - org_id = "345678901234567890" - project_id = "234567890123456789" + org_id = data.zitadel_org.default.id + project_id = data.zitadel_project.default.id app_id = "123456789012345678" } ``` diff --git a/docs/data-sources/application_oidcs.md b/docs/data-sources/application_oidcs.md index fe1f8ba7..3501ac9e 100644 --- a/docs/data-sources/application_oidcs.md +++ b/docs/data-sources/application_oidcs.md @@ -13,8 +13,8 @@ Datasource representing an OIDC application belonging to a project, with all con ```terraform data "zitadel_application_oidcs" "default" { - org_id = "123456789012345678" - project_id = "234567890123456789" + org_id = data.zitadel_org.default.id + project_id = data.zitadel_project.default.id name = "example-name" name_method = "TEXT_QUERY_METHOD_CONTAINS_IGNORE_CASE" } diff --git a/docs/data-sources/machine_user.md b/docs/data-sources/machine_user.md index 2528f28e..9c689b9a 100644 --- a/docs/data-sources/machine_user.md +++ b/docs/data-sources/machine_user.md @@ -13,7 +13,7 @@ Datasource representing a serviceaccount situated under an organization, which t ```terraform data "zitadel_machine_user" "default" { - org_id = "234567890123456789" + org_id = data.zitadel_org.default.id user_id = "123456789012345678" } ``` diff --git a/docs/data-sources/machine_users.md b/docs/data-sources/machine_users.md index fa4d8b66..6047bc0d 100644 --- a/docs/data-sources/machine_users.md +++ b/docs/data-sources/machine_users.md @@ -13,7 +13,7 @@ Datasource representing a serviceaccount situated under an organization, which t ```terraform data "zitadel_machine_users" "default" { - org_id = "123456789012345678" + org_id = data.zitadel_org.default.id user_name = "example-name" user_name_method = "TEXT_QUERY_METHOD_CONTAINS_IGNORE_CASE" } diff --git a/docs/data-sources/project.md b/docs/data-sources/project.md index 65270dc7..4a9c66b3 100644 --- a/docs/data-sources/project.md +++ b/docs/data-sources/project.md @@ -13,7 +13,7 @@ Datasource representing the project, which can then be granted to different orga ```terraform data "zitadel_project" "default" { - org_id = "234567890123456789" + org_id = data.zitadel_org.default.id project_id = "123456789012345678" } ``` diff --git a/docs/data-sources/projects.md b/docs/data-sources/projects.md index 6c869018..fc098e77 100644 --- a/docs/data-sources/projects.md +++ b/docs/data-sources/projects.md @@ -13,7 +13,7 @@ Datasource representing the project, which can then be granted to different orga ```terraform data "zitadel_projects" "default" { - org_id = "123456789012345678" + org_id = data.zitadel_org.default.id name = "example-name" name_method = "TEXT_QUERY_METHOD_CONTAINS_IGNORE_CASE" }