4. Add a service to an application using the Cloud Foundry cli

In this section, you learn how to find a service from the catalog by using the command line and then bind the service to the sample application. This application has code to recognize the bound service and extend its functionality through the API that is added with the service.

    1. Use the bx service offerings command to list all available services for your Bluemix region, ID, and organization privileges. This command requires some patience as it takes quite a while to complete. You can redirect the output to a file or use a pager like more (or less) to make the output easier to read.

For this application, you will add the Watson Language Translator service. To add the service, you need to know the service name in the catalog and the available plans to use. For this deployment, use the standard plan.

    1. Add this service to your current space:
      bx service create language_translator lite "Language Translator-x1"
    2. Verify the service and also get a list of other services in the current space by using the bx service list command.
    3. Bind this service to your application:

bx service bind <your-app-name> "Language Translator-x1"
After binding the application, you are reminded to restage your application. Restaging is needed to add the API credentials for the service to the application runtime environment and restart the application with them.

    1. Restage the application:

bx app restage  <your-app-name>

    1. When the command completes, go back to your application again and enter a sentence and click Send.
      The application displays the language to the left of the message. Try a sentence in another language. For example, you can enter “Aller anfang ist schwer.” which is a German saying similar to “all beginnings are hard.”

Deeper dive

You can learn how this application is exploiting the Watson Language Translator service to identify languages by examining the routes/index.js file. When a service is bound to a Cloud Foundry application, an entry for the service is added to the VCAP_SERVICES environment variable.

The application uses the cfenv() module to create an object with all of the entries from VCAP_SERVICES. If a service with a name matching “Language Translator*” is bound to the application, the application code creates an object to use in accessing the language translator API services. One of these services will analyze the text and return the language of the given text.

In addition to services from the Bluemix catalog, you can also define external API credentials by using the bx service user-provided-create command.

    1. Check the routes for this application with the bx app routes command.

Cloud Foundry applications are accessed through routes that establish a mapping between a web URL or API endpoint and a specific application running in Bluemix.

    1. Add another route to the application by using the bx app route-map or the bx network route-map commands.

For example, you can add the prefix smarter (because it can now recognize a language) to your application:
bx network route-map <your-app-name> mybluemix.net –n smarter-<your-app-name>
After running this command, both routes will now work for the application.

    1. When you are finished with the application and are ready to clean up, stop the application:

bx app stop <your-app-name>

    1. Delete the application from Bluemix and remove all routes associated with it:

bx app delete <your-app-name> -r

    1. Delete the service:

bx service delete "Language Translator-x1"

You have explored managing your account and applications in Bluemix by using the Cloud Foundry command-line interface. This lab focused on the more commonly used commands, but there are many more that can be useful.

Entering only the bx command on the command line will produce a list of the available commands. To get more information on a specific command including the required arguments and options, you can enter bx help <command-name> in the terminal.

For more details on the Bluemix CLI commands, see the Bluemix documentation.