Jump to: navigation, search

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

(Created page with "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: * fas...")
 
Line 11: Line 11:
  
  
'''Fork the Mimic project and add a Branch for the new Vendor:'''
+
== 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.
 
Go to the mimic github repo (https://github.com/rackerlabs/mimic) and fork from the master branch to your repo.
Line 19: Line 19:
 
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.
 
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):'''
+
== 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.)
 
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:''
+
''Create Policy Example:''
  
 
```
 
```
Line 35: Line 35:
 
       """
 
       """
 
       data = request.content.read()
 
       data = request.content.read()
       response = self.{vendor}_response.create_policy(data,  
+
       response = self.{vendor}_response.create_policy(data, customer_id, policy_name)
                                                                                              customer_id, policy_name)
 
 
       request.setResponseCode(response[1])
 
       request.setResponseCode(response[1])
 
       return json.dumps(response[0])
 
       return json.dumps(response[0])
Line 44: Line 43:
 
Import the vendor canned response module from the canned_responses directory  
 
Import the vendor canned response module from the canned_responses directory  
  
'''Add a vendor canned response module to the (mimic/ canned_responses):'''
+
== 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.
 
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.
 
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)  
+
''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
  
    return response_body, 404
+
``` 
  
 
Import the vendor helper file from the utility directory that will contain the error responses
 
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):
+
== 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).  
 
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
+
''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
 +
 
 +
```

Revision as of 18:55, 23 January 2015

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  

```