Skip to content

Commit 489ef3c

Browse files
authored
Merge pull request #51 from Ethenyl/feature/implement-policy-model
Implement Policy model
2 parents a419dd7 + d8ceabf commit 489ef3c

34 files changed

+1201
-61
lines changed

JAMFKit/JAMFKit.xcodeproj/project.pbxproj

+92-4
Large diffs are not rendered by default.

JamfKit/Sources/Models/Computer/Computer.swift

+11-11
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,26 @@ public final class Computer: Identifiable, CustomStringConvertible {
1616

1717
// MARK: - Properties
1818

19-
public var general: ComputerGeneral?
19+
public var general: ComputerGeneral
2020
public var location: ComputerLocation?
2121
public var purchasing: ComputerPurchasing?
2222

2323
public var description: String {
24-
let baseDescription = "[\(String(describing: Computer.self))]"
25-
26-
if let general = self.general {
27-
return "\(baseDescription)[\(general.identifier). \(general.name)]"
28-
}
29-
30-
return baseDescription
24+
return "[\(String(describing: Computer.self))][\(general.identifier). \(general.name)]"
3125
}
3226

3327
// MARK: - Initialization
3428

3529
public required init?(json: [String: Any], node: String = "") {
36-
if let generalNode = json[Computer.GeneralKey] as? [String: Any] {
37-
general = ComputerGeneral(json: generalNode)
30+
guard
31+
let generalNode = json[Computer.GeneralKey] as? [String: Any],
32+
let general = ComputerGeneral(json: generalNode)
33+
else {
34+
return nil
3835
}
3936

37+
self.general = general
38+
4039
if let locationNode = json[Computer.LocationKey] as? [String: Any] {
4140
location = ComputerLocation(json: locationNode)
4241
}
@@ -51,7 +50,8 @@ public final class Computer: Identifiable, CustomStringConvertible {
5150
public func toJSON() -> [String: Any] {
5251
var json = [String: Any]()
5352

54-
if let general = general { json[Computer.GeneralKey] = general.toJSON() }
53+
json[Computer.GeneralKey] = general.toJSON()
54+
5555
if let location = location { json[Computer.LocationKey] = location.toJSON() }
5656
if let purchasing = purchasing { json[Computer.PurchasingKey] = purchasing.toJSON() }
5757

JamfKit/Sources/Models/Computer/ComputerGeneral.swift

+7-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public final class ComputerGeneral: BaseObject {
2020
static let Barcode1Key = "barcode_1"
2121
static let Barcode2Key = "barcode_2"
2222
static let AssetTagKey = "asset_tag"
23+
static let RemoteManagementKey = "remote_management"
2324
static let MdmCapableKey = "mdm_capable"
2425
static let MdmCapableUsersKey = "mdm_capable_users"
2526
static let MdmCapableUserKey = "mdm_capable_user"
@@ -108,7 +109,11 @@ public final class ComputerGeneral: BaseObject {
108109
json[ComputerGeneral.Barcode1Key] = barcode1
109110
json[ComputerGeneral.Barcode2Key] = barcode2
110111
json[ComputerGeneral.AssetTagKey] = assetTag
111-
json[ComputerRemoteManagement.ContainerKey] = remoteManagement ?? remoteManagement?.toJSON()
112+
113+
if let remoteManagement = remoteManagement {
114+
json[ComputerGeneral.RemoteManagementKey] = remoteManagement.toJSON()
115+
}
116+
112117
json[ComputerGeneral.MdmCapableKey] = isMdmCapable
113118
json[ComputerGeneral.MdmCapableUsersKey] = mdmCapableUsers
114119

@@ -145,7 +150,7 @@ public final class ComputerGeneral: BaseObject {
145150

146151
private static func parseRemoteManagement(from json: [String: Any]) -> ComputerRemoteManagement? {
147152
guard
148-
let remoteManagementContainer = json[ComputerRemoteManagement.ContainerKey] as? [String: Any],
153+
let remoteManagementContainer = json[ComputerGeneral.RemoteManagementKey] as? [String: Any],
149154
let remoteManagement = ComputerRemoteManagement(json: remoteManagementContainer)
150155
else {
151156
return nil

JamfKit/Sources/Models/Computer/ComputerRemoteManagement.swift

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ public final class ComputerRemoteManagement: Identifiable {
99

1010
// MARK: - Constants
1111

12-
static let ContainerKey = "remote_management"
1312
static let ManagedKey = "managed"
1413
static let ManagementUsernameKey = "management_username"
1514

JamfKit/Sources/Models/Computer/ComputerCommand.swift JamfKit/Sources/Models/ComputerCommand.swift

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// Copyright © 2018 JAMFKit. All rights reserved.
66
//
77

8+
/// Represents a logical command that can be executed on any hardware element manageg by Jamf.
89
public final class ComputerCommand: Identifiable, CustomStringConvertible {
910

1011
// MARK: - Constants
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//
2+
// Policy.swift
3+
// JAMFKit
4+
//
5+
// Copyright © 2018 JAMFKit. All rights reserved.
6+
//
7+
8+
/// Reprents as logical policy that can be applied to any hardware element managed by Jamf.
9+
public final class Policy: Identifiable {
10+
11+
// MARK: - Constants
12+
13+
static let GeneralKey = "general"
14+
15+
// MARK: - Properties
16+
17+
public var general: PolicyGeneral
18+
19+
public var description: String {
20+
return "[\(String(describing: Policy.self))][\(general.identifier). \(general.name)]"
21+
}
22+
23+
// MARK: - Initialization
24+
25+
public init?(json: [String: Any], node: String = "") {
26+
guard
27+
let generalNode = json[Policy.GeneralKey] as? [String: Any],
28+
let general = PolicyGeneral(json: generalNode)
29+
else {
30+
return nil
31+
}
32+
33+
self.general = general
34+
}
35+
36+
// MARK: - Functions
37+
38+
public func toJSON() -> [String: Any] {
39+
var json = [String: Any]()
40+
41+
json[Policy.GeneralKey] = general.toJSON()
42+
43+
return json
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// PolicyCategory.swift
3+
// JAMFKit
4+
//
5+
// Copyright © 2018 JAMFKit. All rights reserved.
6+
//
7+
8+
public final class PolicyCategory: BaseObject {
9+
10+
// MARK: - Constants
11+
12+
static let PriorityKey = "priority"
13+
14+
// MARK: - Properties
15+
16+
public var priority: UInt
17+
18+
// MARK: - Initialization
19+
20+
public required init?(json: [String : Any], node: String = "") {
21+
guard let priority = json[PolicyCategory.PriorityKey] as? UInt else {
22+
return nil
23+
}
24+
25+
self.priority = priority
26+
27+
super.init(json: json)
28+
}
29+
30+
// MARK: - Functions
31+
32+
public override func toJSON() -> [String : Any] {
33+
var json = super.toJSON()
34+
35+
json[PolicyCategory.PriorityKey] = priority
36+
37+
return json
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//
2+
// PolicyDateTimeLimitations.swift
3+
// JAMFKit
4+
//
5+
// Copyright © 2018 JAMFKit. All rights reserved.
6+
//
7+
8+
public final class PolicyDateTimeLimitations: Identifiable {
9+
10+
// MARK: - Constants
11+
12+
static let ActivationDateKey = "activation_date"
13+
static let ExpirationDateKey = "expiration_date"
14+
static let NoExecuteOnKey = "no_execute_on"
15+
static let NoExecuteStartKey = "no_execute_start"
16+
static let NoExecuteEndKey = "no_execute_end"
17+
18+
// MARK: - Properties
19+
20+
public var activationDate: PreciseDate?
21+
public var expirationDate: PreciseDate?
22+
public var noExecutionOn: [String: String]
23+
public var noExecutionStart: String
24+
public var noExecutionEnd: String
25+
26+
// MARK: - Initialization
27+
28+
public init?(json: [String: Any], node: String = "") {
29+
activationDate = PreciseDate(json: json, node: PolicyDateTimeLimitations.ActivationDateKey)
30+
expirationDate = PreciseDate(json: json, node: PolicyDateTimeLimitations.ExpirationDateKey)
31+
noExecutionOn = json[PolicyDateTimeLimitations.NoExecuteOnKey] as? [String: String] ?? [String: String]()
32+
noExecutionStart = json[PolicyDateTimeLimitations.NoExecuteStartKey] as? String ?? ""
33+
noExecutionEnd = json[PolicyDateTimeLimitations.NoExecuteEndKey] as? String ?? ""
34+
}
35+
36+
// MARK: - Functions
37+
38+
public func toJSON() -> [String: Any] {
39+
var json = [String: Any]()
40+
41+
if let activationDate = activationDate {
42+
json.merge(activationDate.toJSON()) { (_, new) in new }
43+
}
44+
45+
if let expirationDate = expirationDate {
46+
json.merge(expirationDate.toJSON()) { (_, new) in new }
47+
}
48+
49+
if noExecutionOn.count > 0 {
50+
json[PolicyDateTimeLimitations.NoExecuteOnKey] = noExecutionOn
51+
}
52+
53+
json[PolicyDateTimeLimitations.NoExecuteStartKey] = noExecutionStart
54+
json[PolicyDateTimeLimitations.NoExecuteEndKey] = noExecutionEnd
55+
56+
return json
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
//
2+
// PolicyGeneral.swift
3+
// JAMFKit
4+
//
5+
// Copyright © 2018 JAMFKit. All rights reserved.
6+
//
7+
8+
public final class PolicyGeneral: BaseObject {
9+
10+
// MARK: - Constants
11+
12+
static let EnabledKey = "enabled"
13+
static let TriggerKey = "trigger"
14+
static let TriggerCheckinKey = "trigger_checkin"
15+
static let TriggerEnrollmentCompleteKey = "trigger_enrollment_complete"
16+
static let TriggerLoginKey = "trigger_login"
17+
static let TriggerLogoutKey = "trigger_logout"
18+
static let TriggerNetworkStateChangedKey = "trigger_network_state_changed"
19+
static let TriggerStartupKey = "trigger_startup"
20+
static let TriggerOtherKey = "trigger_other"
21+
static let FrequencyKey = "frequency"
22+
static let LocationUserOnlyKey = "location_user_only"
23+
static let TargetDriveKey = "target_drive"
24+
static let OfflineKey = "offline"
25+
static let CategoryKey = "category"
26+
static let DateTimeLimitationsKey = "date_time_limitations"
27+
static let NetworkLimitationsKey = "network_limitations"
28+
static let OverrideDefaultSettingsKey = "override_default_settings"
29+
static let NetworkRequirementsKey = "network_requirements"
30+
static let SiteKey = "site"
31+
32+
// MARK: - Properties
33+
34+
public var isEnabled: Bool
35+
public var trigger: String
36+
public var triggerCheckin: Bool
37+
public var triggerEnrollmentComplete: Bool
38+
public var triggerLogin: Bool
39+
public var triggerLogout: Bool
40+
public var triggerNetworkStateChanged: Bool
41+
public var triggerStartup: Bool
42+
public var triggerOther: String
43+
public var frequency: String
44+
public var locationUserOnly: Bool
45+
public var targetDrive: String
46+
public var offline: Bool
47+
public var category: PolicyCategory?
48+
public var dateTimeLimitations: PolicyDateTimeLimitations?
49+
public var networkLimitations: PolicyNetworkLimitations?
50+
public var overrideDefaultSettings: PolicyOverrideDefaultSettings?
51+
public var networkRequirements: String
52+
public var site: Site?
53+
54+
// MARK: - Initialization
55+
56+
public required init?(json: [String: Any], node: String = "") {
57+
isEnabled = json[PolicyGeneral.EnabledKey] as? Bool ?? false
58+
trigger = json[PolicyGeneral.TriggerKey] as? String ?? ""
59+
triggerCheckin = json[PolicyGeneral.TriggerCheckinKey] as? Bool ?? false
60+
triggerEnrollmentComplete = json[PolicyGeneral.TriggerEnrollmentCompleteKey] as? Bool ?? false
61+
triggerLogin = json[PolicyGeneral.TriggerLoginKey] as? Bool ?? false
62+
triggerLogout = json[PolicyGeneral.TriggerLogoutKey] as? Bool ?? false
63+
triggerNetworkStateChanged = json[PolicyGeneral.TriggerNetworkStateChangedKey] as? Bool ?? false
64+
triggerStartup = json[PolicyGeneral.TriggerStartupKey] as? Bool ?? false
65+
triggerOther = json[PolicyGeneral.TriggerOtherKey] as? String ?? ""
66+
frequency = json[PolicyGeneral.FrequencyKey] as? String ?? ""
67+
locationUserOnly = json[PolicyGeneral.LocationUserOnlyKey] as? Bool ?? false
68+
targetDrive = json[PolicyGeneral.TargetDriveKey] as? String ?? ""
69+
offline = json[PolicyGeneral.OfflineKey] as? Bool ?? false
70+
71+
if let categoryNode = json[PolicyGeneral.CategoryKey] as? [String: Any] {
72+
category = PolicyCategory(json: categoryNode)
73+
}
74+
75+
if let dataLimitationsNode = json[PolicyGeneral.DateTimeLimitationsKey] as? [String: Any] {
76+
dateTimeLimitations = PolicyDateTimeLimitations(json: dataLimitationsNode)
77+
}
78+
79+
if let networkLimitationsNode = json[PolicyGeneral.NetworkLimitationsKey] as? [String: Any] {
80+
networkLimitations = PolicyNetworkLimitations(json: networkLimitationsNode)
81+
}
82+
83+
if let overrideDefaultSettingsNode = json[PolicyGeneral.OverrideDefaultSettingsKey] as? [String: Any] {
84+
overrideDefaultSettings = PolicyOverrideDefaultSettings(json: overrideDefaultSettingsNode)
85+
}
86+
87+
networkRequirements = json[PolicyGeneral.NetworkRequirementsKey] as? String ?? ""
88+
89+
if let siteNode = json[PolicyGeneral.SiteKey] as? [String: Any] {
90+
site = Site(json: siteNode)
91+
}
92+
93+
super.init(json: json)
94+
}
95+
96+
// MARK: - Functions
97+
98+
public override func toJSON() -> [String: Any] {
99+
var json = super.toJSON()
100+
101+
json[PolicyGeneral.EnabledKey] = isEnabled
102+
json[PolicyGeneral.TriggerKey] = trigger
103+
json[PolicyGeneral.TriggerCheckinKey] = triggerCheckin
104+
json[PolicyGeneral.TriggerEnrollmentCompleteKey] = triggerEnrollmentComplete
105+
json[PolicyGeneral.TriggerLoginKey] = triggerLogin
106+
json[PolicyGeneral.TriggerLogoutKey] = triggerLogout
107+
json[PolicyGeneral.TriggerNetworkStateChangedKey] = triggerNetworkStateChanged
108+
json[PolicyGeneral.TriggerStartupKey] = triggerStartup
109+
json[PolicyGeneral.TriggerOtherKey] = triggerOther
110+
json[PolicyGeneral.FrequencyKey] = frequency
111+
json[PolicyGeneral.LocationUserOnlyKey] = locationUserOnly
112+
json[PolicyGeneral.TargetDriveKey] = targetDrive
113+
json[PolicyGeneral.OfflineKey] = offline
114+
115+
if let category = category {
116+
json[PolicyGeneral.CategoryKey] = category.toJSON()
117+
}
118+
119+
if let dateTimeLimitations = dateTimeLimitations {
120+
json[PolicyGeneral.DateTimeLimitationsKey] = dateTimeLimitations.toJSON()
121+
}
122+
123+
if let networkLimitations = networkLimitations {
124+
json[PolicyGeneral.NetworkLimitationsKey] = networkLimitations.toJSON()
125+
}
126+
127+
if let overrideDefaultSettings = overrideDefaultSettings {
128+
json[PolicyGeneral.OverrideDefaultSettingsKey] = overrideDefaultSettings.toJSON()
129+
}
130+
131+
json[PolicyGeneral.NetworkRequirementsKey] = networkRequirements
132+
133+
if let site = site {
134+
json[PolicyGeneral.SiteKey] = site.toJSON()
135+
}
136+
137+
return json
138+
}
139+
}

0 commit comments

Comments
 (0)