summaryrefslogtreecommitdiff
path: root/aws/cloudformation/utils.scm
blob: e618cf83f69d71e271dfc8195eb4d7044e31ea47 (plain)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
(define-module (aws cloudformation utils)
  #:use-module (aws cloudformation utils base32)
  #:use-module (aws cloudformation utils sha-1)
  #:use-module (ice-9 match)
  #:use-module (oop goops)
  #:use-module (rnrs bytevectors)
  #:use-module (srfi srfi-1)
  #:export (<cloudformation-property>
            <cloudformation-object>
            <cloudformation-attribute>
            <cloudformation-resource>
            <cloudformation-stack>
            attributes
            aws-string->symbol
            description
            documentation-url
            id
            name
            outputs
            parameters
            primitive-type
            properties
            required?
            resource-name
            resources
            to-json
            type-check
            type-checker
            update-type
            valid?))

(define (aws-string->symbol str)
  ;; Drop the "AWS::" that's at the beginning of *almost* everything.
  (let ((str (if (string-prefix? "AWS::" str)
                 (string-drop str 5)
                 str)))
    (list->symbol
     (let loop ((i 0)
                (same-word? #t)
                (slash-delimiter? #f))
       (cond
        ((= i (string-length str)) ; end of string
         '())
        ;; "IoT" violates all the rules. grrrr
        ((and (< (+ i 3) (string-length str))
              (eqv? (string-ref str i) #\I)
              (eqv? (string-ref str (+ i 1)) #\o)
              (eqv? (string-ref str (+ i 2)) #\T))
         (cons* #\i #\o #\t (loop (+ i 3) #f #f)))
        ;; Replace "." with "/"
        ((eqv? (string-ref str i) #\.)
         (cons #\/ (loop (1+ i) #f #t)))
        ;; Get rid of "::"
        ((and (eqv? (string-ref str i) #\:)
              (eqv? (string-ref str (1+ i)) #\:))
         (loop (+ i 2) #f #f))
        ((char-upper-case? (string-ref str i)) ; detect camel casing
         (cond
          ;; If we've had a string of uppercase characters and the
          ;; next character is lowercase, we're actually at the
          ;; beginning of a new word.  For example, when we reach the
          ;; "I" in "DBInstance" we need to treat it as the beginning
          ;; of a new word.
          ((and same-word?
                (< (1+ i) (string-length str))
                (char-lower-case? (string-ref str (1+ i))))
           (loop i #f #f))
          ;; Consecutive uppercase characters are part of the same word.
          (same-word?
           (cons (char-downcase (string-ref str i)) (loop (1+ i) #t #f)))
          ;; Encountering an uppercase character after a series of
          ;; non-uppercase characters means that we are at the
          ;; beginning of a new word.
          (else
           (if (or (zero? i) slash-delimiter?)
               (cons (char-downcase (string-ref str i)) (loop (1+ i) #t #f))
               (cons* #\- (char-downcase (string-ref str i))
                      (loop (1+ i) #t #f))))))
        (else
         (cons (string-ref str i) (loop (1+ i) #f #f))))))))

(define-class <cloudformation-property> ()
  (name #:getter name #:init-keyword #:name)
  (type-checker #:getter type-checker #:init-keyword #:type-checker)
  (required? #:getter required? #:init-keyword #:required?)
  (update-type #:getter update-type #:init-keyword #:update-type)
  (documentation-url #:getter documentation-url
                     #:init-keyword #:documentation-url))

(define-method (type-check (procedure <procedure>) x)
  (procedure x))

(define-method (type-check (property <cloudformation-property>) x)
  ((type-checker property) x))

(define-class <cloudformation-class> (<class>)
  (properties #:getter properties)
  (cfn-name #:getter cfn-name)
  (documentation-url #:getter documentation-url))

(define-method (initialize (cfn-class <cloudformation-class>) args)
  (define* (slot-cfn-property slot-name #:key cfn-property #:allow-other-keys)
    (and cfn-property (cons slot-name cfn-property)))
  (define* (init #:key cfn-name documentation-url slots #:allow-other-keys)
    (slot-set! cfn-class 'cfn-name cfn-name)
    (slot-set! cfn-class 'documentation-url documentation-url)
    (slot-set! cfn-class
               'properties
               (filter-map (lambda (slot-args)
                             (apply slot-cfn-property slot-args))
                           slots)))
  (apply init args)
  (next-method))

(define-class <cloudformation-object> ()
  #:metaclass <cloudformation-class>)

(define-method (initialize (obj <cloudformation-object>) args)
  (next-method)
  ;; Ensure that all required properties have been specified, and that
  ;; all specified properties pass the type checker.
  (for-each (match-lambda
              ((slot-name . prop)
               (cond
                ((and (required? prop) (not (slot-bound? obj slot-name)))
                 (error "required property not specified:" slot-name))
                ((slot-bound? obj slot-name)
                 (let ((value (slot-ref obj slot-name)))
                   (unless (type-check prop value)
                     (error "wrong type for property:" slot-name value)))))))
            (slot-ref (class-of obj) 'properties)))

(define-method (cfn-name (obj <cloudformation-object>))
  (cfn-name (class-of obj)))

(define-method (properties (obj <cloudformation-object>))
  (slot-ref (class-of obj) 'properties))

(define-method (to-json/refs obj)
  (to-json obj))

(define-method (to-json (s <string>)) s)
(define-method (to-json (b <boolean>)) b)
(define-method (to-json (n <number>)) n)
(define-method (to-json (p <pair>))
  (if (pair? (car p))
      `(@ ,@(map (match-lambda
                   ((k . v)
                    (cons k (to-json/refs v))))
                 p))
      (cons (to-json/refs (car p)) (to-json (cdr p)))))
(define-method (to-json (null <null>))
  '())

(define-method (to-json (obj <cloudformation-object>))
  (cons '@
   (filter-map (match-lambda
                 ((slot-name . property)
                  (and (slot-bound? obj slot-name)
                       (cons (name property)
                             (to-json/refs (slot-ref obj slot-name))))))
               (properties obj))))

(define-method (valid? (obj <cloudformation-object>))
  (every (match-lambda
           ((slot-name . property)
            (cond
             ((and (required? property) (not (slot-bound? obj slot-name)))
              #f)
             ((not (or (required? property) (slot-bound? obj slot-name)))
              #t)
             (else
              (type-check property (slot-ref obj slot-name))))))
         (slot-ref (class-of obj) 'properties)))

(define-class <cloudformation-attribute> ()
  (name #:getter name #:init-keyword #:name)
  (primitive-type #:getter primitive-type #:init-keyword #:primitive-type))

(define-class <cloudformation-resource-class> (<cloudformation-class>)
  (resource-name-prefix #:getter resource-name-prefix)
  (attributes #:getter attributes))

(define-method (initialize (resource-class <cloudformation-resource-class>) args)
  (define* (init #:key attributes cfn-name #:allow-other-keys)
    (when cfn-name
      (slot-set! resource-class 'resource-name-prefix
                 (string-join (drop (delete "" (string-split cfn-name #\:)) 1)
                              "")))
    (slot-set! resource-class 'attributes attributes))
  (apply init args)
  (next-method))

(define-class <cloudformation-resource> (<cloudformation-object>)
  (id #:getter id #:init-keyword #:id)
  (cfn-id #:getter cfn-id)
  #:metaclass <cloudformation-resource-class>)

(define char-set:alphanumeric
  (char-set-intersection char-set:ascii char-set:letter+digit))

(define (alphanumize s)
  (string-filter char-set:alphanumeric s))

(define (id->cfn-id sym)
  (string-append (let ((camelized
                        (string-concatenate
                         (map (lambda (s)
                                (string-capitalize (alphanumize s)))
                              (string-split (symbol->string sym) #\-)))))
                   (if (> (string-length camelized) 223)
                       (substring camelized 223)
                       camelized))
                 (base32-encode
                  (sha-1->bytevector
                   (sha-1
                    (string->utf8
                     (symbol->string sym)))))))

(define-method (initialize (resource <cloudformation-resource>) args)
  (next-method)
  (unless (slot-bound? resource 'id)
    (error "no id specified for resource:" resource))
  (slot-set! resource 'cfn-id (id->cfn-id (id resource))))

(define-method (type-check (proc <procedure>)
                           (resource <cloudformation-resource>))
  (type-check proc "arn:dummy"))

(define-method (to-json/refs (resource <cloudformation-resource>))
  `(@ ("Ref" . ,(cfn-id resource))))

(define-method (to-json (resource <cloudformation-resource>))
  `(@ ("Type" . ,(cfn-name resource))
      ("Properties" . ,(next-method))))

(define-class <cloudformation-stack> ()
  (description #:getter description #:init-keyword #:description #:init-form "")
  (parameters #:getter parameters #:init-keyword #:parameters)
  (resources #:getter resources #:init-keyword #:resources)
  (outputs #:getter outputs #:init-keyword #:outputs))

(define-method (transitive-resources (stack <cloudformation-stack>))
  (define (scan-object obj)
    (cond
     ((is-a? obj <pair>)
      (match obj
        ((first rest ...)
         (concatenate (cons (scan-object first)
                            (scan-object rest))))
        ((key . value)
         (scan-object value))))
     ((is-a? obj <cloudformation-resource>)
      (list obj))
     ((is-a? obj <cloudformation-object>)
      (scan-properties obj))
     (else
      '())))
  (define (scan-properties obj)
    (append-map (match-lambda
                  ((slot-name . prop)
                   (let ((value (and (slot-bound? obj slot-name)
                                     (slot-ref obj slot-name))))
                     (scan-object value))))
                (properties obj)))
  (append-map (lambda (resource)
                (cons resource (scan-properties resource)))
              (resources stack)))

(define-method (to-json (stack <cloudformation-stack>))
  `(@ ("AWSTemplateFormatVersion" . "2010-09-09")
      ("Description" . ,(description stack))
      ("Resources" .
       (@ ,@(map (lambda (resource)
                   (cons (cfn-id resource)
                         (to-json resource)))
                 (transitive-resources stack))))))