├── Dockerfile ├── ci ├── tasks │ ├── build-candidate.sh │ ├── unit.sh │ ├── util.sh │ ├── integration.sh │ ├── run-integration.sh │ └── ipam-service-release.sh ├── integration │ └── docker-compose.yml └── pipeline.yml ├── .gitignore ├── server ├── server_suite_test.go ├── server_test.go ├── server.go ├── handlers.go └── handlers_test.go ├── glide.yaml ├── main.go ├── README.md ├── Makefile ├── glide.lock └── LICENSE /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM ubuntu:16.04 2 | 3 | ADD bin/voyager-ipam-service voyager-ipam-service 4 | 5 | CMD ./voyager-ipam-service 6 | -------------------------------------------------------------------------------- /ci/tasks/build-candidate.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e -x 4 | source voyager-ipam-service/ci/tasks/util.sh 5 | 6 | check_param GITHUB_USER 7 | check_param GITHUB_PASSWORD 8 | 9 | echo -e "machine github.com\n login $GITHUB_USER\n password $GITHUB_PASSWORD" >> ~/.netrc 10 | 11 | set_env 12 | build_binary "voyager-ipam-service" 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | *.exe 3 | *.dll 4 | *.so 5 | *.dylib 6 | 7 | # Test binary, build with `go test -c` 8 | *.test 9 | 10 | # Output of the go coverage tool, specifically when used with LiteIDE 11 | *.out 12 | 13 | # Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736 14 | .glide/ 15 | bin 16 | *.coverprofile 17 | -------------------------------------------------------------------------------- /server/server_suite_test.go: -------------------------------------------------------------------------------- 1 | package server_test 2 | 3 | import ( 4 | . "github.com/onsi/ginkgo" 5 | . "github.com/onsi/gomega" 6 | 7 | "log" 8 | "math/rand" 9 | "testing" 10 | "time" 11 | ) 12 | 13 | func TestServer(t *testing.T) { 14 | RegisterFailHandler(Fail) 15 | RunSpecs(t, "Server Suite") 16 | } 17 | 18 | var _ = BeforeSuite(func() { 19 | rand.Seed(time.Now().UTC().UnixNano()) 20 | log.Printf("Random seed is set\n\n\n") 21 | }) 22 | -------------------------------------------------------------------------------- /glide.yaml: -------------------------------------------------------------------------------- 1 | package: github.com/RackHD/voyager-ipam-service 2 | import: 3 | - package: github.com/RackHD/ipam 4 | subpackages: 5 | - client 6 | - resources 7 | - package: github.com/RackHD/voyager-utilities 8 | subpackages: 9 | - amqp 10 | - models 11 | - package: github.com/sirupsen/logrus 12 | version: ~0.11.0 13 | - package: github.com/streadway/amqp 14 | testImport: 15 | - package: github.com/onsi/ginkgo 16 | version: ~1.2.0 17 | - package: github.com/onsi/gomega 18 | version: ~1.0.0 19 | -------------------------------------------------------------------------------- /ci/integration/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | services: 3 | rabbitmq: 4 | image: rabbitmq:3-management 5 | ports: 6 | - "5672:5672" 7 | container_name: "rabbitmq" 8 | hostname: "rabbitmq" 9 | 10 | ipam: 11 | image: rackhd/ipam:latest 12 | container_name: "ipam" 13 | hostname: "ipam" 14 | ports: 15 | - "8000:8000" 16 | command: "-mongo ipam-mongo:27017" 17 | depends_on: 18 | - ipam-mongo 19 | 20 | ipam-mongo: 21 | image: mongo:3.2.10 22 | expose: 23 | - "27017" 24 | container_name: "ipam-mongo" 25 | hostname: "ipam-mongo" 26 | -------------------------------------------------------------------------------- /ci/tasks/unit.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -e -x 4 | source voyager-ipam-service/ci/tasks/util.sh 5 | 6 | check_param GITHUB_USER 7 | check_param GITHUB_PASSWORD 8 | 9 | echo -e "machine github.com\n login $GITHUB_USER\n password $GITHUB_PASSWORD" >> ~/.netrc 10 | 11 | export GOPATH=$PWD 12 | export PATH=$PATH:$GOPATH/bin 13 | mkdir -p $GOPATH/src/github.com/RackHD/ 14 | cp -r voyager-ipam-service $GOPATH/src/github.com/RackHD/voyager-ipam-service 15 | 16 | pushd $GOPATH/src/github.com/RackHD/voyager-ipam-service 17 | make deps 18 | make build 19 | make unit-test 20 | echo "Unit test complete." 21 | popd 22 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "flag" 5 | "log" 6 | 7 | "github.com/RackHD/voyager-ipam-service/server" 8 | ) 9 | 10 | var ( 11 | amqpAddress = flag.String("amqp-address", "amqp://guest:guest@rabbitmq:5672/", "AMQP URI") 12 | ipamAddress = flag.String("ipam-address", "ipam:8000", "Address of the IPAM instance to connect to") 13 | ) 14 | 15 | func init() { 16 | flag.Parse() 17 | } 18 | 19 | func main() { 20 | s := server.NewServer(*amqpAddress, *ipamAddress) 21 | if s.MQ == nil { 22 | log.Fatalf("Could not connect to RabbitMQ") 23 | } 24 | defer s.MQ.Close() 25 | 26 | s.Start() 27 | 28 | select {} 29 | } 30 | -------------------------------------------------------------------------------- /ci/tasks/util.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e -x 3 | 4 | check_param() { 5 | local name=$1 6 | local value=$(eval echo '$'$name) 7 | if [ "$value" == 'replace-me' ]; then 8 | echo "environment variable $name must be set" 9 | exit 1 10 | fi 11 | } 12 | 13 | set_env() { 14 | WORK_DIR=${PWD} 15 | export GOPATH=${PWD} 16 | SOURCE_DIR=${GOPATH}/src/github.com/RackHD 17 | mkdir -p ${SOURCE_DIR} 18 | } 19 | 20 | build_binary() { 21 | cp -r $1 ${SOURCE_DIR}/$1 22 | pushd ${SOURCE_DIR}/$1 23 | make deps 24 | make build 25 | cp -r bin ${WORK_DIR}/build 26 | cp Dockerfile ${WORK_DIR}/build 27 | popd 28 | } 29 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # voyager-ipam-service 2 | 3 | Copyright © 2017 Dell Inc. or its subsidiaries. All Rights Reserved. 4 | 5 | ## Licensing 6 | 7 | Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 10 | 11 | RackHD is a Trademark of Dell EMC 12 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | ORGANIZATION = RackHD 2 | PROJECT = voyager-ipam-service 3 | BINARYNAME = voyager-ipam-service 4 | GOOUT = ./bin 5 | export RABBITMQ_URL = amqp://guest:guest@localhost:5672 6 | 7 | default: deps build test 8 | 9 | deps: 10 | go get github.com/onsi/ginkgo/ginkgo 11 | go get github.com/onsi/gomega 12 | go get github.com/satori/go.uuid 13 | go get ./... 14 | 15 | integration-test: 16 | ginkgo -r -race -trace -cover -randomizeAllSpecs --slowSpecThreshold=30 --focus="\bINTEGRATION\b" -v 17 | 18 | unit-test: 19 | ginkgo -r -race -trace -cover -randomizeAllSpecs --slowSpecThreshold=30 --focus="\bUNIT\b" -v 20 | 21 | test: 22 | ginkgo -r -race -trace -cover 23 | 24 | cover-cmd: test 25 | go tool cover -html=cmd/cmd.coverprofile 26 | 27 | build: 28 | go build -o $(GOOUT)/$(BINARYNAME) 29 | -------------------------------------------------------------------------------- /ci/tasks/integration.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e -x 4 | source voyager-ipam-service/ci/tasks/util.sh 5 | 6 | # Get the env ip from locks input 7 | export INTEGRATION_VM_IP=$(cat $PWD/it-env/metadata) 8 | 9 | check_param INTEGRATION_VM_IP 10 | check_param INTEGRATION_VM_SSH_KEY 11 | check_param INTEGRATION_VM_USER 12 | 13 | echo "$INTEGRATION_VM_SSH_KEY" > ssh.key 14 | chmod 400 ssh.key 15 | 16 | project_path=/home/emc/.gvm/pkgsets/go1.7/global/src/github.com/RackHD 17 | ssh -i ssh.key -o "StrictHostKeyChecking no" ${INTEGRATION_VM_USER}@${INTEGRATION_VM_IP} "rm -rf $project_path && mkdir -p $project_path" 18 | scp -i ssh.key -o "StrictHostKeyChecking no" -r voyager-ipam-service ${INTEGRATION_VM_USER}@${INTEGRATION_VM_IP}:$project_path 19 | 20 | integration_file=$PWD/voyager-ipam-service/ci/tasks/run-integration.sh 21 | ssh -i ssh.key -o "StrictHostKeyChecking no" ${INTEGRATION_VM_USER}@${INTEGRATION_VM_IP} 'bash -s' < $integration_file 22 | -------------------------------------------------------------------------------- /ci/tasks/run-integration.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e -x 4 | 5 | [[ -s "/home/emc/.gvm/scripts/gvm" ]] >/dev/null 2>/dev/null 6 | source "/home/emc/.gvm/scripts/gvm" >/dev/null 2>/dev/null 7 | 8 | PROJECT_PATH=$GOPATH/src/github.com/RackHD 9 | COMPOSE_PATH=ci/integration/docker-compose.yml 10 | 11 | cleanUp() 12 | { 13 | # Don't exit on error here. All commands in this cleanUp must run, 14 | # even if some of them fail 15 | set +e 16 | 17 | # Delete all containers 18 | docker rm -f $(docker ps -a -q) 19 | 20 | # Delete all images 21 | docker rmi -f $(docker images -q) 22 | 23 | # Delete any dangling volumes 24 | docker volume rm $(docker volume ls -qf dangling=true) 25 | 26 | # Clean up all cloned repos 27 | cd $GOPATH 28 | rm -rf $GOPATH/src 29 | } 30 | 31 | trap cleanUp EXIT 32 | 33 | pushd $PROJECT_PATH/voyager-ipam-service 34 | echo "Testing IPAM Service" 35 | 36 | docker-compose -f ${COMPOSE_PATH} create 37 | docker-compose -f ${COMPOSE_PATH} start 38 | 39 | make deps 40 | make integration-test 41 | 42 | docker-compose -f ${COMPOSE_PATH} kill 43 | # Delete all containers 44 | docker rm -f $(docker ps -a -q) 45 | 46 | echo "IPAM Service PASS\n\n" 47 | popd 48 | 49 | exit 50 | -------------------------------------------------------------------------------- /ci/tasks/ipam-service-release.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -e -x 4 | source voyager-ipam-service/ci/tasks/util.sh 5 | 6 | check_param GITHUB_EMAIL 7 | check_param GITHUB_USER 8 | check_param GITHUB_PASSWORD 9 | 10 | echo -e "machine github.com\n login $GITHUB_USER\n password $GITHUB_PASSWORD" >> ~/.netrc 11 | git config --global user.email ${GITHUB_EMAIL} 12 | git config --global user.name ${GITHUB_USER} 13 | git config --global push.default current 14 | 15 | export GOPATH=$PWD 16 | export PATH=$PATH:$GOPATH/bin 17 | mkdir -p $GOPATH/src/github.com/RackHD/ 18 | cp -r voyager-ipam-service $GOPATH/src/github.com/RackHD/voyager-ipam-service 19 | 20 | pushd $GOPATH/src/github.com/RackHD/voyager-ipam-service 21 | make deps 22 | make build 23 | 24 | release_version=`cat version | tr -d '\n'` 25 | release_version=$((release_version+1)) 26 | printf ${release_version} > version 27 | 28 | git add version 29 | git commit -m ":airplane: New release v${release_version}" -m "[ci skip]" 30 | 31 | printf "voyager-ipam-service Release v${release_version}" > name 32 | printf "v${release_version}" > tag 33 | tar -czvf voyager-ipam-service-v${release_version}.tar.gz ./bin/* 34 | echo "New version released." 35 | popd 36 | 37 | cp -r $GOPATH/src/github.com/RackHD/voyager-ipam-service release 38 | -------------------------------------------------------------------------------- /glide.lock: -------------------------------------------------------------------------------- 1 | hash: ae838b6138bcfcb053e09171c624a7030ed854e9a9bd8d66729a2e13246a8aca 2 | updated: 2017-01-05T14:30:24.647715319-05:00 3 | imports: 4 | - name: github.com/hashicorp/go-cleanhttp 5 | version: ad28ea4487f05916463e2423a55166280e8254b5 6 | - name: github.com/RackHD/voyager-utilities 7 | version: 3d475365f014ecf9e2b75e80220ebc0ab61d05b3 8 | subpackages: 9 | - amqp 10 | - models 11 | - random 12 | - name: github.com/RackHD/ipam 13 | version: ec32e6d0cf2d123f014227de00495ad6badba931 14 | subpackages: 15 | - client 16 | - controllers/helpers 17 | - interfaces 18 | - models 19 | - resources 20 | - resources/factory 21 | - name: github.com/satori/go.uuid 22 | version: b061729afc07e77a8aa4fad0a2fd840958f1942a 23 | - name: github.com/sirupsen/logrus 24 | version: d26492970760ca5d33129d2d799e34be5c4782eb 25 | - name: github.com/streadway/amqp 26 | version: 63795daa9a446c920826655f26ba31c81c860fd6 27 | - name: golang.org/x/sys 28 | version: d75a52659825e75fff6158388dddc6a5b04f9ba5 29 | subpackages: 30 | - unix 31 | - name: gopkg.in/mgo.v2 32 | version: 3f83fa5005286a7fe593b055f0d7771a7dce4655 33 | subpackages: 34 | - bson 35 | - internal/json 36 | testImports: 37 | - name: github.com/onsi/ginkgo 38 | version: 462326b1628e124b23f42e87a8f2750e3c4e2d24 39 | subpackages: 40 | - config 41 | - internal/codelocation 42 | - internal/containernode 43 | - internal/failer 44 | - internal/leafnodes 45 | - internal/remote 46 | - internal/spec 47 | - internal/specrunner 48 | - internal/suite 49 | - internal/testingtproxy 50 | - internal/writer 51 | - reporters 52 | - reporters/stenographer 53 | - types 54 | - name: github.com/onsi/gomega 55 | version: a78ae492d53aad5a7a232d0d0462c14c400e3ee7 56 | subpackages: 57 | - format 58 | - internal/assertion 59 | - internal/asyncassertion 60 | - internal/testingtsupport 61 | - matchers 62 | - matchers/support/goraph/bipartitegraph 63 | - matchers/support/goraph/edge 64 | - matchers/support/goraph/node 65 | - matchers/support/goraph/util 66 | - types 67 | -------------------------------------------------------------------------------- /server/server_test.go: -------------------------------------------------------------------------------- 1 | package server_test 2 | 3 | import ( 4 | "encoding/json" 5 | "os" 6 | 7 | "github.com/RackHD/ipam/resources" 8 | . "github.com/onsi/ginkgo" 9 | . "github.com/onsi/gomega" 10 | "github.com/RackHD/voyager-ipam-service/server" 11 | "github.com/RackHD/voyager-utilities/models" 12 | "github.com/RackHD/voyager-utilities/random" 13 | log "github.com/sirupsen/logrus" 14 | ) 15 | 16 | var _ = Describe("INTEGRATION", func() { 17 | Describe("AMQP Message Handling", func() { 18 | var srv *server.Server 19 | var amqpServer string 20 | var err error 21 | 22 | BeforeEach(func() { 23 | amqpServer = os.Getenv("RABBITMQ_URL") 24 | srv = server.NewServer(amqpServer, "127.0.0.1:8000") 25 | Expect(srv.MQ).ToNot(BeNil()) 26 | }) 27 | 28 | AfterEach(func() { 29 | srv.MQ.Close() 30 | }) 31 | 32 | Context("when a ip request is recevied", func() { 33 | var pool resources.PoolV1 34 | var subnet resources.SubnetV1 35 | 36 | BeforeEach(func() { 37 | pool = resources.PoolV1{Name: "POOL-FOR-LEASE-TESTS"} 38 | pool, err = srv.IPAM.CreateShowPool(pool) 39 | Expect(err).ToNot(HaveOccurred()) 40 | 41 | subnet = resources.SubnetV1{ 42 | Name: "SUBNET-FOR-LEASE-TESTS", 43 | Pool: pool.ID, 44 | Start: "192.168.1.10", 45 | End: "192.168.1.11", 46 | } 47 | 48 | subnet, err = srv.IPAM.CreateShowSubnet(pool.ID, subnet) 49 | Expect(err).ToNot(HaveOccurred()) 50 | 51 | err = srv.Start() 52 | Expect(err).ToNot(HaveOccurred()) 53 | }) 54 | 55 | AfterEach(func() { 56 | _, err = srv.IPAM.DeletePool(pool.ID, pool) 57 | Expect(err).ToNot(HaveOccurred()) 58 | 59 | err = srv.Stop() 60 | Expect(err).ToNot(HaveOccurred()) 61 | }) 62 | 63 | It("INTEGRATION should return a valid ip", func() { 64 | ipamQueueName := random.RandQueue() 65 | _, sendDeliveries, err := srv.MQ.Listen(models.IpamExchange, models.IpamExchangeType, ipamQueueName, models.IpamSendQueue, "") 66 | Expect(err).ToNot(HaveOccurred()) 67 | 68 | ipReq := models.IpamLeaseReq{ 69 | Action: models.RequestIPAction, 70 | SubnetID: subnet.ID, 71 | } 72 | ipReqBytes, err := json.Marshal(ipReq) 73 | Expect(err).ToNot(HaveOccurred()) 74 | err = srv.MQ.Send(models.IpamExchange, models.IpamExchangeType, models.IpamReceiveQueue, string(ipReqBytes), "", models.IpamSendQueue) 75 | Expect(err).ToNot(HaveOccurred()) 76 | 77 | message := <-sendDeliveries 78 | message.Ack(false) 79 | var resp models.IpamLeaseResp 80 | err = json.Unmarshal(message.Body, &resp) 81 | log.Infof("resp is %+v", resp) 82 | Expect(err).ToNot(HaveOccurred()) 83 | Expect(resp.Failed).To(BeFalse()) 84 | Expect(resp.Error).To(BeEmpty()) 85 | Expect(resp.IP).To(Equal("192.168.1.10")) 86 | Expect(resp.Reservation).ToNot(BeEmpty()) 87 | }) 88 | 89 | }) 90 | }) 91 | }) 92 | -------------------------------------------------------------------------------- /ci/pipeline.yml: -------------------------------------------------------------------------------- 1 | jobs: 2 | - name: unit 3 | plan: 4 | - aggregate: 5 | - get: voyager-ipam-service 6 | trigger: true 7 | - get: concourse-whale 8 | - task: unit 9 | image: concourse-whale 10 | config: 11 | platform: linux 12 | inputs: 13 | - name: voyager-ipam-service 14 | params: 15 | GITHUB_USER: {{github_username}} 16 | GITHUB_PASSWORD: {{github_password}} 17 | run: 18 | path: voyager-ipam-service/ci/tasks/unit.sh 19 | 20 | - name: integration 21 | plan: 22 | - aggregate: 23 | - put: it-env 24 | params: {acquire: true} 25 | - get: voyager-ipam-service 26 | trigger: true 27 | passed: [unit] 28 | - get: concourse-whale 29 | - task: integration 30 | image: concourse-whale 31 | config: 32 | platform: linux 33 | inputs: 34 | - name: voyager-ipam-service 35 | - name: it-env 36 | params: 37 | GITHUB_USER: {{github_username}} 38 | GITHUB_PASSWORD: {{github_password}} 39 | INTEGRATION_VM_USER: {{integration_vm_user}} 40 | INTEGRATION_VM_SSH_KEY: {{integration_vm_ssh_key}} 41 | run: 42 | path: voyager-ipam-service/ci/tasks/integration.sh 43 | ensure: 44 | put: it-env 45 | params: {release: it-env} 46 | 47 | - name: build-docker 48 | serial: true 49 | plan: 50 | - aggregate: 51 | - get: version 52 | params: {bump: patch} 53 | - get: concourse-whale 54 | - get: voyager-ipam-service 55 | trigger: true 56 | passed: [integration] 57 | - task: build 58 | image: concourse-whale 59 | config: 60 | platform: linux 61 | inputs: 62 | - name: voyager-ipam-service 63 | - name: version 64 | outputs: 65 | - name: build 66 | params: 67 | GITHUB_USER: {{github_username}} 68 | GITHUB_PASSWORD: {{github_password}} 69 | run: 70 | path: voyager-ipam-service/ci/tasks/build-candidate.sh 71 | - put: docker-voyager-ipam-service 72 | params: 73 | build: build 74 | tag: version/version 75 | - put: version 76 | params: {file: version/version} 77 | 78 | resources: 79 | - name: voyager-ipam-service 80 | type: git 81 | default-github: &github-secrets 82 | username: {{github_username}} 83 | password: {{github_password}} 84 | skip_ssl_verification: true 85 | source: 86 | uri: https://github.com/RackHD/voyager-ipam-service.git 87 | branch: master 88 | <<: *github-secrets 89 | 90 | - name: version 91 | type: semver 92 | source: 93 | driver: git 94 | uri: https://github.com/RackHD/voyager-ipam-service.git 95 | branch: version 96 | file: version 97 | <<: *github-secrets 98 | 99 | - name: it-env 100 | type: pool 101 | source: 102 | uri: https://github.com/RackHD/voyager-release.git 103 | branch: locks 104 | pool: integration 105 | <<: *github-secrets 106 | 107 | - name: concourse-whale 108 | type: docker-image 109 | source: 110 | repository: {{concourse_whale_repository}} 111 | insecure_registries: [{{docker_insecure_registries}}] 112 | 113 | - name: docker-voyager-ipam-service 114 | type: docker-image 115 | source: 116 | repository: {{docker_ipam_service_private_repository}} 117 | insecure_registries: [{{docker_insecure_registries}}] 118 | -------------------------------------------------------------------------------- /server/server.go: -------------------------------------------------------------------------------- 1 | package server 2 | 3 | import ( 4 | "encoding/json" 5 | "fmt" 6 | 7 | ipamapi "github.com/RackHD/ipam/client" 8 | "github.com/RackHD/voyager-utilities/amqp" 9 | "github.com/RackHD/voyager-utilities/models" 10 | "github.com/RackHD/voyager-utilities/random" 11 | log "github.com/sirupsen/logrus" 12 | samqp "github.com/streadway/amqp" 13 | ) 14 | 15 | // Server defines amqp server 16 | type Server struct { 17 | MQ *amqp.Client 18 | IPAM *ipamapi.Client 19 | } 20 | 21 | // NewServer creates new amqp server 22 | func NewServer(amqpServer, ipamAddress string) *Server { 23 | server := Server{} 24 | server.MQ = amqp.NewClient(amqpServer) 25 | if server.MQ == nil { 26 | log.Fatalf("Could not connect to RabbitMQ server: %s\n", amqpServer) 27 | } 28 | server.IPAM = ipamapi.NewClient(ipamAddress) 29 | return &server 30 | } 31 | 32 | // Start starts the server 33 | func (s *Server) Start() error { 34 | ipamQueueName := random.RandQueue() 35 | _, deliveries, err := s.MQ.Listen(models.IpamExchange, models.IpamExchangeType, ipamQueueName, models.IpamReceiveQueue, "") 36 | if err != nil { 37 | log.Printf("Count not start the server: %s\n", err) 38 | return err 39 | } 40 | 41 | go func() { 42 | for m := range deliveries { 43 | log.Printf("got %dB delivery on exchange %s: [%v] %s", len(m.Body), m.Exchange, m.DeliveryTag, m.Body) 44 | m.Ack(true) 45 | go s.ProcessMessage(&m) 46 | } 47 | }() 48 | 49 | return nil 50 | } 51 | 52 | // Stop stops the ipam amqp server 53 | func (s *Server) Stop() error { 54 | err := s.MQ.Close() 55 | if err != nil { 56 | log.Printf("Server Not started: %s\n", err) 57 | } 58 | return err 59 | } 60 | 61 | // ProcessMessage processes a message 62 | func (s *Server) ProcessMessage(msg *samqp.Delivery) error { 63 | if msg.Exchange != models.IpamExchange { 64 | log.Printf("Unknown exchange name: %s\n", msg.Exchange) 65 | return fmt.Errorf("[ERROR] Unknown exchange name: %s\n", msg.Exchange) 66 | } 67 | return s.processIpamService(msg) 68 | } 69 | 70 | // processIpamService processes a message from the Ipam exchange 71 | func (s *Server) processIpamService(d *samqp.Delivery) error { 72 | var req models.IpamLeaseReq 73 | 74 | err := json.Unmarshal(d.Body, &req) 75 | if err != nil { 76 | log.Println(err) 77 | return fmt.Errorf("[ERROR] %s\n", err) 78 | } 79 | log.Printf("Unmarshalled message: %s\n", req) 80 | 81 | switch req.Action { 82 | case models.RequestIPAction: 83 | resp, err := s.IPHandler(req.SubnetID) 84 | if err != nil { 85 | errResp := models.AmqpResp{ 86 | Failed: true, 87 | Error: err.Error(), 88 | } 89 | r, e := json.Marshal(errResp) 90 | if e != nil { 91 | log.Println(e) 92 | return fmt.Errorf("[ERROR] When marshalling message %s", e) 93 | } 94 | resp = string(r) 95 | } 96 | 97 | err = s.MQ.Send(d.Exchange, models.IpamExchangeType, d.ReplyTo, resp, d.CorrelationId, "") 98 | if err != nil { 99 | log.Println(err) 100 | return fmt.Errorf("failed to send resp to %s due to %s", d.ReplyTo, err) 101 | } 102 | 103 | return nil 104 | 105 | case models.CreateAction: 106 | err := s.processIpamCreate(d) 107 | if err != nil { 108 | log.Println(err) 109 | } 110 | return err 111 | 112 | default: 113 | log.Println("Unknown Message Action") 114 | return fmt.Errorf("[ERROR] Unknown Action: %s", req.Action) 115 | } 116 | } 117 | 118 | func (s *Server) processIpamCreate(d *samqp.Delivery) error { 119 | var amqpIpam models.AMQPMsg 120 | 121 | var response string 122 | var err error 123 | 124 | if err = json.Unmarshal(d.Body, &amqpIpam); err != nil { 125 | log.Println(err) 126 | return fmt.Errorf("[ERROR] %s\n", err) 127 | } 128 | 129 | log.Printf("Received message on voyager-ipam-service Exchange: %s\n", d.Body) 130 | log.Printf("Unmarshalled message: %s\n", amqpIpam) 131 | 132 | switch amqpIpam.ObjectType { 133 | case models.PoolType: 134 | 135 | response, err = s.PoolHandler(d) 136 | if err != nil { 137 | log.Warn(err) 138 | } 139 | 140 | case models.SubnetType: 141 | 142 | response, err = s.SubnetHandler(d) 143 | if err != nil { 144 | log.Warn(err) 145 | } 146 | 147 | case models.ReservationType: 148 | 149 | response, err = s.ReservationHandler(d) 150 | if err != nil { 151 | log.Warn(err) 152 | } 153 | 154 | case models.LeaseType: 155 | 156 | response, err = s.LeaseHandler(d) 157 | if err != nil { 158 | log.Warn(err) 159 | } 160 | 161 | default: 162 | response = fmt.Sprintf("[ERROR] Unknown ObjectType: %s", amqpIpam.ObjectType) 163 | log.Warn(response) 164 | } 165 | 166 | err = s.MQ.Send(d.Exchange, "topic", d.ReplyTo, response, d.CorrelationId, "") 167 | log.Printf("Message Sent : %s\n", response) 168 | return err 169 | } 170 | -------------------------------------------------------------------------------- /server/handlers.go: -------------------------------------------------------------------------------- 1 | package server 2 | 3 | import ( 4 | "encoding/json" 5 | "fmt" 6 | "log" 7 | 8 | "github.com/RackHD/ipam/resources" 9 | "github.com/RackHD/voyager-utilities/models" 10 | "github.com/streadway/amqp" 11 | ) 12 | 13 | // IPHandler handles reservation rabbitmq request 14 | func (s *Server) IPHandler(subnetID string) (string, error) { 15 | 16 | reservation, err := s.IPAM.CreateShowReservation(subnetID, resources.ReservationV1{}) 17 | if err != nil { 18 | resp := fmt.Errorf("[ERROR] getting response from creating reservation %s", err) 19 | log.Println(resp) 20 | return resp.Error(), resp 21 | } 22 | 23 | leases, err := s.IPAM.IndexLeases(reservation.ID) 24 | if err != nil || len(leases.Leases) == 0 { 25 | resp := fmt.Errorf("[ERROR] getting lease, %s", err) 26 | log.Println(resp) 27 | return resp.Error(), resp 28 | } 29 | 30 | resp := models.IpamLeaseResp{ 31 | IP: leases.Leases[0].Address, 32 | Reservation: reservation.ID, 33 | } 34 | response, err := json.Marshal(resp) 35 | if err != nil { 36 | resp := fmt.Errorf("[ERROR] marshalling create reservation response %s", err) 37 | log.Println(resp) 38 | return resp.Error(), resp 39 | } 40 | return string(response), nil 41 | } 42 | 43 | // PoolHandler handles pool rabbitmq request 44 | func (s *Server) PoolHandler(d *amqp.Delivery) (string, error) { 45 | msg := models.IPAMPoolMsg{} 46 | if err := json.Unmarshal(d.Body, &msg); err != nil { 47 | log.Println(err) 48 | return fmt.Sprintf("[ERROR] Unmarshaling AMQP message body %s", err), err 49 | } 50 | 51 | switch msg.Action { 52 | case models.CreateAction: 53 | pool := resources.PoolV1{ 54 | ID: msg.ID, 55 | Name: msg.Name, 56 | Tags: msg.Tags, 57 | Metadata: msg.Metadata, 58 | } 59 | pool, err := s.IPAM.CreateShowPool(pool) 60 | if err != nil { 61 | resp := fmt.Errorf("[ERROR] getting response from creating pool %s", err) 62 | log.Println(resp) 63 | return resp.Error(), resp 64 | } 65 | 66 | var response []byte 67 | response, err = json.Marshal(pool) 68 | if err != nil { 69 | resp := fmt.Errorf("[ERROR] marshalling create pool response %s", err) 70 | log.Println(resp) 71 | return resp.Error(), resp 72 | } 73 | return string(response), nil 74 | 75 | default: 76 | resp := fmt.Errorf("[ERROR] Unknown Action: %s", msg.Action) 77 | log.Println(resp) 78 | return resp.Error(), resp 79 | 80 | } 81 | } 82 | 83 | // SubnetHandler handles subnet rabbitmq request 84 | func (s *Server) SubnetHandler(d *amqp.Delivery) (string, error) { 85 | msg := models.IPAMSubnetMsg{} 86 | if err := json.Unmarshal(d.Body, &msg); err != nil { 87 | log.Println(err) 88 | return fmt.Sprintf("[ERROR] Unmarshaling AMQP message body %s", err), err 89 | } 90 | 91 | switch msg.Action { 92 | case models.CreateAction: 93 | //log.Printf("AMQP-IPAM: %+v", msg) 94 | subnet := resources.SubnetV1{ 95 | ID: msg.ID, 96 | Name: msg.Name, 97 | Tags: msg.Tags, 98 | Metadata: msg.Metadata, 99 | Pool: msg.Pool, 100 | Start: msg.Start, 101 | End: msg.End, 102 | } 103 | 104 | subnet, err := s.IPAM.CreateShowSubnet(subnet.Pool, subnet) 105 | if err != nil { 106 | resp := fmt.Errorf("[ERROR] getting response from creating subnet %s", err) 107 | log.Println(resp) 108 | return resp.Error(), resp 109 | } 110 | 111 | var response []byte 112 | response, err = json.Marshal(subnet) 113 | if err != nil { 114 | resp := fmt.Errorf("[ERROR] marshalling create subnet response %s", err) 115 | log.Println(resp) 116 | return resp.Error(), resp 117 | } 118 | return string(response), nil 119 | 120 | default: 121 | resp := fmt.Errorf("[ERROR] Unknown Action: %s", msg.Action) 122 | log.Println(resp) 123 | return resp.Error(), resp 124 | } 125 | } 126 | 127 | // ReservationHandler handles reservation rabbitmq request 128 | func (s *Server) ReservationHandler(d *amqp.Delivery) (string, error) { 129 | msg := models.IPAMReservationMsg{} 130 | if err := json.Unmarshal(d.Body, &msg); err != nil { 131 | log.Println(err) 132 | return fmt.Sprintf("[ERROR] Unmarshaling AMQP message body %s", err), err 133 | } 134 | 135 | switch msg.Action { 136 | case models.CreateAction: 137 | reservation := resources.ReservationV1{ 138 | ID: msg.ID, 139 | Name: msg.Name, 140 | Tags: msg.Tags, 141 | Metadata: msg.Metadata, 142 | Subnet: msg.Subnet, 143 | } 144 | 145 | reservation, err := s.IPAM.CreateShowReservation(reservation.Subnet, reservation) 146 | if err != nil { 147 | resp := fmt.Errorf("[ERROR] getting response from creating reservation %s", err) 148 | log.Println(resp) 149 | return resp.Error(), resp 150 | } 151 | 152 | leases, err := s.IPAM.IndexLeases(reservation.ID) 153 | if err != nil || len(leases.Leases) != 1 { 154 | resp := fmt.Errorf("[ERROR] getting lease, %s", err) 155 | log.Println(resp) 156 | return resp.Error(), resp 157 | } 158 | 159 | var response []byte 160 | response, err = json.Marshal(reservation) 161 | if err != nil { 162 | resp := fmt.Errorf("[ERROR] marshalling create reservation response %s", err) 163 | log.Println(resp) 164 | return resp.Error(), resp 165 | } 166 | return string(response), nil 167 | 168 | case models.DeleteAction: 169 | 170 | response, err := s.IPAM.DeleteReservation(msg.ID, resources.ReservationV1{}) 171 | if err != nil { 172 | resp := fmt.Errorf("[ERROR] getting response from creating reservation %s", err) 173 | log.Println(resp) 174 | return resp.Error(), resp 175 | } 176 | return response, nil 177 | 178 | default: 179 | resp := fmt.Errorf("[ERROR] Unknown Action: %s", msg.Action) 180 | log.Println(resp) 181 | return resp.Error(), resp 182 | } 183 | } 184 | 185 | // LeaseHandler handles lease rabbitmq request 186 | func (s *Server) LeaseHandler(d *amqp.Delivery) (string, error) { 187 | msg := models.IPAMLeaseMsg{} 188 | if err := json.Unmarshal(d.Body, &msg); err != nil { 189 | log.Println(err) 190 | return fmt.Sprintf("[ERROR] Unmarshaling AMQP message body %s", err), err 191 | } 192 | 193 | switch msg.Action { 194 | case models.ShowAction: 195 | leases, err := s.IPAM.IndexLeases(msg.Reservation) 196 | if err != nil || len(leases.Leases) != 1 { 197 | resp := fmt.Errorf("[ERROR] getting lease, %s", err) 198 | log.Println(resp) 199 | return resp.Error(), resp 200 | } 201 | 202 | var response []byte 203 | response, err = json.Marshal(leases.Leases[0]) 204 | if err != nil { 205 | resp := fmt.Errorf("[ERROR] marshalling lease response %s", err) 206 | log.Println(resp) 207 | return resp.Error(), resp 208 | } 209 | return string(response), nil 210 | 211 | default: 212 | resp := fmt.Errorf("[ERROR] Unknown Action: %s", msg.Action) 213 | log.Println(resp) 214 | return resp.Error(), resp 215 | } 216 | } 217 | -------------------------------------------------------------------------------- /server/handlers_test.go: -------------------------------------------------------------------------------- 1 | package server_test 2 | 3 | import ( 4 | "encoding/json" 5 | "log" 6 | "os" 7 | 8 | "github.com/RackHD/ipam/resources" 9 | "github.com/RackHD/voyager-ipam-service/server" 10 | "github.com/RackHD/voyager-utilities/models" 11 | "github.com/RackHD/voyager-utilities/random" 12 | samqp "github.com/streadway/amqp" 13 | 14 | . "github.com/onsi/ginkgo" 15 | . "github.com/onsi/gomega" 16 | ) 17 | 18 | var _ = Describe("Server test", func() { 19 | Describe("AMQP Message Handling", func() { 20 | var rabbitMQURL string 21 | var testExchange string 22 | var testExchangeType string 23 | var testQueueName string 24 | var testConsumerTag string 25 | var testMessage string 26 | var testRoutingKey string 27 | var srv *server.Server 28 | 29 | BeforeEach(func() { 30 | testExchange = models.IpamExchange 31 | testExchangeType = models.IpamExchangeType 32 | testQueueName = random.RandQueue() 33 | testConsumerTag = models.IpamConsumerTag 34 | testRoutingKey = models.IpamSendQueue 35 | 36 | rabbitMQURL = os.Getenv("RABBITMQ_URL") 37 | srv = server.NewServer(rabbitMQURL, "127.0.0.1:8000") 38 | Expect(srv.MQ).ToNot(BeNil()) 39 | }) 40 | 41 | AfterEach(func() { 42 | srv.MQ.Close() 43 | }) 44 | 45 | Context("When a message comes in to voyager-ipam-service", func() { 46 | var deliveries <-chan samqp.Delivery 47 | var replies <-chan samqp.Delivery 48 | var err error 49 | var replyTo string 50 | 51 | BeforeEach(func() { 52 | testExchange = "voyager-ipam-service" 53 | _, deliveries, err = srv.MQ.Listen(testExchange, testExchangeType, testQueueName, testRoutingKey, testConsumerTag) 54 | Expect(err).ToNot(HaveOccurred()) 55 | 56 | replyTo = random.RandQueue() 57 | _, replies, err = srv.MQ.Listen(testExchange, testExchangeType, replyTo, replyTo, testConsumerTag) 58 | Expect(err).ToNot(HaveOccurred()) 59 | 60 | }) 61 | 62 | Context("When a VALID pool type is requested", func() { 63 | It("INTEGRATION should handle a POOL message with a VALID CREATE ACTION to the 'voyager-ipam-service' exchange", func() { 64 | 65 | testMessage = `{"type":"pool","action":"create","name":"TEST"}` 66 | err = srv.MQ.Send(testExchange, testExchangeType, testRoutingKey, testMessage, "", replyTo) 67 | Expect(err).ToNot(HaveOccurred()) 68 | 69 | d := <-deliveries 70 | d.Ack(false) 71 | err = srv.ProcessMessage(&d) 72 | Expect(err).ToNot(HaveOccurred()) 73 | 74 | message := <-replies 75 | message.Ack(false) 76 | Expect(string(message.Body)).ToNot(ContainSubstring("[ERROR]")) 77 | var pool resources.PoolV1 78 | err = json.Unmarshal(message.Body, &pool) 79 | Expect(err).ToNot(HaveOccurred()) 80 | 81 | _, err = srv.IPAM.DeletePool(pool.ID, pool) 82 | Expect(err).ToNot(HaveOccurred()) 83 | 84 | }) 85 | 86 | It("INTEGRATION should handle a POOL message with an INVALID ACTION to the 'voyager-ipam-service' exchange", func() { 87 | testMessage = `{"type":"pool","action":"BAD_ACTION","name":"TEST"}` 88 | err = srv.MQ.Send(testExchange, testExchangeType, testRoutingKey, testMessage, "", replyTo) 89 | Expect(err).ToNot(HaveOccurred()) 90 | 91 | d := <-deliveries 92 | d.Ack(false) 93 | err = srv.ProcessMessage(&d) 94 | Expect(err).To(HaveOccurred()) 95 | }) 96 | }) 97 | 98 | Context("When a VALID subnet type is requested", func() { 99 | var testSubnet models.IPAMSubnetMsg 100 | var pool resources.PoolV1 101 | 102 | BeforeEach(func() { 103 | testSubnet.ObjectType = models.SubnetType 104 | testSubnet.Action = models.CreateAction 105 | testSubnet.Start = "192.168.1.10" 106 | testSubnet.End = "192.168.1.20" 107 | pool = resources.PoolV1{ 108 | Name: "POOL-FOR-SUBNET-TESTS", 109 | } 110 | pool, err = srv.IPAM.CreateShowPool(pool) 111 | Expect(err).ToNot(HaveOccurred()) 112 | testSubnet.Pool = pool.ID 113 | }) 114 | 115 | AfterEach(func() { 116 | _, err = srv.IPAM.DeletePool(pool.ID, pool) 117 | Expect(err).ToNot(HaveOccurred()) 118 | }) 119 | 120 | It("INTEGRATION should handle a SUBNET message with a VALID CREATE ACTION", func() { 121 | 122 | subnetMsg, err := json.Marshal(testSubnet) 123 | Expect(err).ToNot(HaveOccurred()) 124 | 125 | err = srv.MQ.Send(testExchange, testExchangeType, testRoutingKey, string(subnetMsg), "", replyTo) 126 | Expect(err).ToNot(HaveOccurred()) 127 | 128 | d := <-deliveries 129 | d.Ack(false) 130 | err = srv.ProcessMessage(&d) 131 | Expect(err).ToNot(HaveOccurred()) 132 | 133 | r := <-replies 134 | r.Ack(false) 135 | Expect(string(r.Body)).ToNot(ContainSubstring("[ERROR]")) 136 | 137 | }) 138 | 139 | It("INTEGRATION should handle a SUBNET message with an INVALID ACTION", func() { 140 | testSubnet.Action = "BAD_ACTION" 141 | 142 | subnetMsg, err := json.Marshal(testSubnet) 143 | Expect(err).ToNot(HaveOccurred()) 144 | 145 | err = srv.MQ.Send(testExchange, testExchangeType, testRoutingKey, string(subnetMsg), "", replyTo) 146 | Expect(err).ToNot(HaveOccurred()) 147 | 148 | d := <-deliveries 149 | d.Ack(false) 150 | err = srv.ProcessMessage(&d) 151 | Expect(err).To(HaveOccurred()) 152 | }) 153 | 154 | It("INTEGRATION should handle a SUBNET message with a VALID CREATE ACTION with INVALID POOL ID", func() { 155 | testSubnet.Pool = "INVALID_ID" 156 | 157 | subnetMsg, err := json.Marshal(testSubnet) 158 | if err != nil { 159 | log.Printf("[ERROR] %s\n", err) 160 | } 161 | err = srv.MQ.Send(testExchange, testExchangeType, testRoutingKey, string(subnetMsg), "", replyTo) 162 | Expect(err).ToNot(HaveOccurred()) 163 | 164 | d := <-deliveries 165 | d.Ack(false) 166 | err = srv.ProcessMessage(&d) 167 | Expect(err).ToNot(HaveOccurred()) 168 | r := <-replies 169 | Expect(string(r.Body)).To(ContainSubstring("[ERROR] getting response from creating subnet")) 170 | }) 171 | 172 | It("INTEGRATION should handle a SUBNET message with a VALID CREATE ACTION with INVALID IP RANGE", func() { 173 | testSubnet.Start = "123456789" 174 | testSubnet.End = "987654321" 175 | 176 | subnetMsg, err := json.Marshal(testSubnet) 177 | Expect(err).ToNot(HaveOccurred()) 178 | 179 | err = srv.MQ.Send(testExchange, testExchangeType, testRoutingKey, string(subnetMsg), "", replyTo) 180 | Expect(err).ToNot(HaveOccurred()) 181 | 182 | d := <-deliveries 183 | d.Ack(false) 184 | err = srv.ProcessMessage(&d) 185 | Expect(err).ToNot(HaveOccurred()) 186 | 187 | r := <-replies 188 | Expect(string(r.Body)).To(ContainSubstring("[ERROR] getting response from creating subnet")) 189 | 190 | }) 191 | }) 192 | 193 | Context("When a VALID reservation type is requested", func() { 194 | 195 | var testReservation models.IPAMReservationMsg 196 | var pool resources.PoolV1 197 | var subnet resources.SubnetV1 198 | 199 | BeforeEach(func() { 200 | testReservation.Name = "VALID-RESERVATION" 201 | testReservation.ObjectType = models.ReservationType 202 | testReservation.Action = models.CreateAction 203 | 204 | pool = resources.PoolV1{Name: "POOL-FOR-RESERVATION-TESTS"} 205 | pool, err = srv.IPAM.CreateShowPool(pool) 206 | Expect(err).ToNot(HaveOccurred()) 207 | 208 | subnet = resources.SubnetV1{ 209 | Name: "SUBNET-FOR-RESERVATION-TESTS", 210 | Pool: pool.ID, 211 | Start: "192.168.1.10", 212 | End: "192.168.1.10", 213 | } 214 | 215 | subnet, err = srv.IPAM.CreateShowSubnet(pool.ID, subnet) 216 | Expect(err).ToNot(HaveOccurred()) 217 | testReservation.Subnet = subnet.ID 218 | }) 219 | 220 | AfterEach(func() { 221 | 222 | _, err = srv.IPAM.DeletePool(pool.ID, pool) 223 | Expect(err).ToNot(HaveOccurred()) 224 | 225 | }) 226 | 227 | It("INTEGRATION should handle a RESERVATION message with a VALID CREATE ACTION", func() { 228 | reservationMsg, err := json.Marshal(testReservation) 229 | Expect(err).ToNot(HaveOccurred()) 230 | 231 | err = srv.MQ.Send(testExchange, testExchangeType, testRoutingKey, string(reservationMsg), "", replyTo) 232 | Expect(err).ToNot(HaveOccurred()) 233 | 234 | d := <-deliveries 235 | d.Ack(false) 236 | err = srv.ProcessMessage(&d) 237 | Expect(err).ToNot(HaveOccurred()) 238 | r := <-replies 239 | Expect(string(r.Body)).ToNot(ContainSubstring("[ERROR]")) 240 | }) 241 | 242 | It("INTEGRATION should handle a RESERVATION message with a VALID CREATE ACTION with INVALID SUBNET ID", func() { 243 | testReservation.Subnet = "INVALID_ID" 244 | reservationMsg, err := json.Marshal(testReservation) 245 | Expect(err).ToNot(HaveOccurred()) 246 | 247 | err = srv.MQ.Send(testExchange, testExchangeType, testRoutingKey, string(reservationMsg), "", replyTo) 248 | Expect(err).ToNot(HaveOccurred()) 249 | 250 | d := <-deliveries 251 | d.Ack(false) 252 | 253 | err = srv.ProcessMessage(&d) 254 | Expect(err).ToNot(HaveOccurred()) 255 | 256 | r := <-replies 257 | Expect(string(r.Body)).To(ContainSubstring("[ERROR] getting response from creating reservation")) 258 | }) 259 | 260 | It("INTEGRATION should handle a RESERVATION message with an INVALID ACTION", func() { 261 | testReservation.Action = "BAD_ACTION" 262 | reservationMsg, err := json.Marshal(testReservation) 263 | Expect(err).ToNot(HaveOccurred()) 264 | 265 | err = srv.MQ.Send(testExchange, testExchangeType, testRoutingKey, string(reservationMsg), "", replyTo) 266 | Expect(err).ToNot(HaveOccurred()) 267 | 268 | d := <-deliveries 269 | d.Ack(false) 270 | 271 | err = srv.ProcessMessage(&d) 272 | Expect(err).To(HaveOccurred()) 273 | }) 274 | 275 | It("INTEGRATION should handle a request if Subnet is full", func() { 276 | reservationMsg, err := json.Marshal(testReservation) 277 | Expect(err).ToNot(HaveOccurred()) 278 | 279 | err = srv.MQ.Send(testExchange, testExchangeType, testRoutingKey, string(reservationMsg), "", replyTo) 280 | Expect(err).ToNot(HaveOccurred()) 281 | 282 | d := <-deliveries 283 | d.Ack(false) 284 | err = srv.ProcessMessage(&d) 285 | Expect(err).ToNot(HaveOccurred()) 286 | r := <-replies 287 | Expect(string(r.Body)).ToNot(ContainSubstring("[ERROR]")) 288 | 289 | // Create a second reservation, expect it to fail 290 | testReservation.Name = "very coooooooooooooool" 291 | reservationMsg, err = json.Marshal(testReservation) 292 | Expect(err).ToNot(HaveOccurred()) 293 | 294 | err = srv.MQ.Send(testExchange, testExchangeType, testRoutingKey, string(reservationMsg), "", replyTo) 295 | Expect(err).ToNot(HaveOccurred()) 296 | 297 | d = <-deliveries 298 | d.Ack(false) 299 | err = srv.ProcessMessage(&d) 300 | Expect(err).ToNot(HaveOccurred()) 301 | r = <-replies 302 | Expect(string(r.Body)).To(ContainSubstring("[ERROR] getting response from creating reservation")) 303 | }) 304 | 305 | }) 306 | 307 | Context("When an INVALID type is requested", func() { 308 | It("INTEGRATION should returns error", func() { 309 | testMessage = `{"type":"BAD_TYPE","action":"create","name": "TEST"}` 310 | 311 | err = srv.MQ.Send(testExchange, testExchangeType, testRoutingKey, testMessage, "", replyTo) 312 | Expect(err).ToNot(HaveOccurred()) 313 | 314 | d := <-deliveries 315 | d.Ack(false) 316 | 317 | err = srv.ProcessMessage(&d) 318 | Expect(err).ToNot(HaveOccurred()) 319 | 320 | r := <-replies 321 | Expect(string(r.Body)).To(ContainSubstring("[ERROR] Unknown ObjectType")) 322 | 323 | }) 324 | }) 325 | 326 | }) 327 | }) 328 | }) 329 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | --------------------------------------------------------------------------------