Skip to content

AAP-46310 Add aap_organization data source with name/id lookup support #107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 27 commits into
base: main
Choose a base branch
from

Conversation

arrestle
Copy link
Contributor

@arrestle arrestle commented Jul 9, 2025

What?

Implements data.aap_organization data source to allow referencing AAP organizations by name or ID in Terraform configurations.

Why?

Currently users must hard-code organization IDs when creating resources like aap_inventory in non-default organizations, which is a poor user experience. This data source enables lookup by human-readable organization names, making Terraform configurations more maintainable and user-friendly.

How?

  • Built on base data source patterns from AAP-46290
  • Supports both name and id attributes for flexible lookup
  • Uses named URL computation for state file management
  • Implements proper validation using existing base data source validators
  • Compatible with AAP 2.4 and 2.5 controller API objects

Testing strategy:

Test Coverage:

  • Unit tests: Data source validation, schema, and config validators
  • JSON parsing tests: API response conversion with edge cases
  • Acceptance tests: End-to-end functionality with real AAP instance
  • Integration testing: Verified with aap_inventory resource creation in non-default organizations

Resolves: AAP-46310
Depends on: AAP-46290

Usage Example:

data "aap_organization" "my_org" {
  name = "My Organization"
}

resource "aap_inventory" "example" {
  organization_id = data.aap_organization.my_org.id
  # ... other config
}
sequenceDiagram
    participant User as User (.tf file)
    participant TF as Terraform Core
    participant DS as OrganizationDataSource
    participant API as AAP API
    
    User->>TF: terraform plan
    TF->>DS: Read() - "Get org by name"
    DS->>DS: ValidateConfig() - "Check required fields"
    DS->>DS: CreateNamedURL() - "Build API endpoint"
    DS->>API: HTTP GET /api/v2/organizations/Default
    API->>DS: JSON response {"id":1,"name":"Default"...}
    DS->>DS: ParseHttpResponse() - "Convert to Terraform types"
    DS->>TF: Return org data
    TF->>User: Display plan with org.id = 1
Loading

Anything else?

Architecture Integration:
Diagram 1: Provider Architecture & Data Flow

graph TD
    A[Provider] --> B[DataSources]
    A --> C[Resources]
    
    B --> D[aap_organization]
    B --> E[aap_inventory]
    B --> F[aap_job_template]
    B --> G[aap_workflow_job_template]
    
    D --> M[OrganizationDataSource]
    E --> N[InventoryDataSource]
    F --> O[JobTemplateDataSource]
    G --> P[WorkflowJobTemplateDataSource]
    
    M --> Q[BaseDataSource]
    N --> R[BaseDataSourceWithOrg]
    O --> R
    P --> R
    
    Q --> S[HTTP Client]
    R --> S
    S --> T[Named URL System]
    T --> U[AAP API]
    
    style D stroke:#ff6b6b,stroke-width:3px
    style M stroke:#ff6b6b,stroke-width:3px
    style Q fill:#2c3e50,color:#ffffff
    style R fill:#2c3e50,color:#ffffff
    style T fill:#34495e,color:#ffffff
Loading

@arrestle arrestle marked this pull request as draft July 9, 2025 19:23
Copy link
Contributor

@lranjbar lranjbar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First we've decided to not have reference to the testing frameworks in the public repository. For this reason entire env.sh file should be removed.

Second, please revert all the changes in utils.go. I've added #108 with new acceptance tests to show the bug and solution. These changes does not fix the bug and cause confusion around the problem.

@arrestle arrestle marked this pull request as ready for review July 14, 2025 10:59
@dleehr
Copy link
Collaborator

dleehr commented Jul 14, 2025

Is this ready for review @arrestle? I see it's been moved out of draft but there's no description and the title of the PR doesn't really describe what it does.

To be clear, I see the Jira ID and yes that does connect it to our issue tracker and I can look that up. However, my feedback is that I can't tell what this is at a glance. It should have a meaningful title and a description.

@arrestle arrestle requested review from lranjbar and davemulford July 14, 2025 13:19
Copy link
Contributor

@lranjbar lranjbar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple of things after merge

Copy link
Contributor

@davemulford davemulford left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some minor name change requests. Looks good otherwise, great work!

@arrestle arrestle requested a review from lranjbar July 15, 2025 11:15
@arrestle arrestle changed the title AAP-46310 first draft replacement of pr#90 AAP-46310 Add aap_organization data source with name/id lookup support Jul 15, 2025
@arrestle
Copy link
Contributor Author

Is this ready for review @arrestle? I see it's been moved out of draft but there's no description and the title of the PR doesn't really describe what it does.

To be clear, I see the Jira ID and yes that does connect it to our issue tracker and I can look that up. However, my feedback is that I can't tell what this is at a glance. It should have a meaningful title and a description.

@dleehr good point. At retro we were talking about a PR template. I like this one because I feel it explains the "why" of the PR and not just the "what".

https://www.pullrequest.com/blog/writing-a-great-pull-request-description/

@arrestle arrestle closed this Jul 15, 2025
@arrestle arrestle reopened this Jul 15, 2025
Copy link
Contributor

@davemulford davemulford left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implements the changes I asked about. Looks good!

Copy link
Contributor

@lranjbar lranjbar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also inline with Aaron's findings we will need to override these functions to Organization data source. Revert changes to include name out of the BaseAPIModel these should be attached to the OrganizationAPIModel directly.

internal/provider/base_datasource.go/Read

internal/provider/aap_named_url.go/CreateNamedURL

Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
52.2% Coverage on New Code (required ≥ 80%)
4.0% Duplication on New Code (required ≤ 3%)

See analysis details on SonarQube Cloud

@arrestle arrestle requested review from lranjbar and AaronH88 July 15, 2025 18:22
Copy link
Contributor

@lranjbar lranjbar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for all your hard work on this! I feel like we are finally at the finish line

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants