{"id":21152,"date":"2017-02-23T04:53:28","date_gmt":"2017-02-23T01:53:28","guid":{"rendered":"https:\/\/www.altoros.com\/blog\/?p=21152"},"modified":"2018-05-15T15:37:51","modified_gmt":"2018-05-15T12:37:51","slug":"creating-a-sample-service-broker-for-cloud-foundry-with-pythons-flask","status":"publish","type":"post","link":"https:\/\/www.altoros.com\/blog\/creating-a-sample-service-broker-for-cloud-foundry-with-pythons-flask\/","title":{"rendered":"Creating a Sample Service Broker for Cloud Foundry with Python&#8217;s Flask"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.altoros.com\/blog\/creating-a-sample-service-broker-for-cloud-foundry-with-pythons-flask\/#Two_ways_of_provisioning_services\" >Two ways of provisioning services<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.altoros.com\/blog\/creating-a-sample-service-broker-for-cloud-foundry-with-pythons-flask\/#Whats_needed_to_create\" >What&#8217;s needed to create?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.altoros.com\/blog\/creating-a-sample-service-broker-for-cloud-foundry-with-pythons-flask\/#Prerequisites\" >Prerequisites<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.altoros.com\/blog\/creating-a-sample-service-broker-for-cloud-foundry-with-pythons-flask\/#Defining_the_service_and_the_service_plan\" >Defining the service and the service plan<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.altoros.com\/blog\/creating-a-sample-service-broker-for-cloud-foundry-with-pythons-flask\/#Defining_the_service_catalog\" >Defining the service catalog<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.altoros.com\/blog\/creating-a-sample-service-broker-for-cloud-foundry-with-pythons-flask\/#Running_the_service_broker_app_on_CF\" >Running the service broker app on CF<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.altoros.com\/blog\/creating-a-sample-service-broker-for-cloud-foundry-with-pythons-flask\/#Registering_the_app_as_a_service_broker\" >Registering the app as a service broker<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.altoros.com\/blog\/creating-a-sample-service-broker-for-cloud-foundry-with-pythons-flask\/#Registering_service_instances\" >Registering service instances<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.altoros.com\/blog\/creating-a-sample-service-broker-for-cloud-foundry-with-pythons-flask\/#Adding_API_version_check\" >Adding API version check<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.altoros.com\/blog\/creating-a-sample-service-broker-for-cloud-foundry-with-pythons-flask\/#Adding_authentication\" >Adding authentication<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.altoros.com\/blog\/creating-a-sample-service-broker-for-cloud-foundry-with-pythons-flask\/#Related_reading\" >Related reading<\/a><\/li><\/ul><\/nav><\/div>\n<h3><span class=\"ez-toc-section\" id=\"Two_ways_of_provisioning_services\"><\/span>Two ways of provisioning services<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The Cloud Foundry PaaS sets the scene for not just running cloud-native apps, but also for provisioning services these apps depend on. Services like databases, storage, or queues can be provisioned to applications on demand. So, whenever an app requires access to a service, there are two options:<\/p>\n<ol>\n<li>Connect it to a user-provided service instance<\/li>\n<li>Connect it to a managed service via the marketplace<\/li>\n<\/ol>\n<p><strong>User-provided<\/strong> service instances enable developers to use services that are not available in the marketplace with their applications running on Cloud Foundry. <strong>Managed services<\/strong>, in their turn, are integrated with Cloud Foundry via APIs, provisioning reserved resources and credentials to end users on demand.<\/p>\n<p>There are quite a number of service brokers for well-known services available in the marketplace, including MySQL, MongoDB, or Redis. However, what do you do when there is no service broker for the service you need? You can create it.<\/p>\n<p>&nbsp;<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Whats_needed_to_create\"><\/span>What&#8217;s needed to create?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>This diagram demonstrates the process of creating a service broker:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"623\" height=\"268\" src=\"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2017\/02\/custom-service-broker-for-cloud-foundry-v1.png\" alt=\"custom-service-broker-for-cloud-foundry-v1\" class=\"aligncenter size-full wp-image-21153\" \/><\/p>\n<p>In the <a href=\"https:\/\/github.com\/openservicebrokerapi\/servicebroker\/blob\/v2.12\/spec.md\" target=\"_blank\">Service Broker API documentation<\/a>, a service broker is defined as <em>\u201cthe component of the service that implements the Service Broker API, for which a platform\u2019s marketplace is a client. Service brokers are responsible for advertising a catalog of service offerings and service plans to the marketplace, and acting on requests from the marketplace for provisioning, binding, unbinding, and deprovisioning.\u201d<\/em><\/p>\n<p><em>\u201cThe Service Broker API defines an HTTP interface between the services marketplace of a platform and service brokers.\u201d<\/em> So, what we need to create is an HTTP service.<\/p>\n<p>As an example, we are going to create a service broker app using Python\u2019s <a href=\"https:\/\/flask.palletsprojects.com\/en\/2.0.x\/\" target=\"_blank\">Flask framework<\/a>, which is easy to read and translate to the preferable programming language.<\/p>\n<p>&nbsp;<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Prerequisites\"><\/span>Prerequisites<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>To create a managed service for Cloud Foundry:<\/p>\n<ul>\n<li>We\u2019ll need a Cloud Foundry instance to test the service broker.<\/li>\n<li>We must have admin access to this Cloud Foundry instance to manage service brokers and the services marketplace catalog.<\/li>\n<\/ul>\n<p>In this post, we target Service Broker API v2.11.<\/p>\n<p>&nbsp;<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Defining_the_service_and_the_service_plan\"><\/span>Defining the service and the service plan<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The first thing to do is define the service and its service plan in terms of code. So, we need to advertise at least one service with at least one service plan. In the simplest case, it looks like this:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">plan_one = {\r\n    &quot;id&quot;: &quot;plan_one&quot;,\r\n    &quot;name&quot;: &quot;plan_one&quot;,\r\n    &quot;description&quot;: &quot;Simple free plan&quot;,\r\n    &quot;free&quot;: True\r\n}\r\n\r\nmy_service = {\r\n    'id': 'example_service',\r\n    'name': 'example_service',\r\n    'description': 'Simple service example',\r\n    'bindable': True,\r\n    'plans': &#x5B;plan_one]\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Defining_the_service_catalog\"><\/span>Defining the service catalog<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Defining the first endpoint\u2014the service catalog\u2014in our case looks like this:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">my_services = {&quot;services&quot;: &#x5B;my_service]}\r\n\r\n@app.route('\/v2\/catalog')\r\ndef catalog():\r\n    return jsonify(my_services)<\/pre>\n<p>The final code of the service broker app:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">import os\r\nfrom flask import Flask, jsonify\r\n\r\napp = Flask(__name__)\r\n\r\nlog = app.logger\r\n\r\n# Service plans\r\nplan_one = {\r\n    &quot;id&quot;: &quot;plan_one&quot;,\r\n    &quot;name&quot;: &quot;plan_one&quot;,\r\n    &quot;description&quot;: &quot;Simple free plan&quot;,\r\n    &quot;free&quot;: True\r\n}\r\n\r\n# Services\r\nmy_service = {\r\n    'id': 'example_service',\r\n    'name': 'example_service',\r\n    'description': 'Simple service example',\r\n    'bindable': True,\r\n    'plans': &#x5B;plan_one]\r\n}\r\n\r\nmy_services = {&quot;services&quot;: &#x5B;my_service]}\r\n\r\n@app.route('\/v2\/catalog')\r\ndef catalog():\r\n    return jsonify(my_services)\r\n\r\nif __name__ == &quot;__main__&quot;:\r\n    app.run(host='0.0.0.0', port=int(os.getenv('VCAP_APP_PORT', '5000')))<\/pre>\n<p>That\u2019s it! The code is ready to run and advertise our example service (<code style=\"color: #222222; background-color: #e6e6e6; padding: 1px 2px;\">example_service<\/code>) with one service plan (<code style=\"color: #222222; background-color: #e6e6e6; padding: 1px 2px;\">plan_one<\/code>). It can\u2019t perform anything yet, but it is a bare minimum of the code that can be accepted by Cloud Foundry.<\/p>\n<p>&nbsp;<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Running_the_service_broker_app_on_CF\"><\/span>Running the service broker app on CF<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>In this example, the code is run on Cloud Foundry, which is, nevertheless, not imperative. In fact, the service broker is just a component of the service. We run the service broker app on Cloud Foundry by either typing the code, or checking it out from <a href=\"https:\/\/github.com\/Altoros\/simple-service-broker-for-cf\" target=\"_blank\">our GitHub<\/a>:<\/p>\n<pre style=\"padding-left: 30px;\">git clone https:\/\/github.com\/Altoros\/simple-service-broker-for-cf.git\r\ncd simple-service-broker-for-cf\r\ngit checkout first\r\ncf push<\/pre>\n<p>&nbsp;<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Registering_the_app_as_a_service_broker\"><\/span>Registering the app as a service broker<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Now, we have to register the application as a service broker with Cloud Foundry. Let\u2019s check what service brokers we already have available. In our case, there is none:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"638\" height=\"313\" src=\"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2017\/02\/cloud-foundry-service-broker-not-found-create-one.png\" alt=\"cloud-foundry-service-broker-not-found-create-one\" class=\"aligncenter size-full wp-image-21158\" \/><\/p>\n<p>Let\u2019s create a Cloud Foundry service broker:<\/p>\n<p><a href=\"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2017\/02\/creating-a-simple-service-broker-for-cloud-foundry_v2.png\"><img decoding=\"async\" src=\"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2017\/02\/creating-a-simple-service-broker-for-cloud-foundry_v2.png\" alt=\"creating-a-simple-service-broker-for-cloud-foundry_v2\" width=\"640\" class=\"aligncenter size-full wp-image-21183\" \/><\/a><\/p>\n<p>Note that we are using BOSH Lite for this example. If you are using a full-scale Cloud Foundry deployment, your application domain may differ.<\/p>\n<p>Let\u2019s check what we\u2019ve got now:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"554\" height=\"312\" src=\"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2017\/02\/registering-an-app-as-a-cloud-foundry-service-broker-bosh-lite_v2.png\" alt=\"registering-an-app-as-a-cloud-foundry-service-broker-bosh-lite_v2\" class=\"aligncenter size-full wp-image-21184\" \/><\/p>\n<p>The next step is to make the service accessible by org\/space:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"645\" height=\"311\" src=\"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2017\/02\/registering-the-app-as-a-cloud-foundry-service-broker-make-it-accessible-by-org-space.png\" alt=\"registering-the-app-as-a-cloud-foundry-service-broker-make-it-accessible-by-org-space\" class=\"aligncenter size-full wp-image-21161\" \/><\/p>\n<p>&nbsp;<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Registering_service_instances\"><\/span>Registering service instances<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>If we now try registering a service instance within that service, we will fail:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"643\" height=\"313\" src=\"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2017\/02\/simple-service-broker-for-cloud-foundry_v2.png\" alt=\"simple-service-broker-for-cloud-foundry_v2\" class=\"aligncenter size-full wp-image-21186\" \/><\/p>\n<p>It happens because we haven\u2019t provided support for this action in the code yet. So, let\u2019s extend the code to support instances:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">@app.route('\/v2\/service_instances\/&lt;instance_id&gt;', methods=&#x5B;'PUT', 'DELETE',\r\n           'PATCH'])\r\ndef service_instances(instance_id):\r\n    if request.method == 'PUT':\r\n        return make_response(jsonify({}), 201)\r\n    else:\r\n        return jsonify({})<\/pre>\n<p>Adding this endpoint allows us to define the actual logic for creating, updating, and deleting service instances. We can type this code, or check it out from Git:<\/p>\n<pre style=\"padding-left: 30px;\">git checkout service<\/pre>\n<p>Having added this endpoint, we now need to restart (and, since running on Cloud Foundry, restage) the instance of the service broker:<\/p>\n<pre style=\"padding-left: 30px;\">cf push<\/pre>\n<p>At this point, we are ready to create an instance of the service (actually, nothing is going to happen in our case, but the broker will respond with the 201 status code, making Cloud Foundry believe that the broker has successfully created an instance of the service).<\/p>\n<p>Let\u2019s create an instance of <code style=\"color: #222222; background-color: #e6e6e6; padding: 1px 2px;\">example_service<\/code> with the service plan <code style=\"color: #222222; background-color: #e6e6e6; padding: 1px 2px;\">plan_one<\/code>, where <code style=\"color: #222222; background-color: #e6e6e6; padding: 1px 2px;\">si1<\/code> is the name of the instance:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"643\" height=\"312\" src=\"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2017\/02\/create-an-instance-of-example-service-si1.png\" alt=\"create-an-instance-of-example-service-si1\" class=\"aligncenter size-full wp-image-21163\" \/><\/p>\n<p>&nbsp;<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Adding_API_version_check\"><\/span>Adding API version check<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>According to the <a href=\"https:\/\/github.com\/openservicebrokerapi\/servicebroker\/blob\/v2.12\/spec.md\" target=\"_blank\">Service Broker API documentation<\/a>, <em>\u201crequests from the platform to the service broker <strong>must contain a header<\/strong> that declares the version number of the Service Broker API that the marketplace will use X-Broker-Api-Version: 2.11.\u201d<\/em><\/p>\n<p>So, let\u2019s enable the broker to check the API version number.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">X_BROKER_API_MAJOR_VERSION = 2\r\nX_BROKER_API_MINOR_VERSION = 10\r\nX_BROKER_API_VERSION_NAME = 'X-Broker-Api-Version'\r\n\r\ndef api_version_is_valid(api_version):\r\n    version_data = api_version.split('.')\r\n    result = True\r\n    if (float(version_data&#x5B;0]) &lt; X_BROKER_API_MAJOR_VERSION or\r\n       (float(version_data&#x5B;0]) == X_BROKER_API_MAJOR_VERSION and\r\n       float(version_data&#x5B;1]) &lt; X_BROKER_API_MINOR_VERSION)):\r\n                result = False\r\n    return result\r\n\r\ndef requires_api_version(f):\r\n    @wraps(f)\r\n    def decorated(*args, **kwargs):\r\n        api_version = request.headers.get('X-Broker-Api-Version')\r\n        if (not api_version or not (api_version_is_valid(api_version))):\r\n            abort(412)\r\n        return f(*args, **kwargs)\r\n    return decorated\r\n\r\n@app.errorhandler(412)\r\ndef version_mismatch(error):\r\n    return 'Version mismatch. Expected: {}: {}.{}'.format(\r\n        X_BROKER_API_VERSION_NAME,\r\n        X_BROKER_API_MAJOR_VERSION,\r\n        X_BROKER_API_MINOR_VERSION), 412<\/pre>\n<p>Now, we can style the endpoints:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">@app.route('\/v2\/catalog')\r\n@requires_api_version\r\ndef catalog():\r\n....<\/pre>\n<p>The code can either be typed or checked out from Git:<\/p>\n<pre style=\"padding-left: 30px;\">git checkout api\r\ncf push<\/pre>\n<p>&nbsp;<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Adding_authentication\"><\/span>Adding authentication<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>According to the <a href=\"https:\/\/github.com\/openservicebrokerapi\/servicebroker\/blob\/v2.12\/spec.md\" target=\"_blank\">Service Broker API documentation<\/a>, <em>\u201cthe marketplace <strong>must authenticate<\/strong> with the service broker using HTTP basic authentication (the Authorization: header) on every request. The broker is responsible for validating the username and password and returning a 401 Unauthorized message if credentials are invalid. It is recommended that brokers support secure communication from platform marketplaces over TLS.\u201d<\/em><\/p>\n<p>So, let\u2019s make our broker a little more secure:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">def check_auth(username, password):\r\n    &quot;&quot;&quot;This function is called to check if a username \/\r\n    password combination is valid.\r\n    &quot;&quot;&quot;\r\n    if not (username == 'alex' and password == 'bigsecret'):\r\n        log.warning('Authentication failed')\r\n    return username == 'alex' and password == 'bigsecret'\r\n\r\ndef authenticate():\r\n    &quot;&quot;&quot;Sends a 401 response that enables basic auth&quot;&quot;&quot;\r\n    return Response('Could not verify your access level for that URL.\\n'\r\n                    'You have to login with proper credentials', 401,\r\n                    {'WWW-Authenticate': 'Basic realm=&quot;Login Required&quot;'})\r\n\r\ndef requires_auth(f):\r\n    &quot;&quot;&quot;Cloud Controller (final release v145+) authenticates with the Broker\r\n    using HTTP basic authentication (the Authorization: header) on every\r\n    request and will reject any broker registrations that do not contain a\r\n    username and password. The broker is responsible for checking the username\r\n    and password and returning a 401 Unauthorized message if credentials are\r\n    invalid.\r\n    Cloud Controller supports connecting to a broker using SSL if additional\r\n    security is desired.&quot;&quot;&quot;\r\n    @wraps(f)\r\n    def decorated(*args, **kwargs):\r\n        auth = request.authorization\r\n        if not auth or not check_auth(auth.username, auth.password):\r\n            return authenticate()\r\n        return f(*args, **kwargs)\r\n    return decorated<\/pre>\n<p>Now, we can style the endpoints:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">@app.route('\/v2\/catalog')\r\n@requires_auth\r\n@requires_api_version\r\ndef catalog():\r\n....<\/pre>\n<p>We can either type this code, or check it out from Git:<\/p>\n<pre style=\"padding-left: 30px;\">git checkout auth\r\ncf push<\/pre>\n<p>To make sure it works as expected, we\u2019ll delete all service instances and the service broker itself:<\/p>\n<pre style=\"padding-left: 30px;\">cf delete-service si1\r\ncf disable-service-access example_service\r\ncf delete-service-broker sb1<\/pre>\n<p>Now, if we try registering the service broker the way we did before, we will fail:<\/p>\n<p><a href=\"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2017\/02\/registering-service-instances-failed_v2.png\"><img decoding=\"async\" src=\"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2017\/02\/registering-service-instances-failed_v2.png\" alt=\"registering-service-instances-failed_v2\" width=\"640\" class=\"aligncenter size-full wp-image-21185\" \/><\/a><\/p>\n<p>Registration fails because the credentials for this service broker have been changed. Let\u2019s try using new credentials:<\/p>\n<p><a href=\"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2017\/02\/adding-authentication-for-a-cloud-foundry-service-broker-v12.png\"><img decoding=\"async\" src=\"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2017\/02\/adding-authentication-for-a-cloud-foundry-service-broker-v12.png\" alt=\"adding-authentication-for-a-cloud-foundry-service-broker-v12\" width=\"640\" class=\"aligncenter size-full wp-image-21182\" \/><\/a><\/p>\n<p>This is quite a simple example of creating a service broker. However, it is good enough to get an idea of how it works. Now, you can move on adding more functionality.<\/p>\n<p>You can access the source code of the developed service broker <a href=\"https:\/\/github.com\/Altoros\/simple-service-broker-for-cf\" target=\"_blank\">here<\/a>.<\/p>\n<p>&nbsp;<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Related_reading\"><\/span>Related reading<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li><a href=\"https:\/\/www.altoros.com\/blog\/building-a-cloud-foundry-service-broker-for-hp-idol\/\">Introducing a Cloud Foundry Service Broker for HP IDOL<\/a><\/li>\n<li><a href=\"https:\/\/www.altoros.com\/blog\/creating-a-custom-cloud-foundry-buildpack-from-scratch-whats-under-the-hood\/\">Creating a Custom Cloud Foundry Buildpack from Scratch: What\u2019s Under the Hood<\/a><\/li>\n<\/ul>\n<hr\/>\n<p><center><\/p>\n<p><small>This blog post was written by <a href=\"https:\/\/www.altoros.com\/blog\/author\/alex-zakharov\/\">Alexey Zakharov<\/a> and <a href=\"https:\/\/www.altoros.com\/blog\/author\/alex-prismakov\/\">Aliaksandr Prysmakou<\/a>.<\/small><\/p>\n<p><\/center><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Two ways of provisioning services<\/p>\n<p>The Cloud Foundry PaaS sets the scene for not just running cloud-native apps, but also for provisioning services these apps depend on. Services like databases, storage, or queues can be provisioned to applications on demand. So, whenever an app requires access to a service, there are [&#8230;]<\/p>\n","protected":false},"author":40,"featured_media":21190,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"content-type":"","footnotes":"","_links_to":"","_links_to_target":""},"categories":[214],"tags":[873,206],"class_list":["post-21152","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-cloud-native","tag-oss-cloud-foundry"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Creating a Sample Service Broker for Cloud Foundry with Python&#039;s Flask | Altoros<\/title>\n<meta name=\"description\" content=\"Follow the instructions to build a custom service broker using the Flask framework and run it on Cloud Foundry. Learn how to define a service catalog, register instances, add API version check and authentication, etc.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.altoros.com\/blog\/creating-a-sample-service-broker-for-cloud-foundry-with-pythons-flask\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Creating a Sample Service Broker for Cloud Foundry with Python&#039;s Flask | Altoros\" \/>\n<meta property=\"og:description\" content=\"Two ways of provisioning services The Cloud Foundry PaaS sets the scene for not just running cloud-native apps, but also for provisioning services these apps depend on. Services like databases, storage, or queues can be provisioned to applications on demand. So, whenever an app requires access to a service, there are [...]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.altoros.com\/blog\/creating-a-sample-service-broker-for-cloud-foundry-with-pythons-flask\/\" \/>\n<meta property=\"og:site_name\" content=\"Altoros\" \/>\n<meta property=\"article:published_time\" content=\"2017-02-23T01:53:28+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-05-15T12:37:51+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2017\/02\/simple-service-broker-for-cloud-foundry-v13.gif\" \/>\n\t<meta property=\"og:image:width\" content=\"640\" \/>\n\t<meta property=\"og:image:height\" content=\"312\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/gif\" \/>\n<meta name=\"author\" content=\"Alexey Zakharov\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Alexey Zakharov\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/creating-a-sample-service-broker-for-cloud-foundry-with-pythons-flask\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/creating-a-sample-service-broker-for-cloud-foundry-with-pythons-flask\\\/\"},\"author\":{\"name\":\"Alexey Zakharov\",\"@id\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/#\\\/schema\\\/person\\\/673368fe7e3d4fe7e27f311625fa6ca3\"},\"headline\":\"Creating a Sample Service Broker for Cloud Foundry with Python&#8217;s Flask\",\"datePublished\":\"2017-02-23T01:53:28+00:00\",\"dateModified\":\"2018-05-15T12:37:51+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/creating-a-sample-service-broker-for-cloud-foundry-with-pythons-flask\\\/\"},\"wordCount\":1601,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/creating-a-sample-service-broker-for-cloud-foundry-with-pythons-flask\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/wp-content\\\/uploads\\\/2017\\\/02\\\/simple-service-broker-for-cloud-foundry-v13.gif\",\"keywords\":[\"Cloud-Native\",\"OSS Cloud Foundry\"],\"articleSection\":[\"Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.altoros.com\\\/blog\\\/creating-a-sample-service-broker-for-cloud-foundry-with-pythons-flask\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/creating-a-sample-service-broker-for-cloud-foundry-with-pythons-flask\\\/\",\"url\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/creating-a-sample-service-broker-for-cloud-foundry-with-pythons-flask\\\/\",\"name\":\"Creating a Sample Service Broker for Cloud Foundry with Python's Flask | Altoros\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/creating-a-sample-service-broker-for-cloud-foundry-with-pythons-flask\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/creating-a-sample-service-broker-for-cloud-foundry-with-pythons-flask\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/wp-content\\\/uploads\\\/2017\\\/02\\\/simple-service-broker-for-cloud-foundry-v13.gif\",\"datePublished\":\"2017-02-23T01:53:28+00:00\",\"dateModified\":\"2018-05-15T12:37:51+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/#\\\/schema\\\/person\\\/673368fe7e3d4fe7e27f311625fa6ca3\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/creating-a-sample-service-broker-for-cloud-foundry-with-pythons-flask\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.altoros.com\\\/blog\\\/creating-a-sample-service-broker-for-cloud-foundry-with-pythons-flask\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/creating-a-sample-service-broker-for-cloud-foundry-with-pythons-flask\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/wp-content\\\/uploads\\\/2017\\\/02\\\/simple-service-broker-for-cloud-foundry-v13.gif\",\"contentUrl\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/wp-content\\\/uploads\\\/2017\\\/02\\\/simple-service-broker-for-cloud-foundry-v13.gif\",\"width\":640,\"height\":312},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/creating-a-sample-service-broker-for-cloud-foundry-with-pythons-flask\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Creating a Sample Service Broker for Cloud Foundry with Python&#8217;s Flask\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/\",\"name\":\"Altoros\",\"description\":\"Insight\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/#\\\/schema\\\/person\\\/673368fe7e3d4fe7e27f311625fa6ca3\",\"name\":\"Alexey Zakharov\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/wp-content\\\/uploads\\\/2017\\\/02\\\/alexei-zaharov-v1-150x150.jpg\",\"url\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/wp-content\\\/uploads\\\/2017\\\/02\\\/alexei-zaharov-v1-150x150.jpg\",\"contentUrl\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/wp-content\\\/uploads\\\/2017\\\/02\\\/alexei-zaharov-v1-150x150.jpg\",\"caption\":\"Alexey Zakharov\"},\"description\":\"Alexey Zakharov is a Cloud Foundry\\\/DevOps Engineer at Altoros. His main focus areas are cloud automation, virtualization, and training DevOps engineers in all things cloud. As a part of the Cloud Foundry team at Altoros, Alexey has built multiple CF environments. He is also a contributor to and big fan of Logsearch, a solution that can aggregate all CF logs together.\",\"url\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/author\\\/alex-zakharov\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Creating a Sample Service Broker for Cloud Foundry with Python's Flask | Altoros","description":"Follow the instructions to build a custom service broker using the Flask framework and run it on Cloud Foundry. Learn how to define a service catalog, register instances, add API version check and authentication, etc.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.altoros.com\/blog\/creating-a-sample-service-broker-for-cloud-foundry-with-pythons-flask\/","og_locale":"en_US","og_type":"article","og_title":"Creating a Sample Service Broker for Cloud Foundry with Python's Flask | Altoros","og_description":"Two ways of provisioning services The Cloud Foundry PaaS sets the scene for not just running cloud-native apps, but also for provisioning services these apps depend on. Services like databases, storage, or queues can be provisioned to applications on demand. So, whenever an app requires access to a service, there are [...]","og_url":"https:\/\/www.altoros.com\/blog\/creating-a-sample-service-broker-for-cloud-foundry-with-pythons-flask\/","og_site_name":"Altoros","article_published_time":"2017-02-23T01:53:28+00:00","article_modified_time":"2018-05-15T12:37:51+00:00","og_image":[{"width":640,"height":312,"url":"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2017\/02\/simple-service-broker-for-cloud-foundry-v13.gif","type":"image\/gif"}],"author":"Alexey Zakharov","twitter_misc":{"Written by":"Alexey Zakharov","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.altoros.com\/blog\/creating-a-sample-service-broker-for-cloud-foundry-with-pythons-flask\/#article","isPartOf":{"@id":"https:\/\/www.altoros.com\/blog\/creating-a-sample-service-broker-for-cloud-foundry-with-pythons-flask\/"},"author":{"name":"Alexey Zakharov","@id":"https:\/\/www.altoros.com\/blog\/#\/schema\/person\/673368fe7e3d4fe7e27f311625fa6ca3"},"headline":"Creating a Sample Service Broker for Cloud Foundry with Python&#8217;s Flask","datePublished":"2017-02-23T01:53:28+00:00","dateModified":"2018-05-15T12:37:51+00:00","mainEntityOfPage":{"@id":"https:\/\/www.altoros.com\/blog\/creating-a-sample-service-broker-for-cloud-foundry-with-pythons-flask\/"},"wordCount":1601,"commentCount":0,"image":{"@id":"https:\/\/www.altoros.com\/blog\/creating-a-sample-service-broker-for-cloud-foundry-with-pythons-flask\/#primaryimage"},"thumbnailUrl":"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2017\/02\/simple-service-broker-for-cloud-foundry-v13.gif","keywords":["Cloud-Native","OSS Cloud Foundry"],"articleSection":["Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.altoros.com\/blog\/creating-a-sample-service-broker-for-cloud-foundry-with-pythons-flask\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.altoros.com\/blog\/creating-a-sample-service-broker-for-cloud-foundry-with-pythons-flask\/","url":"https:\/\/www.altoros.com\/blog\/creating-a-sample-service-broker-for-cloud-foundry-with-pythons-flask\/","name":"Creating a Sample Service Broker for Cloud Foundry with Python's Flask | Altoros","isPartOf":{"@id":"https:\/\/www.altoros.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.altoros.com\/blog\/creating-a-sample-service-broker-for-cloud-foundry-with-pythons-flask\/#primaryimage"},"image":{"@id":"https:\/\/www.altoros.com\/blog\/creating-a-sample-service-broker-for-cloud-foundry-with-pythons-flask\/#primaryimage"},"thumbnailUrl":"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2017\/02\/simple-service-broker-for-cloud-foundry-v13.gif","datePublished":"2017-02-23T01:53:28+00:00","dateModified":"2018-05-15T12:37:51+00:00","author":{"@id":"https:\/\/www.altoros.com\/blog\/#\/schema\/person\/673368fe7e3d4fe7e27f311625fa6ca3"},"breadcrumb":{"@id":"https:\/\/www.altoros.com\/blog\/creating-a-sample-service-broker-for-cloud-foundry-with-pythons-flask\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.altoros.com\/blog\/creating-a-sample-service-broker-for-cloud-foundry-with-pythons-flask\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.altoros.com\/blog\/creating-a-sample-service-broker-for-cloud-foundry-with-pythons-flask\/#primaryimage","url":"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2017\/02\/simple-service-broker-for-cloud-foundry-v13.gif","contentUrl":"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2017\/02\/simple-service-broker-for-cloud-foundry-v13.gif","width":640,"height":312},{"@type":"BreadcrumbList","@id":"https:\/\/www.altoros.com\/blog\/creating-a-sample-service-broker-for-cloud-foundry-with-pythons-flask\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.altoros.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Creating a Sample Service Broker for Cloud Foundry with Python&#8217;s Flask"}]},{"@type":"WebSite","@id":"https:\/\/www.altoros.com\/blog\/#website","url":"https:\/\/www.altoros.com\/blog\/","name":"Altoros","description":"Insight","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.altoros.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.altoros.com\/blog\/#\/schema\/person\/673368fe7e3d4fe7e27f311625fa6ca3","name":"Alexey Zakharov","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2017\/02\/alexei-zaharov-v1-150x150.jpg","url":"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2017\/02\/alexei-zaharov-v1-150x150.jpg","contentUrl":"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2017\/02\/alexei-zaharov-v1-150x150.jpg","caption":"Alexey Zakharov"},"description":"Alexey Zakharov is a Cloud Foundry\/DevOps Engineer at Altoros. His main focus areas are cloud automation, virtualization, and training DevOps engineers in all things cloud. As a part of the Cloud Foundry team at Altoros, Alexey has built multiple CF environments. He is also a contributor to and big fan of Logsearch, a solution that can aggregate all CF logs together.","url":"https:\/\/www.altoros.com\/blog\/author\/alex-zakharov\/"}]}},"_links":{"self":[{"href":"https:\/\/www.altoros.com\/blog\/wp-json\/wp\/v2\/posts\/21152","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.altoros.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.altoros.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.altoros.com\/blog\/wp-json\/wp\/v2\/users\/40"}],"replies":[{"embeddable":true,"href":"https:\/\/www.altoros.com\/blog\/wp-json\/wp\/v2\/comments?post=21152"}],"version-history":[{"count":25,"href":"https:\/\/www.altoros.com\/blog\/wp-json\/wp\/v2\/posts\/21152\/revisions"}],"predecessor-version":[{"id":33241,"href":"https:\/\/www.altoros.com\/blog\/wp-json\/wp\/v2\/posts\/21152\/revisions\/33241"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.altoros.com\/blog\/wp-json\/wp\/v2\/media\/21190"}],"wp:attachment":[{"href":"https:\/\/www.altoros.com\/blog\/wp-json\/wp\/v2\/media?parent=21152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.altoros.com\/blog\/wp-json\/wp\/v2\/categories?post=21152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.altoros.com\/blog\/wp-json\/wp\/v2\/tags?post=21152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}