Skip to content

Commit

Permalink
Merge pull request #16 from alectrona/make-mdmEnvironments-managed
Browse files Browse the repository at this point in the history
Make mdm environments managed
  • Loading branch information
SMartorelli authored Nov 4, 2024
2 parents b60839c + 27cf321 commit 70a7854
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
24 changes: 23 additions & 1 deletion migrator/AppContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,39 @@ struct AppContext {

// MARK: - Device Management Related Constants

/// UserDefaults key indicating the list of managed environments.
private static let mdmEnvironmentsUserDefaultsKey: String = "mdmEnvironments"

/// A list of managed environments to be tracked on the user's device.
/// Each `ManagedEnvironment` includes:
/// - `name`: The environment's name (e.g., Production, QA).
/// - `serverURL`: The server URL used to identify the environment through the installed management profile.
/// The `serverURL` can optionally include the `/mdm/ServerURL` suffix, and will be added if missing.
/// - `reconPolicyID`: The policy ID required to run an inventory update via Jamf Self Service.
/// This workflow is specifically designed for devices managed by Jamf Pro.
static let mdmEnvironments: [ManagedEnvironment] = [
static let fallbackMdmEnvironments: [ManagedEnvironment] = [
ManagedEnvironment(name: "TestExample", serverURL: "https://test.url/mdm/ServerURL", reconPolicyID: "022"),
ManagedEnvironment(name: "ProdExample", serverURL: "https://prod.url/mdm/ServerURL", reconPolicyID: "033")
]

/// Read the list of managed environments from UserDefaults with the
static var mdmEnvironments: [ManagedEnvironment] {
let managedEnvironments = UserDefaults.standard.array(forKey: mdmEnvironmentsUserDefaultsKey) as? [[String: String]] ?? []

/// Use managedEnvironments if not empty, otherwise fallback to fallbackMdmEnvironments
return managedEnvironments.isEmpty ? fallbackMdmEnvironments : managedEnvironments.compactMap { dict in
guard
let name = dict["name"],
let serverURL = dict["serverURL"],
let reconPolicyID = dict["reconPolicyID"]
else {
return nil
}

return ManagedEnvironment(name: name, serverURL: serverURL, reconPolicyID: reconPolicyID)
}
}

/// Path the Jamf Self Service .app
static let fallbackStorePath: String = "/Applications/Company Self Service.app"

Expand Down
19 changes: 19 additions & 0 deletions migrator/Model/DeviceManagement/ManagedEnvironment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,23 @@ struct ManagedEnvironment {
var name: String
var serverURL: String
var reconPolicyID: String

// MARK: - Initializers

init(name: String, serverURL: String, reconPolicyID: String) {
self.name = name
self.reconPolicyID = reconPolicyID

/// Allow for serverURL to be input without the "/mdm/ServerURL" suffix.
if !serverURL.hasSuffix("/mdm/ServerURL") {
if serverURL.hasSuffix("/") {
self.serverURL = serverURL + "mdm/ServerURL"
} else {
self.serverURL = serverURL + "/mdm/ServerURL"
}
} else {
/// The serverURL is already formatted correctly.
self.serverURL = serverURL
}
}
}

0 comments on commit 70a7854

Please sign in to comment.