generated from microsoft/python-package-template
-
Notifications
You must be signed in to change notification settings - Fork 8
/
abstract_offer.py
139 lines (113 loc) · 4.68 KB
/
abstract_offer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# ---------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# ---------------------------------------------------------
"""Offer Interface"""
from abc import abstractmethod
import yaml
from adal import AuthenticationContext
from azure.core.exceptions import ClientAuthenticationError
from azure.identity import CredentialUnavailableError
from azureiai.managed_apps.confs import ResellerConfiguration
from azureiai.managed_apps.confs.variant import FeatureAvailability
from azureiai.managed_apps.utils import (
get_draft_instance_id,
get_variant_draft_instance_id,
CONFIG_YML,
)
from swagger_client import (
BranchesApi,
ProductApi,
ProductAvailabilityApi,
PropertyApi,
SubmissionApi,
VariantApi,
)
class AbstractOffer:
"""Azure Partner Portal - Offer Interface"""
def __init__(self, name=None, config_yaml=CONFIG_YML):
self.name = name
self.config_yaml = config_yaml
self._authorization = None
self._apis = {
"product": ProductApi(),
"variant": VariantApi(),
"property": PropertyApi(),
"branches": BranchesApi(),
"product_availability": ProductAvailabilityApi(),
"submission": SubmissionApi(),
}
self._ids = {
"product_id": "",
"plan_id": None,
"submission_id": None,
"availability_draft_instance_id": None,
"availability_id": None,
"offer_id": "",
}
def get_auth(self) -> str:
"""
Create Authentication Header
:return: Authorization Header contents
"""
if self._authorization is None:
try:
from azure.identity import AzureCliCredential
from azure.mgmt.resource import SubscriptionClient
credential = AzureCliCredential()
token = credential.get_token("https://api.partner.microsoft.com").token
self._authorization = f"Bearer {token}"
except (CredentialUnavailableError, ClientAuthenticationError):
with open(self.config_yaml, encoding="utf8") as file:
settings = yaml.safe_load(file)
client_id = settings["aad_id"]
client_secret = settings["aad_secret"]
tenant_id = settings["tenant_id"]
auth_context = AuthenticationContext(f"https://login.microsoftonline.com/{tenant_id}")
token_response = auth_context.acquire_token_with_client_credentials(
resource="https://api.partner.microsoft.com",
client_id=client_id,
client_secret=client_secret,
)
self._authorization = f"Bearer {token_response['accessToken'].token}"
return self._authorization
def get_product_id(self) -> str:
"""
Get or Set Product ID
Will call create method if AMA has not yet been created.
:return: Product ID of new Managed Application
"""
if self._ids["product_id"] == "":
self.create()
return self._ids["product_id"]
def get_offer_id(self) -> str:
"""
Get Offer ID
Return Offer ID generated at creation time.
:return: Offer ID of new Managed Application
"""
if self._ids["offer_id"] == "":
self.create()
return self._ids["offer_id"]
def get_plan_id(self) -> str:
"""
Get or Set Product ID
Will call create method if AMA has not yet been created.
:return: Product ID of new Managed Application
"""
if self._ids["plan_id"] == "":
self.create()
return self._ids["plan_id"]
@abstractmethod
def create(self) -> str:
"""Create new Azure Managed Application and set product id."""
def _get_draft_instance_id(self, module: str, retry: int = 0):
"""Call Branch API to get Configuration ID"""
return get_draft_instance_id(self.get_product_id(), self.get_auth(), module, retry)
def _get_variant_draft_instance_id(self, module: str, retry: int = 0) -> str:
return get_variant_draft_instance_id(self.get_product_id(), self.get_auth(), module, retry)
def _set_resell_through_csps(self):
reseller = ResellerConfiguration(product_id=self.get_product_id(), authorization=self.get_auth())
reseller.set()
def _set_pricing_and_availability(self, azure_subscription):
feature_availability = FeatureAvailability(product_id=self.get_product_id(), authorization=self.get_auth())
feature_availability.set(azure_subscription=azure_subscription)