-
Notifications
You must be signed in to change notification settings - Fork 11
/
protocol.clj
103 lines (84 loc) · 2.71 KB
/
protocol.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
(ns puppetlabs.pcp.protocol
(:require [clojure.string :as str]
[puppetlabs.kitchensink.core :as ks]
[schema.core :as s]))
(def ISO8601
"Schema validates if string conforms to ISO8601"
(s/pred ks/datetime? 'datetime?))
(def Uri
"Schema for PCP node Uri"
(s/pred (partial re-matches #"^pcp://[^/]*/[^/]+$") 'uri?))
(def ExplodedUri
"Schema for PCP node Exploded Uri - an Uri split into the client and type components"
(s/pair s/Str "client" s/Str "type"))
(def InventoryChange
"Schema for a single change in inventory record"
{:client Uri :change (s/enum -1 1)})
(def MessageId
"A message identifier, as a string"
(s/pred ks/uuid?))
(def v2-Envelope
"Defines the envelope format of a v2 message"
{:id MessageId
:message_type s/Str
(s/optional-key :target) Uri
(s/optional-key :sender) Uri
(s/optional-key :in_reply_to) MessageId
(s/optional-key :data) s/Any})
(def v1-Envelope
"Defines the envelope format of a v1 message"
{:id MessageId
(s/optional-key :in-reply-to) MessageId
:sender Uri
:targets [Uri]
:message_type s/Str
:expires ISO8601
(s/optional-key :destination_report) s/Bool})
(def AssociateResponse
"Schema for http://puppetlabs.com/associate_response"
{:id MessageId
:success s/Bool
(s/optional-key :reason) s/Str})
(def InventoryRequest
"Data schema for http://puppetlabs.com/inventory_request"
{:query [Uri]
(s/optional-key :subscribe) s/Bool})
(def InventoryResponse
"Data schema for http://puppetlabs.com/inventory_response"
{:uris [Uri]})
(def InventoryUpdate
"Data schema for http://puppetlabs.com/inventory_update"
{:changes [InventoryChange]})
(def DestinationReport
"Defines the data field for a destination report body"
{:id MessageId
:targets [Uri]})
(def ErrorMessage
"Data schema for http://puppetlabs.com/error_message"
{(s/optional-key :id) MessageId
:description s/Str})
(def v2-ErrorMessage
"Data schema for http://puppetlabs.com/error_message"
s/Str)
(def TTLExpiredMessage
"Data schema for http://puppetlabs.com/ttl_expired"
{:id MessageId})
(def VersionErrorMessage
"Data schema for http://puppetlabs.com/version_error"
{:id MessageId
:target s/Str
:reason s/Str})
(def DebugChunk
"Data schema for a debug chunk"
{:hops [{(s/required-key :server) Uri
(s/optional-key :stage) s/Str
(s/required-key :time) ISO8601}]})
(s/defn explode-uri :- ExplodedUri
"Parse an Uri string into its component parts. Raises if incomplete"
[uri :- Uri]
(str/split (subs uri 6) #"/"))
(s/defn uri-wildcard? :- (s/maybe ExplodedUri)
[uri :- Uri]
(let [chunks (explode-uri uri)]
(if (some (partial = "*") chunks)
chunks)))