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