Skip to content

Commit

Permalink
Merge pull request #109 from cloudblue/LITE-13315-changes-to-support-…
Browse files Browse the repository at this point in the history
…different-parameter-types

Parameter with new types
  • Loading branch information
marcserrat committed Jun 26, 2020
2 parents 86388fc + 388fc9a commit a75d7c1
Show file tree
Hide file tree
Showing 19 changed files with 552 additions and 14 deletions.
6 changes: 6 additions & 0 deletions connect/models/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,9 @@ class Constraints(BaseModel):

reconciliation = None # type: bool
""" (bool) True if vendor has marked parameters as for reconciliation purposes """

min_length = None # type: int
""" (integer) Only for password type """

max_length = None # type: int
""" (integer) Only for password type """
2 changes: 2 additions & 0 deletions connect/models/param.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class Param(BaseModel):
value_choices = None # type: Optional[List[ValueChoice]]
""" (List[str]|None) Available dropdown choices for parameter. """

structured_value = None # type: Optional[dict]

phase = None # type: Optional[str]
""" (str|None) Param phase. """

Expand Down
5 changes: 4 additions & 1 deletion connect/models/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ class ConstraintsSchema(BaseSchema):
choices = fields.Nested(ValueChoiceSchema, many=True)
unique = fields.Bool()
reconciliation = fields.Bool()
min_length = fields.Integer()
max_length = fields.Integer()

@post_load
def make_object(self, data):
Expand Down Expand Up @@ -315,11 +317,11 @@ class ParamSchema(BaseSchema):
value = fields.Str()
value_error = fields.Str()
value_choice = fields.Str(many=True)

title = fields.Str()
scope = fields.Str()
constraints = fields.Nested(ConstraintsSchema)
value_choices = fields.Nested(ValueChoiceSchema, many=True)
structured_value = fields.Dict()
phase = fields.Str()
reconciliation = fields.Bool()
events = fields.Nested(EventsSchema)
Expand Down Expand Up @@ -537,6 +539,7 @@ class TierAccountSchema(BaseSchema):
marketplace = fields.Nested(MarketplaceSchema, only=('id', 'name', 'icon'))
hub = fields.Nested(HubSchema, only=('id', 'name'))
version = fields.Int()
tax_id = fields.Str()

events = fields.Nested(EventsSchema)
scopes = fields.List(fields.Str())
Expand Down
14 changes: 14 additions & 0 deletions connect/resources/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import functools
import logging
import warnings
from typing import Any, List, Dict, Tuple

import requests
Expand Down Expand Up @@ -83,6 +84,13 @@ def put(self, path='', **kwargs):
response = requests.put(**kwargs)
return self._check_and_pack_response(response)

@function_log()
def delete(self, path='', **kwargs):
# type: (str, Any) -> Tuple[str, int]
kwargs = self._fix_request_kwargs(path, kwargs)
response = requests.delete(**kwargs)
return self._check_and_pack_response(response)

def _fix_request_kwargs(self, path, prev_kwargs, **kwargs):
# type: (str, Dict[str, Any], Dict[str, Any]) -> Dict[str, Any]
""" Set correct kwargs for requests """
Expand Down Expand Up @@ -170,6 +178,12 @@ def search(self, filters=None):
response, _ = ApiClient(self._api.config, self._api.base_path + compiled).get()
else:
self.logger.info('Get list request with filters - {}'.format(filters))
for k, v in filters.items():
if k.endswith('__in') and isinstance(v, (tuple, list)):
warnings.warn(
'{}: __in operator is deprecated, Use RQL syntax'.format(k),
DeprecationWarning,
)
response, _ = self._api.get(params=filters)
return self.model_class.deserialize(response)

Expand Down
3 changes: 3 additions & 0 deletions connect/resources/fulfillment.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@ def update_param_asset_request(self, request_id, data, note=None):

def create_purchase_request(self, obj):
return self._asset_requests.create(obj)

def search_asset_request(self, obj):
return self._asset_requests.search(obj)
77 changes: 77 additions & 0 deletions connect/resources/product.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import json
from .base import BaseResource


class ProductsResource(BaseResource):
""" Allows listing and obtaining several types of objects.
:param Config config: Config object or ``None`` to use environment config (default).
"""
resource = 'products'

def list_parameters(self, product_id):
""" List parameters for a product.
:param str product_id: Primary key of the product to search for.
:return: response object with templates contents.
"""
"""
# type: (Dict[str, Any]) -> List[Any]
"""
response, _ = self._api.get(
'/public/v1/products/' + product_id + '/parameters/'
)
response = json.loads(response)
return response

def create_parameter(self, product_id, body):
""" Create parameter for a product.
:param str product_id: Primary key of the product to create parameter.
:param str body: Body of the parameter to create.
:return: response object with templates contents.
"""
"""
# type: (Dict[str, Any]) -> List[Any]
"""
if not product_id:
raise ValueError('Invalid ID')
path = '/public/v1/products/' + product_id + '/parameters/'
response = self._api.post(
path=path,
json=body
)
return response

def update_parameter(self, product_id, parameter_id, body):
""" Update parameter for a product.
:param str product_id: Primary key of the product to update parameter.
:param str parameter_id: Primary key of the parameter to update.
:param str body: Body of the parameter to update.
:return: response object with templates contents.
"""
"""
# type: (Dict[str, Any]) -> List[Any]
"""
if not product_id:
raise ValueError('Invalid ID')
path = '/public/v1/products/' + product_id + '/parameters/' + parameter_id
response = self._api.put(
path=path,
json=body
)
return response

def delete_parameter(self, product_id, parameter_id):
""" Delete parameter for a product.
:param str product_id: Primary key of the product to delete parameter.
:param str parameter_id: Primary key of the parameter to delete.
:return: response object with templates contents.
"""
"""
# type: (Dict[str, Any]) -> List[Any]
"""
if not product_id:
raise ValueError('Invalid ID')
path = '/public/v1/products/' + product_id + '/parameters/' + parameter_id
response = self._api.delete(
path=path
)
return response
21 changes: 12 additions & 9 deletions connect/resources/tier_config_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,24 @@ def inquire(self, id_tcr):
response = self._api.post(path='{}/inquire'.format(id_tcr))
return response

def approve(self, id_tcr, id_template):
def approve(self, tcr_id, template_id=None):
""" Approve a Tier Configuration Request
:param str id_tcr: Primary key of the tier configuration request to approve.
:param str id_template: Primary key of the template.
:param str tcr_id: Primary key of the tier configuration request to approve.
:param str template_id: Primary key of the template.
:return: Template object.
"""
if not id_tcr:
if not tcr_id:
raise ValueError('Invalid ID')
response = self._api.post(
path='{}/approve'.format(id_tcr),
json={
request_kwargs = {
'path': '{}/approve'.format(tcr_id)
}
if template_id:
request_kwargs['json'] = {
'template': {
'id': id_template,
'id': template_id
}
})
}
response = self._api.post(**request_kwargs)
return response

def fail(self, id_tcr, reason):
Expand Down
4 changes: 2 additions & 2 deletions examples/config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"apiEndpoint": "https://api.cnct.tech/public/v1",
"apiKey": "ApiKey SU-000-000-000:0000000000000000000000000000000000000000",
"apiEndpoint": "https://api.cnct.info/public/v1",
"apiKey": "ApiKey SU-000-000-000:0000000000000000000000000000000000000",
"products": "PRD-000-000-000"
}
32 changes: 32 additions & 0 deletions examples/manage_asset_request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# -*- coding: utf-8 -*-

# This file is part of the Ingram Micro Cloud Blue Connect SDK.
# Copyright (c) 2019-2020 Ingram Micro. All Rights Reserved.
import json
from connect.config import Config
from connect.resources.fulfillment import FulfillmentResource


class AssetRequest():
configuration = Config(file='examples/config.json')

def list_asset_request(self):
asset_request = FulfillmentResource(config=self.configuration)
return asset_request.search_asset_request()

def create_asset_request(self, body):
asset_request = FulfillmentResource(config=self.configuration)
return asset_request.create_purchase_request(body)


def main():
asset_request_example = AssetRequest()
with open('./tests/data/create_purchase_request_body_param.json') as json_file:
body = json.load(json_file)
result = asset_request_example.create_asset_request(body)
for asset in result:
print(asset.id)


if __name__ == '__main__':
main()
38 changes: 38 additions & 0 deletions examples/manage_parameter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# -*- coding: utf-8 -*-

# This file is part of the Ingram Micro Cloud Blue Connect SDK.
# Copyright (c) 2019-2020 Ingram Micro. All Rights Reserved.
import json
from connect.config import Config
from connect.resources.product import ProductsResource


class Parameters():
configuration = Config(file='examples/config.json')

def list_parameters(self, product_id):
product = ProductsResource(config=self.configuration)
return product.list_parameters(product_id)

def create_parameter(self, product_id, body):
product = ProductsResource(config=self.configuration)
return product.create_parameter(product_id, body)

def update_parameter(self, product_id, parameter_id, body):
product = ProductsResource(config=self.configuration)
return product.update_parameter(product_id, parameter_id, body)

def delete_parameter(self, product_id, param_id):
product = ProductsResource(config=self.configuration)
return product.delete_parameter(product_id, param_id)


def main():
parameters_example = Parameters()
with open('./tests/data/create_parameter_request.json') as json_file:
body = json.load(json_file)
parameters_example.create_parameter('PRD-075-401-854', body)


if __name__ == '__main__':
main()
28 changes: 28 additions & 0 deletions tests/data/create_parameter_request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "magic_param_c",
"title": "Title of the Magic Parameter C",
"description": "This is a backup email for emergency",
"type": "checkbox",
"scope": "asset",
"phase": "ordering",
"constraints": {
"required": false,
"hidden": false,
"unique": false,
"choices": [
{
"label": "val1",
"value": "test1",
"default": true
},
{
"label": "val2",
"value": "test2"
},
{
"label": "val3",
"value": "test3"
}
]
}
}
Loading

0 comments on commit a75d7c1

Please sign in to comment.