{"record":{"id":"fd3b414f9875f556","repo":"hashicorp/packer","slug":"unexpected-number-of-organizations-expected-1-ac","errorCode":null,"errorMessage":"unexpected number of organizations: expected 1, actual: %v","messagePattern":"unexpected number of organizations: expected 1, actual: (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/hcp/api/client.go","lineNumber":124,"sourceCode":"\t}\n\n\treturn client, nil\n}\n\nfunc (c *Client) loadOrganizationID() error {\n\tif env.HasOrganizationID() {\n\t\tc.OrganizationID = os.Getenv(env.HCPOrganizationID)\n\t\treturn nil\n\t}\n\t// Get the organization ID.\n\tlistOrgParams := organizationSvc.NewOrganizationServiceListParams()\n\tlistOrgResp, err := c.Organization.OrganizationServiceList(listOrgParams, nil)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"unable to fetch organization list: %v\", err)\n\t}\n\torgLen := len(listOrgResp.Payload.Organizations)\n\tif orgLen != 1 {\n\t\treturn fmt.Errorf(\"unexpected number of organizations: expected 1, actual: %v\", orgLen)\n\t}\n\tc.OrganizationID = listOrgResp.Payload.Organizations[0].ID\n\treturn nil\n}\n\nfunc (c *Client) loadProjectID() error {\n\tif env.HasProjectID() {\n\t\tc.ProjectID = os.Getenv(env.HCPProjectID)\n\t\terr := c.ValidateRegistryForProject()\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"project validation for id %q responded in error: %v\", c.ProjectID, err)\n\t\t}\n\t\treturn nil\n\t}\n\t// Get the project using the organization ID.\n\tlistProjParams := projectSvc.NewProjectServiceListParams()\n\tlistProjParams.ScopeID = &c.OrganizationID\n\tscopeType := string(rmmodels.HashicorpCloudResourcemanagerResourceIDResourceTypeORGANIZATION)","sourceCodeStart":106,"sourceCodeEnd":142,"githubUrl":"https://github.com/hashicorp/packer/blob/eb36e3c3e48a036f3e8cc94087636ee72e1303c9/internal/hcp/api/client.go#L106-L142","documentation":"After a successful organization list, loadOrganizationID requires exactly one organization to be associated with the credentials; any other count (0 or >1) yields `unexpected number of organizations: expected 1, actual: %v`. The client infers the org from the principal, which is only unambiguous when exactly one org exists.","triggerScenarios":"NewClient → loadOrganizationID gets a 200 from OrganizationServiceList but payload.Organizations has length 0 (principal has no org access) or length > 1 (service principal / user belongs to multiple organizations).","commonSituations":"Using a user credential (via `hcp auth login`) that is a member of several HCP organizations; a brand-new service principal with no organization bindings; credentials for the wrong tenant.","solutions":["Use a service principal bound to a single organization instead of a multi-org user credential","If using a multi-org principal, configure the client so it doesn't need to infer the org (supply the org/project explicitly where supported)","Check the HCP portal which organizations the principal can access and remove or correct memberships","Recreate the service principal in the intended organization"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Check org count expectations with the HCP CLI before running packer\n// hcp organizations list  ->  ensure exactly one org is visible to the principal","typeGuard":null,"tryCatchPattern":"if err != nil && strings.Contains(err.Error(), \"unexpected number of organizations\") {\n    return fmt.Errorf(\"principal must belong to exactly one HCP organization; use a dedicated service principal: %w\", err)\n}","preventionTips":["Use a service principal scoped to a single organization, not a multi-org user token","Confirm org memberships in the HCP portal before configuring credentials","Avoid sharing user credentials across organizations in CI","Recreate mis-scoped principals in the intended organization"],"tags":["hcp","configuration","multi-organization"],"backgroundTag":"ambiguous-hcp-organization","analyzedSha":"eb36e3c3e48a036f3e8cc94087636ee72e1303c9","analyzedAt":"2026-09-05T13:20:43.127Z","contentChangedAt":"2026-09-05T13:20:43.127Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}