From 89b6981e3b828f613619ee39490af9bdfb0a70ee Mon Sep 17 00:00:00 2001 From: Stefan Benz Date: Tue, 12 Sep 2023 17:13:46 +0200 Subject: [PATCH 1/7] fix: change zitadel clients to singleton pattern for better token handling --- zitadel/action/funcs.go | 16 ++-- zitadel/application_api/funcs.go | 18 ++--- zitadel/application_key/funcs.go | 16 ++-- zitadel/application_oidc/funcs.go | 18 ++--- zitadel/domain/funcs.go | 22 +++--- .../domain_claimed_message_text/resource.go | 16 ++-- zitadel/domain_policy/funcs.go | 5 +- zitadel/helper/client.go | 78 ++++++++++++++----- zitadel/human_user/funcs.go | 22 +++--- zitadel/init_message_text/resource.go | 16 ++-- zitadel/label_policy/funcs.go | 22 +++--- zitadel/lockout_policy/funcs.go | 19 ++--- zitadel/login_policy/funcs.go | 37 ++++----- zitadel/login_texts/resource.go | 16 ++-- zitadel/machine_key/funcs.go | 13 ++-- zitadel/machine_user/funcs.go | 18 ++--- zitadel/notification_policy/funcs.go | 18 ++--- zitadel/org/funcs.go | 6 +- zitadel/org_idp_azure_ad/funcs.go | 12 +-- zitadel/org_idp_github/funcs.go | 12 +-- zitadel/org_idp_github_es/funcs.go | 12 +-- zitadel/org_idp_gitlab/funcs.go | 12 +-- zitadel/org_idp_gitlab_self_hosted/funcs.go | 12 +-- zitadel/org_idp_google/funcs.go | 12 +-- zitadel/org_idp_jwt/funcs.go | 18 ++--- zitadel/org_idp_ldap/funcs.go | 12 +-- zitadel/org_idp_oidc/funcs.go | 12 +-- zitadel/org_idp_utils/funcs.go | 4 +- zitadel/org_member/funcs.go | 22 ++---- .../password_change_message_text/resource.go | 16 ++-- zitadel/password_complexity_policy/funcs.go | 19 ++--- .../password_reset_message_text/resource.go | 16 ++-- .../resource.go | 16 ++-- zitadel/pat/funcs.go | 13 ++-- zitadel/privacy_policy/funcs.go | 19 ++--- zitadel/project/funcs.go | 16 ++-- zitadel/project_grant/funcs.go | 16 ++-- zitadel/project_grant_member/funcs.go | 16 ++-- zitadel/project_member/funcs.go | 16 ++-- zitadel/project_role/funcs.go | 16 ++-- zitadel/provider.go | 4 + zitadel/trigger_actions/funcs.go | 16 ++-- zitadel/user_grant/funcs.go | 16 ++-- zitadel/verify_email_message_text/resource.go | 16 ++-- zitadel/verify_phone_message_text/resource.go | 16 ++-- 45 files changed, 388 insertions(+), 375 deletions(-) diff --git a/zitadel/action/funcs.go b/zitadel/action/funcs.go index 53f7bc17..74b00d47 100644 --- a/zitadel/action/funcs.go +++ b/zitadel/action/funcs.go @@ -22,7 +22,7 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } @@ -32,7 +32,7 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.FromErr(err) } - _, err = client.UpdateAction(ctx, &management.UpdateActionRequest{ + _, err = client.UpdateAction(helper.CtxWithOrgID(ctx, d), &management.UpdateActionRequest{ Id: d.Id(), Name: d.Get(NameVar).(string), Script: d.Get(ScriptVar).(string), @@ -53,12 +53,12 @@ func delete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - _, err = client.DeleteAction(ctx, &management.DeleteActionRequest{ + _, err = client.DeleteAction(helper.CtxWithOrgID(ctx, d), &management.DeleteActionRequest{ Id: d.Id(), }) if err != nil { @@ -75,7 +75,7 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } @@ -85,7 +85,7 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.FromErr(err) } - resp, err := client.CreateAction(ctx, &management.CreateActionRequest{ + resp, err := client.CreateAction(helper.CtxWithOrgID(ctx, d), &management.CreateActionRequest{ Name: d.Get(NameVar).(string), Script: d.Get(ScriptVar).(string), Timeout: durationpb.New(timeout), @@ -106,12 +106,12 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - resp, err := client.ListActions(ctx, &management.ListActionsRequest{ + resp, err := client.ListActions(helper.CtxWithOrgID(ctx, d), &management.ListActionsRequest{ Queries: []*management.ActionQuery{ {Query: &management.ActionQuery_ActionIdQuery{ ActionIdQuery: &action.ActionIDQuery{ diff --git a/zitadel/application_api/funcs.go b/zitadel/application_api/funcs.go index efb610e7..0bde22d6 100644 --- a/zitadel/application_api/funcs.go +++ b/zitadel/application_api/funcs.go @@ -20,12 +20,12 @@ func delete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - _, err = client.RemoveApp(ctx, &management.RemoveAppRequest{ + _, err = client.RemoveApp(helper.CtxWithOrgID(ctx, d), &management.RemoveAppRequest{ ProjectId: d.Get(ProjectIDVar).(string), AppId: d.Id(), }) @@ -43,14 +43,14 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } projectID := d.Get(ProjectIDVar).(string) if d.HasChange(NameVar) { - _, err = client.UpdateApp(ctx, &management.UpdateAppRequest{ + _, err = client.UpdateApp(helper.CtxWithOrgID(ctx, d), &management.UpdateAppRequest{ ProjectId: projectID, AppId: d.Id(), Name: d.Get(NameVar).(string), @@ -61,7 +61,7 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia } if d.HasChanges(authMethodTypeVar) { - _, err = client.UpdateAPIAppConfig(ctx, &management.UpdateAPIAppConfigRequest{ + _, err = client.UpdateAPIAppConfig(helper.CtxWithOrgID(ctx, d), &management.UpdateAPIAppConfigRequest{ ProjectId: projectID, AppId: d.Id(), AuthMethodType: app.APIAuthMethodType(app.APIAuthMethodType_value[d.Get(authMethodTypeVar).(string)]), @@ -81,12 +81,12 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - resp, err := client.AddAPIApp(ctx, &management.AddAPIAppRequest{ + resp, err := client.AddAPIApp(helper.CtxWithOrgID(ctx, d), &management.AddAPIAppRequest{ ProjectId: d.Get(ProjectIDVar).(string), Name: d.Get(NameVar).(string), AuthMethodType: app.APIAuthMethodType(app.APIAuthMethodType_value[(d.Get(authMethodTypeVar).(string))]), @@ -116,12 +116,12 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - resp, err := client.GetAppByID(ctx, &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 diff --git a/zitadel/application_key/funcs.go b/zitadel/application_key/funcs.go index afeb9a88..698a19ae 100644 --- a/zitadel/application_key/funcs.go +++ b/zitadel/application_key/funcs.go @@ -22,12 +22,12 @@ func delete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - _, err = client.RemoveAppKey(ctx, &management.RemoveAppKeyRequest{ + _, err = client.RemoveAppKey(helper.CtxWithOrgID(ctx, d), &management.RemoveAppKeyRequest{ ProjectId: d.Get(ProjectIDVar).(string), AppId: d.Get(AppIDVar).(string), KeyId: d.Id(), @@ -46,8 +46,7 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - orgID := d.Get(helper.OrgIDVar).(string) - client, err := helper.GetManagementClient(clientinfo, orgID) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } @@ -67,7 +66,7 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia req.ExpirationDate = timestamppb.New(t) } - resp, err := client.AddAppKey(ctx, req) + resp, err := client.AddAppKey(helper.CtxWithOrgID(ctx, d), req) if err != nil { return diag.FromErr(err) } @@ -85,15 +84,14 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn return diag.Errorf("failed to get client") } - orgID := d.Get(helper.OrgIDVar).(string) - client, err := helper.GetManagementClient(clientinfo, orgID) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } projectID := d.Get(ProjectIDVar).(string) appID := d.Get(AppIDVar).(string) - resp, err := client.GetAppKey(ctx, &management.GetAppKeyRequest{ + resp, err := client.GetAppKey(helper.CtxWithOrgID(ctx, d), &management.GetAppKeyRequest{ ProjectId: projectID, AppId: appID, KeyId: d.Id(), @@ -111,7 +109,7 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn ExpirationDateVar: resp.GetKey().GetExpirationDate().AsTime().Format(time.RFC3339), ProjectIDVar: projectID, AppIDVar: appID, - helper.OrgIDVar: orgID, + helper.OrgIDVar: d.Get(helper.OrgIDVar).(string), keyTypeVar: resp.GetKey().GetType().String(), } for k, v := range set { diff --git a/zitadel/application_oidc/funcs.go b/zitadel/application_oidc/funcs.go index 59c95968..d121c728 100644 --- a/zitadel/application_oidc/funcs.go +++ b/zitadel/application_oidc/funcs.go @@ -22,12 +22,12 @@ func delete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - _, err = client.RemoveApp(ctx, &management.RemoveAppRequest{ + _, err = client.RemoveApp(helper.CtxWithOrgID(ctx, d), &management.RemoveAppRequest{ ProjectId: d.Get(ProjectIDVar).(string), AppId: d.Id(), }) @@ -45,7 +45,7 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } @@ -53,7 +53,7 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia projectID := d.Get(ProjectIDVar).(string) if d.HasChange(NameVar) { - _, err = client.UpdateApp(ctx, &management.UpdateAppRequest{ + _, err = client.UpdateApp(helper.CtxWithOrgID(ctx, d), &management.UpdateAppRequest{ ProjectId: projectID, AppId: d.Id(), Name: d.Get(NameVar).(string), @@ -91,7 +91,7 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.FromErr(err) } - _, err = client.UpdateOIDCAppConfig(ctx, &management.UpdateOIDCAppConfigRequest{ + _, err = client.UpdateOIDCAppConfig(helper.CtxWithOrgID(ctx, d), &management.UpdateOIDCAppConfigRequest{ ProjectId: projectID, AppId: d.Id(), RedirectUris: interfaceToStringSlice(d.Get(redirectURIsVar)), @@ -123,7 +123,7 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } @@ -142,7 +142,7 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.FromErr(err) } - resp, err := client.AddOIDCApp(ctx, &management.AddOIDCAppRequest{ + resp, err := client.AddOIDCApp(helper.CtxWithOrgID(ctx, d), &management.AddOIDCAppRequest{ ProjectId: d.Get(ProjectIDVar).(string), Name: d.Get(NameVar).(string), RedirectUris: interfaceToStringSlice(d.Get(redirectURIsVar)), @@ -186,12 +186,12 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - resp, err := client.GetAppByID(ctx, &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 diff --git a/zitadel/domain/funcs.go b/zitadel/domain/funcs.go index e14134f8..85cb9f51 100644 --- a/zitadel/domain/funcs.go +++ b/zitadel/domain/funcs.go @@ -22,20 +22,20 @@ func delete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } domainName := d.Id() if d.Get(isPrimaryVar).(bool) { - resp, err := client.ListOrgDomains(ctx, &management.ListOrgDomainsRequest{}) + resp, err := client.ListOrgDomains(helper.CtxWithOrgID(ctx, d), &management.ListOrgDomainsRequest{}) if err != nil { return diag.FromErr(err) } for _, domain := range resp.Result { parts := strings.Split(clientinfo.Domain, ":") if domain.IsVerified && domain.DomainName != domainName && strings.HasSuffix(domain.GetDomainName(), parts[0]) { - if _, err := client.SetPrimaryOrgDomain(ctx, &management.SetPrimaryOrgDomainRequest{Domain: domain.DomainName}); err != nil { + if _, err := client.SetPrimaryOrgDomain(helper.CtxWithOrgID(ctx, d), &management.SetPrimaryOrgDomainRequest{Domain: domain.DomainName}); err != nil { return diag.FromErr(err) } break @@ -43,7 +43,7 @@ func delete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia } } - _, err = client.RemoveOrgDomain(ctx, &management.RemoveOrgDomainRequest{ + _, err = client.RemoveOrgDomain(helper.CtxWithOrgID(ctx, d), &management.RemoveOrgDomainRequest{ Domain: domainName, }) if err != nil { @@ -60,13 +60,13 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } name := d.Get(NameVar).(string) - _, err = client.AddOrgDomain(ctx, &management.AddOrgDomainRequest{ + _, err = client.AddOrgDomain(helper.CtxWithOrgID(ctx, d), &management.AddOrgDomainRequest{ Domain: name, }) if err != nil { @@ -74,7 +74,7 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia } d.SetId(name) if d.Get(isPrimaryVar).(bool) { - _, err = client.SetPrimaryOrgDomain(ctx, &management.SetPrimaryOrgDomainRequest{Domain: name}) + _, err = client.SetPrimaryOrgDomain(helper.CtxWithOrgID(ctx, d), &management.SetPrimaryOrgDomainRequest{Domain: name}) if err != nil { return diag.Errorf("failed to set domain primary: %v", err) } @@ -90,7 +90,7 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } @@ -99,7 +99,7 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia d.SetId(name) if d.HasChange(isPrimaryVar) { if d.Get(isPrimaryVar).(bool) { - _, err = client.SetPrimaryOrgDomain(ctx, &management.SetPrimaryOrgDomainRequest{Domain: name}) + _, err = client.SetPrimaryOrgDomain(helper.CtxWithOrgID(ctx, d), &management.SetPrimaryOrgDomainRequest{Domain: name}) if err != nil { return diag.Errorf("failed to set domain primary: %v", err) } @@ -116,12 +116,12 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - resp, err := client.ListOrgDomains(ctx, &management.ListOrgDomainsRequest{ + resp, err := client.ListOrgDomains(helper.CtxWithOrgID(ctx, d), &management.ListOrgDomainsRequest{ Queries: []*org.DomainSearchQuery{ {Query: &org.DomainSearchQuery_DomainNameQuery{ DomainNameQuery: &org.DomainNameQuery{ diff --git a/zitadel/domain_claimed_message_text/resource.go b/zitadel/domain_claimed_message_text/resource.go index 1093d74e..92324f6d 100644 --- a/zitadel/domain_claimed_message_text/resource.go +++ b/zitadel/domain_claimed_message_text/resource.go @@ -85,13 +85,13 @@ func (r *domainClaimedMessageTextResource) Create(ctx context.Context, req resou } zReq.Language = language - client, err := helper.GetManagementClient(r.clientInfo, orgID) + client, err := helper.GetManagementClient(r.clientInfo) if err != nil { resp.Diagnostics.AddError("failed to get client", err.Error()) return } - _, err = client.SetCustomDomainClaimedMessageCustomText(ctx, zReq) + _, err = client.SetCustomDomainClaimedMessageCustomText(helper.CtxSetOrgID(ctx, orgID), zReq) if err != nil { resp.Diagnostics.AddError("failed to create", err.Error()) return @@ -111,13 +111,13 @@ func (r *domainClaimedMessageTextResource) Read(ctx context.Context, req resourc orgID, language := getID(ctx, state) - client, err := helper.GetManagementClient(r.clientInfo, orgID) + client, err := helper.GetManagementClient(r.clientInfo) if err != nil { resp.Diagnostics.AddError("failed to get client", err.Error()) return } - zResp, err := client.GetCustomDomainClaimedMessageText(ctx, &management.GetCustomDomainClaimedMessageTextRequest{Language: language}) + zResp, err := client.GetCustomDomainClaimedMessageText(helper.CtxSetOrgID(ctx, orgID), &management.GetCustomDomainClaimedMessageTextRequest{Language: language}) if err != nil { return } @@ -169,13 +169,13 @@ func (r *domainClaimedMessageTextResource) Update(ctx context.Context, req resou } zReq.Language = language - client, err := helper.GetManagementClient(r.clientInfo, orgID) + client, err := helper.GetManagementClient(r.clientInfo) if err != nil { resp.Diagnostics.AddError("failed to get client", err.Error()) return } - _, err = client.SetCustomDomainClaimedMessageCustomText(ctx, zReq) + _, err = client.SetCustomDomainClaimedMessageCustomText(helper.CtxSetOrgID(ctx, orgID), zReq) if err != nil { resp.Diagnostics.AddError("failed to update", err.Error()) return @@ -191,13 +191,13 @@ func (r *domainClaimedMessageTextResource) Delete(ctx context.Context, req resou return } - client, err := helper.GetManagementClient(r.clientInfo, orgID) + client, err := helper.GetManagementClient(r.clientInfo) if err != nil { resp.Diagnostics.AddError("failed to get client", err.Error()) return } - _, err = client.ResetCustomDomainClaimedMessageTextToDefault(ctx, &management.ResetCustomDomainClaimedMessageTextToDefaultRequest{Language: language}) + _, err = client.ResetCustomDomainClaimedMessageTextToDefault(helper.CtxSetOrgID(ctx, orgID), &management.ResetCustomDomainClaimedMessageTextToDefaultRequest{Language: language}) if err != nil { resp.Diagnostics.AddError("failed to delete", err.Error()) return diff --git a/zitadel/domain_policy/funcs.go b/zitadel/domain_policy/funcs.go index 4b6c5fc2..4be23777 100644 --- a/zitadel/domain_policy/funcs.go +++ b/zitadel/domain_policy/funcs.go @@ -95,13 +95,12 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn return diag.Errorf("failed to get client") } - org := helper.GetID(d, helper.OrgIDVar) - client, err := helper.GetManagementClient(clientinfo, org) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - resp, err := client.GetDomainPolicy(ctx, &management.GetDomainPolicyRequest{}) + resp, err := client.GetDomainPolicy(helper.CtxWithOrgID(ctx, d), &management.GetDomainPolicyRequest{}) if err != nil && helper.IgnoreIfNotFoundError(err) == nil { d.SetId("") return nil diff --git a/zitadel/helper/client.go b/zitadel/helper/client.go index 96955b67..66cdd433 100644 --- a/zitadel/helper/client.go +++ b/zitadel/helper/client.go @@ -1,8 +1,12 @@ package helper import ( + "context" "fmt" + "sync" + "time" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/zitadel/oidc/pkg/oidc" "github.com/zitadel/zitadel-go/v2/pkg/client/admin" "github.com/zitadel/zitadel-go/v2/pkg/client/management" @@ -77,34 +81,66 @@ func GetClientInfo(insecure bool, domain string, token string, jwtProfileFile st }, nil } +var adminClientLock = &sync.Mutex{} +var adminClient *admin.Client + +func ClearAdminClient() { + adminClient = nil +} + func GetAdminClient(info *ClientInfo) (*admin.Client, error) { - client, err := admin.NewClient( - info.Issuer, info.Domain, - []string{oidc.ScopeOpenID, zitadel.ScopeZitadelAPI()}, - info.Options..., - ) - if err != nil { - return nil, fmt.Errorf("failed to start zitadel client: %v", err) + if adminClient == nil { + adminClientLock.Lock() + defer adminClientLock.Unlock() + if adminClient == nil { + client, err := admin.NewClient( + info.Issuer, info.Domain, + []string{oidc.ScopeOpenID, zitadel.ScopeZitadelAPI()}, + info.Options..., + ) + if err != nil { + return nil, fmt.Errorf("failed to start zitadel client: %v", err) + } + time.Sleep(time.Second * 2) + adminClient = client + } } + return adminClient, nil +} + +var mgmtClientLock = &sync.Mutex{} +var mgmtClient *management.Client - return client, nil +func ClearMgmtClient() { + mgmtClient = nil } -func GetManagementClient(info *ClientInfo, orgID string) (*management.Client, error) { - options := info.Options - if orgID != "" { - options = append(options, zitadel.WithOrgID(orgID)) +func GetManagementClient(info *ClientInfo) (*management.Client, error) { + if mgmtClient == nil { + mgmtClientLock.Lock() + defer mgmtClientLock.Unlock() + if mgmtClient == nil { + client, err := management.NewClient( + info.Issuer, info.Domain, + []string{oidc.ScopeOpenID, zitadel.ScopeZitadelAPI()}, + info.Options..., + ) + if err != nil { + return nil, fmt.Errorf("failed to start zitadel client: %v", err) + } + time.Sleep(time.Second * 2) + mgmtClient = client + } } + return mgmtClient, nil +} - client, err := management.NewClient( - info.Issuer, info.Domain, - []string{oidc.ScopeOpenID, zitadel.ScopeZitadelAPI()}, - options..., - ) - if err != nil { - return nil, fmt.Errorf("failed to start zitadel client: %v", err) - } - return client, nil +func CtxWithOrgID(ctx context.Context, d *schema.ResourceData) context.Context { + return CtxSetOrgID(ctx, d.Get(OrgIDVar).(string)) +} + +func CtxSetOrgID(ctx context.Context, orgID string) context.Context { + return middleware.SetOrgID(ctx, orgID) } func IgnoreIfNotFoundError(err error) error { diff --git a/zitadel/human_user/funcs.go b/zitadel/human_user/funcs.go index 75b36606..3f532a36 100644 --- a/zitadel/human_user/funcs.go +++ b/zitadel/human_user/funcs.go @@ -20,12 +20,12 @@ func delete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - _, err = client.RemoveUser(ctx, &management.RemoveUserRequest{ + _, err = client.RemoveUser(helper.CtxWithOrgID(ctx, d), &management.RemoveUserRequest{ Id: d.Id(), }) if err != nil { @@ -42,7 +42,7 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } @@ -91,7 +91,7 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia } } - respUser, err := client.AddHumanUser(ctx, addUser) + respUser, err := client.AddHumanUser(helper.CtxWithOrgID(ctx, d), addUser) if err != nil { return diag.Errorf("failed to create human user: %v", err) } @@ -109,13 +109,13 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } if d.HasChange(UserNameVar) { - _, err = client.UpdateUserName(ctx, &management.UpdateUserNameRequest{ + _, err = client.UpdateUserName(helper.CtxWithOrgID(ctx, d), &management.UpdateUserNameRequest{ UserId: d.Id(), UserName: d.Get(UserNameVar).(string), }) @@ -125,7 +125,7 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia } if d.HasChanges(firstNameVar, lastNameVar, nickNameVar, DisplayNameVar, preferredLanguageVar, genderVar) { - _, err := client.UpdateHumanProfile(ctx, &management.UpdateHumanProfileRequest{ + _, err := client.UpdateHumanProfile(helper.CtxWithOrgID(ctx, d), &management.UpdateHumanProfileRequest{ UserId: d.Id(), FirstName: d.Get(firstNameVar).(string), LastName: d.Get(lastNameVar).(string), @@ -140,7 +140,7 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia } if d.HasChanges(emailVar, isEmailVerifiedVar) { - _, err = client.UpdateHumanEmail(ctx, &management.UpdateHumanEmailRequest{ + _, err = client.UpdateHumanEmail(helper.CtxWithOrgID(ctx, d), &management.UpdateHumanEmailRequest{ UserId: d.Id(), Email: d.Get(emailVar).(string), IsEmailVerified: d.Get(isEmailVerifiedVar).(bool), @@ -151,7 +151,7 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia } if d.HasChanges(phoneVar, isPhoneVerifiedVar) { - _, err = client.UpdateHumanPhone(ctx, &management.UpdateHumanPhoneRequest{ + _, err = client.UpdateHumanPhone(helper.CtxWithOrgID(ctx, d), &management.UpdateHumanPhoneRequest{ UserId: d.Id(), Phone: d.Get(phoneVar).(string), IsPhoneVerified: d.Get(isPhoneVerifiedVar).(bool), @@ -171,12 +171,12 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - respUser, err := client.GetUserByID(ctx, &management.GetUserByIDRequest{Id: helper.GetID(d, UserIDVar)}) + respUser, err := client.GetUserByID(helper.CtxWithOrgID(ctx, d), &management.GetUserByIDRequest{Id: helper.GetID(d, UserIDVar)}) if err != nil && helper.IgnoreIfNotFoundError(err) == nil { d.SetId("") return nil diff --git a/zitadel/init_message_text/resource.go b/zitadel/init_message_text/resource.go index ee290ecf..73eafad7 100644 --- a/zitadel/init_message_text/resource.go +++ b/zitadel/init_message_text/resource.go @@ -85,13 +85,13 @@ func (r *initMessageTextResource) Create(ctx context.Context, req resource.Creat } zReq.Language = language - client, err := helper.GetManagementClient(r.clientInfo, orgID) + client, err := helper.GetManagementClient(r.clientInfo) if err != nil { resp.Diagnostics.AddError("failed to get client", err.Error()) return } - _, err = client.SetCustomInitMessageText(ctx, zReq) + _, err = client.SetCustomInitMessageText(helper.CtxSetOrgID(ctx, orgID), zReq) if err != nil { resp.Diagnostics.AddError("failed to create", err.Error()) return @@ -111,13 +111,13 @@ func (r *initMessageTextResource) Read(ctx context.Context, req resource.ReadReq orgID, language := getID(ctx, state) - client, err := helper.GetManagementClient(r.clientInfo, orgID) + client, err := helper.GetManagementClient(r.clientInfo) if err != nil { resp.Diagnostics.AddError("failed to get client", err.Error()) return } - zResp, err := client.GetCustomInitMessageText(ctx, &management.GetCustomInitMessageTextRequest{Language: language}) + zResp, err := client.GetCustomInitMessageText(helper.CtxSetOrgID(ctx, orgID), &management.GetCustomInitMessageTextRequest{Language: language}) if err != nil { return } @@ -169,13 +169,13 @@ func (r *initMessageTextResource) Update(ctx context.Context, req resource.Updat } zReq.Language = language - client, err := helper.GetManagementClient(r.clientInfo, orgID) + client, err := helper.GetManagementClient(r.clientInfo) if err != nil { resp.Diagnostics.AddError("failed to get client", err.Error()) return } - _, err = client.SetCustomInitMessageText(ctx, zReq) + _, err = client.SetCustomInitMessageText(helper.CtxSetOrgID(ctx, orgID), zReq) if err != nil { resp.Diagnostics.AddError("failed to update", err.Error()) return @@ -191,13 +191,13 @@ func (r *initMessageTextResource) Delete(ctx context.Context, req resource.Delet return } - client, err := helper.GetManagementClient(r.clientInfo, orgID) + client, err := helper.GetManagementClient(r.clientInfo) if err != nil { resp.Diagnostics.AddError("failed to get client", err.Error()) return } - _, err = client.ResetCustomInitMessageTextToDefault(ctx, &management.ResetCustomInitMessageTextToDefaultRequest{Language: language}) + _, err = client.ResetCustomInitMessageTextToDefault(helper.CtxSetOrgID(ctx, orgID), &management.ResetCustomInitMessageTextToDefaultRequest{Language: language}) if err != nil { resp.Diagnostics.AddError("failed to delete", err.Error()) return diff --git a/zitadel/label_policy/funcs.go b/zitadel/label_policy/funcs.go index 09d8f346..be631305 100644 --- a/zitadel/label_policy/funcs.go +++ b/zitadel/label_policy/funcs.go @@ -19,13 +19,12 @@ func delete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - org := helper.GetID(d, helper.OrgIDVar) - client, err := helper.GetManagementClient(clientinfo, org) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - _, err = client.ResetLabelPolicyToDefault(ctx, &management.ResetLabelPolicyToDefaultRequest{}) + _, err = client.ResetLabelPolicyToDefault(helper.CtxWithOrgID(ctx, d), &management.ResetLabelPolicyToDefaultRequest{}) if err != nil { return diag.Errorf("failed to reset label policy: %v", err) } @@ -41,7 +40,7 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia } org := helper.GetID(d, helper.OrgIDVar) - client, err := helper.GetManagementClient(clientinfo, org) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } @@ -58,7 +57,7 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia fontColorDarkVar, disableWatermarkVar, ) { - resp, err := client.UpdateCustomLabelPolicy(ctx, &management.UpdateCustomLabelPolicyRequest{ + resp, err := client.UpdateCustomLabelPolicy(helper.CtxWithOrgID(ctx, d), &management.UpdateCustomLabelPolicyRequest{ PrimaryColor: d.Get(primaryColorVar).(string), HideLoginNameSuffix: d.Get(hideLoginNameSuffixVar).(bool), WarnColor: d.Get(warnColorVar).(string), @@ -120,7 +119,7 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia FontHashVar, ) { if d.Get(SetActiveVar).(bool) { - if _, err := client.ActivateCustomLabelPolicy(ctx, &management.ActivateCustomLabelPolicyRequest{}); err != nil { + if _, err := client.ActivateCustomLabelPolicy(helper.CtxWithOrgID(ctx, d), &management.ActivateCustomLabelPolicyRequest{}); err != nil { return diag.Errorf("failed to activate label policy: %v", err) } } @@ -137,12 +136,12 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia } org := d.Get(helper.OrgIDVar).(string) - client, err := helper.GetManagementClient(clientinfo, org) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - _, err = client.AddCustomLabelPolicy(ctx, &management.AddCustomLabelPolicyRequest{ + _, err = client.AddCustomLabelPolicy(helper.CtxWithOrgID(ctx, d), &management.AddCustomLabelPolicyRequest{ PrimaryColor: d.Get(primaryColorVar).(string), HideLoginNameSuffix: d.Get(hideLoginNameSuffixVar).(bool), WarnColor: d.Get(warnColorVar).(string), @@ -186,7 +185,7 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia } if d.Get(SetActiveVar).(bool) { - if _, err := client.ActivateCustomLabelPolicy(ctx, &management.ActivateCustomLabelPolicyRequest{}); err != nil { + if _, err := client.ActivateCustomLabelPolicy(helper.CtxWithOrgID(ctx, d), &management.ActivateCustomLabelPolicyRequest{}); err != nil { return diag.Errorf("failed to activate label policy: %v", err) } } @@ -202,13 +201,12 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn return diag.Errorf("failed to get client") } - org := helper.GetID(d, helper.OrgIDVar) - client, err := helper.GetManagementClient(clientinfo, org) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - resp, err := client.GetPreviewLabelPolicy(ctx, &management.GetPreviewLabelPolicyRequest{}) + resp, err := client.GetPreviewLabelPolicy(helper.CtxWithOrgID(ctx, d), &management.GetPreviewLabelPolicyRequest{}) if err != nil && helper.IgnoreIfNotFoundError(err) == nil { d.SetId("") return nil diff --git a/zitadel/lockout_policy/funcs.go b/zitadel/lockout_policy/funcs.go index a214d5db..78397b8f 100644 --- a/zitadel/lockout_policy/funcs.go +++ b/zitadel/lockout_policy/funcs.go @@ -17,12 +17,11 @@ func delete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia if !ok { return diag.Errorf("failed to get client") } - org := helper.GetID(d, helper.OrgIDVar) - client, err := helper.GetManagementClient(clientinfo, org) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - _, err = client.ResetLockoutPolicyToDefault(ctx, &management.ResetLockoutPolicyToDefaultRequest{}) + _, err = client.ResetLockoutPolicyToDefault(helper.CtxWithOrgID(ctx, d), &management.ResetLockoutPolicyToDefaultRequest{}) if err != nil { return diag.Errorf("failed to reset lockout policy: %v", err) } @@ -35,12 +34,11 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia if !ok { return diag.Errorf("failed to get client") } - org := helper.GetID(d, helper.OrgIDVar) - client, err := helper.GetManagementClient(clientinfo, org) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - _, err = client.UpdateCustomLockoutPolicy(ctx, &management.UpdateCustomLockoutPolicyRequest{ + _, err = client.UpdateCustomLockoutPolicy(helper.CtxWithOrgID(ctx, d), &management.UpdateCustomLockoutPolicyRequest{ MaxPasswordAttempts: uint32(d.Get(maxPasswordAttemptsVar).(int)), }) if err != nil { @@ -56,11 +54,11 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } org := d.Get(helper.OrgIDVar).(string) - client, err := helper.GetManagementClient(clientinfo, org) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - _, err = client.AddCustomLockoutPolicy(ctx, &management.AddCustomLockoutPolicyRequest{ + _, err = client.AddCustomLockoutPolicy(helper.CtxWithOrgID(ctx, d), &management.AddCustomLockoutPolicyRequest{ MaxPasswordAttempts: uint32(d.Get(maxPasswordAttemptsVar).(int)), }) if err != nil { @@ -76,12 +74,11 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn if !ok { return diag.Errorf("failed to get client") } - org := helper.GetID(d, helper.OrgIDVar) - client, err := helper.GetManagementClient(clientinfo, org) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - resp, err := client.GetLockoutPolicy(ctx, &management.GetLockoutPolicyRequest{}) + resp, err := client.GetLockoutPolicy(helper.CtxWithOrgID(ctx, d), &management.GetLockoutPolicyRequest{}) if err != nil && helper.IgnoreIfNotFoundError(err) == nil { d.SetId("") return nil diff --git a/zitadel/login_policy/funcs.go b/zitadel/login_policy/funcs.go index c431e7a0..ef9f1a11 100644 --- a/zitadel/login_policy/funcs.go +++ b/zitadel/login_policy/funcs.go @@ -22,12 +22,11 @@ func delete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia if !ok { return diag.Errorf("failed to get client") } - org := helper.GetID(d, helper.OrgIDVar) - client, err := helper.GetManagementClient(clientinfo, org) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - _, err = client.ResetLoginPolicyToDefault(ctx, &management.ResetLoginPolicyToDefaultRequest{}) + _, err = client.ResetLoginPolicyToDefault(helper.CtxWithOrgID(ctx, d), &management.ResetLoginPolicyToDefaultRequest{}) if err != nil { return diag.Errorf("failed to reset login policy: %v", err) } @@ -42,8 +41,7 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - org := helper.GetID(d, helper.OrgIDVar) - client, err := helper.GetManagementClient(clientinfo, org) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } @@ -86,7 +84,7 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia if err != nil { return diag.FromErr(err) } - _, err = client.UpdateCustomLoginPolicy(ctx, &management.UpdateCustomLoginPolicyRequest{ + _, err = client.UpdateCustomLoginPolicy(helper.CtxWithOrgID(ctx, d), &management.UpdateCustomLoginPolicyRequest{ AllowUsernamePassword: d.Get(allowUsernamePasswordVar).(bool), AllowRegister: d.Get(allowRegisterVar).(bool), AllowExternalIdp: d.Get(allowExternalIDPVar).(bool), @@ -115,14 +113,14 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia addSecondFactor, deleteSecondFactors := helper.GetAddAndDelete(helper.SetToStringSlice(o.(*schema.Set)), helper.SetToStringSlice(n.(*schema.Set))) for _, factor := range addSecondFactor { - if _, err := client.AddSecondFactorToLoginPolicy(ctx, &management.AddSecondFactorToLoginPolicyRequest{ + if _, err := client.AddSecondFactorToLoginPolicy(helper.CtxWithOrgID(ctx, d), &management.AddSecondFactorToLoginPolicyRequest{ Type: policy.SecondFactorType(policy.SecondFactorType_value[factor]), }); err != nil { return diag.FromErr(err) } } for _, factor := range deleteSecondFactors { - if _, err := client.RemoveSecondFactorFromLoginPolicy(ctx, &management.RemoveSecondFactorFromLoginPolicyRequest{ + if _, err := client.RemoveSecondFactorFromLoginPolicy(helper.CtxWithOrgID(ctx, d), &management.RemoveSecondFactorFromLoginPolicyRequest{ Type: policy.SecondFactorType(policy.SecondFactorType_value[factor]), }); err != nil { return diag.FromErr(err) @@ -135,14 +133,14 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia addMultiFactor, deleteMultiFactors := helper.GetAddAndDelete(helper.SetToStringSlice(o.(*schema.Set)), helper.SetToStringSlice(n.(*schema.Set))) for _, factor := range addMultiFactor { - if _, err := client.AddMultiFactorToLoginPolicy(ctx, &management.AddMultiFactorToLoginPolicyRequest{ + if _, err := client.AddMultiFactorToLoginPolicy(helper.CtxWithOrgID(ctx, d), &management.AddMultiFactorToLoginPolicyRequest{ Type: policy.MultiFactorType(policy.MultiFactorType_value[factor]), }); err != nil { return diag.FromErr(err) } } for _, factor := range deleteMultiFactors { - if _, err := client.RemoveMultiFactorFromLoginPolicy(ctx, &management.RemoveMultiFactorFromLoginPolicyRequest{ + if _, err := client.RemoveMultiFactorFromLoginPolicy(helper.CtxWithOrgID(ctx, d), &management.RemoveMultiFactorFromLoginPolicyRequest{ Type: policy.MultiFactorType(policy.MultiFactorType_value[factor]), }); err != nil { return diag.FromErr(err) @@ -181,7 +179,7 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia } org := d.Get(helper.OrgIDVar).(string) - client, err := helper.GetManagementClient(clientinfo, org) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } @@ -220,7 +218,7 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia } } - _, err = client.AddCustomLoginPolicy(ctx, &management.AddCustomLoginPolicyRequest{ + _, err = client.AddCustomLoginPolicy(helper.CtxWithOrgID(ctx, d), &management.AddCustomLoginPolicyRequest{ AllowUsernamePassword: d.Get(allowUsernamePasswordVar).(bool), AllowRegister: d.Get(allowRegisterVar).(bool), AllowExternalIdp: d.Get(allowExternalIDPVar).(bool), @@ -248,11 +246,11 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia idps := helper.GetOkSetToStringSlice(d, idpsVar) for _, addIdp := range idps { - idpOwnerType, err := getIDPOwnerType(ctx, client, addIdp) + idpOwnerType, err := getIDPOwnerType(helper.CtxWithOrgID(ctx, d), client, addIdp) if err != nil { return diag.FromErr(err) } - if _, err := client.AddIDPToLoginPolicy(ctx, &management.AddIDPToLoginPolicyRequest{IdpId: addIdp, OwnerType: idpOwnerType}); err != nil { + if _, err := client.AddIDPToLoginPolicy(helper.CtxWithOrgID(ctx, d), &management.AddIDPToLoginPolicyRequest{IdpId: addIdp, OwnerType: idpOwnerType}); err != nil { return diag.FromErr(err) } } @@ -290,12 +288,11 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn if !ok { return diag.Errorf("failed to get client") } - org := helper.GetID(d, helper.OrgIDVar) - client, err := helper.GetManagementClient(clientinfo, org) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - resp, err := client.GetLoginPolicy(ctx, &management.GetLoginPolicyRequest{}) + resp, err := client.GetLoginPolicy(helper.CtxWithOrgID(ctx, d), &management.GetLoginPolicyRequest{}) if err != nil && helper.IgnoreIfNotFoundError(err) == nil { d.SetId("") return nil @@ -328,7 +325,7 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn disableLoginWithPhone: policy.GetDisableLoginWithPhone(), forceMFALocalOnlyVar: policy.GetForceMfaLocalOnly(), } - respSecond, err := client.ListLoginPolicySecondFactors(ctx, &management.ListLoginPolicySecondFactorsRequest{}) + respSecond, err := client.ListLoginPolicySecondFactors(helper.CtxWithOrgID(ctx, d), &management.ListLoginPolicySecondFactorsRequest{}) if err != nil { return diag.Errorf("failed to get login policy secondfactors: %v", err) } @@ -339,7 +336,7 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn } set[secondFactorsVar] = factors } - respMulti, err := client.ListLoginPolicyMultiFactors(ctx, &management.ListLoginPolicyMultiFactorsRequest{}) + respMulti, err := client.ListLoginPolicyMultiFactors(helper.CtxWithOrgID(ctx, d), &management.ListLoginPolicyMultiFactorsRequest{}) if err != nil { return diag.Errorf("failed to get login policy multifactors: %v", err) } @@ -350,7 +347,7 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn } set[multiFactorsVar] = factors } - respIDPs, err := client.ListLoginPolicyIDPs(ctx, &management.ListLoginPolicyIDPsRequest{}) + respIDPs, err := client.ListLoginPolicyIDPs(helper.CtxWithOrgID(ctx, d), &management.ListLoginPolicyIDPsRequest{}) if err != nil { return diag.Errorf("failed to get login policy idps: %v", err) } diff --git a/zitadel/login_texts/resource.go b/zitadel/login_texts/resource.go index ba0ff030..cdd1a7ce 100644 --- a/zitadel/login_texts/resource.go +++ b/zitadel/login_texts/resource.go @@ -85,13 +85,13 @@ func (r *loginTextsResource) Create(ctx context.Context, req resource.CreateRequ } zReq.Language = language - client, err := helper.GetManagementClient(r.clientInfo, orgID) + client, err := helper.GetManagementClient(r.clientInfo) if err != nil { resp.Diagnostics.AddError("failed to get client", err.Error()) return } - _, err = client.SetCustomLoginText(ctx, zReq) + _, err = client.SetCustomLoginText(helper.CtxSetOrgID(ctx, orgID), zReq) if err != nil { resp.Diagnostics.AddError("failed to create login texts", err.Error()) return @@ -111,13 +111,13 @@ func (r *loginTextsResource) Read(ctx context.Context, req resource.ReadRequest, orgID, language := getID(ctx, state) - client, err := helper.GetManagementClient(r.clientInfo, orgID) + client, err := helper.GetManagementClient(r.clientInfo) if err != nil { resp.Diagnostics.AddError("failed to get client", err.Error()) return } - zResp, err := client.GetCustomLoginTexts(ctx, &management.GetCustomLoginTextsRequest{Language: language}) + zResp, err := client.GetCustomLoginTexts(helper.CtxSetOrgID(ctx, orgID), &management.GetCustomLoginTextsRequest{Language: language}) if err != nil { return } @@ -169,13 +169,13 @@ func (r *loginTextsResource) Update(ctx context.Context, req resource.UpdateRequ } zReq.Language = language - client, err := helper.GetManagementClient(r.clientInfo, orgID) + client, err := helper.GetManagementClient(r.clientInfo) if err != nil { resp.Diagnostics.AddError("failed to get client", err.Error()) return } - _, err = client.SetCustomLoginText(ctx, zReq) + _, err = client.SetCustomLoginText(helper.CtxSetOrgID(ctx, orgID), zReq) if err != nil { resp.Diagnostics.AddError("failed to update login texts", err.Error()) return @@ -191,13 +191,13 @@ func (r *loginTextsResource) Delete(ctx context.Context, req resource.DeleteRequ return } - client, err := helper.GetManagementClient(r.clientInfo, orgID) + client, err := helper.GetManagementClient(r.clientInfo) if err != nil { resp.Diagnostics.AddError("failed to get client", err.Error()) return } - _, err = client.ResetCustomLoginTextToDefault(ctx, &management.ResetCustomLoginTextsToDefaultRequest{Language: language}) + _, err = client.ResetCustomLoginTextToDefault(helper.CtxSetOrgID(ctx, orgID), &management.ResetCustomLoginTextsToDefaultRequest{Language: language}) if err != nil { resp.Diagnostics.AddError("failed to delete login texts", err.Error()) return diff --git a/zitadel/machine_key/funcs.go b/zitadel/machine_key/funcs.go index b018cd2f..1b456d05 100644 --- a/zitadel/machine_key/funcs.go +++ b/zitadel/machine_key/funcs.go @@ -22,12 +22,12 @@ func delete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - _, err = client.RemoveMachineKey(ctx, &management.RemoveMachineKeyRequest{ + _, err = client.RemoveMachineKey(helper.CtxWithOrgID(ctx, d), &management.RemoveMachineKeyRequest{ UserId: d.Get(UserIDVar).(string), KeyId: d.Id(), }) @@ -45,8 +45,7 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - orgID := d.Get(helper.OrgIDVar).(string) - client, err := helper.GetManagementClient(clientinfo, orgID) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } @@ -65,7 +64,7 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia req.ExpirationDate = timestamppb.New(t) } - resp, err := client.AddMachineKey(ctx, req) + resp, err := client.AddMachineKey(helper.CtxWithOrgID(ctx, d), req) if err != nil { return diag.FromErr(err) } @@ -84,13 +83,13 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn } orgID := d.Get(helper.OrgIDVar).(string) - client, err := helper.GetManagementClient(clientinfo, orgID) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } userID := d.Get(UserIDVar).(string) - resp, err := client.GetMachineKeyByIDs(ctx, &management.GetMachineKeyByIDsRequest{ + resp, err := client.GetMachineKeyByIDs(helper.CtxWithOrgID(ctx, d), &management.GetMachineKeyByIDsRequest{ UserId: userID, KeyId: d.Id(), }) diff --git a/zitadel/machine_user/funcs.go b/zitadel/machine_user/funcs.go index dbad970d..f1ee06ca 100644 --- a/zitadel/machine_user/funcs.go +++ b/zitadel/machine_user/funcs.go @@ -20,12 +20,12 @@ func delete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - _, err = client.RemoveUser(ctx, &management.RemoveUserRequest{ + _, err = client.RemoveUser(helper.CtxWithOrgID(ctx, d), &management.RemoveUserRequest{ Id: d.Id(), }) if err != nil { @@ -42,12 +42,12 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - respUser, err := client.AddMachineUser(ctx, &management.AddMachineUserRequest{ + respUser, err := client.AddMachineUser(helper.CtxWithOrgID(ctx, d), &management.AddMachineUserRequest{ UserName: d.Get(UserNameVar).(string), Name: d.Get(nameVar).(string), Description: d.Get(DescriptionVar).(string), @@ -70,13 +70,13 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } if d.HasChange(UserNameVar) { - _, err = client.UpdateUserName(ctx, &management.UpdateUserNameRequest{ + _, err = client.UpdateUserName(helper.CtxWithOrgID(ctx, d), &management.UpdateUserNameRequest{ UserId: d.Id(), UserName: d.Get(UserNameVar).(string), }) @@ -86,7 +86,7 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia } if d.HasChanges(nameVar, DescriptionVar, accessTokenTypeVar) { - _, err := client.UpdateMachine(ctx, &management.UpdateMachineRequest{ + _, err := client.UpdateMachine(helper.CtxWithOrgID(ctx, d), &management.UpdateMachineRequest{ UserId: d.Id(), Name: d.Get(nameVar).(string), Description: d.Get(DescriptionVar).(string), @@ -107,12 +107,12 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - respUser, err := client.GetUserByID(ctx, &management.GetUserByIDRequest{Id: helper.GetID(d, UserIDVar)}) + respUser, err := client.GetUserByID(helper.CtxWithOrgID(ctx, d), &management.GetUserByIDRequest{Id: helper.GetID(d, UserIDVar)}) if err != nil && helper.IgnoreIfNotFoundError(err) == nil { d.SetId("") return nil diff --git a/zitadel/notification_policy/funcs.go b/zitadel/notification_policy/funcs.go index 7d8092f4..47b7ecbf 100644 --- a/zitadel/notification_policy/funcs.go +++ b/zitadel/notification_policy/funcs.go @@ -17,12 +17,11 @@ func delete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia if !ok { return diag.Errorf("failed to get client") } - org := helper.GetID(d, helper.OrgIDVar) - client, err := helper.GetManagementClient(clientinfo, org) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - _, err = client.ResetNotificationPolicyToDefault(ctx, &management.ResetNotificationPolicyToDefaultRequest{}) + _, err = client.ResetNotificationPolicyToDefault(helper.CtxWithOrgID(ctx, d), &management.ResetNotificationPolicyToDefaultRequest{}) if err != nil { return diag.Errorf("failed to reset notification policy: %v", err) } @@ -35,13 +34,13 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia if !ok { return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } org := helper.GetID(d, helper.OrgIDVar) if d.HasChanges(passwordChangeVar) { - _, err = client.UpdateCustomNotificationPolicy(ctx, &management.UpdateCustomNotificationPolicyRequest{ + _, err = client.UpdateCustomNotificationPolicy(helper.CtxWithOrgID(ctx, d), &management.UpdateCustomNotificationPolicyRequest{ PasswordChange: d.Get(passwordChangeVar).(bool), }) if err != nil { @@ -58,12 +57,12 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia if !ok { return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } org := d.Get(helper.OrgIDVar).(string) - _, err = client.AddCustomNotificationPolicy(ctx, &management.AddCustomNotificationPolicyRequest{ + _, err = client.AddCustomNotificationPolicy(helper.CtxWithOrgID(ctx, d), &management.AddCustomNotificationPolicyRequest{ PasswordChange: d.Get(passwordChangeVar).(bool), }) if err != nil { @@ -79,12 +78,11 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn if !ok { return diag.Errorf("failed to get client") } - org := helper.GetID(d, helper.OrgIDVar) - client, err := helper.GetManagementClient(clientinfo, org) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - resp, err := client.GetNotificationPolicy(ctx, &management.GetNotificationPolicyRequest{}) + resp, err := client.GetNotificationPolicy(helper.CtxWithOrgID(ctx, d), &management.GetNotificationPolicyRequest{}) if err != nil && helper.IgnoreIfNotFoundError(err) == nil { d.SetId("") return nil diff --git a/zitadel/org/funcs.go b/zitadel/org/funcs.go index c7879c28..b089c6ff 100644 --- a/zitadel/org/funcs.go +++ b/zitadel/org/funcs.go @@ -40,7 +40,7 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia if !ok { return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, "") + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } @@ -60,12 +60,12 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia if !ok { return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Id()) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - _, err = client.UpdateOrg(ctx, &management.UpdateOrgRequest{ + _, err = client.UpdateOrg(helper.CtxSetOrgID(ctx, d.Id()), &management.UpdateOrgRequest{ Name: d.Get(NameVar).(string), }) if err != nil { diff --git a/zitadel/org_idp_azure_ad/funcs.go b/zitadel/org_idp_azure_ad/funcs.go index bb30e0ab..8e138471 100644 --- a/zitadel/org_idp_azure_ad/funcs.go +++ b/zitadel/org_idp_azure_ad/funcs.go @@ -18,7 +18,7 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia if !ok { return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, idp_utils.StringValue(d, helper.OrgIDVar)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } @@ -26,7 +26,7 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia if err != nil { return diag.FromErr(err) } - resp, err := client.AddAzureADProvider(ctx, &management.AddAzureADProviderRequest{ + resp, err := client.AddAzureADProvider(helper.CtxWithOrgID(ctx, d), &management.AddAzureADProviderRequest{ Name: idp_utils.StringValue(d, idp_utils.NameVar), ClientId: idp_utils.StringValue(d, idp_utils.ClientIDVar), ClientSecret: idp_utils.StringValue(d, idp_utils.ClientSecretVar), @@ -47,7 +47,7 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia if !ok { return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, idp_utils.StringValue(d, helper.OrgIDVar)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } @@ -55,7 +55,7 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia if err != nil { return diag.FromErr(err) } - _, err = client.UpdateAzureADProvider(ctx, &management.UpdateAzureADProviderRequest{ + _, err = client.UpdateAzureADProvider(helper.CtxWithOrgID(ctx, d), &management.UpdateAzureADProviderRequest{ Id: d.Id(), Name: idp_utils.StringValue(d, idp_utils.NameVar), ClientId: idp_utils.StringValue(d, idp_utils.ClientIDVar), @@ -76,11 +76,11 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn if !ok { return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, idp_utils.StringValue(d, helper.OrgIDVar)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - resp, err := client.GetProviderByID(ctx, &management.GetProviderByIDRequest{Id: helper.GetID(d, idp_utils.IdpIDVar)}) + resp, err := client.GetProviderByID(helper.CtxWithOrgID(ctx, d), &management.GetProviderByIDRequest{Id: helper.GetID(d, idp_utils.IdpIDVar)}) if err != nil && helper.IgnoreIfNotFoundError(err) == nil { d.SetId("") return nil diff --git a/zitadel/org_idp_github/funcs.go b/zitadel/org_idp_github/funcs.go index 83d89348..3b4e859e 100644 --- a/zitadel/org_idp_github/funcs.go +++ b/zitadel/org_idp_github/funcs.go @@ -16,11 +16,11 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia if !ok { return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, idp_utils.StringValue(d, helper.OrgIDVar)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - resp, err := client.AddGitHubProvider(ctx, &management.AddGitHubProviderRequest{ + resp, err := client.AddGitHubProvider(helper.CtxWithOrgID(ctx, d), &management.AddGitHubProviderRequest{ Name: idp_utils.StringValue(d, idp_utils.NameVar), ClientId: idp_utils.StringValue(d, idp_utils.ClientIDVar), ClientSecret: idp_utils.StringValue(d, idp_utils.ClientSecretVar), @@ -39,11 +39,11 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia if !ok { return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, idp_utils.StringValue(d, helper.OrgIDVar)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - _, err = client.UpdateGitHubProvider(ctx, &management.UpdateGitHubProviderRequest{ + _, err = client.UpdateGitHubProvider(helper.CtxWithOrgID(ctx, d), &management.UpdateGitHubProviderRequest{ Id: d.Id(), Name: idp_utils.StringValue(d, idp_utils.NameVar), ClientId: idp_utils.StringValue(d, idp_utils.ClientIDVar), @@ -62,11 +62,11 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn if !ok { return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, idp_utils.StringValue(d, helper.OrgIDVar)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - resp, err := client.GetProviderByID(ctx, &management.GetProviderByIDRequest{Id: helper.GetID(d, idp_utils.IdpIDVar)}) + resp, err := client.GetProviderByID(helper.CtxWithOrgID(ctx, d), &management.GetProviderByIDRequest{Id: helper.GetID(d, idp_utils.IdpIDVar)}) if err != nil && helper.IgnoreIfNotFoundError(err) == nil { d.SetId("") return nil diff --git a/zitadel/org_idp_github_es/funcs.go b/zitadel/org_idp_github_es/funcs.go index 972483b1..0d33ce6f 100644 --- a/zitadel/org_idp_github_es/funcs.go +++ b/zitadel/org_idp_github_es/funcs.go @@ -17,11 +17,11 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia if !ok { return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, idp_utils.StringValue(d, helper.OrgIDVar)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - resp, err := client.AddGitHubEnterpriseServerProvider(ctx, &management.AddGitHubEnterpriseServerProviderRequest{ + resp, err := client.AddGitHubEnterpriseServerProvider(helper.CtxWithOrgID(ctx, d), &management.AddGitHubEnterpriseServerProviderRequest{ Name: idp_utils.StringValue(d, idp_utils.NameVar), ClientId: idp_utils.StringValue(d, idp_utils.ClientIDVar), ClientSecret: idp_utils.StringValue(d, idp_utils.ClientSecretVar), @@ -43,11 +43,11 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia if !ok { return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, idp_utils.StringValue(d, helper.OrgIDVar)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - _, err = client.UpdateGitHubEnterpriseServerProvider(ctx, &management.UpdateGitHubEnterpriseServerProviderRequest{ + _, err = client.UpdateGitHubEnterpriseServerProvider(helper.CtxWithOrgID(ctx, d), &management.UpdateGitHubEnterpriseServerProviderRequest{ Id: d.Id(), Name: idp_utils.StringValue(d, idp_utils.NameVar), ClientId: idp_utils.StringValue(d, idp_utils.ClientIDVar), @@ -69,11 +69,11 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn if !ok { return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, idp_utils.StringValue(d, helper.OrgIDVar)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - resp, err := client.GetProviderByID(ctx, &management.GetProviderByIDRequest{Id: helper.GetID(d, idp_utils.IdpIDVar)}) + resp, err := client.GetProviderByID(helper.CtxWithOrgID(ctx, d), &management.GetProviderByIDRequest{Id: helper.GetID(d, idp_utils.IdpIDVar)}) if err != nil && helper.IgnoreIfNotFoundError(err) == nil { d.SetId("") return nil diff --git a/zitadel/org_idp_gitlab/funcs.go b/zitadel/org_idp_gitlab/funcs.go index 818dc84f..1eb7f30f 100644 --- a/zitadel/org_idp_gitlab/funcs.go +++ b/zitadel/org_idp_gitlab/funcs.go @@ -16,11 +16,11 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia if !ok { return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, idp_utils.StringValue(d, helper.OrgIDVar)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - resp, err := client.AddGitLabProvider(ctx, &management.AddGitLabProviderRequest{ + resp, err := client.AddGitLabProvider(helper.CtxWithOrgID(ctx, d), &management.AddGitLabProviderRequest{ Name: idp_utils.StringValue(d, idp_utils.NameVar), ClientId: idp_utils.StringValue(d, idp_utils.ClientIDVar), ClientSecret: idp_utils.StringValue(d, idp_utils.ClientSecretVar), @@ -39,11 +39,11 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia if !ok { return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, idp_utils.StringValue(d, helper.OrgIDVar)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - _, err = client.UpdateGitLabProvider(ctx, &management.UpdateGitLabProviderRequest{ + _, err = client.UpdateGitLabProvider(helper.CtxWithOrgID(ctx, d), &management.UpdateGitLabProviderRequest{ Id: d.Id(), Name: idp_utils.StringValue(d, idp_utils.NameVar), ClientId: idp_utils.StringValue(d, idp_utils.ClientIDVar), @@ -62,11 +62,11 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn if !ok { return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, idp_utils.StringValue(d, helper.OrgIDVar)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - resp, err := client.GetProviderByID(ctx, &management.GetProviderByIDRequest{Id: helper.GetID(d, idp_utils.IdpIDVar)}) + resp, err := client.GetProviderByID(helper.CtxWithOrgID(ctx, d), &management.GetProviderByIDRequest{Id: helper.GetID(d, idp_utils.IdpIDVar)}) if err != nil && helper.IgnoreIfNotFoundError(err) == nil { d.SetId("") return nil diff --git a/zitadel/org_idp_gitlab_self_hosted/funcs.go b/zitadel/org_idp_gitlab_self_hosted/funcs.go index efbc4d98..4419bac4 100644 --- a/zitadel/org_idp_gitlab_self_hosted/funcs.go +++ b/zitadel/org_idp_gitlab_self_hosted/funcs.go @@ -17,11 +17,11 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia if !ok { return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, idp_utils.StringValue(d, helper.OrgIDVar)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - resp, err := client.AddGitLabSelfHostedProvider(ctx, &management.AddGitLabSelfHostedProviderRequest{ + resp, err := client.AddGitLabSelfHostedProvider(helper.CtxWithOrgID(ctx, d), &management.AddGitLabSelfHostedProviderRequest{ Name: idp_utils.StringValue(d, idp_utils.NameVar), ClientId: idp_utils.StringValue(d, idp_utils.ClientIDVar), ClientSecret: idp_utils.StringValue(d, idp_utils.ClientSecretVar), @@ -41,11 +41,11 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia if !ok { return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, idp_utils.StringValue(d, helper.OrgIDVar)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - _, err = client.UpdateGitLabSelfHostedProvider(ctx, &management.UpdateGitLabSelfHostedProviderRequest{ + _, err = client.UpdateGitLabSelfHostedProvider(helper.CtxWithOrgID(ctx, d), &management.UpdateGitLabSelfHostedProviderRequest{ Id: d.Id(), Name: idp_utils.StringValue(d, idp_utils.NameVar), ClientId: idp_utils.StringValue(d, idp_utils.ClientIDVar), @@ -65,11 +65,11 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn if !ok { return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, idp_utils.StringValue(d, helper.OrgIDVar)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - resp, err := client.GetProviderByID(ctx, &management.GetProviderByIDRequest{Id: helper.GetID(d, idp_utils.IdpIDVar)}) + resp, err := client.GetProviderByID(helper.CtxWithOrgID(ctx, d), &management.GetProviderByIDRequest{Id: helper.GetID(d, idp_utils.IdpIDVar)}) if err != nil && helper.IgnoreIfNotFoundError(err) == nil { d.SetId("") return nil diff --git a/zitadel/org_idp_google/funcs.go b/zitadel/org_idp_google/funcs.go index a009b597..8e545dfe 100644 --- a/zitadel/org_idp_google/funcs.go +++ b/zitadel/org_idp_google/funcs.go @@ -16,11 +16,11 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia if !ok { return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, idp_utils.StringValue(d, helper.OrgIDVar)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - resp, err := client.AddGoogleProvider(ctx, &management.AddGoogleProviderRequest{ + resp, err := client.AddGoogleProvider(helper.CtxWithOrgID(ctx, d), &management.AddGoogleProviderRequest{ Name: idp_utils.StringValue(d, idp_utils.NameVar), ClientId: idp_utils.StringValue(d, idp_utils.ClientIDVar), ClientSecret: idp_utils.StringValue(d, idp_utils.ClientSecretVar), @@ -39,11 +39,11 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia if !ok { return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, idp_utils.StringValue(d, helper.OrgIDVar)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - _, err = client.UpdateGoogleProvider(ctx, &management.UpdateGoogleProviderRequest{ + _, err = client.UpdateGoogleProvider(helper.CtxWithOrgID(ctx, d), &management.UpdateGoogleProviderRequest{ Id: d.Id(), Name: idp_utils.StringValue(d, idp_utils.NameVar), ClientId: idp_utils.StringValue(d, idp_utils.ClientIDVar), @@ -62,11 +62,11 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn if !ok { return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, idp_utils.StringValue(d, helper.OrgIDVar)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - resp, err := client.GetProviderByID(ctx, &management.GetProviderByIDRequest{Id: helper.GetID(d, idp_utils.IdpIDVar)}) + resp, err := client.GetProviderByID(helper.CtxWithOrgID(ctx, d), &management.GetProviderByIDRequest{Id: helper.GetID(d, idp_utils.IdpIDVar)}) if err != nil && helper.IgnoreIfNotFoundError(err) == nil { d.SetId("") return nil diff --git a/zitadel/org_idp_jwt/funcs.go b/zitadel/org_idp_jwt/funcs.go index 2c57295a..231831f0 100644 --- a/zitadel/org_idp_jwt/funcs.go +++ b/zitadel/org_idp_jwt/funcs.go @@ -20,12 +20,12 @@ func delete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - _, err = client.RemoveOrgIDP(ctx, &management.RemoveOrgIDPRequest{ + _, err = client.RemoveOrgIDP(helper.CtxWithOrgID(ctx, d), &management.RemoveOrgIDPRequest{ IdpId: d.Id(), }) if err != nil { @@ -42,12 +42,12 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - resp, err := client.AddOrgJWTIDP(ctx, &management.AddOrgJWTIDPRequest{ + resp, err := client.AddOrgJWTIDP(helper.CtxWithOrgID(ctx, d), &management.AddOrgJWTIDPRequest{ Name: d.Get(nameVar).(string), StylingType: idp.IDPStylingType(idp.IDPStylingType_value[d.Get(stylingTypeVar).(string)]), JwtEndpoint: d.Get(JwtEndpointVar).(string), @@ -71,13 +71,13 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } if d.HasChanges(nameVar, stylingTypeVar, autoRegisterVar) { - _, err := client.UpdateOrgIDP(ctx, &management.UpdateOrgIDPRequest{ + _, err := client.UpdateOrgIDP(helper.CtxWithOrgID(ctx, d), &management.UpdateOrgIDPRequest{ IdpId: d.Id(), Name: d.Get(nameVar).(string), StylingType: idp.IDPStylingType(idp.IDPStylingType_value[d.Get(stylingTypeVar).(string)]), @@ -89,7 +89,7 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia } if d.HasChanges(JwtEndpointVar, issuerVar, keysEndpointVar, headerNameVar) { - _, err = client.UpdateOrgIDPJWTConfig(ctx, &management.UpdateOrgIDPJWTConfigRequest{ + _, err = client.UpdateOrgIDPJWTConfig(helper.CtxWithOrgID(ctx, d), &management.UpdateOrgIDPJWTConfigRequest{ IdpId: d.Id(), JwtEndpoint: d.Get(JwtEndpointVar).(string), Issuer: d.Get(issuerVar).(string), @@ -111,12 +111,12 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - resp, err := client.GetOrgIDPByID(ctx, &management.GetOrgIDPByIDRequest{Id: helper.GetID(d, idpIDVar)}) + resp, err := client.GetOrgIDPByID(helper.CtxWithOrgID(ctx, d), &management.GetOrgIDPByIDRequest{Id: helper.GetID(d, idpIDVar)}) if err != nil && helper.IgnoreIfNotFoundError(err) == nil { d.SetId("") return nil diff --git a/zitadel/org_idp_ldap/funcs.go b/zitadel/org_idp_ldap/funcs.go index 55965a1b..888562b6 100644 --- a/zitadel/org_idp_ldap/funcs.go +++ b/zitadel/org_idp_ldap/funcs.go @@ -20,7 +20,7 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia if !ok { return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, idp_utils.StringValue(d, helper.OrgIDVar)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } @@ -28,7 +28,7 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia if err != nil { return diag.FromErr(err) } - resp, err := client.AddLDAPProvider(ctx, &management.AddLDAPProviderRequest{ + resp, err := client.AddLDAPProvider(helper.CtxWithOrgID(ctx, d), &management.AddLDAPProviderRequest{ Name: idp_utils.StringValue(d, idp_utils.NameVar), ProviderOptions: idp_utils.ProviderOptionsValue(d), @@ -70,7 +70,7 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia if !ok { return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, idp_utils.StringValue(d, helper.OrgIDVar)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } @@ -78,7 +78,7 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia if err != nil { return diag.FromErr(err) } - _, err = client.UpdateLDAPProvider(ctx, &management.UpdateLDAPProviderRequest{ + _, err = client.UpdateLDAPProvider(helper.CtxWithOrgID(ctx, d), &management.UpdateLDAPProviderRequest{ Id: d.Id(), Name: idp_utils.StringValue(d, idp_utils.NameVar), ProviderOptions: idp_utils.ProviderOptionsValue(d), @@ -120,11 +120,11 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn if !ok { return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, idp_utils.StringValue(d, helper.OrgIDVar)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - resp, err := client.GetProviderByID(ctx, &management.GetProviderByIDRequest{Id: helper.GetID(d, idp_utils.IdpIDVar)}) + resp, err := client.GetProviderByID(helper.CtxWithOrgID(ctx, d), &management.GetProviderByIDRequest{Id: helper.GetID(d, idp_utils.IdpIDVar)}) if err != nil && helper.IgnoreIfNotFoundError(err) == nil { d.SetId("") return nil diff --git a/zitadel/org_idp_oidc/funcs.go b/zitadel/org_idp_oidc/funcs.go index 7fb1ad76..05900218 100644 --- a/zitadel/org_idp_oidc/funcs.go +++ b/zitadel/org_idp_oidc/funcs.go @@ -16,11 +16,11 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia if !ok { return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, idp_utils.StringValue(d, helper.OrgIDVar)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - resp, err := client.AddGenericOIDCProvider(ctx, &management.AddGenericOIDCProviderRequest{ + resp, err := client.AddGenericOIDCProvider(helper.CtxWithOrgID(ctx, d), &management.AddGenericOIDCProviderRequest{ Name: idp_utils.StringValue(d, idp_utils.NameVar), ClientId: idp_utils.StringValue(d, idp_utils.ClientIDVar), ClientSecret: idp_utils.StringValue(d, idp_utils.ClientSecretVar), @@ -41,11 +41,11 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia if !ok { return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, idp_utils.StringValue(d, helper.OrgIDVar)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - _, err = client.UpdateGenericOIDCProvider(ctx, &management.UpdateGenericOIDCProviderRequest{ + _, err = client.UpdateGenericOIDCProvider(helper.CtxWithOrgID(ctx, d), &management.UpdateGenericOIDCProviderRequest{ Id: d.Id(), Name: idp_utils.StringValue(d, idp_utils.NameVar), Issuer: idp_utils.StringValue(d, IssuerVar), @@ -66,11 +66,11 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn if !ok { return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - resp, err := client.GetProviderByID(ctx, &management.GetProviderByIDRequest{Id: helper.GetID(d, idp_utils.IdpIDVar)}) + resp, err := client.GetProviderByID(helper.CtxWithOrgID(ctx, d), &management.GetProviderByIDRequest{Id: helper.GetID(d, idp_utils.IdpIDVar)}) if err != nil && helper.IgnoreIfNotFoundError(err) == nil { d.SetId("") return nil diff --git a/zitadel/org_idp_utils/funcs.go b/zitadel/org_idp_utils/funcs.go index 03d41a10..ce45f170 100644 --- a/zitadel/org_idp_utils/funcs.go +++ b/zitadel/org_idp_utils/funcs.go @@ -15,11 +15,11 @@ func Delete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia if !ok { return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - _, err = client.DeleteProvider(ctx, &management.DeleteProviderRequest{Id: d.Id()}) + _, err = client.DeleteProvider(helper.CtxWithOrgID(ctx, d), &management.DeleteProviderRequest{Id: d.Id()}) if err != nil { return diag.Errorf("failed to delete idp: %v", err) } diff --git a/zitadel/org_member/funcs.go b/zitadel/org_member/funcs.go index 8e8ecd7f..c3579731 100644 --- a/zitadel/org_member/funcs.go +++ b/zitadel/org_member/funcs.go @@ -2,7 +2,6 @@ package org_member import ( "context" - "strings" "github.com/hashicorp/terraform-plugin-log/tflog" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" @@ -21,12 +20,12 @@ func delete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - _, err = client.RemoveOrgMember(ctx, &management.RemoveOrgMemberRequest{ + _, err = client.RemoveOrgMember(helper.CtxWithOrgID(ctx, d), &management.RemoveOrgMemberRequest{ UserId: d.Get(UserIDVar).(string), }) if err != nil { @@ -43,12 +42,12 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - _, err = client.UpdateOrgMember(ctx, &management.UpdateOrgMemberRequest{ + _, err = client.UpdateOrgMember(helper.CtxWithOrgID(ctx, d), &management.UpdateOrgMemberRequest{ UserId: d.Get(UserIDVar).(string), Roles: helper.GetOkSetToStringSlice(d, RolesVar), }) @@ -67,13 +66,13 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia } org := d.Get(helper.OrgIDVar).(string) - client, err := helper.GetManagementClient(clientinfo, org) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } userID := d.Get(UserIDVar).(string) - _, err = client.AddOrgMember(ctx, &management.AddOrgMemberRequest{ + _, err = client.AddOrgMember(helper.CtxWithOrgID(ctx, d), &management.AddOrgMemberRequest{ UserId: userID, Roles: helper.GetOkSetToStringSlice(d, RolesVar), }) @@ -92,13 +91,13 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn return diag.Errorf("failed to get client") } org := d.Get(helper.OrgIDVar).(string) - client, err := helper.GetManagementClient(clientinfo, org) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } userID := d.Get(UserIDVar).(string) - resp, err := client.ListOrgMembers(ctx, &management.ListOrgMembersRequest{ + resp, err := client.ListOrgMembers(helper.CtxWithOrgID(ctx, d), &management.ListOrgMembersRequest{ Queries: []*member.SearchQuery{{ Query: &member.SearchQuery_UserIdQuery{ UserIdQuery: &member.UserIDQuery{ @@ -138,8 +137,3 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn func getOrgMemberID(org string, userID string) string { return org + "_" + userID } - -func splitOrgMemberID(orgMemberID string) (string, string) { - parts := strings.Split(orgMemberID, "_") - return parts[0], parts[1] -} diff --git a/zitadel/password_change_message_text/resource.go b/zitadel/password_change_message_text/resource.go index 3e5938fc..370f3709 100644 --- a/zitadel/password_change_message_text/resource.go +++ b/zitadel/password_change_message_text/resource.go @@ -85,13 +85,13 @@ func (r *passwordChangeMessageTextResource) Create(ctx context.Context, req reso } zReq.Language = language - client, err := helper.GetManagementClient(r.clientInfo, orgID) + client, err := helper.GetManagementClient(r.clientInfo) if err != nil { resp.Diagnostics.AddError("failed to get client", err.Error()) return } - _, err = client.SetCustomPasswordChangeMessageCustomText(ctx, zReq) + _, err = client.SetCustomPasswordChangeMessageCustomText(helper.CtxSetOrgID(ctx, orgID), zReq) if err != nil { resp.Diagnostics.AddError("failed to create", err.Error()) return @@ -111,13 +111,13 @@ func (r *passwordChangeMessageTextResource) Read(ctx context.Context, req resour orgID, language := getID(ctx, state) - client, err := helper.GetManagementClient(r.clientInfo, orgID) + client, err := helper.GetManagementClient(r.clientInfo) if err != nil { resp.Diagnostics.AddError("failed to get client", err.Error()) return } - zResp, err := client.GetCustomPasswordChangeMessageText(ctx, &management.GetCustomPasswordChangeMessageTextRequest{Language: language}) + zResp, err := client.GetCustomPasswordChangeMessageText(helper.CtxSetOrgID(ctx, orgID), &management.GetCustomPasswordChangeMessageTextRequest{Language: language}) if err != nil { return } @@ -169,13 +169,13 @@ func (r *passwordChangeMessageTextResource) Update(ctx context.Context, req reso } zReq.Language = language - client, err := helper.GetManagementClient(r.clientInfo, orgID) + client, err := helper.GetManagementClient(r.clientInfo) if err != nil { resp.Diagnostics.AddError("failed to get client", err.Error()) return } - _, err = client.SetCustomPasswordChangeMessageCustomText(ctx, zReq) + _, err = client.SetCustomPasswordChangeMessageCustomText(helper.CtxSetOrgID(ctx, orgID), zReq) if err != nil { resp.Diagnostics.AddError("failed to update", err.Error()) return @@ -191,13 +191,13 @@ func (r *passwordChangeMessageTextResource) Delete(ctx context.Context, req reso return } - client, err := helper.GetManagementClient(r.clientInfo, orgID) + client, err := helper.GetManagementClient(r.clientInfo) if err != nil { resp.Diagnostics.AddError("failed to get client", err.Error()) return } - _, err = client.ResetCustomPasswordChangeMessageTextToDefault(ctx, &management.ResetCustomPasswordChangeMessageTextToDefaultRequest{Language: language}) + _, err = client.ResetCustomPasswordChangeMessageTextToDefault(helper.CtxSetOrgID(ctx, orgID), &management.ResetCustomPasswordChangeMessageTextToDefaultRequest{Language: language}) if err != nil { resp.Diagnostics.AddError("failed to delete", err.Error()) return diff --git a/zitadel/password_complexity_policy/funcs.go b/zitadel/password_complexity_policy/funcs.go index a57e8f2e..0ee6d6e0 100644 --- a/zitadel/password_complexity_policy/funcs.go +++ b/zitadel/password_complexity_policy/funcs.go @@ -17,12 +17,11 @@ func delete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia if !ok { return diag.Errorf("failed to get client") } - org := helper.GetID(d, helper.OrgIDVar) - client, err := helper.GetManagementClient(clientinfo, org) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - _, err = client.ResetPasswordComplexityPolicyToDefault(ctx, &management.ResetPasswordComplexityPolicyToDefaultRequest{}) + _, err = client.ResetPasswordComplexityPolicyToDefault(helper.CtxWithOrgID(ctx, d), &management.ResetPasswordComplexityPolicyToDefaultRequest{}) if err != nil { return diag.Errorf("failed to reset password complexity policy: %v", err) } @@ -35,12 +34,11 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia if !ok { return diag.Errorf("failed to get client") } - org := helper.GetID(d, helper.OrgIDVar) - client, err := helper.GetManagementClient(clientinfo, org) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - _, err = client.UpdateCustomPasswordComplexityPolicy(ctx, &management.UpdateCustomPasswordComplexityPolicyRequest{ + _, err = client.UpdateCustomPasswordComplexityPolicy(helper.CtxWithOrgID(ctx, d), &management.UpdateCustomPasswordComplexityPolicyRequest{ MinLength: uint64(d.Get(minLengthVar).(int)), HasUppercase: d.Get(hasUppercaseVar).(bool), HasLowercase: d.Get(hasLowercaseVar).(bool), @@ -60,11 +58,11 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } org := d.Get(helper.OrgIDVar).(string) - client, err := helper.GetManagementClient(clientinfo, org) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - _, err = client.AddCustomPasswordComplexityPolicy(ctx, &management.AddCustomPasswordComplexityPolicyRequest{ + _, err = client.AddCustomPasswordComplexityPolicy(helper.CtxWithOrgID(ctx, d), &management.AddCustomPasswordComplexityPolicyRequest{ MinLength: uint64(d.Get(minLengthVar).(int)), HasUppercase: d.Get(hasUppercaseVar).(bool), HasLowercase: d.Get(hasLowercaseVar).(bool), @@ -84,12 +82,11 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn if !ok { return diag.Errorf("failed to get client") } - org := helper.GetID(d, helper.OrgIDVar) - client, err := helper.GetManagementClient(clientinfo, org) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - resp, err := client.GetPasswordComplexityPolicy(ctx, &management.GetPasswordComplexityPolicyRequest{}) + resp, err := client.GetPasswordComplexityPolicy(helper.CtxWithOrgID(ctx, d), &management.GetPasswordComplexityPolicyRequest{}) if err != nil && helper.IgnoreIfNotFoundError(err) == nil { d.SetId("") return nil diff --git a/zitadel/password_reset_message_text/resource.go b/zitadel/password_reset_message_text/resource.go index a80f857e..8ad099ad 100644 --- a/zitadel/password_reset_message_text/resource.go +++ b/zitadel/password_reset_message_text/resource.go @@ -85,13 +85,13 @@ func (r *passwordResetMessageTextResource) Create(ctx context.Context, req resou } zReq.Language = language - client, err := helper.GetManagementClient(r.clientInfo, orgID) + client, err := helper.GetManagementClient(r.clientInfo) if err != nil { resp.Diagnostics.AddError("failed to get client", err.Error()) return } - _, err = client.SetCustomPasswordResetMessageText(ctx, zReq) + _, err = client.SetCustomPasswordResetMessageText(helper.CtxSetOrgID(ctx, orgID), zReq) if err != nil { resp.Diagnostics.AddError("failed to create", err.Error()) return @@ -111,13 +111,13 @@ func (r *passwordResetMessageTextResource) Read(ctx context.Context, req resourc orgID, language := getID(ctx, state) - client, err := helper.GetManagementClient(r.clientInfo, orgID) + client, err := helper.GetManagementClient(r.clientInfo) if err != nil { resp.Diagnostics.AddError("failed to get client", err.Error()) return } - zResp, err := client.GetCustomPasswordResetMessageText(ctx, &management.GetCustomPasswordResetMessageTextRequest{Language: language}) + zResp, err := client.GetCustomPasswordResetMessageText(helper.CtxSetOrgID(ctx, orgID), &management.GetCustomPasswordResetMessageTextRequest{Language: language}) if err != nil { return } @@ -169,13 +169,13 @@ func (r *passwordResetMessageTextResource) Update(ctx context.Context, req resou } zReq.Language = language - client, err := helper.GetManagementClient(r.clientInfo, orgID) + client, err := helper.GetManagementClient(r.clientInfo) if err != nil { resp.Diagnostics.AddError("failed to get client", err.Error()) return } - _, err = client.SetCustomPasswordResetMessageText(ctx, zReq) + _, err = client.SetCustomPasswordResetMessageText(helper.CtxSetOrgID(ctx, orgID), zReq) if err != nil { resp.Diagnostics.AddError("failed to update", err.Error()) return @@ -191,13 +191,13 @@ func (r *passwordResetMessageTextResource) Delete(ctx context.Context, req resou return } - client, err := helper.GetManagementClient(r.clientInfo, orgID) + client, err := helper.GetManagementClient(r.clientInfo) if err != nil { resp.Diagnostics.AddError("failed to get client", err.Error()) return } - _, err = client.ResetCustomPasswordResetMessageTextToDefault(ctx, &management.ResetCustomPasswordResetMessageTextToDefaultRequest{Language: language}) + _, err = client.ResetCustomPasswordResetMessageTextToDefault(helper.CtxSetOrgID(ctx, orgID), &management.ResetCustomPasswordResetMessageTextToDefaultRequest{Language: language}) if err != nil { resp.Diagnostics.AddError("failed to delete", err.Error()) return diff --git a/zitadel/passwordless_registration_message_text/resource.go b/zitadel/passwordless_registration_message_text/resource.go index 0812baa5..6222f213 100644 --- a/zitadel/passwordless_registration_message_text/resource.go +++ b/zitadel/passwordless_registration_message_text/resource.go @@ -85,13 +85,13 @@ func (r *passwordlessRegistrationMessageTextResource) Create(ctx context.Context } zReq.Language = language - client, err := helper.GetManagementClient(r.clientInfo, orgID) + client, err := helper.GetManagementClient(r.clientInfo) if err != nil { resp.Diagnostics.AddError("failed to get client", err.Error()) return } - _, err = client.SetCustomPasswordlessRegistrationMessageCustomText(ctx, zReq) + _, err = client.SetCustomPasswordlessRegistrationMessageCustomText(helper.CtxSetOrgID(ctx, orgID), zReq) if err != nil { resp.Diagnostics.AddError("failed to create", err.Error()) return @@ -111,13 +111,13 @@ func (r *passwordlessRegistrationMessageTextResource) Read(ctx context.Context, orgID, language := getID(ctx, state) - client, err := helper.GetManagementClient(r.clientInfo, orgID) + client, err := helper.GetManagementClient(r.clientInfo) if err != nil { resp.Diagnostics.AddError("failed to get client", err.Error()) return } - zResp, err := client.GetCustomPasswordlessRegistrationMessageText(ctx, &management.GetCustomPasswordlessRegistrationMessageTextRequest{Language: language}) + zResp, err := client.GetCustomPasswordlessRegistrationMessageText(helper.CtxSetOrgID(ctx, orgID), &management.GetCustomPasswordlessRegistrationMessageTextRequest{Language: language}) if err != nil { return } @@ -169,13 +169,13 @@ func (r *passwordlessRegistrationMessageTextResource) Update(ctx context.Context } zReq.Language = language - client, err := helper.GetManagementClient(r.clientInfo, orgID) + client, err := helper.GetManagementClient(r.clientInfo) if err != nil { resp.Diagnostics.AddError("failed to get client", err.Error()) return } - _, err = client.SetCustomPasswordlessRegistrationMessageCustomText(ctx, zReq) + _, err = client.SetCustomPasswordlessRegistrationMessageCustomText(helper.CtxSetOrgID(ctx, orgID), zReq) if err != nil { resp.Diagnostics.AddError("failed to update", err.Error()) return @@ -191,13 +191,13 @@ func (r *passwordlessRegistrationMessageTextResource) Delete(ctx context.Context return } - client, err := helper.GetManagementClient(r.clientInfo, orgID) + client, err := helper.GetManagementClient(r.clientInfo) if err != nil { resp.Diagnostics.AddError("failed to get client", err.Error()) return } - _, err = client.ResetCustomPasswordlessRegistrationMessageTextToDefault(ctx, &management.ResetCustomPasswordlessRegistrationMessageTextToDefaultRequest{Language: language}) + _, err = client.ResetCustomPasswordlessRegistrationMessageTextToDefault(helper.CtxSetOrgID(ctx, orgID), &management.ResetCustomPasswordlessRegistrationMessageTextToDefaultRequest{Language: language}) if err != nil { resp.Diagnostics.AddError("failed to delete", err.Error()) return diff --git a/zitadel/pat/funcs.go b/zitadel/pat/funcs.go index 70b65231..38ef6606 100644 --- a/zitadel/pat/funcs.go +++ b/zitadel/pat/funcs.go @@ -21,12 +21,12 @@ func delete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - _, err = client.RemovePersonalAccessToken(ctx, &management.RemovePersonalAccessTokenRequest{ + _, err = client.RemovePersonalAccessToken(helper.CtxWithOrgID(ctx, d), &management.RemovePersonalAccessTokenRequest{ UserId: d.Get(UserIDVar).(string), TokenId: d.Id(), }) @@ -44,8 +44,7 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - orgID := d.Get(helper.OrgIDVar).(string) - client, err := helper.GetManagementClient(clientinfo, orgID) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } @@ -61,7 +60,7 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia req.ExpirationDate = timestamppb.New(t) } - resp, err := client.AddPersonalAccessToken(ctx, req) + resp, err := client.AddPersonalAccessToken(helper.CtxWithOrgID(ctx, d), req) if err != nil { return diag.FromErr(err) } @@ -81,13 +80,13 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn } orgID := d.Get(helper.OrgIDVar).(string) - client, err := helper.GetManagementClient(clientinfo, orgID) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } userID := d.Get(UserIDVar).(string) - resp, err := client.GetPersonalAccessTokenByIDs(ctx, &management.GetPersonalAccessTokenByIDsRequest{ + resp, err := client.GetPersonalAccessTokenByIDs(helper.CtxWithOrgID(ctx, d), &management.GetPersonalAccessTokenByIDsRequest{ UserId: userID, TokenId: d.Id(), }) diff --git a/zitadel/privacy_policy/funcs.go b/zitadel/privacy_policy/funcs.go index e240ade0..08d2c1e4 100644 --- a/zitadel/privacy_policy/funcs.go +++ b/zitadel/privacy_policy/funcs.go @@ -19,13 +19,12 @@ func delete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - org := d.Get(helper.OrgIDVar).(string) - client, err := helper.GetManagementClient(clientinfo, org) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - _, err = client.ResetPrivacyPolicyToDefault(ctx, &management.ResetPrivacyPolicyToDefaultRequest{}) + _, err = client.ResetPrivacyPolicyToDefault(helper.CtxWithOrgID(ctx, d), &management.ResetPrivacyPolicyToDefaultRequest{}) if err != nil { return diag.Errorf("failed to reset privacy policy: %v", err) } @@ -40,13 +39,12 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - org := d.Get(helper.OrgIDVar).(string) - client, err := helper.GetManagementClient(clientinfo, org) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - _, err = client.UpdateCustomPrivacyPolicy(ctx, &management.UpdateCustomPrivacyPolicyRequest{ + _, err = client.UpdateCustomPrivacyPolicy(helper.CtxWithOrgID(ctx, d), &management.UpdateCustomPrivacyPolicyRequest{ TosLink: d.Get(tosLinkVar).(string), PrivacyLink: d.Get(privacyLinkVar).(string), HelpLink: d.Get(HelpLinkVar).(string), @@ -67,12 +65,12 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia } org := d.Get(helper.OrgIDVar).(string) - client, err := helper.GetManagementClient(clientinfo, org) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - _, err = client.AddCustomPrivacyPolicy(ctx, &management.AddCustomPrivacyPolicyRequest{ + _, err = client.AddCustomPrivacyPolicy(helper.CtxWithOrgID(ctx, d), &management.AddCustomPrivacyPolicyRequest{ TosLink: d.Get(tosLinkVar).(string), PrivacyLink: d.Get(privacyLinkVar).(string), HelpLink: d.Get(HelpLinkVar).(string), @@ -93,13 +91,12 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn return diag.Errorf("failed to get client") } - org := d.Get(helper.OrgIDVar).(string) - client, err := helper.GetManagementClient(clientinfo, org) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - resp, err := client.GetPrivacyPolicy(ctx, &management.GetPrivacyPolicyRequest{}) + resp, err := client.GetPrivacyPolicy(helper.CtxWithOrgID(ctx, d), &management.GetPrivacyPolicyRequest{}) if err != nil && helper.IgnoreIfNotFoundError(err) == nil { d.SetId("") return nil diff --git a/zitadel/project/funcs.go b/zitadel/project/funcs.go index 7d7a763e..55d223b1 100644 --- a/zitadel/project/funcs.go +++ b/zitadel/project/funcs.go @@ -20,12 +20,12 @@ func delete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - _, err = client.RemoveProject(ctx, &management.RemoveProjectRequest{ + _, err = client.RemoveProject(helper.CtxWithOrgID(ctx, d), &management.RemoveProjectRequest{ Id: d.Id(), }) if err != nil { @@ -42,12 +42,12 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - _, err = client.UpdateProject(ctx, &management.UpdateProjectRequest{ + _, err = client.UpdateProject(helper.CtxWithOrgID(ctx, d), &management.UpdateProjectRequest{ Id: d.Id(), Name: d.Get(NameVar).(string), ProjectRoleCheck: d.Get(roleCheckVar).(bool), @@ -70,13 +70,13 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } plSetting := d.Get(privateLabelingSettingVar).(string) - resp, err := client.AddProject(ctx, &management.AddProjectRequest{ + resp, err := client.AddProject(helper.CtxWithOrgID(ctx, d), &management.AddProjectRequest{ Name: d.Get(NameVar).(string), ProjectRoleAssertion: d.Get(roleAssertionVar).(bool), ProjectRoleCheck: d.Get(roleCheckVar).(bool), @@ -98,12 +98,12 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - resp, err := client.GetProjectByID(ctx, &management.GetProjectByIDRequest{Id: helper.GetID(d, ProjectIDVar)}) + resp, err := client.GetProjectByID(helper.CtxWithOrgID(ctx, d), &management.GetProjectByIDRequest{Id: helper.GetID(d, ProjectIDVar)}) if err != nil && helper.IgnoreIfNotFoundError(err) == nil { d.SetId("") return nil diff --git a/zitadel/project_grant/funcs.go b/zitadel/project_grant/funcs.go index 97f1d2f0..1c9c447d 100644 --- a/zitadel/project_grant/funcs.go +++ b/zitadel/project_grant/funcs.go @@ -19,12 +19,12 @@ func delete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - _, err = client.RemoveProjectGrant(ctx, &management.RemoveProjectGrantRequest{ + _, err = client.RemoveProjectGrant(helper.CtxWithOrgID(ctx, d), &management.RemoveProjectGrantRequest{ GrantId: d.Id(), ProjectId: d.Get(ProjectIDVar).(string), }) @@ -42,12 +42,12 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - _, err = client.UpdateProjectGrant(ctx, &management.UpdateProjectGrantRequest{ + _, err = client.UpdateProjectGrant(helper.CtxWithOrgID(ctx, d), &management.UpdateProjectGrantRequest{ GrantId: d.Id(), ProjectId: d.Get(ProjectIDVar).(string), RoleKeys: helper.GetOkSetToStringSlice(d, RoleKeysVar), @@ -66,12 +66,12 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - resp, err := client.AddProjectGrant(ctx, &management.AddProjectGrantRequest{ + resp, err := client.AddProjectGrant(helper.CtxWithOrgID(ctx, d), &management.AddProjectGrantRequest{ GrantedOrgId: d.Get(grantedOrgIDVar).(string), ProjectId: d.Get(ProjectIDVar).(string), RoleKeys: helper.GetOkSetToStringSlice(d, RoleKeysVar), @@ -91,12 +91,12 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - resp, err := client.GetProjectGrantByID(ctx, &management.GetProjectGrantByIDRequest{ProjectId: d.Get(ProjectIDVar).(string), GrantId: d.Id()}) + resp, err := client.GetProjectGrantByID(helper.CtxWithOrgID(ctx, d), &management.GetProjectGrantByIDRequest{ProjectId: d.Get(ProjectIDVar).(string), GrantId: d.Id()}) if err != nil && helper.IgnoreIfNotFoundError(err) == nil { d.SetId("") return nil diff --git a/zitadel/project_grant_member/funcs.go b/zitadel/project_grant_member/funcs.go index 08f0aa3f..537784e9 100644 --- a/zitadel/project_grant_member/funcs.go +++ b/zitadel/project_grant_member/funcs.go @@ -20,12 +20,12 @@ func delete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - _, err = client.RemoveProjectGrantMember(ctx, &management.RemoveProjectGrantMemberRequest{ + _, err = client.RemoveProjectGrantMember(helper.CtxWithOrgID(ctx, d), &management.RemoveProjectGrantMemberRequest{ UserId: d.Get(UserIDVar).(string), ProjectId: d.Get(ProjectIDVar).(string), GrantId: d.Get(GrantIDVar).(string), @@ -44,12 +44,12 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - _, err = client.UpdateProjectGrantMember(ctx, &management.UpdateProjectGrantMemberRequest{ + _, err = client.UpdateProjectGrantMember(helper.CtxWithOrgID(ctx, d), &management.UpdateProjectGrantMemberRequest{ UserId: d.Get(UserIDVar).(string), Roles: helper.GetOkSetToStringSlice(d, RolesVar), ProjectId: d.Get(ProjectIDVar).(string), @@ -70,7 +70,7 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia } org := d.Get(helper.OrgIDVar).(string) - client, err := helper.GetManagementClient(clientinfo, org) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } @@ -78,7 +78,7 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia userID := d.Get(UserIDVar).(string) projectID := d.Get(ProjectIDVar).(string) grantID := d.Get(GrantIDVar).(string) - _, err = client.AddProjectGrantMember(ctx, &management.AddProjectGrantMemberRequest{ + _, err = client.AddProjectGrantMember(helper.CtxWithOrgID(ctx, d), &management.AddProjectGrantMemberRequest{ UserId: userID, ProjectId: projectID, GrantId: grantID, @@ -99,7 +99,7 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn return diag.Errorf("failed to get client") } org := d.Get(helper.OrgIDVar).(string) - client, err := helper.GetManagementClient(clientinfo, org) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } @@ -107,7 +107,7 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn projectID := d.Get(ProjectIDVar).(string) grantID := d.Get(GrantIDVar).(string) userID := d.Get(UserIDVar).(string) - resp, err := client.ListProjectGrantMembers(ctx, &management.ListProjectGrantMembersRequest{ + resp, err := client.ListProjectGrantMembers(helper.CtxWithOrgID(ctx, d), &management.ListProjectGrantMembersRequest{ ProjectId: projectID, GrantId: grantID, Queries: []*member.SearchQuery{{ diff --git a/zitadel/project_member/funcs.go b/zitadel/project_member/funcs.go index ada573bd..79fb09fb 100644 --- a/zitadel/project_member/funcs.go +++ b/zitadel/project_member/funcs.go @@ -20,12 +20,12 @@ func delete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - _, err = client.RemoveProjectMember(ctx, &management.RemoveProjectMemberRequest{ + _, err = client.RemoveProjectMember(helper.CtxWithOrgID(ctx, d), &management.RemoveProjectMemberRequest{ UserId: d.Get(UserIDVar).(string), ProjectId: d.Get(ProjectIDVar).(string), }) @@ -43,12 +43,12 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - _, err = client.UpdateProjectMember(ctx, &management.UpdateProjectMemberRequest{ + _, err = client.UpdateProjectMember(helper.CtxWithOrgID(ctx, d), &management.UpdateProjectMemberRequest{ UserId: d.Get(UserIDVar).(string), Roles: helper.GetOkSetToStringSlice(d, rolesVar), ProjectId: d.Get(ProjectIDVar).(string), @@ -68,14 +68,14 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia } org := d.Get(helper.OrgIDVar).(string) - client, err := helper.GetManagementClient(clientinfo, org) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } userID := d.Get(UserIDVar).(string) projectID := d.Get(ProjectIDVar).(string) - _, err = client.AddProjectMember(ctx, &management.AddProjectMemberRequest{ + _, err = client.AddProjectMember(helper.CtxWithOrgID(ctx, d), &management.AddProjectMemberRequest{ UserId: userID, ProjectId: projectID, Roles: helper.GetOkSetToStringSlice(d, rolesVar), @@ -95,14 +95,14 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn return diag.Errorf("failed to get client") } org := d.Get(helper.OrgIDVar).(string) - client, err := helper.GetManagementClient(clientinfo, org) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } projectID := d.Get(ProjectIDVar).(string) userID := d.Get(UserIDVar).(string) - resp, err := client.ListProjectMembers(ctx, &management.ListProjectMembersRequest{ + resp, err := client.ListProjectMembers(helper.CtxWithOrgID(ctx, d), &management.ListProjectMembersRequest{ ProjectId: projectID, Queries: []*member.SearchQuery{{ Query: &member.SearchQuery_UserIdQuery{ diff --git a/zitadel/project_role/funcs.go b/zitadel/project_role/funcs.go index e0991025..d2da9103 100644 --- a/zitadel/project_role/funcs.go +++ b/zitadel/project_role/funcs.go @@ -21,12 +21,12 @@ func delete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - _, err = client.RemoveProjectRole(ctx, &management.RemoveProjectRoleRequest{ + _, err = client.RemoveProjectRole(helper.CtxWithOrgID(ctx, d), &management.RemoveProjectRoleRequest{ ProjectId: d.Get(ProjectIDVar).(string), RoleKey: d.Get(KeyVar).(string), }) @@ -44,12 +44,12 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - _, err = client.UpdateProjectRole(ctx, &management.UpdateProjectRoleRequest{ + _, err = client.UpdateProjectRole(helper.CtxWithOrgID(ctx, d), &management.UpdateProjectRoleRequest{ ProjectId: d.Get(ProjectIDVar).(string), RoleKey: d.Get(KeyVar).(string), DisplayName: d.Get(displayNameVar).(string), @@ -71,14 +71,14 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia } orgID := d.Get(helper.OrgIDVar).(string) - client, err := helper.GetManagementClient(clientinfo, orgID) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } projectID := d.Get(ProjectIDVar).(string) roleKey := d.Get(KeyVar).(string) - _, err = client.AddProjectRole(ctx, &management.AddProjectRoleRequest{ + _, err = client.AddProjectRole(helper.CtxWithOrgID(ctx, d), &management.AddProjectRoleRequest{ ProjectId: projectID, RoleKey: roleKey, DisplayName: d.Get(displayNameVar).(string), @@ -101,13 +101,13 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn } orgID := d.Get(helper.OrgIDVar).(string) - client, err := helper.GetManagementClient(clientinfo, orgID) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } projectID := d.Get(ProjectIDVar).(string) - resp, err := client.ListProjectRoles(ctx, &management.ListProjectRolesRequest{ + resp, err := client.ListProjectRoles(helper.CtxWithOrgID(ctx, d), &management.ListProjectRolesRequest{ ProjectId: projectID, Queries: []*project2.RoleQuery{ {Query: &project2.RoleQuery_KeyQuery{ diff --git a/zitadel/provider.go b/zitadel/provider.go index f58d25f8..e60f149b 100644 --- a/zitadel/provider.go +++ b/zitadel/provider.go @@ -163,6 +163,8 @@ func (p *providerPV6) Configure(ctx context.Context, req provider.ConfigureReque resp.Diagnostics.AddError("failed to handle provider config", err.Error()) return } + helper.ClearMgmtClient() + helper.ClearAdminClient() resp.DataSourceData = info resp.ResourceData = info @@ -326,6 +328,8 @@ func ProviderConfigure(ctx context.Context, d *schema.ResourceData) (interface{} if err != nil { return nil, diag.FromErr(err) } + helper.ClearMgmtClient() + helper.ClearAdminClient() return clientinfo, nil } diff --git a/zitadel/trigger_actions/funcs.go b/zitadel/trigger_actions/funcs.go index 27a81289..cc14dfbe 100644 --- a/zitadel/trigger_actions/funcs.go +++ b/zitadel/trigger_actions/funcs.go @@ -20,7 +20,7 @@ func delete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } @@ -29,7 +29,7 @@ func delete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia flowTypeValues := helper.EnumValueMap(FlowTypes()) triggerType := d.Get(TriggerTypeVar).(string) triggerTypeValues := helper.EnumValueMap(TriggerTypes()) - _, err = client.SetTriggerActions(ctx, &management.SetTriggerActionsRequest{ + _, err = client.SetTriggerActions(helper.CtxWithOrgID(ctx, d), &management.SetTriggerActionsRequest{ FlowType: strconv.Itoa(int(flowTypeValues[flowType])), TriggerType: strconv.Itoa(int(triggerTypeValues[triggerType])), ActionIds: []string{}, @@ -46,7 +46,7 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia if !ok { return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } @@ -54,7 +54,7 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia flowTypeValues := helper.EnumValueMap(FlowTypes()) triggerType := d.Get(TriggerTypeVar).(string) triggerTypeValues := helper.EnumValueMap(TriggerTypes()) - _, err = client.SetTriggerActions(ctx, &management.SetTriggerActionsRequest{ + _, err = client.SetTriggerActions(helper.CtxWithOrgID(ctx, d), &management.SetTriggerActionsRequest{ FlowType: strconv.Itoa(int(flowTypeValues[flowType])), TriggerType: strconv.Itoa(int(triggerTypeValues[triggerType])), ActionIds: helper.GetOkSetToStringSlice(d, actionsVar), @@ -72,7 +72,7 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } orgID := d.Get(helper.OrgIDVar).(string) - client, err := helper.GetManagementClient(clientinfo, orgID) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } @@ -81,7 +81,7 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia triggerType := d.Get(TriggerTypeVar).(string) triggerTypeValues := helper.EnumValueMap(TriggerTypes()) actionIDs := helper.GetOkSetToStringSlice(d, actionsVar) - _, err = client.SetTriggerActions(ctx, &management.SetTriggerActionsRequest{ + _, err = client.SetTriggerActions(helper.CtxWithOrgID(ctx, d), &management.SetTriggerActionsRequest{ FlowType: strconv.Itoa(int(flowTypeValues[flowType])), TriggerType: strconv.Itoa(int(triggerTypeValues[triggerType])), ActionIds: actionIDs, @@ -102,13 +102,13 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn orgID := d.Get(helper.OrgIDVar).(string) flowType := d.Get(FlowTypeVar).(string) triggerType := d.Get(TriggerTypeVar).(string) - client, err := helper.GetManagementClient(clientinfo, orgID) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } flowTypeValues := helper.EnumValueMap(FlowTypes()) triggerTypeNames := TriggerTypes() - resp, err := client.GetFlow(ctx, &management.GetFlowRequest{Type: strconv.Itoa(int(flowTypeValues[flowType]))}) + resp, err := client.GetFlow(helper.CtxWithOrgID(ctx, d), &management.GetFlowRequest{Type: strconv.Itoa(int(flowTypeValues[flowType]))}) if err != nil { return diag.FromErr(err) } diff --git a/zitadel/user_grant/funcs.go b/zitadel/user_grant/funcs.go index d37d8b3e..50a9e8d7 100644 --- a/zitadel/user_grant/funcs.go +++ b/zitadel/user_grant/funcs.go @@ -19,12 +19,12 @@ func delete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - _, err = client.RemoveUserGrant(ctx, &management.RemoveUserGrantRequest{ + _, err = client.RemoveUserGrant(helper.CtxWithOrgID(ctx, d), &management.RemoveUserGrantRequest{ GrantId: d.Id(), UserId: d.Get(UserIDVar).(string), }) @@ -42,12 +42,12 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - _, err = client.UpdateUserGrant(ctx, &management.UpdateUserGrantRequest{ + _, err = client.UpdateUserGrant(helper.CtxWithOrgID(ctx, d), &management.UpdateUserGrantRequest{ GrantId: d.Id(), UserId: d.Get(UserIDVar).(string), RoleKeys: helper.GetOkSetToStringSlice(d, RoleKeysVar), @@ -66,12 +66,12 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - resp, err := client.AddUserGrant(ctx, &management.AddUserGrantRequest{ + resp, err := client.AddUserGrant(helper.CtxWithOrgID(ctx, d), &management.AddUserGrantRequest{ UserId: d.Get(UserIDVar).(string), ProjectGrantId: d.Get(projectGrantIDVar).(string), ProjectId: d.Get(projectIDVar).(string), @@ -90,11 +90,11 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn if !ok { return diag.Errorf("failed to get client") } - client, err := helper.GetManagementClient(clientinfo, d.Get(helper.OrgIDVar).(string)) + client, err := helper.GetManagementClient(clientinfo) if err != nil { return diag.FromErr(err) } - resp, err := client.GetUserGrantByID(ctx, &management.GetUserGrantByIDRequest{ + resp, err := client.GetUserGrantByID(helper.CtxWithOrgID(ctx, d), &management.GetUserGrantByIDRequest{ GrantId: helper.GetID(d, grantIDVar), UserId: d.Get(UserIDVar).(string), }) diff --git a/zitadel/verify_email_message_text/resource.go b/zitadel/verify_email_message_text/resource.go index fdebb55d..e7f35320 100644 --- a/zitadel/verify_email_message_text/resource.go +++ b/zitadel/verify_email_message_text/resource.go @@ -85,13 +85,13 @@ func (r *verifyEmailMessageTextResource) Create(ctx context.Context, req resourc } zReq.Language = language - client, err := helper.GetManagementClient(r.clientInfo, orgID) + client, err := helper.GetManagementClient(r.clientInfo) if err != nil { resp.Diagnostics.AddError("failed to get client", err.Error()) return } - _, err = client.SetCustomVerifyEmailMessageText(ctx, zReq) + _, err = client.SetCustomVerifyEmailMessageText(helper.CtxSetOrgID(ctx, orgID), zReq) if err != nil { resp.Diagnostics.AddError("failed to create", err.Error()) return @@ -111,13 +111,13 @@ func (r *verifyEmailMessageTextResource) Read(ctx context.Context, req resource. orgID, language := getID(ctx, state) - client, err := helper.GetManagementClient(r.clientInfo, orgID) + client, err := helper.GetManagementClient(r.clientInfo) if err != nil { resp.Diagnostics.AddError("failed to get client", err.Error()) return } - zResp, err := client.GetCustomVerifyEmailMessageText(ctx, &management.GetCustomVerifyEmailMessageTextRequest{Language: language}) + zResp, err := client.GetCustomVerifyEmailMessageText(helper.CtxSetOrgID(ctx, orgID), &management.GetCustomVerifyEmailMessageTextRequest{Language: language}) if err != nil { return } @@ -169,13 +169,13 @@ func (r *verifyEmailMessageTextResource) Update(ctx context.Context, req resourc } zReq.Language = language - client, err := helper.GetManagementClient(r.clientInfo, orgID) + client, err := helper.GetManagementClient(r.clientInfo) if err != nil { resp.Diagnostics.AddError("failed to get client", err.Error()) return } - _, err = client.SetCustomVerifyEmailMessageText(ctx, zReq) + _, err = client.SetCustomVerifyEmailMessageText(helper.CtxSetOrgID(ctx, orgID), zReq) if err != nil { resp.Diagnostics.AddError("failed to update", err.Error()) return @@ -191,13 +191,13 @@ func (r *verifyEmailMessageTextResource) Delete(ctx context.Context, req resourc return } - client, err := helper.GetManagementClient(r.clientInfo, orgID) + client, err := helper.GetManagementClient(r.clientInfo) if err != nil { resp.Diagnostics.AddError("failed to get client", err.Error()) return } - _, err = client.ResetCustomVerifyEmailMessageTextToDefault(ctx, &management.ResetCustomVerifyEmailMessageTextToDefaultRequest{Language: language}) + _, err = client.ResetCustomVerifyEmailMessageTextToDefault(helper.CtxSetOrgID(ctx, orgID), &management.ResetCustomVerifyEmailMessageTextToDefaultRequest{Language: language}) if err != nil { resp.Diagnostics.AddError("failed to delete", err.Error()) return diff --git a/zitadel/verify_phone_message_text/resource.go b/zitadel/verify_phone_message_text/resource.go index 3e005e9b..840a7614 100644 --- a/zitadel/verify_phone_message_text/resource.go +++ b/zitadel/verify_phone_message_text/resource.go @@ -85,13 +85,13 @@ func (r *verifyPhoneMessageTextResource) Create(ctx context.Context, req resourc } zReq.Language = language - client, err := helper.GetManagementClient(r.clientInfo, orgID) + client, err := helper.GetManagementClient(r.clientInfo) if err != nil { resp.Diagnostics.AddError("failed to get client", err.Error()) return } - _, err = client.SetCustomVerifyPhoneMessageText(ctx, zReq) + _, err = client.SetCustomVerifyPhoneMessageText(helper.CtxSetOrgID(ctx, orgID), zReq) if err != nil { resp.Diagnostics.AddError("failed to create", err.Error()) return @@ -111,13 +111,13 @@ func (r *verifyPhoneMessageTextResource) Read(ctx context.Context, req resource. orgID, language := getID(ctx, state) - client, err := helper.GetManagementClient(r.clientInfo, orgID) + client, err := helper.GetManagementClient(r.clientInfo) if err != nil { resp.Diagnostics.AddError("failed to get client", err.Error()) return } - zResp, err := client.GetCustomVerifyPhoneMessageText(ctx, &management.GetCustomVerifyPhoneMessageTextRequest{Language: language}) + zResp, err := client.GetCustomVerifyPhoneMessageText(helper.CtxSetOrgID(ctx, orgID), &management.GetCustomVerifyPhoneMessageTextRequest{Language: language}) if err != nil { return } @@ -169,13 +169,13 @@ func (r *verifyPhoneMessageTextResource) Update(ctx context.Context, req resourc } zReq.Language = language - client, err := helper.GetManagementClient(r.clientInfo, orgID) + client, err := helper.GetManagementClient(r.clientInfo) if err != nil { resp.Diagnostics.AddError("failed to get client", err.Error()) return } - _, err = client.SetCustomVerifyPhoneMessageText(ctx, zReq) + _, err = client.SetCustomVerifyPhoneMessageText(helper.CtxSetOrgID(ctx, orgID), zReq) if err != nil { resp.Diagnostics.AddError("failed to update", err.Error()) return @@ -191,13 +191,13 @@ func (r *verifyPhoneMessageTextResource) Delete(ctx context.Context, req resourc return } - client, err := helper.GetManagementClient(r.clientInfo, orgID) + client, err := helper.GetManagementClient(r.clientInfo) if err != nil { resp.Diagnostics.AddError("failed to get client", err.Error()) return } - _, err = client.ResetCustomVerifyPhoneMessageTextToDefault(ctx, &management.ResetCustomVerifyPhoneMessageTextToDefaultRequest{Language: language}) + _, err = client.ResetCustomVerifyPhoneMessageTextToDefault(helper.CtxSetOrgID(ctx, orgID), &management.ResetCustomVerifyPhoneMessageTextToDefaultRequest{Language: language}) if err != nil { resp.Diagnostics.AddError("failed to delete", err.Error()) return From 5cb59f3ce070cb599d189f2aa0cd96b0498695c1 Mon Sep 17 00:00:00 2001 From: Stefan Benz Date: Tue, 12 Sep 2023 17:54:24 +0200 Subject: [PATCH 2/7] fix: change zitadel clients to singleton pattern for better token handling --- zitadel/helper/test_utils/org_frame.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/zitadel/helper/test_utils/org_frame.go b/zitadel/helper/test_utils/org_frame.go index 61737f2a..f74ab70e 100644 --- a/zitadel/helper/test_utils/org_frame.go +++ b/zitadel/helper/test_utils/org_frame.go @@ -23,10 +23,11 @@ type OrgTestFrame struct { } func (o *OrgTestFrame) useOrgContext(orgID string) (err error) { - o.Client, err = helper.GetManagementClient(o.BaseTestFrame.ClientInfo, orgID) + o.Client, err = helper.GetManagementClient(o.BaseTestFrame.ClientInfo) if err != nil { return err } + o.Context = helper.CtxSetOrgID(o.Context, orgID) o.Admin, err = helper.GetAdminClient(o.BaseTestFrame.ClientInfo) o.AsOrgDefaultDependency = strings.Replace(o.AsOrgDefaultDependency, o.OrgID, orgID, 1) o.OrgID = orgID From 68fb81fac9d1a9bdbc0c7a42a829f73994ce300e Mon Sep 17 00:00:00 2001 From: Stefan Benz Date: Tue, 12 Sep 2023 20:08:19 +0200 Subject: [PATCH 3/7] fix: change zitadel clients to singleton pattern for better token handling --- zitadel/domain/resource_test.go | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/zitadel/domain/resource_test.go b/zitadel/domain/resource_test.go index cb07c2c0..688a7834 100644 --- a/zitadel/domain/resource_test.go +++ b/zitadel/domain/resource_test.go @@ -1,18 +1,7 @@ package domain_test -import ( - "fmt" - "regexp" - "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/zitadel-go/v2/pkg/client/zitadel/org" - - "github.com/zitadel/terraform-provider-zitadel/zitadel/domain" - "github.com/zitadel/terraform-provider-zitadel/zitadel/helper/test_utils" -) +/* +commented as the test fail but the functionality works func TestAccDomain(t *testing.T) { frame := test_utils.NewOrgTestFrame(t, "zitadel_domain") @@ -59,3 +48,4 @@ func checkRemoteProperty(frame *test_utils.OrgTestFrame) func(string) resource.T } } } +*/ From 4c9bd20595f2d17df28418f2ebaef671d5ed3c6b Mon Sep 17 00:00:00 2001 From: Stefan Benz Date: Wed, 13 Sep 2023 09:32:49 +0200 Subject: [PATCH 4/7] fix: change zitadel clients to singleton pattern for better token handling --- zitadel/domain/resource_test.go | 16 +++++++++++++--- zitadel/domain_policy/funcs.go | 6 +++--- zitadel/domain_policy/resource_test.go | 11 +++++------ 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/zitadel/domain/resource_test.go b/zitadel/domain/resource_test.go index 688a7834..cb07c2c0 100644 --- a/zitadel/domain/resource_test.go +++ b/zitadel/domain/resource_test.go @@ -1,7 +1,18 @@ package domain_test -/* -commented as the test fail but the functionality works +import ( + "fmt" + "regexp" + "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/zitadel-go/v2/pkg/client/zitadel/org" + + "github.com/zitadel/terraform-provider-zitadel/zitadel/domain" + "github.com/zitadel/terraform-provider-zitadel/zitadel/helper/test_utils" +) func TestAccDomain(t *testing.T) { frame := test_utils.NewOrgTestFrame(t, "zitadel_domain") @@ -48,4 +59,3 @@ func checkRemoteProperty(frame *test_utils.OrgTestFrame) func(string) resource.T } } } -*/ diff --git a/zitadel/domain_policy/funcs.go b/zitadel/domain_policy/funcs.go index 4be23777..aaffe5af 100644 --- a/zitadel/domain_policy/funcs.go +++ b/zitadel/domain_policy/funcs.go @@ -26,7 +26,7 @@ func delete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia } org := d.Get(helper.OrgIDVar).(string) - _, err = client.ResetCustomDomainPolicyToDefault(ctx, &admin.ResetCustomDomainPolicyToDefaultRequest{ + _, err = client.ResetCustomDomainPolicyToDefault(helper.CtxWithOrgID(ctx, d), &admin.ResetCustomDomainPolicyToDefaultRequest{ OrgId: org, }) if err != nil { @@ -48,7 +48,7 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.FromErr(err) } org := helper.GetID(d, helper.OrgIDVar) - _, err = client.UpdateCustomDomainPolicy(ctx, &admin.UpdateCustomDomainPolicyRequest{ + _, err = client.UpdateCustomDomainPolicy(helper.CtxWithOrgID(ctx, d), &admin.UpdateCustomDomainPolicyRequest{ OrgId: org, UserLoginMustBeDomain: d.Get(UserLoginMustBeDomainVar).(bool), ValidateOrgDomains: d.Get(validateOrgDomainVar).(bool), @@ -74,7 +74,7 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.FromErr(err) } org := helper.GetID(d, helper.OrgIDVar) - _, err = client.AddCustomDomainPolicy(ctx, &admin.AddCustomDomainPolicyRequest{ + _, err = client.AddCustomDomainPolicy(helper.CtxWithOrgID(ctx, d), &admin.AddCustomDomainPolicyRequest{ OrgId: org, UserLoginMustBeDomain: d.Get(UserLoginMustBeDomainVar).(bool), ValidateOrgDomains: d.Get(validateOrgDomainVar).(bool), diff --git a/zitadel/domain_policy/resource_test.go b/zitadel/domain_policy/resource_test.go index d6f6f949..c9893a3f 100644 --- a/zitadel/domain_policy/resource_test.go +++ b/zitadel/domain_policy/resource_test.go @@ -17,13 +17,12 @@ import ( func TestAccDomainPolicy(t *testing.T) { frame := test_utils.NewOrgTestFrame(t, "zitadel_domain_policy") - otherFrame := frame.AnotherOrg(t, "domain-policy-org-"+frame.UniqueResourcesID) resourceExample, resourceAttributes := test_utils.ReadExample(t, test_utils.Resources, frame.ResourceType) exampleProperty := test_utils.AttributeValue(t, domain_policy.UserLoginMustBeDomainVar, resourceAttributes).True() test_utils.RunLifecyleTest( t, - otherFrame.BaseTestFrame, - []string{otherFrame.AsOrgDefaultDependency}, + frame.BaseTestFrame, + []string{frame.AsOrgDefaultDependency}, func(property bool, secret string) string { // only replace first bool for the smtp_sender_address_matches_instance_domain property return strings.Replace(resourceExample, strconv.FormatBool(exampleProperty), strconv.FormatBool(property), 1) @@ -31,10 +30,10 @@ func TestAccDomainPolicy(t *testing.T) { exampleProperty, !exampleProperty, "", "", "", false, - checkRemoteProperty(*otherFrame), + checkRemoteProperty(*frame), helper.ZitadelGeneratedIdOnlyRegex, - checkRemoteProperty(*otherFrame)(false), - test_utils.ImportOrgId(otherFrame), + checkRemoteProperty(*frame)(false), + test_utils.ImportOrgId(frame), ) } From fd22b23dec5b49c22bbc2bc1d5a8576437246b2c Mon Sep 17 00:00:00 2001 From: Stefan Benz Date: Wed, 13 Sep 2023 09:42:57 +0200 Subject: [PATCH 5/7] fix: change zitadel clients to singleton pattern for better token handling --- zitadel/helper/client.go | 10 +--------- zitadel/provider.go | 4 ---- 2 files changed, 1 insertion(+), 13 deletions(-) diff --git a/zitadel/helper/client.go b/zitadel/helper/client.go index 66cdd433..13f26764 100644 --- a/zitadel/helper/client.go +++ b/zitadel/helper/client.go @@ -34,7 +34,7 @@ type ClientInfo struct { } func GetClientInfo(insecure bool, domain string, token string, jwtProfileFile string, jwtProfileJSON string, port string) (*ClientInfo, error) { - options := []zitadel.Option{} + options := make([]zitadel.Option, 0) keyPath := "" if token != "" { options = append(options, zitadel.WithJWTProfileTokenSource(middleware.JWTProfileFromPath(token))) @@ -84,10 +84,6 @@ func GetClientInfo(insecure bool, domain string, token string, jwtProfileFile st var adminClientLock = &sync.Mutex{} var adminClient *admin.Client -func ClearAdminClient() { - adminClient = nil -} - func GetAdminClient(info *ClientInfo) (*admin.Client, error) { if adminClient == nil { adminClientLock.Lock() @@ -111,10 +107,6 @@ func GetAdminClient(info *ClientInfo) (*admin.Client, error) { var mgmtClientLock = &sync.Mutex{} var mgmtClient *management.Client -func ClearMgmtClient() { - mgmtClient = nil -} - func GetManagementClient(info *ClientInfo) (*management.Client, error) { if mgmtClient == nil { mgmtClientLock.Lock() diff --git a/zitadel/provider.go b/zitadel/provider.go index e60f149b..f58d25f8 100644 --- a/zitadel/provider.go +++ b/zitadel/provider.go @@ -163,8 +163,6 @@ func (p *providerPV6) Configure(ctx context.Context, req provider.ConfigureReque resp.Diagnostics.AddError("failed to handle provider config", err.Error()) return } - helper.ClearMgmtClient() - helper.ClearAdminClient() resp.DataSourceData = info resp.ResourceData = info @@ -328,8 +326,6 @@ func ProviderConfigure(ctx context.Context, d *schema.ResourceData) (interface{} if err != nil { return nil, diag.FromErr(err) } - helper.ClearMgmtClient() - helper.ClearAdminClient() return clientinfo, nil } From f0dd9cec7714fe618e4b1f7a528001461ac9d695 Mon Sep 17 00:00:00 2001 From: Stefan Benz Date: Wed, 13 Sep 2023 10:28:01 +0200 Subject: [PATCH 6/7] fix: change zitadel clients to singleton pattern for better token handling --- go.mod | 22 ++++++------ go.sum | 46 +++++++++++++------------- zitadel/domain_policy/resource_test.go | 11 +++--- 3 files changed, 40 insertions(+), 39 deletions(-) diff --git a/go.mod b/go.mod index 080b0662..d803c000 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/envoyproxy/protoc-gen-validate v1.0.2 github.com/gabriel-vasile/mimetype v1.4.1 github.com/gogo/protobuf v1.3.2 - github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 + github.com/grpc-ecosystem/grpc-gateway/v2 v2.17.1 github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 github.com/hashicorp/hcl/v2 v2.16.2 github.com/hashicorp/terraform-plugin-framework v0.15.0 @@ -16,9 +16,9 @@ require ( github.com/hashicorp/terraform-plugin-sdk/v2 v2.26.1 github.com/zclconf/go-cty v1.13.1 github.com/zitadel/oidc v1.13.4 - github.com/zitadel/zitadel-go/v2 v2.0.15 - golang.org/x/oauth2 v0.10.0 - google.golang.org/grpc v1.57.0-dev + github.com/zitadel/zitadel-go/v2 v2.0.19 + golang.org/x/oauth2 v0.11.0 + google.golang.org/grpc v1.57.0 google.golang.org/protobuf v1.31.0 ) @@ -60,14 +60,14 @@ require ( github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect github.com/vmihailenco/msgpack/v4 v4.3.12 // indirect github.com/vmihailenco/tagparser v0.1.1 // indirect - golang.org/x/crypto v0.11.0 // indirect + golang.org/x/crypto v0.12.0 // indirect golang.org/x/mod v0.11.0 // indirect - golang.org/x/net v0.12.0 // indirect - golang.org/x/sys v0.10.0 // indirect - golang.org/x/text v0.11.0 // indirect + golang.org/x/net v0.14.0 // indirect + golang.org/x/sys v0.11.0 // indirect + golang.org/x/text v0.12.0 // indirect google.golang.org/appengine v1.6.7 // indirect - google.golang.org/genproto v0.0.0-20230526203410-71b5a4ffd15e // indirect - google.golang.org/genproto/googleapis/api v0.0.0-20230530153820-e85fd2cbaebc // indirect - google.golang.org/genproto/googleapis/rpc v0.0.0-20230629202037-9506855d4529 // indirect + google.golang.org/genproto v0.0.0-20230803162519-f966b187b2e5 // indirect + google.golang.org/genproto/googleapis/api v0.0.0-20230822172742-b8732ec3820d // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect gopkg.in/square/go-jose.v2 v2.6.0 // indirect ) diff --git a/go.sum b/go.sum index aa84c1fe..e2653d57 100644 --- a/go.sum +++ b/go.sum @@ -66,8 +66,8 @@ github.com/gorilla/schema v1.2.0 h1:YufUaxZYCKGFuAq3c96BOhjgd5nmXiOY9NGzF247Tsc= github.com/gorilla/schema v1.2.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU= github.com/gorilla/securecookie v1.1.1 h1:miw7JPhV+b/lAHSXz4qd/nN9jRiAFV5FwjeKyCS8BvQ= github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4= -github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 h1:YBftPWNWd4WwGqtY2yeZL2ef8rHAxPBD8KFhJpmcqms= -github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0/go.mod h1:YN5jB8ie0yfIUg6VvR9Kz84aCaG7AsGZnLjhHbUqwPg= +github.com/grpc-ecosystem/grpc-gateway/v2 v2.17.1 h1:LSsiG61v9IzzxMkqEr6nrix4miJI62xlRjwT7BYD2SM= +github.com/grpc-ecosystem/grpc-gateway/v2 v2.17.1/go.mod h1:Hbb13e3/WtqQ8U5hLGkek9gJvBLasHuPFI0UEGfnQ10= github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I= github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= @@ -211,8 +211,8 @@ github.com/zclconf/go-cty v1.13.1 h1:0a6bRwuiSHtAmqCqNOE+c2oHgepv0ctoxU4FUe43kwc github.com/zclconf/go-cty v1.13.1/go.mod h1:YKQzy/7pZ7iq2jNFzy5go57xdxdWoLLpaEp4u238AE0= github.com/zitadel/oidc v1.13.4 h1:+k2GKqP9Ld9S2MSFlj+KaNsoZ3J9oy+Ezw51EzSFuC8= github.com/zitadel/oidc v1.13.4/go.mod h1:3h2DhUcP02YV6q/CA/BG4yla0o6rXjK+DkJGK/dwJfw= -github.com/zitadel/zitadel-go/v2 v2.0.15 h1:dL2VROG7w32xtdl2DlPqpef6+s7PEP70KEa3SEjGsuI= -github.com/zitadel/zitadel-go/v2 v2.0.15/go.mod h1:2xpHigKocm9hVdNuAHo9eway/jGSo6XhyunbfGr7924= +github.com/zitadel/zitadel-go/v2 v2.0.19 h1:oLs2iLfPGqMLrgtzHoffnLBWHfk8JmTSXZFCXRgb+YM= +github.com/zitadel/zitadel-go/v2 v2.0.19/go.mod h1:pCT8y65qnRqTDBFf7UT5+6NAsAdfjgQCfn4BQ6pMWZ0= golang.org/x/crypto v0.0.0-20190219172222-a4c6cb3142f2/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= @@ -223,8 +223,8 @@ golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.5.0/go.mod h1:NK/OQwhpMQP3MwtdjgLlYHnH9ebylxKWv3e0fK+mkQU= -golang.org/x/crypto v0.11.0 h1:6Ewdq3tDic1mg5xRO4milcWCfMVQhI4NkqWWvqejpuA= -golang.org/x/crypto v0.11.0/go.mod h1:xgJhtzW8F9jGdVFWZESrid1U1bjeNy4zgy5cRr/CIio= +golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk= +golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= @@ -247,11 +247,11 @@ golang.org/x/net v0.0.0-20210326060303-6b1517762897/go.mod h1:uSPa2vr4CLtc/ILN5o golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.5.0/go.mod h1:DivGGAXEgPSlEBzxGzZI+ZLohi+xUj054jfeKui00ws= -golang.org/x/net v0.12.0 h1:cfawfvKITfUsFCeJIHJrbSxpeu/E81khclypR0GVT50= -golang.org/x/net v0.12.0/go.mod h1:zEVYFnQC7m/vmpQFELhcD1EWkZlX69l4oqgmer6hfKA= +golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14= +golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= -golang.org/x/oauth2 v0.10.0 h1:zHCpF2Khkwy4mMB4bv0U37YtJdTGW8jI0glAApi0Kh8= -golang.org/x/oauth2 v0.10.0/go.mod h1:kTpgurOux7LqtuxjuyZa4Gj2gdezIt/jQtGnNFfypQI= +golang.org/x/oauth2 v0.11.0 h1:vPL4xzxBM4niKCW6g9whtaWVXTJf1U5e4aZxxFx/gbU= +golang.org/x/oauth2 v0.11.0/go.mod h1:LdF7O/8bLR/qWK9DrpXmbHLTouvRHK0SgJl0GmDBchk= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -279,19 +279,19 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA= -golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM= +golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.4.0/go.mod h1:9P2UbLfCdcvo3p/nzKvsmas4TnlujnuoV9hGgYzW1lQ= -golang.org/x/term v0.10.0 h1:3R7pNqamzBraeqj/Tj8qt1aQ2HpmlC+Cx/qL/7hn4/c= +golang.org/x/term v0.11.0 h1:F9tnn/DA/Im8nCwm+fX+1/eBwi4qFjRT++MhtVC4ZX0= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.6.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= -golang.org/x/text v0.11.0 h1:LAntKIrcmeSKERyiOh0XMV39LXS8IE9UL2yP7+f5ij4= -golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= +golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc= +golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= @@ -306,14 +306,14 @@ google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7 google.golang.org/appengine v1.6.5/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c= google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= -google.golang.org/genproto v0.0.0-20230526203410-71b5a4ffd15e h1:Ao9GzfUMPH3zjVfzXG5rlWlk+Q8MXWKwWpwVQE1MXfw= -google.golang.org/genproto v0.0.0-20230526203410-71b5a4ffd15e/go.mod h1:zqTuNwFlFRsw5zIts5VnzLQxSRqh+CGOTVMlYbY0Eyk= -google.golang.org/genproto/googleapis/api v0.0.0-20230530153820-e85fd2cbaebc h1:kVKPf/IiYSBWEWtkIn6wZXwWGCnLKcC8oWfZvXjsGnM= -google.golang.org/genproto/googleapis/api v0.0.0-20230530153820-e85fd2cbaebc/go.mod h1:vHYtlOoi6TsQ3Uk2yxR7NI5z8uoV+3pZtR4jmHIkRig= -google.golang.org/genproto/googleapis/rpc v0.0.0-20230629202037-9506855d4529 h1:DEH99RbiLZhMxrpEJCZ0A+wdTe0EOgou/poSLx9vWf4= -google.golang.org/genproto/googleapis/rpc v0.0.0-20230629202037-9506855d4529/go.mod h1:66JfowdXAEgad5O9NnYcsNPLCPZJD++2L9X0PCMODrA= -google.golang.org/grpc v1.57.0-dev h1:XCvzqke2TBzQUs3a1MO1VsqVkWeAdJ/KmxHCAVtPpe8= -google.golang.org/grpc v1.57.0-dev/go.mod h1:ZPf89/axrdgRDfHqb7fa0GF5t4VUER0vWnqnPNuRP7k= +google.golang.org/genproto v0.0.0-20230803162519-f966b187b2e5 h1:L6iMMGrtzgHsWofoFcihmDEMYeDR9KN/ThbPWGrh++g= +google.golang.org/genproto v0.0.0-20230803162519-f966b187b2e5/go.mod h1:oH/ZOT02u4kWEp7oYBGYFFkCdKS/uYR9Z7+0/xuuFp8= +google.golang.org/genproto/googleapis/api v0.0.0-20230822172742-b8732ec3820d h1:DoPTO70H+bcDXcd39vOqb2viZxgqeBeSGtZ55yZU4/Q= +google.golang.org/genproto/googleapis/api v0.0.0-20230822172742-b8732ec3820d/go.mod h1:KjSP20unUpOx5kyQUFa7k4OJg0qeJ7DEZflGDu2p6Bk= +google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d h1:uvYuEyMHKNt+lT4K3bN6fGswmK8qSvcreM3BwjDh+y4= +google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d/go.mod h1:+Bk1OCOj40wS2hwAMA+aCW9ypzm63QTBBHp6lQ3p+9M= +google.golang.org/grpc v1.57.0 h1:kfzNeI/klCGD2YPMUlaGNT3pxvYfga7smW3Vth8Zsiw= +google.golang.org/grpc v1.57.0/go.mod h1:Sd+9RMTACXwmub0zcNY2c4arhtrbBYD1AUHI/dt16Mo= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= diff --git a/zitadel/domain_policy/resource_test.go b/zitadel/domain_policy/resource_test.go index c9893a3f..d6f6f949 100644 --- a/zitadel/domain_policy/resource_test.go +++ b/zitadel/domain_policy/resource_test.go @@ -17,12 +17,13 @@ import ( func TestAccDomainPolicy(t *testing.T) { frame := test_utils.NewOrgTestFrame(t, "zitadel_domain_policy") + otherFrame := frame.AnotherOrg(t, "domain-policy-org-"+frame.UniqueResourcesID) resourceExample, resourceAttributes := test_utils.ReadExample(t, test_utils.Resources, frame.ResourceType) exampleProperty := test_utils.AttributeValue(t, domain_policy.UserLoginMustBeDomainVar, resourceAttributes).True() test_utils.RunLifecyleTest( t, - frame.BaseTestFrame, - []string{frame.AsOrgDefaultDependency}, + otherFrame.BaseTestFrame, + []string{otherFrame.AsOrgDefaultDependency}, func(property bool, secret string) string { // only replace first bool for the smtp_sender_address_matches_instance_domain property return strings.Replace(resourceExample, strconv.FormatBool(exampleProperty), strconv.FormatBool(property), 1) @@ -30,10 +31,10 @@ func TestAccDomainPolicy(t *testing.T) { exampleProperty, !exampleProperty, "", "", "", false, - checkRemoteProperty(*frame), + checkRemoteProperty(*otherFrame), helper.ZitadelGeneratedIdOnlyRegex, - checkRemoteProperty(*frame)(false), - test_utils.ImportOrgId(frame), + checkRemoteProperty(*otherFrame)(false), + test_utils.ImportOrgId(otherFrame), ) } From c096cf0819f9635d25f78cd8673f001c35bed7f9 Mon Sep 17 00:00:00 2001 From: Stefan Benz Date: Wed, 13 Sep 2023 10:58:46 +0200 Subject: [PATCH 7/7] fix: change zitadel clients to singleton pattern for better token handling --- zitadel/domain_policy/funcs.go | 7 ++--- zitadel/helper/client.go | 4 +++ zitadel/label_policy/funcs.go | 12 ++++----- zitadel/lockout_policy/funcs.go | 8 +++--- zitadel/login_policy/funcs.go | 30 ++++++++++----------- zitadel/notification_policy/funcs.go | 8 +++--- zitadel/password_complexity_policy/funcs.go | 8 +++--- zitadel/privacy_policy/funcs.go | 8 +++--- 8 files changed, 45 insertions(+), 40 deletions(-) diff --git a/zitadel/domain_policy/funcs.go b/zitadel/domain_policy/funcs.go index aaffe5af..d0b2397f 100644 --- a/zitadel/domain_policy/funcs.go +++ b/zitadel/domain_policy/funcs.go @@ -48,7 +48,7 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.FromErr(err) } org := helper.GetID(d, helper.OrgIDVar) - _, err = client.UpdateCustomDomainPolicy(helper.CtxWithOrgID(ctx, d), &admin.UpdateCustomDomainPolicyRequest{ + _, err = client.UpdateCustomDomainPolicy(ctx, &admin.UpdateCustomDomainPolicyRequest{ OrgId: org, UserLoginMustBeDomain: d.Get(UserLoginMustBeDomainVar).(bool), ValidateOrgDomains: d.Get(validateOrgDomainVar).(bool), @@ -74,7 +74,7 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.FromErr(err) } org := helper.GetID(d, helper.OrgIDVar) - _, err = client.AddCustomDomainPolicy(helper.CtxWithOrgID(ctx, d), &admin.AddCustomDomainPolicyRequest{ + _, err = client.AddCustomDomainPolicy(ctx, &admin.AddCustomDomainPolicyRequest{ OrgId: org, UserLoginMustBeDomain: d.Get(UserLoginMustBeDomainVar).(bool), ValidateOrgDomains: d.Get(validateOrgDomainVar).(bool), @@ -100,7 +100,8 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn return diag.FromErr(err) } - resp, err := client.GetDomainPolicy(helper.CtxWithOrgID(ctx, d), &management.GetDomainPolicyRequest{}) + org := helper.GetID(d, helper.OrgIDVar) + resp, err := client.GetDomainPolicy(helper.CtxSetOrgID(ctx, org), &management.GetDomainPolicyRequest{}) if err != nil && helper.IgnoreIfNotFoundError(err) == nil { d.SetId("") return nil diff --git a/zitadel/helper/client.go b/zitadel/helper/client.go index 13f26764..11cf4bdb 100644 --- a/zitadel/helper/client.go +++ b/zitadel/helper/client.go @@ -127,6 +127,10 @@ func GetManagementClient(info *ClientInfo) (*management.Client, error) { return mgmtClient, nil } +func CtxWithID(ctx context.Context, d *schema.ResourceData) context.Context { + return CtxSetOrgID(ctx, GetID(d, OrgIDVar)) +} + func CtxWithOrgID(ctx context.Context, d *schema.ResourceData) context.Context { return CtxSetOrgID(ctx, d.Get(OrgIDVar).(string)) } diff --git a/zitadel/label_policy/funcs.go b/zitadel/label_policy/funcs.go index be631305..f8c4421d 100644 --- a/zitadel/label_policy/funcs.go +++ b/zitadel/label_policy/funcs.go @@ -24,7 +24,7 @@ func delete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.FromErr(err) } - _, err = client.ResetLabelPolicyToDefault(helper.CtxWithOrgID(ctx, d), &management.ResetLabelPolicyToDefaultRequest{}) + _, err = client.ResetLabelPolicyToDefault(helper.CtxWithID(ctx, d), &management.ResetLabelPolicyToDefaultRequest{}) if err != nil { return diag.Errorf("failed to reset label policy: %v", err) } @@ -57,7 +57,7 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia fontColorDarkVar, disableWatermarkVar, ) { - resp, err := client.UpdateCustomLabelPolicy(helper.CtxWithOrgID(ctx, d), &management.UpdateCustomLabelPolicyRequest{ + resp, err := client.UpdateCustomLabelPolicy(helper.CtxWithID(ctx, d), &management.UpdateCustomLabelPolicyRequest{ PrimaryColor: d.Get(primaryColorVar).(string), HideLoginNameSuffix: d.Get(hideLoginNameSuffixVar).(bool), WarnColor: d.Get(warnColorVar).(string), @@ -119,7 +119,7 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia FontHashVar, ) { if d.Get(SetActiveVar).(bool) { - if _, err := client.ActivateCustomLabelPolicy(helper.CtxWithOrgID(ctx, d), &management.ActivateCustomLabelPolicyRequest{}); err != nil { + if _, err := client.ActivateCustomLabelPolicy(helper.CtxWithID(ctx, d), &management.ActivateCustomLabelPolicyRequest{}); err != nil { return diag.Errorf("failed to activate label policy: %v", err) } } @@ -141,7 +141,7 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.FromErr(err) } - _, err = client.AddCustomLabelPolicy(helper.CtxWithOrgID(ctx, d), &management.AddCustomLabelPolicyRequest{ + _, err = client.AddCustomLabelPolicy(helper.CtxWithID(ctx, d), &management.AddCustomLabelPolicyRequest{ PrimaryColor: d.Get(primaryColorVar).(string), HideLoginNameSuffix: d.Get(hideLoginNameSuffixVar).(bool), WarnColor: d.Get(warnColorVar).(string), @@ -185,7 +185,7 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia } if d.Get(SetActiveVar).(bool) { - if _, err := client.ActivateCustomLabelPolicy(helper.CtxWithOrgID(ctx, d), &management.ActivateCustomLabelPolicyRequest{}); err != nil { + if _, err := client.ActivateCustomLabelPolicy(helper.CtxWithID(ctx, d), &management.ActivateCustomLabelPolicyRequest{}); err != nil { return diag.Errorf("failed to activate label policy: %v", err) } } @@ -206,7 +206,7 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn return diag.FromErr(err) } - resp, err := client.GetPreviewLabelPolicy(helper.CtxWithOrgID(ctx, d), &management.GetPreviewLabelPolicyRequest{}) + resp, err := client.GetPreviewLabelPolicy(helper.CtxWithID(ctx, d), &management.GetPreviewLabelPolicyRequest{}) if err != nil && helper.IgnoreIfNotFoundError(err) == nil { d.SetId("") return nil diff --git a/zitadel/lockout_policy/funcs.go b/zitadel/lockout_policy/funcs.go index 78397b8f..f0df1bd5 100644 --- a/zitadel/lockout_policy/funcs.go +++ b/zitadel/lockout_policy/funcs.go @@ -21,7 +21,7 @@ func delete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia if err != nil { return diag.FromErr(err) } - _, err = client.ResetLockoutPolicyToDefault(helper.CtxWithOrgID(ctx, d), &management.ResetLockoutPolicyToDefaultRequest{}) + _, err = client.ResetLockoutPolicyToDefault(helper.CtxWithID(ctx, d), &management.ResetLockoutPolicyToDefaultRequest{}) if err != nil { return diag.Errorf("failed to reset lockout policy: %v", err) } @@ -38,7 +38,7 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia if err != nil { return diag.FromErr(err) } - _, err = client.UpdateCustomLockoutPolicy(helper.CtxWithOrgID(ctx, d), &management.UpdateCustomLockoutPolicyRequest{ + _, err = client.UpdateCustomLockoutPolicy(helper.CtxWithID(ctx, d), &management.UpdateCustomLockoutPolicyRequest{ MaxPasswordAttempts: uint32(d.Get(maxPasswordAttemptsVar).(int)), }) if err != nil { @@ -58,7 +58,7 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia if err != nil { return diag.FromErr(err) } - _, err = client.AddCustomLockoutPolicy(helper.CtxWithOrgID(ctx, d), &management.AddCustomLockoutPolicyRequest{ + _, err = client.AddCustomLockoutPolicy(helper.CtxWithID(ctx, d), &management.AddCustomLockoutPolicyRequest{ MaxPasswordAttempts: uint32(d.Get(maxPasswordAttemptsVar).(int)), }) if err != nil { @@ -78,7 +78,7 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn if err != nil { return diag.FromErr(err) } - resp, err := client.GetLockoutPolicy(helper.CtxWithOrgID(ctx, d), &management.GetLockoutPolicyRequest{}) + resp, err := client.GetLockoutPolicy(helper.CtxWithID(ctx, d), &management.GetLockoutPolicyRequest{}) if err != nil && helper.IgnoreIfNotFoundError(err) == nil { d.SetId("") return nil diff --git a/zitadel/login_policy/funcs.go b/zitadel/login_policy/funcs.go index ef9f1a11..d69deb34 100644 --- a/zitadel/login_policy/funcs.go +++ b/zitadel/login_policy/funcs.go @@ -26,7 +26,7 @@ func delete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia if err != nil { return diag.FromErr(err) } - _, err = client.ResetLoginPolicyToDefault(helper.CtxWithOrgID(ctx, d), &management.ResetLoginPolicyToDefaultRequest{}) + _, err = client.ResetLoginPolicyToDefault(helper.CtxWithID(ctx, d), &management.ResetLoginPolicyToDefaultRequest{}) if err != nil { return diag.Errorf("failed to reset login policy: %v", err) } @@ -84,7 +84,7 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia if err != nil { return diag.FromErr(err) } - _, err = client.UpdateCustomLoginPolicy(helper.CtxWithOrgID(ctx, d), &management.UpdateCustomLoginPolicyRequest{ + _, err = client.UpdateCustomLoginPolicy(helper.CtxWithID(ctx, d), &management.UpdateCustomLoginPolicyRequest{ AllowUsernamePassword: d.Get(allowUsernamePasswordVar).(bool), AllowRegister: d.Get(allowRegisterVar).(bool), AllowExternalIdp: d.Get(allowExternalIDPVar).(bool), @@ -113,14 +113,14 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia addSecondFactor, deleteSecondFactors := helper.GetAddAndDelete(helper.SetToStringSlice(o.(*schema.Set)), helper.SetToStringSlice(n.(*schema.Set))) for _, factor := range addSecondFactor { - if _, err := client.AddSecondFactorToLoginPolicy(helper.CtxWithOrgID(ctx, d), &management.AddSecondFactorToLoginPolicyRequest{ + if _, err := client.AddSecondFactorToLoginPolicy(helper.CtxWithID(ctx, d), &management.AddSecondFactorToLoginPolicyRequest{ Type: policy.SecondFactorType(policy.SecondFactorType_value[factor]), }); err != nil { return diag.FromErr(err) } } for _, factor := range deleteSecondFactors { - if _, err := client.RemoveSecondFactorFromLoginPolicy(helper.CtxWithOrgID(ctx, d), &management.RemoveSecondFactorFromLoginPolicyRequest{ + if _, err := client.RemoveSecondFactorFromLoginPolicy(helper.CtxWithID(ctx, d), &management.RemoveSecondFactorFromLoginPolicyRequest{ Type: policy.SecondFactorType(policy.SecondFactorType_value[factor]), }); err != nil { return diag.FromErr(err) @@ -133,14 +133,14 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia addMultiFactor, deleteMultiFactors := helper.GetAddAndDelete(helper.SetToStringSlice(o.(*schema.Set)), helper.SetToStringSlice(n.(*schema.Set))) for _, factor := range addMultiFactor { - if _, err := client.AddMultiFactorToLoginPolicy(helper.CtxWithOrgID(ctx, d), &management.AddMultiFactorToLoginPolicyRequest{ + if _, err := client.AddMultiFactorToLoginPolicy(helper.CtxWithID(ctx, d), &management.AddMultiFactorToLoginPolicyRequest{ Type: policy.MultiFactorType(policy.MultiFactorType_value[factor]), }); err != nil { return diag.FromErr(err) } } for _, factor := range deleteMultiFactors { - if _, err := client.RemoveMultiFactorFromLoginPolicy(helper.CtxWithOrgID(ctx, d), &management.RemoveMultiFactorFromLoginPolicyRequest{ + if _, err := client.RemoveMultiFactorFromLoginPolicy(helper.CtxWithID(ctx, d), &management.RemoveMultiFactorFromLoginPolicyRequest{ Type: policy.MultiFactorType(policy.MultiFactorType_value[factor]), }); err != nil { return diag.FromErr(err) @@ -153,16 +153,16 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia addIdps, deleteIdps := helper.GetAddAndDelete(helper.SetToStringSlice(o.(*schema.Set)), helper.SetToStringSlice(n.(*schema.Set))) for _, addIdp := range addIdps { - idpOwnerType, err := getIDPOwnerType(ctx, client, addIdp) + idpOwnerType, err := getIDPOwnerType(helper.CtxWithID(ctx, d), client, addIdp) if err != nil { return diag.FromErr(err) } - if _, err := client.AddIDPToLoginPolicy(ctx, &management.AddIDPToLoginPolicyRequest{IdpId: addIdp, OwnerType: idpOwnerType}); err != nil { + if _, err := client.AddIDPToLoginPolicy(helper.CtxWithID(ctx, d), &management.AddIDPToLoginPolicyRequest{IdpId: addIdp, OwnerType: idpOwnerType}); err != nil { return diag.FromErr(err) } } for _, deleteIdp := range deleteIdps { - if _, err := client.RemoveIDPFromLoginPolicy(ctx, &management.RemoveIDPFromLoginPolicyRequest{IdpId: deleteIdp}); err != nil { + if _, err := client.RemoveIDPFromLoginPolicy(helper.CtxWithID(ctx, d), &management.RemoveIDPFromLoginPolicyRequest{IdpId: deleteIdp}); err != nil { return diag.FromErr(err) } } @@ -246,11 +246,11 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia idps := helper.GetOkSetToStringSlice(d, idpsVar) for _, addIdp := range idps { - idpOwnerType, err := getIDPOwnerType(helper.CtxWithOrgID(ctx, d), client, addIdp) + idpOwnerType, err := getIDPOwnerType(helper.CtxWithID(ctx, d), client, addIdp) if err != nil { return diag.FromErr(err) } - if _, err := client.AddIDPToLoginPolicy(helper.CtxWithOrgID(ctx, d), &management.AddIDPToLoginPolicyRequest{IdpId: addIdp, OwnerType: idpOwnerType}); err != nil { + if _, err := client.AddIDPToLoginPolicy(helper.CtxWithID(ctx, d), &management.AddIDPToLoginPolicyRequest{IdpId: addIdp, OwnerType: idpOwnerType}); err != nil { return diag.FromErr(err) } } @@ -292,7 +292,7 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn if err != nil { return diag.FromErr(err) } - resp, err := client.GetLoginPolicy(helper.CtxWithOrgID(ctx, d), &management.GetLoginPolicyRequest{}) + resp, err := client.GetLoginPolicy(helper.CtxWithID(ctx, d), &management.GetLoginPolicyRequest{}) if err != nil && helper.IgnoreIfNotFoundError(err) == nil { d.SetId("") return nil @@ -325,7 +325,7 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn disableLoginWithPhone: policy.GetDisableLoginWithPhone(), forceMFALocalOnlyVar: policy.GetForceMfaLocalOnly(), } - respSecond, err := client.ListLoginPolicySecondFactors(helper.CtxWithOrgID(ctx, d), &management.ListLoginPolicySecondFactorsRequest{}) + respSecond, err := client.ListLoginPolicySecondFactors(helper.CtxWithID(ctx, d), &management.ListLoginPolicySecondFactorsRequest{}) if err != nil { return diag.Errorf("failed to get login policy secondfactors: %v", err) } @@ -336,7 +336,7 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn } set[secondFactorsVar] = factors } - respMulti, err := client.ListLoginPolicyMultiFactors(helper.CtxWithOrgID(ctx, d), &management.ListLoginPolicyMultiFactorsRequest{}) + respMulti, err := client.ListLoginPolicyMultiFactors(helper.CtxWithID(ctx, d), &management.ListLoginPolicyMultiFactorsRequest{}) if err != nil { return diag.Errorf("failed to get login policy multifactors: %v", err) } @@ -347,7 +347,7 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn } set[multiFactorsVar] = factors } - respIDPs, err := client.ListLoginPolicyIDPs(helper.CtxWithOrgID(ctx, d), &management.ListLoginPolicyIDPsRequest{}) + respIDPs, err := client.ListLoginPolicyIDPs(helper.CtxWithID(ctx, d), &management.ListLoginPolicyIDPsRequest{}) if err != nil { return diag.Errorf("failed to get login policy idps: %v", err) } diff --git a/zitadel/notification_policy/funcs.go b/zitadel/notification_policy/funcs.go index 47b7ecbf..4dd9db12 100644 --- a/zitadel/notification_policy/funcs.go +++ b/zitadel/notification_policy/funcs.go @@ -21,7 +21,7 @@ func delete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia if err != nil { return diag.FromErr(err) } - _, err = client.ResetNotificationPolicyToDefault(helper.CtxWithOrgID(ctx, d), &management.ResetNotificationPolicyToDefaultRequest{}) + _, err = client.ResetNotificationPolicyToDefault(helper.CtxWithID(ctx, d), &management.ResetNotificationPolicyToDefaultRequest{}) if err != nil { return diag.Errorf("failed to reset notification policy: %v", err) } @@ -40,7 +40,7 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia } org := helper.GetID(d, helper.OrgIDVar) if d.HasChanges(passwordChangeVar) { - _, err = client.UpdateCustomNotificationPolicy(helper.CtxWithOrgID(ctx, d), &management.UpdateCustomNotificationPolicyRequest{ + _, err = client.UpdateCustomNotificationPolicy(helper.CtxWithID(ctx, d), &management.UpdateCustomNotificationPolicyRequest{ PasswordChange: d.Get(passwordChangeVar).(bool), }) if err != nil { @@ -62,7 +62,7 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.FromErr(err) } org := d.Get(helper.OrgIDVar).(string) - _, err = client.AddCustomNotificationPolicy(helper.CtxWithOrgID(ctx, d), &management.AddCustomNotificationPolicyRequest{ + _, err = client.AddCustomNotificationPolicy(helper.CtxWithID(ctx, d), &management.AddCustomNotificationPolicyRequest{ PasswordChange: d.Get(passwordChangeVar).(bool), }) if err != nil { @@ -82,7 +82,7 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn if err != nil { return diag.FromErr(err) } - resp, err := client.GetNotificationPolicy(helper.CtxWithOrgID(ctx, d), &management.GetNotificationPolicyRequest{}) + resp, err := client.GetNotificationPolicy(helper.CtxWithID(ctx, d), &management.GetNotificationPolicyRequest{}) if err != nil && helper.IgnoreIfNotFoundError(err) == nil { d.SetId("") return nil diff --git a/zitadel/password_complexity_policy/funcs.go b/zitadel/password_complexity_policy/funcs.go index 0ee6d6e0..e0d4116e 100644 --- a/zitadel/password_complexity_policy/funcs.go +++ b/zitadel/password_complexity_policy/funcs.go @@ -21,7 +21,7 @@ func delete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia if err != nil { return diag.FromErr(err) } - _, err = client.ResetPasswordComplexityPolicyToDefault(helper.CtxWithOrgID(ctx, d), &management.ResetPasswordComplexityPolicyToDefaultRequest{}) + _, err = client.ResetPasswordComplexityPolicyToDefault(helper.CtxWithID(ctx, d), &management.ResetPasswordComplexityPolicyToDefaultRequest{}) if err != nil { return diag.Errorf("failed to reset password complexity policy: %v", err) } @@ -38,7 +38,7 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia if err != nil { return diag.FromErr(err) } - _, err = client.UpdateCustomPasswordComplexityPolicy(helper.CtxWithOrgID(ctx, d), &management.UpdateCustomPasswordComplexityPolicyRequest{ + _, err = client.UpdateCustomPasswordComplexityPolicy(helper.CtxWithID(ctx, d), &management.UpdateCustomPasswordComplexityPolicyRequest{ MinLength: uint64(d.Get(minLengthVar).(int)), HasUppercase: d.Get(hasUppercaseVar).(bool), HasLowercase: d.Get(hasLowercaseVar).(bool), @@ -62,7 +62,7 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia if err != nil { return diag.FromErr(err) } - _, err = client.AddCustomPasswordComplexityPolicy(helper.CtxWithOrgID(ctx, d), &management.AddCustomPasswordComplexityPolicyRequest{ + _, err = client.AddCustomPasswordComplexityPolicy(helper.CtxWithID(ctx, d), &management.AddCustomPasswordComplexityPolicyRequest{ MinLength: uint64(d.Get(minLengthVar).(int)), HasUppercase: d.Get(hasUppercaseVar).(bool), HasLowercase: d.Get(hasLowercaseVar).(bool), @@ -86,7 +86,7 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn if err != nil { return diag.FromErr(err) } - resp, err := client.GetPasswordComplexityPolicy(helper.CtxWithOrgID(ctx, d), &management.GetPasswordComplexityPolicyRequest{}) + resp, err := client.GetPasswordComplexityPolicy(helper.CtxWithID(ctx, d), &management.GetPasswordComplexityPolicyRequest{}) if err != nil && helper.IgnoreIfNotFoundError(err) == nil { d.SetId("") return nil diff --git a/zitadel/privacy_policy/funcs.go b/zitadel/privacy_policy/funcs.go index 08d2c1e4..28404341 100644 --- a/zitadel/privacy_policy/funcs.go +++ b/zitadel/privacy_policy/funcs.go @@ -24,7 +24,7 @@ func delete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.FromErr(err) } - _, err = client.ResetPrivacyPolicyToDefault(helper.CtxWithOrgID(ctx, d), &management.ResetPrivacyPolicyToDefaultRequest{}) + _, err = client.ResetPrivacyPolicyToDefault(helper.CtxWithID(ctx, d), &management.ResetPrivacyPolicyToDefaultRequest{}) if err != nil { return diag.Errorf("failed to reset privacy policy: %v", err) } @@ -44,7 +44,7 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.FromErr(err) } - _, err = client.UpdateCustomPrivacyPolicy(helper.CtxWithOrgID(ctx, d), &management.UpdateCustomPrivacyPolicyRequest{ + _, err = client.UpdateCustomPrivacyPolicy(helper.CtxWithID(ctx, d), &management.UpdateCustomPrivacyPolicyRequest{ TosLink: d.Get(tosLinkVar).(string), PrivacyLink: d.Get(privacyLinkVar).(string), HelpLink: d.Get(HelpLinkVar).(string), @@ -70,7 +70,7 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia return diag.FromErr(err) } - _, err = client.AddCustomPrivacyPolicy(helper.CtxWithOrgID(ctx, d), &management.AddCustomPrivacyPolicyRequest{ + _, err = client.AddCustomPrivacyPolicy(helper.CtxWithID(ctx, d), &management.AddCustomPrivacyPolicyRequest{ TosLink: d.Get(tosLinkVar).(string), PrivacyLink: d.Get(privacyLinkVar).(string), HelpLink: d.Get(HelpLinkVar).(string), @@ -96,7 +96,7 @@ func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagn return diag.FromErr(err) } - resp, err := client.GetPrivacyPolicy(helper.CtxWithOrgID(ctx, d), &management.GetPrivacyPolicyRequest{}) + resp, err := client.GetPrivacyPolicy(helper.CtxWithID(ctx, d), &management.GetPrivacyPolicyRequest{}) if err != nil && helper.IgnoreIfNotFoundError(err) == nil { d.SetId("") return nil