Skip to content

Commit

Permalink
Merge pull request #107 from cloudblue/vendor-apiary-scenario-param
Browse files Browse the repository at this point in the history
Change to use params to store Vendor data in Apiary-Scenario.
  • Loading branch information
marcserrat committed Jun 9, 2020
2 parents 6a5eefc + c91dd69 commit 86388fc
Show file tree
Hide file tree
Showing 28 changed files with 1,090 additions and 252 deletions.
2 changes: 2 additions & 0 deletions connect/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from .agreement import Agreement
from .agreement_stats import AgreementStats
from .anniversary import Anniversary
from .asset_request import AssetRequest
from .asset import Asset
from .base import BaseModel
from .billing import Billing
Expand Down Expand Up @@ -74,6 +75,7 @@
'Agreement',
'AgreementStats',
'Anniversary',
'AssetRequest',
'Asset',
'Attributes',
'BaseModel',
Expand Down
143 changes: 143 additions & 0 deletions connect/models/asset_request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
# -*- 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 datetime
from typing import Union

from .asset import Asset
from .base import BaseModel
from .contract import Contract
from .conversation import Conversation
from .marketplace import Marketplace
from .user import User
from .schemas import AssetRequestSchema


class AssetRequest(BaseModel):
""" Represents a request for the :py:class:`connect.resource.FulfillmentAutomation`
resource.
"""

_schema = AssetRequestSchema()

type = None # type: str
""" (str) Asset status. See :py:class:`.Asset` class for details. """

created = None # type: datetime.datetime
""" (datetime.datetime) Date of request creation. """

updated = None # type: datetime.datetime
""" (datetime.datetime) Date of last request modification. """

status = None # type: str
""" (str) Status of request. One of:
- pending
- inquiring
- failed
- approved
Valid status changes:
- pending -> inquiring
- pending -> failed
- pending -> approved
- inquiring -> failed
- inquiring -> approved
- inquiring -> pending
"""

params_form_url = None # type: str
""" (str) URL for customer/reseller/provider for modifying param value
based on vendor's feedback.
"""

activation_key = None # type: str
""" (str) Activation key content for activating the subscription on vendor portal.
This markdown formatted message is sent to customer.
"""

reason = None # type: str
""" (str) Fail reason in case of status of request is failed. """

note = None # type: str
""" (str) Details of note. """

asset = None # type: Asset
""" (:py:class:`.Asset`) Asset object. """

contract = None # type: Contract
""" (:py:class:`.Contract`) Contract object. """

marketplace = None # type: Marketplace
""" (:py:class:`.Marketplace`) Marketplace object. """

assignee = None # type: Union[User, str, None]
""" (:py:class:`.User` | None) Details of the user assigned to the request. """

@property
def new_items(self):
"""
:return: New items.
:rtype: List[Item]
"""
return list(filter(
lambda item: item.quantity > 0 and item.old_quantity == 0,
self.asset.items))

@property
def changed_items(self):
"""
:return: Changed items.
:rtype: List[Item]
"""
return list(filter(
lambda item: item.quantity > 0 and item.old_quantity > 0,
self.asset.items))

@property
def removed_items(self):
"""
:return: Removed items.
:rtype: List[Item]
"""
return list(filter(
lambda item: item.quantity == 0 and item.old_quantity > 0,
self.asset.items))

def needs_migration(self, migration_key='migration_info'):
"""
Indicates whether the request contains data to be migrated from a legacy product.
Migration is performed by an external service. All you have to do for a request that
needs migration is to skip processing by raising a
:py:class:`connect.exceptions.SkipRequest` exception.
:param str migration_key: The name of the parameter that contains the migration data
(optional; default value is ``migration_info``).
:return: Whether the request needs migrating.
:rtype: bool
"""
param = self.asset.get_param_by_id(migration_key)
return param is not None and bool(param.value)

def get_conversation(self, config=None):
"""
:param Config config: Configuration, or ``None`` to use the environment config (default).
:return: The conversation for this request, or ``None`` if there is none.
:rtype: Conversation|None
"""
from connect.resources.base import ApiClient

client = ApiClient(config, base_path='conversations')
response, _ = client.get(params={'instance_id': self.id})
try:
conversations = Conversation.deserialize(response)
if conversations and conversations[0].id:
response, _ = client.get(conversations[0].id)
return Conversation.deserialize(response)
else:
return None
except ValueError:
return None
136 changes: 7 additions & 129 deletions connect/models/fulfillment.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,141 +3,19 @@
# This file is part of the Ingram Micro Cloud Blue Connect SDK.
# Copyright (c) 2019-2020 Ingram Micro. All Rights Reserved.

import datetime
from typing import Union
from deprecation import deprecated

from .asset import Asset
from .base import BaseModel
from .contract import Contract
from .conversation import Conversation
from .marketplace import Marketplace
from .user import User
from .asset_request import AssetRequest
from .schemas import FulfillmentSchema


class Fulfillment(BaseModel):
class Fulfillment(AssetRequest):
""" Represents a request for the :py:class:`connect.resource.FulfillmentAutomation`
resource.
"""

_schema = FulfillmentSchema()

type = None # type: str
""" (str) Asset status. See :py:class:`.Asset` class for details. """

created = None # type: datetime.datetime
""" (datetime.datetime) Date of request creation. """

updated = None # type: datetime.datetime
""" (datetime.datetime) Date of last request modification. """

status = None # type: str
""" (str) Status of request. One of:
- pending
- inquiring
- failed
- approved
Valid status changes:
- pending -> inquiring
- pending -> failed
- pending -> approved
- inquiring -> failed
- inquiring -> approved
- inquiring -> pending
"""

params_form_url = None # type: str
""" (str) URL for customer/reseller/provider for modifying param value
based on vendor's feedback.
"""

activation_key = None # type: str
""" (str) Activation key content for activating the subscription on vendor portal.
This markdown formatted message is sent to customer.
"""

reason = None # type: str
""" (str) Fail reason in case of status of request is failed. """

note = None # type: str
""" (str) Details of note. """

asset = None # type: Asset
""" (:py:class:`.Asset`) Asset object. """

contract = None # type: Contract
""" (:py:class:`.Contract`) Contract object. """

marketplace = None # type: Marketplace
""" (:py:class:`.Marketplace`) Marketplace object. """

assignee = None # type: Union[User, str, None]
""" (:py:class:`.User` | None) Details of the user assigned to the request. """

@property
def new_items(self):
"""
:return: New items.
:rtype: List[Item]
"""
return list(filter(
lambda item: item.quantity > 0 and item.old_quantity == 0,
self.asset.items))

@property
def changed_items(self):
"""
:return: Changed items.
:rtype: List[Item]
"""
return list(filter(
lambda item: item.quantity > 0 and item.old_quantity > 0,
self.asset.items))

@property
def removed_items(self):
"""
:return: Removed items.
:rtype: List[Item]
"""
return list(filter(
lambda item: item.quantity == 0 and item.old_quantity > 0,
self.asset.items))

def needs_migration(self, migration_key='migration_info'):
"""
Indicates whether the request contains data to be migrated from a legacy product.
Migration is performed by an external service. All you have to do for a request that
needs migration is to skip processing by raising a
:py:class:`connect.exceptions.SkipRequest` exception.
:param str migration_key: The name of the parameter that contains the migration data
(optional; default value is ``migration_info``).
:return: Whether the request needs migrating.
:rtype: bool
"""
param = self.asset.get_param_by_id(migration_key)
return param is not None and bool(param.value)

def get_conversation(self, config=None):
"""
:param Config config: Configuration, or ``None`` to use the environment config (default).
:return: The conversation for this request, or ``None`` if there is none.
:rtype: Conversation|None
"""
from connect.resources.base import ApiClient

client = ApiClient(config, base_path='conversations')
response, _ = client.get(params={'instance_id': self.id})
try:
conversations = Conversation.deserialize(response)
if conversations and conversations[0].id:
response, _ = client.get(conversations[0].id)
return Conversation.deserialize(response)
else:
return None
except ValueError:
return None
@deprecated(deprecated_in='19.2',
details='Use `connect.models.AssetRequest` instead.')
def __init__(self, *args, **kwargs):
super(Fulfillment, self).__init__(*args, **kwargs)
15 changes: 14 additions & 1 deletion connect/models/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# This file is part of the Ingram Micro Cloud Blue Connect SDK.
# Copyright (c) 2019-2020 Ingram Micro. All Rights Reserved.

from deprecation import deprecated
from marshmallow import Schema, fields, post_load
import six

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


class FulfillmentSchema(BaseSchema):
class AssetRequestSchema(BaseSchema):
type = fields.Str()
created = fields.DateTime()
updated = fields.DateTime()
Expand All @@ -635,6 +636,18 @@ class FulfillmentSchema(BaseSchema):
marketplace = fields.Nested(MarketplaceSchema)
assignee = AssigneeField()

@post_load
def make_object(self, data):
from connect.models import AssetRequest
return AssetRequest(**data)


class FulfillmentSchema(AssetRequestSchema):
@deprecated(deprecated_in='19.2',
details='Use `connect.models.schemas.AssetRequestSchema` instead.')
def __init__(self, *args, **kwargs):
super(FulfillmentSchema, self).__init__(*args, **kwargs)

@post_load
def make_object(self, data):
from connect.models import Fulfillment
Expand Down
4 changes: 3 additions & 1 deletion connect/resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from .tier_account_request_automation import TierAccountRequestAutomation
from .billing_request import BillingRequest
from .recurring_asset import RecurringAsset
from .asset_request import AssetRequestResource

__all__ = [
'Directory',
Expand All @@ -23,5 +24,6 @@
'TierAccountRequestAutomation',
'Subscription',
'BillingRequest',
'RecurringAsset'
'RecurringAsset',
'AssetRequestResource'
]
34 changes: 34 additions & 0 deletions connect/resources/asset_request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# -*- coding: utf-8 -*-

# This file is part of the Ingram Micro Cloud Blue Connect SDK.
# Copyright (c) 2019-2020 Ingram Micro. All Rights Reserved.

from connect.models import AssetRequest
from .base import BaseResource


class AssetRequestResource(BaseResource):
""" Asset Request Resource. """
resource = 'requests'
model_class = AssetRequest

def update_param_asset_request(self, request_id, data, note):
""" Update Asset Request param
:param str id_request: Primary key of the request to update.
:param str data: params to update.
{
"params": [{
"id": "PM-9861-7949-8492-0001",
"value": "32323323"
}]
}
:return: Asset Request Attributes Object.
"""
if not request_id:
raise ValueError('Invalid ID')
body = {"note": note, "asset": data}
response = self._api.put(
path='{}/'.format(request_id),
json=body
)
return response

0 comments on commit 86388fc

Please sign in to comment.