github.com/webonyx/up@v0.7.4-0.20180808230834-91b94e551323/platform/lambda/stack/resources/resources_test.go (about) 1 package resources 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "os" 7 "strings" 8 9 "github.com/apex/up" 10 "github.com/apex/up/config" 11 ) 12 13 // keys returns keys from a map. 14 func keys(m Map) (v []string) { 15 for k := range m { 16 v = append(v, k) 17 } 18 return 19 } 20 21 // getResource returns resource by name. 22 func getResource(c *Config, name string) Map { 23 tmpl := New(c) 24 r := tmpl["Resources"].(Map) 25 v, _ := r[name].(Map) 26 return v 27 } 28 29 // dump a resource to stdout. 30 func dump(c *Config, name string) { 31 tmpl := New(c) 32 r := tmpl["Resources"].(Map) 33 34 v, ok := r[name].(Map) 35 if !ok { 36 k := strings.Join(keys(r), "\n - ") 37 panic(fmt.Sprintf("resource %q does not exist in:\n\n - %s", name, k)) 38 } 39 40 { 41 enc := json.NewEncoder(os.Stdout) 42 enc.SetIndent("", " ") 43 enc.Encode(v) 44 } 45 } 46 47 func Example_api() { 48 c := &Config{ 49 Config: &up.Config{ 50 Name: "polls", 51 }, 52 } 53 54 dump(c, "Api") 55 // Output: 56 // { 57 // "Properties": { 58 // "BinaryMediaTypes": [ 59 // "*/*" 60 // ], 61 // "Description": "Managed by Up.", 62 // "Name": { 63 // "Ref": "Name" 64 // } 65 // }, 66 // "Type": "AWS::ApiGateway::RestApi" 67 // } 68 } 69 70 func Example_apiRootMethod() { 71 c := &Config{ 72 Config: &up.Config{ 73 Name: "polls", 74 }, 75 } 76 77 dump(c, "ApiRootMethod") 78 // Output: 79 // { 80 // "Properties": { 81 // "AuthorizationType": "NONE", 82 // "HttpMethod": "ANY", 83 // "Integration": { 84 // "IntegrationHttpMethod": "POST", 85 // "Type": "AWS_PROXY", 86 // "Uri": { 87 // "Fn::Join": [ 88 // "", 89 // [ 90 // "arn:aws:apigateway:", 91 // { 92 // "Ref": "AWS::Region" 93 // }, 94 // ":lambda:path/2015-03-31/functions/", 95 // { 96 // "Fn::Join": [ 97 // ":", 98 // [ 99 // "arn", 100 // "aws", 101 // "lambda", 102 // { 103 // "Ref": "AWS::Region" 104 // }, 105 // { 106 // "Ref": "AWS::AccountId" 107 // }, 108 // "function", 109 // { 110 // "Fn::Join": [ 111 // ":", 112 // [ 113 // { 114 // "Ref": "FunctionName" 115 // }, 116 // "${stageVariables.qualifier}" 117 // ] 118 // ] 119 // } 120 // ] 121 // ] 122 // }, 123 // "/invocations" 124 // ] 125 // ] 126 // } 127 // }, 128 // "ResourceId": { 129 // "Fn::GetAtt": [ 130 // "Api", 131 // "RootResourceId" 132 // ] 133 // }, 134 // "RestApiId": { 135 // "Ref": "Api" 136 // } 137 // }, 138 // "Type": "AWS::ApiGateway::Method" 139 // } 140 } 141 142 func Example_apiProxyResource() { 143 c := &Config{ 144 Config: &up.Config{ 145 Name: "polls", 146 }, 147 } 148 149 dump(c, "ApiProxyResource") 150 // Output: 151 // { 152 // "Properties": { 153 // "ParentId": { 154 // "Fn::GetAtt": [ 155 // "Api", 156 // "RootResourceId" 157 // ] 158 // }, 159 // "PathPart": "{proxy+}", 160 // "RestApiId": { 161 // "Ref": "Api" 162 // } 163 // }, 164 // "Type": "AWS::ApiGateway::Resource" 165 // } 166 } 167 168 func Example_apiProxyMethod() { 169 c := &Config{ 170 Config: &up.Config{ 171 Name: "polls", 172 }, 173 } 174 175 dump(c, "ApiProxyMethod") 176 // Output: 177 // { 178 // "Properties": { 179 // "AuthorizationType": "NONE", 180 // "HttpMethod": "ANY", 181 // "Integration": { 182 // "IntegrationHttpMethod": "POST", 183 // "Type": "AWS_PROXY", 184 // "Uri": { 185 // "Fn::Join": [ 186 // "", 187 // [ 188 // "arn:aws:apigateway:", 189 // { 190 // "Ref": "AWS::Region" 191 // }, 192 // ":lambda:path/2015-03-31/functions/", 193 // { 194 // "Fn::Join": [ 195 // ":", 196 // [ 197 // "arn", 198 // "aws", 199 // "lambda", 200 // { 201 // "Ref": "AWS::Region" 202 // }, 203 // { 204 // "Ref": "AWS::AccountId" 205 // }, 206 // "function", 207 // { 208 // "Fn::Join": [ 209 // ":", 210 // [ 211 // { 212 // "Ref": "FunctionName" 213 // }, 214 // "${stageVariables.qualifier}" 215 // ] 216 // ] 217 // } 218 // ] 219 // ] 220 // }, 221 // "/invocations" 222 // ] 223 // ] 224 // } 225 // }, 226 // "ResourceId": { 227 // "Ref": "ApiProxyResource" 228 // }, 229 // "RestApiId": { 230 // "Ref": "Api" 231 // } 232 // }, 233 // "Type": "AWS::ApiGateway::Method" 234 // } 235 } 236 237 func Example_stageAlias() { 238 c := &Config{ 239 Config: &up.Config{ 240 Name: "polls", 241 Stages: config.Stages{ 242 "production": &config.Stage{ 243 Name: "production", 244 }, 245 }, 246 }, 247 Versions: Versions{ 248 "production": "15", 249 }, 250 } 251 252 dump(c, "ApiFunctionAliasProduction") 253 // Output: 254 // { 255 // "Properties": { 256 // "Description": "Managed by Up.", 257 // "FunctionName": { 258 // "Ref": "FunctionName" 259 // }, 260 // "FunctionVersion": "15", 261 // "Name": "production" 262 // }, 263 // "Type": "AWS::Lambda::Alias" 264 // } 265 } 266 267 func Example_stagePermission() { 268 c := &Config{ 269 Config: &up.Config{ 270 Name: "polls", 271 Stages: config.Stages{ 272 "production": &config.Stage{ 273 Name: "production", 274 }, 275 }, 276 }, 277 Versions: Versions{ 278 "production": "15", 279 }, 280 } 281 282 dump(c, "ApiLambdaPermissionProduction") 283 // Output: 284 // { 285 // "DependsOn": "ApiFunctionAliasProduction", 286 // "Properties": { 287 // "Action": "lambda:invokeFunction", 288 // "FunctionName": { 289 // "Fn::Join": [ 290 // ":", 291 // [ 292 // "arn", 293 // "aws", 294 // "lambda", 295 // { 296 // "Ref": "AWS::Region" 297 // }, 298 // { 299 // "Ref": "AWS::AccountId" 300 // }, 301 // "function", 302 // { 303 // "Fn::Join": [ 304 // ":", 305 // [ 306 // { 307 // "Ref": "FunctionName" 308 // }, 309 // "production" 310 // ] 311 // ] 312 // } 313 // ] 314 // ] 315 // }, 316 // "Principal": "apigateway.amazonaws.com", 317 // "SourceArn": { 318 // "Fn::Join": [ 319 // "", 320 // [ 321 // "arn:aws:execute-api", 322 // ":", 323 // { 324 // "Ref": "AWS::Region" 325 // }, 326 // ":", 327 // { 328 // "Ref": "AWS::AccountId" 329 // }, 330 // ":", 331 // { 332 // "Ref": "Api" 333 // }, 334 // "/*" 335 // ] 336 // ] 337 // } 338 // }, 339 // "Type": "AWS::Lambda::Permission" 340 // } 341 } 342 343 func Example_stageDeployment() { 344 c := &Config{ 345 Config: &up.Config{ 346 Name: "polls", 347 Stages: config.Stages{ 348 "production": &config.Stage{ 349 Name: "production", 350 }, 351 }, 352 }, 353 Versions: Versions{ 354 "production": "15", 355 }, 356 } 357 358 dump(c, "ApiDeploymentProduction") 359 // Output: 360 // { 361 // "DependsOn": [ 362 // "ApiRootMethod", 363 // "ApiProxyMethod", 364 // "ApiFunctionAliasProduction" 365 // ], 366 // "Properties": { 367 // "RestApiId": { 368 // "Ref": "Api" 369 // }, 370 // "StageDescription": { 371 // "Variables": { 372 // "qualifier": "production" 373 // } 374 // }, 375 // "StageName": "production" 376 // }, 377 // "Type": "AWS::ApiGateway::Deployment" 378 // } 379 } 380 381 func Example_stageDomain() { 382 c := &Config{ 383 Config: &up.Config{ 384 Name: "polls", 385 Stages: config.Stages{ 386 "production": &config.Stage{ 387 Name: "production", 388 Domain: "up-example.com", 389 Cert: "arn::something", 390 }, 391 }, 392 }, 393 Versions: Versions{ 394 "production": "15", 395 }, 396 } 397 398 dump(c, "ApiDomainProduction") 399 // Output: 400 // { 401 // "Properties": { 402 // "CertificateArn": "arn::something", 403 // "DomainName": "up-example.com" 404 // }, 405 // "Type": "AWS::ApiGateway::DomainName" 406 // } 407 } 408 409 func Example_stagePathMapping() { 410 c := &Config{ 411 Config: &up.Config{ 412 Name: "polls", 413 Stages: config.Stages{ 414 "production": &config.Stage{ 415 Name: "production", 416 Domain: "up-example.com", 417 }, 418 }, 419 }, 420 Versions: Versions{ 421 "production": "15", 422 }, 423 } 424 425 dump(c, "ApiDomainProductionPathMapping") 426 // Output: 427 // { 428 // "DependsOn": [ 429 // "ApiDeploymentProduction", 430 // "ApiDomainProduction" 431 // ], 432 // "Properties": { 433 // "BasePath": "", 434 // "DomainName": "up-example.com", 435 // "RestApiId": { 436 // "Ref": "Api" 437 // }, 438 // "Stage": "production" 439 // }, 440 // "Type": "AWS::ApiGateway::BasePathMapping" 441 // } 442 } 443 444 func Example_stageDNSZone() { 445 c := &Config{ 446 Config: &up.Config{ 447 Name: "polls", 448 Stages: config.Stages{ 449 "production": &config.Stage{ 450 Name: "production", 451 Domain: "up-example.com", 452 }, 453 }, 454 }, 455 Versions: Versions{ 456 "production": "15", 457 }, 458 } 459 460 dump(c, "DnsZoneUpExampleCom") 461 // Output: 462 // { 463 // "Properties": { 464 // "Name": "up-example.com" 465 // }, 466 // "Type": "AWS::Route53::HostedZone" 467 // } 468 } 469 470 func Example_stageDNSZoneRecord() { 471 c := &Config{ 472 Config: &up.Config{ 473 Name: "polls", 474 Stages: config.Stages{ 475 "production": &config.Stage{ 476 Name: "production", 477 Domain: "up-example.com", 478 }, 479 }, 480 }, 481 Versions: Versions{ 482 "production": "15", 483 }, 484 } 485 486 dump(c, "DnsZoneUpExampleComRecordUpExampleCom") 487 // Output: 488 // { 489 // "Properties": { 490 // "AliasTarget": { 491 // "DNSName": { 492 // "Fn::GetAtt": [ 493 // "ApiDomainProduction", 494 // "DistributionDomainName" 495 // ] 496 // }, 497 // "HostedZoneId": "Z2FDTNDATAQYW2" 498 // }, 499 // "Comment": "Managed by Up.", 500 // "HostedZoneId": { 501 // "Ref": "DnsZoneUpExampleCom" 502 // }, 503 // "Name": "up-example.com", 504 // "Type": "A" 505 // }, 506 // "Type": "AWS::Route53::RecordSet" 507 // } 508 } 509 510 func Example_dnsZone() { 511 c := &Config{ 512 Config: &up.Config{ 513 Name: "polls", 514 DNS: config.DNS{ 515 Zones: []*config.Zone{ 516 { 517 Name: "up-example.com", 518 }, 519 }, 520 }, 521 }, 522 } 523 524 dump(c, "DnsZoneUpExampleCom") 525 // Output: 526 // { 527 // "Properties": { 528 // "Name": "up-example.com" 529 // }, 530 // "Type": "AWS::Route53::HostedZone" 531 // } 532 } 533 534 func Example_dnsZoneRecord() { 535 c := &Config{ 536 Config: &up.Config{ 537 Name: "polls", 538 DNS: config.DNS{ 539 Zones: []*config.Zone{ 540 { 541 Name: "up-example.com", 542 Records: []*config.Record{ 543 { 544 Name: "blog.up-example.com", 545 Type: "CNAME", 546 TTL: 600, 547 Value: []string{"example.medium.com"}, 548 }, 549 }, 550 }, 551 }, 552 }, 553 }, 554 } 555 556 dump(c, "DnsZoneUpExampleComRecordBlogUpExampleComCNAME") 557 // Output: 558 // { 559 // "Properties": { 560 // "Comment": "Managed by Up.", 561 // "HostedZoneId": { 562 // "Ref": "DnsZoneUpExampleCom" 563 // }, 564 // "Name": "blog.up-example.com", 565 // "ResourceRecords": [ 566 // "example.medium.com" 567 // ], 568 // "TTL": "600", 569 // "Type": "CNAME" 570 // }, 571 // "Type": "AWS::Route53::RecordSet" 572 // } 573 }