File: websitemanagementservice.py

package info (click to toggle)
python-azure 20201208%2Bgit-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,437,920 kB
  • sloc: python: 4,287,452; javascript: 269; makefile: 198; sh: 187; xml: 106
file content (304 lines) | stat: -rw-r--r-- 12,184 bytes parent folder | download | duplicates (3)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#-------------------------------------------------------------------------
# Copyright (c) Microsoft.  All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#--------------------------------------------------------------------------
from .constants import (
    DEFAULT_HTTP_TIMEOUT,
    MANAGEMENT_HOST,
)
from .models import (
    MetricDefinitions,
    MetricResponses,
    PublishData,
    Site,
    Sites,
    WebSpace,
    WebSpaces,
)
from .servicemanagementclient import (
    _ServiceManagementClient,
)
from ._common_conversion import (
    _str,
)
from ._serialization import (
    _XmlSerializer,
)

class WebsiteManagementService(_ServiceManagementClient):
    ''' Note that this class is a preliminary work on WebSite
        management. Since it lack a lot a features, final version
        can be slightly different from the current one.
    '''

    def __init__(self, subscription_id=None, cert_file=None,
                 host=MANAGEMENT_HOST, request_session=None,
                 timeout=DEFAULT_HTTP_TIMEOUT):
        '''
        Initializes the website management service.

        subscription_id:
            Subscription to manage.
        cert_file:
            Path to .pem certificate file (httplib), or location of the
            certificate in your Personal certificate store (winhttp) in the
            CURRENT_USER\my\CertificateName format.
            If a request_session is specified, then this is unused.
        host:
            Live ServiceClient URL. Defaults to Azure public cloud.
        request_session:
            Session object to use for http requests. If this is specified, it
            replaces the default use of httplib or winhttp. Also, the cert_file
            parameter is unused when a session is passed in.
            The session object handles authentication, and as such can support
            multiple types of authentication: .pem certificate, oauth.
            For example, you can pass in a Session instance from the requests
            library. To use .pem certificate authentication with requests
            library, set the path to the .pem file on the session.cert
            attribute.
        timeout:
            Optional. Timeout for the http request, in seconds.
        '''
        super(WebsiteManagementService, self).__init__(
            subscription_id, cert_file, host, request_session, timeout)

    #--Operations for web sites ----------------------------------------
    def list_webspaces(self):
        '''
        List the webspaces defined on the account.
        '''
        return self._perform_get(self._get_list_webspaces_path(),
                                 WebSpaces)

    def get_webspace(self, webspace_name):
        '''
        Get details of a specific webspace.

        webspace_name:
            The name of the webspace.
        '''
        return self._perform_get(self._get_webspace_details_path(webspace_name),
                                 WebSpace)

    def list_sites(self, webspace_name):
        '''
        List the web sites defined on this webspace.

        webspace_name:
            The name of the webspace.
        '''
        return self._perform_get(self._get_sites_path(webspace_name),
                                 Sites)

    def get_site(self, webspace_name, website_name):
        '''
        List the web sites defined on this webspace.

        webspace_name:
            The name of the webspace.
        website_name:
            The name of the website.
        '''
        return self._perform_get(self._get_sites_details_path(webspace_name,
                                                              website_name),
                                 Site)

    def create_site(self, webspace_name, website_name, geo_region, host_names,
                    plan='VirtualDedicatedPlan', compute_mode='Shared',
                    server_farm=None, site_mode=None):
        '''
        Create a website.

        webspace_name:
            The name of the webspace.
        website_name:
            The name of the website.
        geo_region:
            The geographical region of the webspace that will be created.
        host_names:
            An array of fully qualified domain names for website. Only one
            hostname can be specified in the azurewebsites.net domain.
            The hostname should match the name of the website. Custom domains
            can only be specified for Shared or Standard websites.
        plan:
            This value must be 'VirtualDedicatedPlan'.
        compute_mode:
            This value should be 'Shared' for the Free or Paid Shared
            offerings, or 'Dedicated' for the Standard offering. The default
            value is 'Shared'. If you set it to 'Dedicated', you must specify
            a value for the server_farm parameter.
        server_farm:
            The name of the Server Farm associated with this website. This is
            a required value for Standard mode.
        site_mode:
            Can be None, 'Limited' or 'Basic'. This value is 'Limited' for the
            Free offering, and 'Basic' for the Paid Shared offering. Standard
            mode does not use the site_mode parameter; it uses the compute_mode
            parameter.
        '''
        xml = _XmlSerializer.create_website_to_xml(webspace_name, website_name, geo_region, plan, host_names, compute_mode, server_farm, site_mode)
        return self._perform_post(
            self._get_sites_path(webspace_name),
            xml,
            Site)

    def delete_site(self, webspace_name, website_name,
                    delete_empty_server_farm=False, delete_metrics=False):
        '''
        Delete a website.

        webspace_name:
            The name of the webspace.
        website_name:
            The name of the website.
        delete_empty_server_farm:
            If the site being deleted is the last web site in a server farm,
            you can delete the server farm by setting this to True.
        delete_metrics:
            To also delete the metrics for the site that you are deleting, you
            can set this to True.
        '''
        path = self._get_sites_details_path(webspace_name, website_name)
        query = ''
        if delete_empty_server_farm:
            query += '&deleteEmptyServerFarm=true'
        if delete_metrics:
            query += '&deleteMetrics=true'
        if query:
            path = path + '?' + query.lstrip('&')
        return self._perform_delete(path)

    def update_site(self, webspace_name, website_name, state=None):
        '''
        Update a web site.

        webspace_name:
            The name of the webspace.
        website_name:
            The name of the website.
        state:
            The wanted state ('Running' or 'Stopped' accepted)
        '''
        xml = _XmlSerializer.update_website_to_xml(state)
        return self._perform_put(
            self._get_sites_details_path(webspace_name, website_name),
            xml, as_async=True)

    def restart_site(self, webspace_name, website_name):
        '''
        Restart a web site.

        webspace_name:
            The name of the webspace.
        website_name:
            The name of the website.
        '''
        return self._perform_post(
            self._get_restart_path(webspace_name, website_name),
            None, as_async=True)

    def get_historical_usage_metrics(self, webspace_name, website_name,
                                     metrics = None, start_time=None, end_time=None, time_grain=None):
        '''
        Get historical usage metrics.

        webspace_name:
            The name of the webspace.
        website_name:
            The name of the website.
        metrics:
            Optional. List of metrics name. Otherwise, all metrics returned.
        start_time:
            Optional. An ISO8601 date. Otherwise, current hour is used.
        end_time:
            Optional. An ISO8601 date. Otherwise, current time is used.
        time_grain:
            Optional. A rollup name, as P1D. OTherwise, default rollup for the metrics is used.
        More information and metrics name at:
        http://msdn.microsoft.com/en-us/library/azure/dn166964.aspx
        '''        
        metrics = ('names='+','.join(metrics)) if metrics else ''
        start_time = ('StartTime='+start_time) if start_time else ''
        end_time = ('EndTime='+end_time) if end_time else ''
        time_grain = ('TimeGrain='+time_grain) if time_grain else ''
        parameters = ('&'.join(v for v in (metrics, start_time, end_time, time_grain) if v))
        parameters = '?'+parameters if parameters else ''
        return self._perform_get(self._get_historical_usage_metrics_path(webspace_name, website_name) + parameters,
                                 MetricResponses)

    def get_metric_definitions(self, webspace_name, website_name):
        '''
        Get metric definitions of metrics available of this web site.

        webspace_name:
            The name of the webspace.
        website_name:
            The name of the website.
        '''
        return self._perform_get(self._get_metric_definitions_path(webspace_name, website_name),
                                 MetricDefinitions)

    def get_publish_profile_xml(self, webspace_name, website_name):
        '''
        Get a site's publish profile as a string

        webspace_name:
            The name of the webspace.
        website_name:
            The name of the website.
        '''
        return self._perform_get(self._get_publishxml_path(webspace_name, website_name),
                                 None).body.decode("utf-8")

    def get_publish_profile(self, webspace_name, website_name):
        '''
        Get a site's publish profile as an object

        webspace_name:
            The name of the webspace.
        website_name:
            The name of the website.
        '''
        return self._perform_get(self._get_publishxml_path(webspace_name, website_name),
                                 PublishData)

    #--Helper functions --------------------------------------------------
    def _get_list_webspaces_path(self):
        return self._get_path('services/webspaces', None)

    def _get_webspace_details_path(self, webspace_name):
        return self._get_path('services/webspaces/', webspace_name)

    def _get_sites_path(self, webspace_name):
        return self._get_path('services/webspaces/',
                              webspace_name) + '/sites'

    def _get_sites_details_path(self, webspace_name, website_name):
        return self._get_path('services/webspaces/',
                              webspace_name) + '/sites/' + _str(website_name)

    def _get_restart_path(self, webspace_name, website_name):
        return self._get_path('services/webspaces/',
                              webspace_name) + '/sites/' + _str(website_name) + '/restart/' 

    def _get_historical_usage_metrics_path(self, webspace_name, website_name):
        return self._get_path('services/webspaces/',
                              webspace_name) + '/sites/' + _str(website_name) + '/metrics/' 
                               
    def _get_metric_definitions_path(self, webspace_name, website_name):
        return self._get_path('services/webspaces/',
                              webspace_name) + '/sites/' + _str(website_name) + '/metricdefinitions/' 

    def _get_publishxml_path(self, webspace_name, website_name):
        return self._get_path('services/webspaces/',
                              webspace_name) + '/sites/' + _str(website_name) + '/publishxml/'