Skip to content

Commit a75d7c1

Browse files
authored
Merge pull request #109 from cloudblue/LITE-13315-changes-to-support-different-parameter-types
Parameter with new types
2 parents 86388fc + 388fc9a commit a75d7c1

19 files changed

+552
-14
lines changed

connect/models/constraints.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,9 @@ class Constraints(BaseModel):
2929

3030
reconciliation = None # type: bool
3131
""" (bool) True if vendor has marked parameters as for reconciliation purposes """
32+
33+
min_length = None # type: int
34+
""" (integer) Only for password type """
35+
36+
max_length = None # type: int
37+
""" (integer) Only for password type """

connect/models/param.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ class Param(BaseModel):
4848
value_choices = None # type: Optional[List[ValueChoice]]
4949
""" (List[str]|None) Available dropdown choices for parameter. """
5050

51+
structured_value = None # type: Optional[dict]
52+
5153
phase = None # type: Optional[str]
5254
""" (str|None) Param phase. """
5355

connect/models/schemas.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ class ConstraintsSchema(BaseSchema):
130130
choices = fields.Nested(ValueChoiceSchema, many=True)
131131
unique = fields.Bool()
132132
reconciliation = fields.Bool()
133+
min_length = fields.Integer()
134+
max_length = fields.Integer()
133135

134136
@post_load
135137
def make_object(self, data):
@@ -315,11 +317,11 @@ class ParamSchema(BaseSchema):
315317
value = fields.Str()
316318
value_error = fields.Str()
317319
value_choice = fields.Str(many=True)
318-
319320
title = fields.Str()
320321
scope = fields.Str()
321322
constraints = fields.Nested(ConstraintsSchema)
322323
value_choices = fields.Nested(ValueChoiceSchema, many=True)
324+
structured_value = fields.Dict()
323325
phase = fields.Str()
324326
reconciliation = fields.Bool()
325327
events = fields.Nested(EventsSchema)
@@ -537,6 +539,7 @@ class TierAccountSchema(BaseSchema):
537539
marketplace = fields.Nested(MarketplaceSchema, only=('id', 'name', 'icon'))
538540
hub = fields.Nested(HubSchema, only=('id', 'name'))
539541
version = fields.Int()
542+
tax_id = fields.Str()
540543

541544
events = fields.Nested(EventsSchema)
542545
scopes = fields.List(fields.Str())

connect/resources/base.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import functools
77
import logging
8+
import warnings
89
from typing import Any, List, Dict, Tuple
910

1011
import requests
@@ -83,6 +84,13 @@ def put(self, path='', **kwargs):
8384
response = requests.put(**kwargs)
8485
return self._check_and_pack_response(response)
8586

87+
@function_log()
88+
def delete(self, path='', **kwargs):
89+
# type: (str, Any) -> Tuple[str, int]
90+
kwargs = self._fix_request_kwargs(path, kwargs)
91+
response = requests.delete(**kwargs)
92+
return self._check_and_pack_response(response)
93+
8694
def _fix_request_kwargs(self, path, prev_kwargs, **kwargs):
8795
# type: (str, Dict[str, Any], Dict[str, Any]) -> Dict[str, Any]
8896
""" Set correct kwargs for requests """
@@ -170,6 +178,12 @@ def search(self, filters=None):
170178
response, _ = ApiClient(self._api.config, self._api.base_path + compiled).get()
171179
else:
172180
self.logger.info('Get list request with filters - {}'.format(filters))
181+
for k, v in filters.items():
182+
if k.endswith('__in') and isinstance(v, (tuple, list)):
183+
warnings.warn(
184+
'{}: __in operator is deprecated, Use RQL syntax'.format(k),
185+
DeprecationWarning,
186+
)
173187
response, _ = self._api.get(params=filters)
174188
return self.model_class.deserialize(response)
175189

connect/resources/fulfillment.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,6 @@ def update_param_asset_request(self, request_id, data, note=None):
3737

3838
def create_purchase_request(self, obj):
3939
return self._asset_requests.create(obj)
40+
41+
def search_asset_request(self, obj):
42+
return self._asset_requests.search(obj)

connect/resources/product.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import json
2+
from .base import BaseResource
3+
4+
5+
class ProductsResource(BaseResource):
6+
""" Allows listing and obtaining several types of objects.
7+
:param Config config: Config object or ``None`` to use environment config (default).
8+
"""
9+
resource = 'products'
10+
11+
def list_parameters(self, product_id):
12+
""" List parameters for a product.
13+
:param str product_id: Primary key of the product to search for.
14+
:return: response object with templates contents.
15+
"""
16+
"""
17+
# type: (Dict[str, Any]) -> List[Any]
18+
"""
19+
response, _ = self._api.get(
20+
'/public/v1/products/' + product_id + '/parameters/'
21+
)
22+
response = json.loads(response)
23+
return response
24+
25+
def create_parameter(self, product_id, body):
26+
""" Create parameter for a product.
27+
:param str product_id: Primary key of the product to create parameter.
28+
:param str body: Body of the parameter to create.
29+
:return: response object with templates contents.
30+
"""
31+
"""
32+
# type: (Dict[str, Any]) -> List[Any]
33+
"""
34+
if not product_id:
35+
raise ValueError('Invalid ID')
36+
path = '/public/v1/products/' + product_id + '/parameters/'
37+
response = self._api.post(
38+
path=path,
39+
json=body
40+
)
41+
return response
42+
43+
def update_parameter(self, product_id, parameter_id, body):
44+
""" Update parameter for a product.
45+
:param str product_id: Primary key of the product to update parameter.
46+
:param str parameter_id: Primary key of the parameter to update.
47+
:param str body: Body of the parameter to update.
48+
:return: response object with templates contents.
49+
"""
50+
"""
51+
# type: (Dict[str, Any]) -> List[Any]
52+
"""
53+
if not product_id:
54+
raise ValueError('Invalid ID')
55+
path = '/public/v1/products/' + product_id + '/parameters/' + parameter_id
56+
response = self._api.put(
57+
path=path,
58+
json=body
59+
)
60+
return response
61+
62+
def delete_parameter(self, product_id, parameter_id):
63+
""" Delete parameter for a product.
64+
:param str product_id: Primary key of the product to delete parameter.
65+
:param str parameter_id: Primary key of the parameter to delete.
66+
:return: response object with templates contents.
67+
"""
68+
"""
69+
# type: (Dict[str, Any]) -> List[Any]
70+
"""
71+
if not product_id:
72+
raise ValueError('Invalid ID')
73+
path = '/public/v1/products/' + product_id + '/parameters/' + parameter_id
74+
response = self._api.delete(
75+
path=path
76+
)
77+
return response

connect/resources/tier_config_request.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,24 @@ def inquire(self, id_tcr):
4040
response = self._api.post(path='{}/inquire'.format(id_tcr))
4141
return response
4242

43-
def approve(self, id_tcr, id_template):
43+
def approve(self, tcr_id, template_id=None):
4444
""" Approve a Tier Configuration Request
45-
:param str id_tcr: Primary key of the tier configuration request to approve.
46-
:param str id_template: Primary key of the template.
45+
:param str tcr_id: Primary key of the tier configuration request to approve.
46+
:param str template_id: Primary key of the template.
4747
:return: Template object.
4848
"""
49-
if not id_tcr:
49+
if not tcr_id:
5050
raise ValueError('Invalid ID')
51-
response = self._api.post(
52-
path='{}/approve'.format(id_tcr),
53-
json={
51+
request_kwargs = {
52+
'path': '{}/approve'.format(tcr_id)
53+
}
54+
if template_id:
55+
request_kwargs['json'] = {
5456
'template': {
55-
'id': id_template,
57+
'id': template_id
5658
}
57-
})
59+
}
60+
response = self._api.post(**request_kwargs)
5861
return response
5962

6063
def fail(self, id_tcr, reason):

examples/config.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"apiEndpoint": "https://api.cnct.tech/public/v1",
3-
"apiKey": "ApiKey SU-000-000-000:0000000000000000000000000000000000000000",
2+
"apiEndpoint": "https://api.cnct.info/public/v1",
3+
"apiKey": "ApiKey SU-000-000-000:0000000000000000000000000000000000000",
44
"products": "PRD-000-000-000"
55
}

examples/manage_asset_request.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# This file is part of the Ingram Micro Cloud Blue Connect SDK.
4+
# Copyright (c) 2019-2020 Ingram Micro. All Rights Reserved.
5+
import json
6+
from connect.config import Config
7+
from connect.resources.fulfillment import FulfillmentResource
8+
9+
10+
class AssetRequest():
11+
configuration = Config(file='examples/config.json')
12+
13+
def list_asset_request(self):
14+
asset_request = FulfillmentResource(config=self.configuration)
15+
return asset_request.search_asset_request()
16+
17+
def create_asset_request(self, body):
18+
asset_request = FulfillmentResource(config=self.configuration)
19+
return asset_request.create_purchase_request(body)
20+
21+
22+
def main():
23+
asset_request_example = AssetRequest()
24+
with open('./tests/data/create_purchase_request_body_param.json') as json_file:
25+
body = json.load(json_file)
26+
result = asset_request_example.create_asset_request(body)
27+
for asset in result:
28+
print(asset.id)
29+
30+
31+
if __name__ == '__main__':
32+
main()

examples/manage_parameter.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# This file is part of the Ingram Micro Cloud Blue Connect SDK.
4+
# Copyright (c) 2019-2020 Ingram Micro. All Rights Reserved.
5+
import json
6+
from connect.config import Config
7+
from connect.resources.product import ProductsResource
8+
9+
10+
class Parameters():
11+
configuration = Config(file='examples/config.json')
12+
13+
def list_parameters(self, product_id):
14+
product = ProductsResource(config=self.configuration)
15+
return product.list_parameters(product_id)
16+
17+
def create_parameter(self, product_id, body):
18+
product = ProductsResource(config=self.configuration)
19+
return product.create_parameter(product_id, body)
20+
21+
def update_parameter(self, product_id, parameter_id, body):
22+
product = ProductsResource(config=self.configuration)
23+
return product.update_parameter(product_id, parameter_id, body)
24+
25+
def delete_parameter(self, product_id, param_id):
26+
product = ProductsResource(config=self.configuration)
27+
return product.delete_parameter(product_id, param_id)
28+
29+
30+
def main():
31+
parameters_example = Parameters()
32+
with open('./tests/data/create_parameter_request.json') as json_file:
33+
body = json.load(json_file)
34+
parameters_example.create_parameter('PRD-075-401-854', body)
35+
36+
37+
if __name__ == '__main__':
38+
main()

0 commit comments

Comments
 (0)