Skip to content

Rename hosts name template gitops#48787

Merged
juan-fdz-hawa merged 1 commit into
38806-macos-iosipados-rename-hostsfrom
48623-rename-hosts-name-template-gitops
Jul 6, 2026
Merged

Rename hosts name template gitops#48787
juan-fdz-hawa merged 1 commit into
38806-macos-iosipados-rename-hostsfrom
48623-rename-hosts-name-template-gitops

Conversation

@juan-fdz-hawa

@juan-fdz-hawa juan-fdz-hawa commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Relates to #48623

Plumbs mdm.name_template through PATCH /teams/{id} and team-spec apply (create and edit, which GitOps rides on).

Checklist for submitter

If some of the following don't apply, delete the relevant line.

Testing

  • Added/updated automated tests
  • QA'd all new/changed functionality manually

Summary by CodeRabbit

  • New Features

    • Team host name template settings can now be updated consistently through team settings and GitOps/spec-based workflows.
    • New teams created from specs can inherit a host name template automatically.
  • Bug Fixes

    • Invalid host name templates are now rejected reliably.
    • Empty or omitted template values now behave correctly, including clearing an existing template.
    • Reapplying the same template no longer creates duplicate activity records.

@codecov

codecov Bot commented Jul 6, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 77.77778% with 8 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (38806-macos-iosipados-rename-hosts@f5aeda7). Learn more about missing BASE report.

Files with missing lines Patch % Lines
ee/server/service/teams.go 77.77% 4 Missing and 4 partials ⚠️
Additional details and impacted files
@@                          Coverage Diff                          @@
##             38806-macos-iosipados-rename-hosts   #48787   +/-   ##
=====================================================================
  Coverage                                      ?   68.03%           
=====================================================================
  Files                                         ?     3683           
  Lines                                         ?   234281           
  Branches                                      ?    12419           
=====================================================================
  Hits                                          ?   159399           
  Misses                                        ?    60533           
  Partials                                      ?    14349           
Flag Coverage Δ
backend 69.69% <77.77%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@juan-fdz-hawa juan-fdz-hawa force-pushed the 48623-rename-hosts-name-template-endpoint branch from 049d8eb to 119918f Compare July 6, 2026 18:47
@juan-fdz-hawa juan-fdz-hawa force-pushed the 48623-rename-hosts-name-template-gitops branch from 183821a to 02ae944 Compare July 6, 2026 18:48
@juan-fdz-hawa

Copy link
Copy Markdown
Contributor Author

@coderabbitai full review

@coderabbitai

coderabbitai Bot commented Jul 6, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Full review finished.

@coderabbitai

coderabbitai Bot commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

Changes

This PR extends ee/server/service/teams.go to validate, update, and enforce the per-team MDM.HostNameTemplate setting across three code paths: ModifyTeam (team PATCH requests), createTeamFromSpec, and editTeamFromSpec (GitOps batch apply). Each path now tracks whether the template value changed, validates non-empty values via fleet.ValidateHostNameTemplate, updates team configuration, and conditionally invokes applyHostNameTemplateChange after persistence. Supporting changes include added comments in an existing unit test and a new integration test, TestTeamHostNameTemplate, covering validation, persistence, idempotency, dry-run behavior, template clearing, and activity logging for both PATCH and spec-apply flows.

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant Service as ee/server/service
  participant Validator as fleet.ValidateHostNameTemplate
  participant Enforcer as applyHostNameTemplateChange
  participant Store

  Client->>Service: PATCH team or apply spec (MDM.HostNameTemplate)
  Service->>Validator: validate template value
  Validator-->>Service: validation result
  alt valid
    Service->>Store: save team config with updated template
    Service->>Enforcer: apply enforcement if template changed
  else invalid
    Service-->>Client: return validation error
  end
Loading

Possibly related issues

Possibly related PRs

  • fleetdm/fleet#48693: Adds the persistence/enforcement-state datastore that applyHostNameTemplateChange relies on for this PR's enforcement flow.
  • fleetdm/fleet#48699: Adds the underlying name_template model fields, validation helpers, and activity type consumed by this PR's PATCH/GitOps wiring.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description is missing most required template sections and only includes a partial testing note. Add the missing checklist sections relevant to this change, especially GitOps-specific verification, any manual QA, and any other applicable items from the template.
✅ Passed checks (4 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title is related to the change, but it is vague and awkwardly phrased.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch 48623-rename-hosts-name-template-gitops

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick comments (2)
ee/server/service/teams.go (2)

1526-1534: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

HostNameTemplate validation error short-circuits instead of aggregating with other spec errors.

createTeamFromSpec collects most spec validation failures into invalid and reports them together (see fleet.ValidateMDMProfileSpecs, host_expiry_settings, etc. at Lines 1539-1594), which is friendlier for GitOps dry-run users fixing multiple issues at once. The new HostNameTemplate check at Lines 1528-1533 instead returns immediately on error, so if the spec has both an invalid template and another invalid field, only the template error surfaces.

Consider moving the invalid := &fleet.InvalidArgumentError{} declaration above this block and using invalid.Append("mdm.host_name_template", err.Error()) (or embed the InvalidArgumentError) instead of an early return, for consistency with the rest of this function.

♻️ Sketch of aggregation approach
-	nameTemplate := spec.MDM.HostNameTemplate.Value
-	if nameTemplate != "" {
-		validated, err := fleet.ValidateHostNameTemplate(nameTemplate)
-		if err != nil {
-			return nil, ctxerr.Wrap(ctx, err)
-		}
-		nameTemplate = validated
-	}
-
 	invalid := &fleet.InvalidArgumentError{}
+	nameTemplate := spec.MDM.HostNameTemplate.Value
+	if nameTemplate != "" {
+		validated, err := fleet.ValidateHostNameTemplate(nameTemplate)
+		if err != nil {
+			invalid.Append("mdm.host_name_template", err.Error())
+		} else {
+			nameTemplate = validated
+		}
+	}
 	if enableDiskEncryption && svc.config.Server.PrivateKey == "" {

Also applies to: 1616-1616, 1677-1682

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@ee/server/service/teams.go` around lines 1526 - 1534, The HostNameTemplate
validation in createTeamFromSpec is returning immediately instead of being added
to the same aggregated InvalidArgumentError as the other spec checks. Move the
invalid accumulator so it is available before the spec validation blocks, and in
the HostNameTemplate branch append the validation failure (for example under the
mdm.host_name_template key) rather than calling ctxerr.Wrap and returning early.
Keep the existing validation logic in createTeamFromSpec consistent with the
other aggregated checks used around ValidateMDMProfileSpecs and
host_expiry_settings, and apply the same pattern to the other HostNameTemplate
validation sites referenced in the diff.

1810-1823: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Same early-return validation gap as createTeamFromSpec.

editTeamFromSpec also aggregates most validation failures into invalid (declared at Line 1900, used through Line 2020), but the HostNameTemplate check here (Lines 1811-1822) returns immediately on validation error instead of appending to invalid. This means, e.g., a GitOps apply with both an invalid host name template and an invalid custom setting only reports the first error encountered.

Since invalid is declared later in this function (Line 1900), moving its declaration above this block (or aggregating the error separately) would let both issues surface together for GitOps dry runs.

Also applies to: 2089-2094

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@ee/server/service/teams.go` around lines 1810 - 1823, The HostNameTemplate
validation in editTeamFromSpec returns immediately on error, which bypasses the
function’s existing invalid aggregation. Update editTeamFromSpec so the
fleet.ValidateHostNameTemplate failure is added to invalid instead of exiting
early, matching the createTeamFromSpec behavior and allowing GitOps dry runs to
report multiple issues together. Use the existing invalid collector in
editTeamFromSpec and keep the HostNameTemplate assignment logic gated on
successful validation; apply the same fix to the duplicate HostNameTemplate path
noted around 2089-2094.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@ee/server/service/teams.go`:
- Around line 1526-1534: The HostNameTemplate validation in createTeamFromSpec
is returning immediately instead of being added to the same aggregated
InvalidArgumentError as the other spec checks. Move the invalid accumulator so
it is available before the spec validation blocks, and in the HostNameTemplate
branch append the validation failure (for example under the
mdm.host_name_template key) rather than calling ctxerr.Wrap and returning early.
Keep the existing validation logic in createTeamFromSpec consistent with the
other aggregated checks used around ValidateMDMProfileSpecs and
host_expiry_settings, and apply the same pattern to the other HostNameTemplate
validation sites referenced in the diff.
- Around line 1810-1823: The HostNameTemplate validation in editTeamFromSpec
returns immediately on error, which bypasses the function’s existing invalid
aggregation. Update editTeamFromSpec so the fleet.ValidateHostNameTemplate
failure is added to invalid instead of exiting early, matching the
createTeamFromSpec behavior and allowing GitOps dry runs to report multiple
issues together. Use the existing invalid collector in editTeamFromSpec and keep
the HostNameTemplate assignment logic gated on successful validation; apply the
same fix to the duplicate HostNameTemplate path noted around 2089-2094.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 6b500532-972c-4ab7-b495-43ec221090f1

📥 Commits

Reviewing files that changed from the base of the PR and between 119918f and 02ae944.

📒 Files selected for processing (3)
  • ee/server/service/teams.go
  • server/service/apple_mdm_test.go
  • server/service/integration_enterprise_test.go

@juan-fdz-hawa juan-fdz-hawa force-pushed the 48623-rename-hosts-name-template-gitops branch from 02ae944 to accd784 Compare July 6, 2026 19:36
@juan-fdz-hawa juan-fdz-hawa force-pushed the 48623-rename-hosts-name-template-gitops branch from accd784 to 0d92176 Compare July 6, 2026 19:39
@juan-fdz-hawa juan-fdz-hawa marked this pull request as ready for review July 6, 2026 19:40
@juan-fdz-hawa juan-fdz-hawa requested a review from a team as a code owner July 6, 2026 19:40

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Claude Code Review

This repository is configured for manual code reviews. Comment @claude review to trigger a review and subscribe this PR to future pushes, or @claude review once for a one-time review.

Tip: disable this comment in your organization's Code Review settings.

@lucasmrod lucasmrod left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM!

Base automatically changed from 48623-rename-hosts-name-template-endpoint to 38806-macos-iosipados-rename-hosts July 6, 2026 20:36
Relates to #48623

Plumbs mdm.name_template through PATCH /teams/{id} and team-spec apply
(create and edit, which GitOps rides on).
@juan-fdz-hawa juan-fdz-hawa force-pushed the 48623-rename-hosts-name-template-gitops branch from 0d92176 to 59cad67 Compare July 6, 2026 20:38
@juan-fdz-hawa juan-fdz-hawa merged commit f278514 into 38806-macos-iosipados-rename-hosts Jul 6, 2026
24 checks passed
@juan-fdz-hawa juan-fdz-hawa deleted the 48623-rename-hosts-name-template-gitops branch July 6, 2026 20:39
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.

Rename hosts: update-template endpoint, enforcement pipeline, and verification

2 participants