Jump to: navigation, search

Poppy/Provider - Getting Started/Mimic Driver

< Poppy‎ | Provider - Getting Started
Revision as of 15:51, 19 February 2015 by Malini (talk | contribs) (Bringing it all together)

Mimic Driver

Mimic is an API-compatible mock service for Openstack Compute. It is backed by in-memory data structure rather than a potentially expensive database.

Mimic helps with:

  • fast set-up
  • instant response
  • cost efficient
  • enables offline development
  • enables ability to test unusual behaviors/errors of an api
  • acts as a central repository for mocked responses from services


Fork the Mimic project and add a Branch for the new Vendor:

Go to the mimic github repo (https://github.com/rackerlabs/mimic) and fork from the master branch to your repo.

In your github repo create a branch from the master for the new vendor (e.g. Akamai, fastly, etc)

On your development machine clone your mimic repo and then create a local branch that matches the remote github branch. This will automatically set your local repo to the new branch. Once development is complete then submit a Pull Request to the mimic repo.

Add a vendor api module to the rest directory (mimic/rest):

This module contains the vendor api class ({Vendor}Api) and is where all associated endpoints are called from with their permitted verbs (e.g. POST, GET, etc.)

Create Policy Example:

 @app.route('/partner-api/v1/network/production/properties/'  
              '<string:customer_id>/sub-properties/'                     
              '<string:policy_name>/policy',
              methods=['PUT']) 
 def create_policy(self, request, customer_id, policy_name):
      """
      Returns PUT policy.
      """
      data = request.content.read()
      response = self.{vendor}_response.create_policy(data, customer_id, policy_name)
      request.setResponseCode(response[1])
      return json.dumps(response[0])  

Import the vendor canned response module from the canned_responses directory

Add a vendor canned response module to the (mimic/ canned_responses):

This module is where all of the responses come from for each verb of each endpoint. There is error checking so that error responses can be returned.

The vendor cache variable (self.{vendor}_cache )is a class level dictionary variable that is used to store policies that were created during the session. This variable is used for getting and deleting requests.

Get Policy Example:

 def get_policy(self, customer_id, policy_name):
     """Returns service details json.
     :return: a JSON-serializable dictionary matching the format of the JSON
                   response for {Vendor} GET policy
                   ("/partner-api/v1/network/production/properties/customer_id/'
                   'sub-properties/policy_name/policy'") request.
     """
     if policy_name in self.{vendor}_cache: 
         return self.{vendor}_cache[policy_name], 200  
     response_body = policy_not_found(customer_id, policy_name)   
     return response_body, 404  

Import the vendor helper file from the utility directory that will contain the error responses

Add a vendor help file to the utils directory (mimic/utils):

This module contains canned error responses. Each method is for a type of error (e.g. value_not_string, or type_not_valid).

value_not_string Response Example:

 def value_not_string(rule, field, subfld_index, value):
     """Value of the value filed is not a string response
     :param rule: A required int parameter
     :param field: A required string parameter
     :param subfld_index: A required int parameter
     :param value: A required string parameter
     :returns: This method returns dictionary for a mimic {vendor} error response
     """
     resp = {
         'field': ['rules', rule, field, subfld_index, 'value'],
         'message': "{0} is not of type 'string'".format(value),
         'code': 4010
     }  
     return resp  

Modify mimic/resource.py file:

This module is where to add a route to the root level path of the vendor. Import the vendor module from the rest directory

Adding the Vendor Path Example:

 @app.route(“/{vendor}”, branch=True)
 def get_{vendor}_api(self, request):
 return {vendor}_api.{Vendor}Api(self.core).app.resource()

Bringing it all together

Once you have the mimic driver in place, the next step is to start the mimic server

 Starting the Mimic server
  if you have your changes merged into Mimic master:
    pip install mimic; twistd -n mimic
 else:
   cd to_wherever_you_have_mimic_updated;  twistd -n mimic

Now you have the mimic running !!! You should be able to start using the endpoints, just like you would against your actual provider endpoint (fastly, akamai, MaxCDN or any of your favorite CDN provider) with whatever client you normally would. Just take care to use the mimic host, instead of the actual provider endpoint.

Configuring poppy to use your Mimic server Poppy gets the provider endpoints from poppy.conf. So running poppy against your mimic server is just one config change away.

 Updating poppy.conf
1. Locate the config section for your cdn provider. It will look like this,
   [drivers:provider:your_favorite_cdn_provider]
   host: your_favorite_cdn_provider/whatever
2. Update the host value, to point to your mimic server.
  [drivers:provider:your_favorite_cdn_provider]
  host: mimic_server:8900/your_favorite_cdn_provider/whatever