Jump to: navigation, search

Difference between revisions of "Poppy/Provider - Getting Started/Mimic Driver"

Line 90: Line 90:
   def get_{vendor}_api(self, request):
   def get_{vendor}_api(self, request):
   return {vendor}_api.{Vendor}Api(self.core).app.resource()
   return {vendor}_api.{Vendor}Api(self.core).app.resource()
== Bringing it all together ==

Revision as of 15:13, 19 February 2015

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:

 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)
      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
                   '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