hashicorp/packer · error

unexpected number of organizations: expected 1, actual: %v

Error message

unexpected number of organizations: expected 1, actual: %v

What it means

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.

Source

Thrown at internal/hcp/api/client.go:124

	}

	return client, nil
}

func (c *Client) loadOrganizationID() error {
	if env.HasOrganizationID() {
		c.OrganizationID = os.Getenv(env.HCPOrganizationID)
		return nil
	}
	// Get the organization ID.
	listOrgParams := organizationSvc.NewOrganizationServiceListParams()
	listOrgResp, err := c.Organization.OrganizationServiceList(listOrgParams, nil)
	if err != nil {
		return fmt.Errorf("unable to fetch organization list: %v", err)
	}
	orgLen := len(listOrgResp.Payload.Organizations)
	if orgLen != 1 {
		return fmt.Errorf("unexpected number of organizations: expected 1, actual: %v", orgLen)
	}
	c.OrganizationID = listOrgResp.Payload.Organizations[0].ID
	return nil
}

func (c *Client) loadProjectID() error {
	if env.HasProjectID() {
		c.ProjectID = os.Getenv(env.HCPProjectID)
		err := c.ValidateRegistryForProject()
		if err != nil {
			return fmt.Errorf("project validation for id %q responded in error: %v", c.ProjectID, err)
		}
		return nil
	}
	// Get the project using the organization ID.
	listProjParams := projectSvc.NewProjectServiceListParams()
	listProjParams.ScopeID = &c.OrganizationID
	scopeType := string(rmmodels.HashicorpCloudResourcemanagerResourceIDResourceTypeORGANIZATION)

View on GitHub (pinned to eb36e3c3e4)

Solutions

  1. Use a service principal bound to a single organization instead of a multi-org user credential
  2. 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)
  3. Check the HCP portal which organizations the principal can access and remove or correct memberships
  4. Recreate the service principal in the intended organization
Defensive patterns

Strategy: validation

Validate before calling

// Check org count expectations with the HCP CLI before running packer
// hcp organizations list  ->  ensure exactly one org is visible to the principal

Try / catch

if err != nil && strings.Contains(err.Error(), "unexpected number of organizations") {
    return fmt.Errorf("principal must belong to exactly one HCP organization; use a dedicated service principal: %w", err)
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of hashicorp/packer@eb36e3c3e4 (2026-09-05). Data as JSON: /api/errors/fd3b414f9875f556. Report an issue: GitHub.