Skip to content

Commit 86388fc

Browse files
authored
Merge pull request #107 from cloudblue/vendor-apiary-scenario-param
Change to use params to store Vendor data in Apiary-Scenario.
2 parents 6a5eefc + c91dd69 commit 86388fc

28 files changed

+1090
-252
lines changed

connect/models/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from .agreement import Agreement
1010
from .agreement_stats import AgreementStats
1111
from .anniversary import Anniversary
12+
from .asset_request import AssetRequest
1213
from .asset import Asset
1314
from .base import BaseModel
1415
from .billing import Billing
@@ -74,6 +75,7 @@
7475
'Agreement',
7576
'AgreementStats',
7677
'Anniversary',
78+
'AssetRequest',
7779
'Asset',
7880
'Attributes',
7981
'BaseModel',

connect/models/asset_request.py

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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+
6+
import datetime
7+
from typing import Union
8+
9+
from .asset import Asset
10+
from .base import BaseModel
11+
from .contract import Contract
12+
from .conversation import Conversation
13+
from .marketplace import Marketplace
14+
from .user import User
15+
from .schemas import AssetRequestSchema
16+
17+
18+
class AssetRequest(BaseModel):
19+
""" Represents a request for the :py:class:`connect.resource.FulfillmentAutomation`
20+
resource.
21+
"""
22+
23+
_schema = AssetRequestSchema()
24+
25+
type = None # type: str
26+
""" (str) Asset status. See :py:class:`.Asset` class for details. """
27+
28+
created = None # type: datetime.datetime
29+
""" (datetime.datetime) Date of request creation. """
30+
31+
updated = None # type: datetime.datetime
32+
""" (datetime.datetime) Date of last request modification. """
33+
34+
status = None # type: str
35+
""" (str) Status of request. One of:
36+
37+
- pending
38+
- inquiring
39+
- failed
40+
- approved
41+
42+
Valid status changes:
43+
44+
- pending -> inquiring
45+
- pending -> failed
46+
- pending -> approved
47+
- inquiring -> failed
48+
- inquiring -> approved
49+
- inquiring -> pending
50+
"""
51+
52+
params_form_url = None # type: str
53+
""" (str) URL for customer/reseller/provider for modifying param value
54+
based on vendor's feedback.
55+
"""
56+
57+
activation_key = None # type: str
58+
""" (str) Activation key content for activating the subscription on vendor portal.
59+
This markdown formatted message is sent to customer.
60+
"""
61+
62+
reason = None # type: str
63+
""" (str) Fail reason in case of status of request is failed. """
64+
65+
note = None # type: str
66+
""" (str) Details of note. """
67+
68+
asset = None # type: Asset
69+
""" (:py:class:`.Asset`) Asset object. """
70+
71+
contract = None # type: Contract
72+
""" (:py:class:`.Contract`) Contract object. """
73+
74+
marketplace = None # type: Marketplace
75+
""" (:py:class:`.Marketplace`) Marketplace object. """
76+
77+
assignee = None # type: Union[User, str, None]
78+
""" (:py:class:`.User` | None) Details of the user assigned to the request. """
79+
80+
@property
81+
def new_items(self):
82+
"""
83+
:return: New items.
84+
:rtype: List[Item]
85+
"""
86+
return list(filter(
87+
lambda item: item.quantity > 0 and item.old_quantity == 0,
88+
self.asset.items))
89+
90+
@property
91+
def changed_items(self):
92+
"""
93+
:return: Changed items.
94+
:rtype: List[Item]
95+
"""
96+
return list(filter(
97+
lambda item: item.quantity > 0 and item.old_quantity > 0,
98+
self.asset.items))
99+
100+
@property
101+
def removed_items(self):
102+
"""
103+
:return: Removed items.
104+
:rtype: List[Item]
105+
"""
106+
return list(filter(
107+
lambda item: item.quantity == 0 and item.old_quantity > 0,
108+
self.asset.items))
109+
110+
def needs_migration(self, migration_key='migration_info'):
111+
"""
112+
Indicates whether the request contains data to be migrated from a legacy product.
113+
Migration is performed by an external service. All you have to do for a request that
114+
needs migration is to skip processing by raising a
115+
:py:class:`connect.exceptions.SkipRequest` exception.
116+
117+
:param str migration_key: The name of the parameter that contains the migration data
118+
(optional; default value is ``migration_info``).
119+
:return: Whether the request needs migrating.
120+
:rtype: bool
121+
"""
122+
param = self.asset.get_param_by_id(migration_key)
123+
return param is not None and bool(param.value)
124+
125+
def get_conversation(self, config=None):
126+
"""
127+
:param Config config: Configuration, or ``None`` to use the environment config (default).
128+
:return: The conversation for this request, or ``None`` if there is none.
129+
:rtype: Conversation|None
130+
"""
131+
from connect.resources.base import ApiClient
132+
133+
client = ApiClient(config, base_path='conversations')
134+
response, _ = client.get(params={'instance_id': self.id})
135+
try:
136+
conversations = Conversation.deserialize(response)
137+
if conversations and conversations[0].id:
138+
response, _ = client.get(conversations[0].id)
139+
return Conversation.deserialize(response)
140+
else:
141+
return None
142+
except ValueError:
143+
return None

connect/models/fulfillment.py

Lines changed: 7 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -3,141 +3,19 @@
33
# This file is part of the Ingram Micro Cloud Blue Connect SDK.
44
# Copyright (c) 2019-2020 Ingram Micro. All Rights Reserved.
55

6-
import datetime
7-
from typing import Union
6+
from deprecation import deprecated
87

9-
from .asset import Asset
10-
from .base import BaseModel
11-
from .contract import Contract
12-
from .conversation import Conversation
13-
from .marketplace import Marketplace
14-
from .user import User
8+
from .asset_request import AssetRequest
159
from .schemas import FulfillmentSchema
1610

1711

18-
class Fulfillment(BaseModel):
12+
class Fulfillment(AssetRequest):
1913
""" Represents a request for the :py:class:`connect.resource.FulfillmentAutomation`
2014
resource.
2115
"""
22-
2316
_schema = FulfillmentSchema()
2417

25-
type = None # type: str
26-
""" (str) Asset status. See :py:class:`.Asset` class for details. """
27-
28-
created = None # type: datetime.datetime
29-
""" (datetime.datetime) Date of request creation. """
30-
31-
updated = None # type: datetime.datetime
32-
""" (datetime.datetime) Date of last request modification. """
33-
34-
status = None # type: str
35-
""" (str) Status of request. One of:
36-
37-
- pending
38-
- inquiring
39-
- failed
40-
- approved
41-
42-
Valid status changes:
43-
44-
- pending -> inquiring
45-
- pending -> failed
46-
- pending -> approved
47-
- inquiring -> failed
48-
- inquiring -> approved
49-
- inquiring -> pending
50-
"""
51-
52-
params_form_url = None # type: str
53-
""" (str) URL for customer/reseller/provider for modifying param value
54-
based on vendor's feedback.
55-
"""
56-
57-
activation_key = None # type: str
58-
""" (str) Activation key content for activating the subscription on vendor portal.
59-
This markdown formatted message is sent to customer.
60-
"""
61-
62-
reason = None # type: str
63-
""" (str) Fail reason in case of status of request is failed. """
64-
65-
note = None # type: str
66-
""" (str) Details of note. """
67-
68-
asset = None # type: Asset
69-
""" (:py:class:`.Asset`) Asset object. """
70-
71-
contract = None # type: Contract
72-
""" (:py:class:`.Contract`) Contract object. """
73-
74-
marketplace = None # type: Marketplace
75-
""" (:py:class:`.Marketplace`) Marketplace object. """
76-
77-
assignee = None # type: Union[User, str, None]
78-
""" (:py:class:`.User` | None) Details of the user assigned to the request. """
79-
80-
@property
81-
def new_items(self):
82-
"""
83-
:return: New items.
84-
:rtype: List[Item]
85-
"""
86-
return list(filter(
87-
lambda item: item.quantity > 0 and item.old_quantity == 0,
88-
self.asset.items))
89-
90-
@property
91-
def changed_items(self):
92-
"""
93-
:return: Changed items.
94-
:rtype: List[Item]
95-
"""
96-
return list(filter(
97-
lambda item: item.quantity > 0 and item.old_quantity > 0,
98-
self.asset.items))
99-
100-
@property
101-
def removed_items(self):
102-
"""
103-
:return: Removed items.
104-
:rtype: List[Item]
105-
"""
106-
return list(filter(
107-
lambda item: item.quantity == 0 and item.old_quantity > 0,
108-
self.asset.items))
109-
110-
def needs_migration(self, migration_key='migration_info'):
111-
"""
112-
Indicates whether the request contains data to be migrated from a legacy product.
113-
Migration is performed by an external service. All you have to do for a request that
114-
needs migration is to skip processing by raising a
115-
:py:class:`connect.exceptions.SkipRequest` exception.
116-
117-
:param str migration_key: The name of the parameter that contains the migration data
118-
(optional; default value is ``migration_info``).
119-
:return: Whether the request needs migrating.
120-
:rtype: bool
121-
"""
122-
param = self.asset.get_param_by_id(migration_key)
123-
return param is not None and bool(param.value)
124-
125-
def get_conversation(self, config=None):
126-
"""
127-
:param Config config: Configuration, or ``None`` to use the environment config (default).
128-
:return: The conversation for this request, or ``None`` if there is none.
129-
:rtype: Conversation|None
130-
"""
131-
from connect.resources.base import ApiClient
132-
133-
client = ApiClient(config, base_path='conversations')
134-
response, _ = client.get(params={'instance_id': self.id})
135-
try:
136-
conversations = Conversation.deserialize(response)
137-
if conversations and conversations[0].id:
138-
response, _ = client.get(conversations[0].id)
139-
return Conversation.deserialize(response)
140-
else:
141-
return None
142-
except ValueError:
143-
return None
18+
@deprecated(deprecated_in='19.2',
19+
details='Use `connect.models.AssetRequest` instead.')
20+
def __init__(self, *args, **kwargs):
21+
super(Fulfillment, self).__init__(*args, **kwargs)

connect/models/schemas.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# This file is part of the Ingram Micro Cloud Blue Connect SDK.
44
# Copyright (c) 2019-2020 Ingram Micro. All Rights Reserved.
55

6+
from deprecation import deprecated
67
from marshmallow import Schema, fields, post_load
78
import six
89

@@ -621,7 +622,7 @@ def _deserialize(self, value, attr=None, data=None):
621622
return User.deserialize_json(value)
622623

623624

624-
class FulfillmentSchema(BaseSchema):
625+
class AssetRequestSchema(BaseSchema):
625626
type = fields.Str()
626627
created = fields.DateTime()
627628
updated = fields.DateTime()
@@ -635,6 +636,18 @@ class FulfillmentSchema(BaseSchema):
635636
marketplace = fields.Nested(MarketplaceSchema)
636637
assignee = AssigneeField()
637638

639+
@post_load
640+
def make_object(self, data):
641+
from connect.models import AssetRequest
642+
return AssetRequest(**data)
643+
644+
645+
class FulfillmentSchema(AssetRequestSchema):
646+
@deprecated(deprecated_in='19.2',
647+
details='Use `connect.models.schemas.AssetRequestSchema` instead.')
648+
def __init__(self, *args, **kwargs):
649+
super(FulfillmentSchema, self).__init__(*args, **kwargs)
650+
638651
@post_load
639652
def make_object(self, data):
640653
from connect.models import Fulfillment

connect/resources/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from .tier_account_request_automation import TierAccountRequestAutomation
1313
from .billing_request import BillingRequest
1414
from .recurring_asset import RecurringAsset
15+
from .asset_request import AssetRequestResource
1516

1617
__all__ = [
1718
'Directory',
@@ -23,5 +24,6 @@
2324
'TierAccountRequestAutomation',
2425
'Subscription',
2526
'BillingRequest',
26-
'RecurringAsset'
27+
'RecurringAsset',
28+
'AssetRequestResource'
2729
]

connect/resources/asset_request.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
6+
from connect.models import AssetRequest
7+
from .base import BaseResource
8+
9+
10+
class AssetRequestResource(BaseResource):
11+
""" Asset Request Resource. """
12+
resource = 'requests'
13+
model_class = AssetRequest
14+
15+
def update_param_asset_request(self, request_id, data, note):
16+
""" Update Asset Request param
17+
:param str id_request: Primary key of the request to update.
18+
:param str data: params to update.
19+
{
20+
"params": [{
21+
"id": "PM-9861-7949-8492-0001",
22+
"value": "32323323"
23+
}]
24+
}
25+
:return: Asset Request Attributes Object.
26+
"""
27+
if not request_id:
28+
raise ValueError('Invalid ID')
29+
body = {"note": note, "asset": data}
30+
response = self._api.put(
31+
path='{}/'.format(request_id),
32+
json=body
33+
)
34+
return response

0 commit comments

Comments
 (0)