~andrewjbeach/juju-ci-tools/make-local-patcher

« back to all changes in this revision

Viewing changes to azure-sdk-for-python-master/azure/servicemanagement/__init__.py

  • Committer: Curtis Hovey
  • Date: 2015-06-11 19:35:22 UTC
  • mto: This revision was merged to the branch mainline in revision 983.
  • Revision ID: curtis@canonical.com-20150611193522-o2nqkqb04o2i75wv
Remove euca_dump_logs because it has not been used this year.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#-------------------------------------------------------------------------
 
2
# Copyright (c) Microsoft.  All rights reserved.
 
3
#
 
4
# Licensed under the Apache License, Version 2.0 (the "License");
 
5
# you may not use this file except in compliance with the License.
 
6
# You may obtain a copy of the License at
 
7
#   http://www.apache.org/licenses/LICENSE-2.0
 
8
#
 
9
# Unless required by applicable law or agreed to in writing, software
 
10
# distributed under the License is distributed on an "AS IS" BASIS,
 
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
12
# See the License for the specific language governing permissions and
 
13
# limitations under the License.
 
14
#--------------------------------------------------------------------------
 
15
from xml.dom import minidom
 
16
from azure import (
 
17
    WindowsAzureData,
 
18
    _Base64String,
 
19
    _create_entry,
 
20
    _dict_of,
 
21
    _encode_base64,
 
22
    _general_error_handler,
 
23
    _get_children_from_path,
 
24
    _get_first_child_node_value,
 
25
    _list_of,
 
26
    _scalar_list_of,
 
27
    _str,
 
28
    )
 
29
 
 
30
#-----------------------------------------------------------------------------
 
31
# Constants for Azure app environment settings.
 
32
AZURE_MANAGEMENT_CERTFILE = 'AZURE_MANAGEMENT_CERTFILE'
 
33
AZURE_MANAGEMENT_SUBSCRIPTIONID = 'AZURE_MANAGEMENT_SUBSCRIPTIONID'
 
34
 
 
35
# x-ms-version for service management.
 
36
X_MS_VERSION = '2013-06-01'
 
37
 
 
38
#-----------------------------------------------------------------------------
 
39
# Data classes
 
40
 
 
41
 
 
42
class StorageServices(WindowsAzureData):
 
43
 
 
44
    def __init__(self):
 
45
        self.storage_services = _list_of(StorageService)
 
46
 
 
47
    def __iter__(self):
 
48
        return iter(self.storage_services)
 
49
 
 
50
    def __len__(self):
 
51
        return len(self.storage_services)
 
52
 
 
53
    def __getitem__(self, index):
 
54
        return self.storage_services[index]
 
55
 
 
56
 
 
57
class StorageService(WindowsAzureData):
 
58
 
 
59
    def __init__(self):
 
60
        self.url = ''
 
61
        self.service_name = ''
 
62
        self.storage_service_properties = StorageAccountProperties()
 
63
        self.storage_service_keys = StorageServiceKeys()
 
64
        self.extended_properties = _dict_of(
 
65
            'ExtendedProperty', 'Name', 'Value')
 
66
        self.capabilities = _scalar_list_of(str, 'Capability')
 
67
 
 
68
 
 
69
class StorageAccountProperties(WindowsAzureData):
 
70
 
 
71
    def __init__(self):
 
72
        self.description = u''
 
73
        self.affinity_group = u''
 
74
        self.location = u''
 
75
        self.label = _Base64String()
 
76
        self.status = u''
 
77
        self.endpoints = _scalar_list_of(str, 'Endpoint')
 
78
        self.geo_replication_enabled = False
 
79
        self.geo_primary_region = u''
 
80
        self.status_of_primary = u''
 
81
        self.geo_secondary_region = u''
 
82
        self.status_of_secondary = u''
 
83
        self.last_geo_failover_time = u''
 
84
 
 
85
 
 
86
class StorageServiceKeys(WindowsAzureData):
 
87
 
 
88
    def __init__(self):
 
89
        self.primary = u''
 
90
        self.secondary = u''
 
91
 
 
92
 
 
93
class Locations(WindowsAzureData):
 
94
 
 
95
    def __init__(self):
 
96
        self.locations = _list_of(Location)
 
97
 
 
98
    def __iter__(self):
 
99
        return iter(self.locations)
 
100
 
 
101
    def __len__(self):
 
102
        return len(self.locations)
 
103
 
 
104
    def __getitem__(self, index):
 
105
        return self.locations[index]
 
106
 
 
107
 
 
108
class Location(WindowsAzureData):
 
109
 
 
110
    def __init__(self):
 
111
        self.name = u''
 
112
        self.display_name = u''
 
113
        self.available_services = _scalar_list_of(str, 'AvailableService')
 
114
 
 
115
 
 
116
class AffinityGroup(WindowsAzureData):
 
117
 
 
118
    def __init__(self):
 
119
        self.name = ''
 
120
        self.label = _Base64String()
 
121
        self.description = u''
 
122
        self.location = u''
 
123
        self.hosted_services = HostedServices()
 
124
        self.storage_services = StorageServices()
 
125
        self.capabilities = _scalar_list_of(str, 'Capability')
 
126
 
 
127
 
 
128
class AffinityGroups(WindowsAzureData):
 
129
 
 
130
    def __init__(self):
 
131
        self.affinity_groups = _list_of(AffinityGroup)
 
132
 
 
133
    def __iter__(self):
 
134
        return iter(self.affinity_groups)
 
135
 
 
136
    def __len__(self):
 
137
        return len(self.affinity_groups)
 
138
 
 
139
    def __getitem__(self, index):
 
140
        return self.affinity_groups[index]
 
141
 
 
142
 
 
143
class HostedServices(WindowsAzureData):
 
144
 
 
145
    def __init__(self):
 
146
        self.hosted_services = _list_of(HostedService)
 
147
 
 
148
    def __iter__(self):
 
149
        return iter(self.hosted_services)
 
150
 
 
151
    def __len__(self):
 
152
        return len(self.hosted_services)
 
153
 
 
154
    def __getitem__(self, index):
 
155
        return self.hosted_services[index]
 
156
 
 
157
 
 
158
class HostedService(WindowsAzureData):
 
159
 
 
160
    def __init__(self):
 
161
        self.url = u''
 
162
        self.service_name = u''
 
163
        self.hosted_service_properties = HostedServiceProperties()
 
164
        self.deployments = Deployments()
 
165
 
 
166
 
 
167
class HostedServiceProperties(WindowsAzureData):
 
168
 
 
169
    def __init__(self):
 
170
        self.description = u''
 
171
        self.location = u''
 
172
        self.affinity_group = u''
 
173
        self.label = _Base64String()
 
174
        self.status = u''
 
175
        self.date_created = u''
 
176
        self.date_last_modified = u''
 
177
        self.extended_properties = _dict_of(
 
178
            'ExtendedProperty', 'Name', 'Value')
 
179
 
 
180
 
 
181
class VirtualNetworkSites(WindowsAzureData):
 
182
 
 
183
    def __init__(self):
 
184
        self.virtual_network_sites = _list_of(VirtualNetworkSite)
 
185
 
 
186
    def __iter__(self):
 
187
        return iter(self.virtual_network_sites)
 
188
 
 
189
    def __len__(self):
 
190
        return len(self.virtual_network_sites)
 
191
 
 
192
    def __getitem__(self, index):
 
193
        return self.virtual_network_sites[index]
 
194
 
 
195
 
 
196
class VirtualNetworkSite(WindowsAzureData):
 
197
 
 
198
    def __init__(self):
 
199
        self.name = u''
 
200
        self.id = u''
 
201
        self.affinity_group = u''
 
202
        self.subnets = Subnets()
 
203
 
 
204
 
 
205
class Subnets(WindowsAzureData):
 
206
 
 
207
    def __init__(self):
 
208
        self.subnets = _list_of(Subnet)
 
209
 
 
210
    def __iter__(self):
 
211
        return iter(self.subnets)
 
212
 
 
213
    def __len__(self):
 
214
        return len(self.subnets)
 
215
 
 
216
    def __getitem__(self, index):
 
217
        return self.subnets[index]
 
218
 
 
219
 
 
220
class Subnet(WindowsAzureData):
 
221
 
 
222
    def __init__(self):
 
223
        self.name = u''
 
224
        self.address_prefix = u''
 
225
 
 
226
 
 
227
 
 
228
class Deployments(WindowsAzureData):
 
229
 
 
230
    def __init__(self):
 
231
        self.deployments = _list_of(Deployment)
 
232
 
 
233
    def __iter__(self):
 
234
        return iter(self.deployments)
 
235
 
 
236
    def __len__(self):
 
237
        return len(self.deployments)
 
238
 
 
239
    def __getitem__(self, index):
 
240
        return self.deployments[index]
 
241
 
 
242
 
 
243
class Deployment(WindowsAzureData):
 
244
 
 
245
    def __init__(self):
 
246
        self.name = u''
 
247
        self.deployment_slot = u''
 
248
        self.private_id = u''
 
249
        self.status = u''
 
250
        self.label = _Base64String()
 
251
        self.url = u''
 
252
        self.configuration = _Base64String()
 
253
        self.role_instance_list = RoleInstanceList()
 
254
        self.upgrade_status = UpgradeStatus()
 
255
        self.upgrade_domain_count = u''
 
256
        self.role_list = RoleList()
 
257
        self.sdk_version = u''
 
258
        self.input_endpoint_list = InputEndpoints()
 
259
        self.locked = False
 
260
        self.rollback_allowed = False
 
261
        self.persistent_vm_downtime_info = PersistentVMDowntimeInfo()
 
262
        self.created_time = u''
 
263
        self.virtual_network_name = u''
 
264
        self.last_modified_time = u''
 
265
        self.extended_properties = _dict_of(
 
266
            'ExtendedProperty', 'Name', 'Value')
 
267
 
 
268
 
 
269
class RoleInstanceList(WindowsAzureData):
 
270
 
 
271
    def __init__(self):
 
272
        self.role_instances = _list_of(RoleInstance)
 
273
 
 
274
    def __iter__(self):
 
275
        return iter(self.role_instances)
 
276
 
 
277
    def __len__(self):
 
278
        return len(self.role_instances)
 
279
 
 
280
    def __getitem__(self, index):
 
281
        return self.role_instances[index]
 
282
 
 
283
 
 
284
class RoleInstance(WindowsAzureData):
 
285
 
 
286
    def __init__(self):
 
287
        self.role_name = u''
 
288
        self.instance_name = u''
 
289
        self.instance_status = u''
 
290
        self.instance_upgrade_domain = 0
 
291
        self.instance_fault_domain = 0
 
292
        self.instance_size = u''
 
293
        self.instance_state_details = u''
 
294
        self.instance_error_code = u''
 
295
        self.ip_address = u''
 
296
        self.instance_endpoints = InstanceEndpoints()
 
297
        self.power_state = u''
 
298
        self.fqdn = u''
 
299
        self.host_name = u''
 
300
 
 
301
 
 
302
class InstanceEndpoints(WindowsAzureData):
 
303
 
 
304
    def __init__(self):
 
305
        self.instance_endpoints = _list_of(InstanceEndpoint)
 
306
 
 
307
    def __iter__(self):
 
308
        return iter(self.instance_endpoints)
 
309
 
 
310
    def __len__(self):
 
311
        return len(self.instance_endpoints)
 
312
 
 
313
    def __getitem__(self, index):
 
314
        return self.instance_endpoints[index]
 
315
 
 
316
 
 
317
class InstanceEndpoint(WindowsAzureData):
 
318
 
 
319
    def __init__(self):
 
320
        self.name = u''
 
321
        self.vip = u''
 
322
        self.public_port = u''
 
323
        self.local_port = u''
 
324
        self.protocol = u''
 
325
 
 
326
 
 
327
class UpgradeStatus(WindowsAzureData):
 
328
 
 
329
    def __init__(self):
 
330
        self.upgrade_type = u''
 
331
        self.current_upgrade_domain_state = u''
 
332
        self.current_upgrade_domain = u''
 
333
 
 
334
 
 
335
class InputEndpoints(WindowsAzureData):
 
336
 
 
337
    def __init__(self):
 
338
        self.input_endpoints = _list_of(InputEndpoint)
 
339
 
 
340
    def __iter__(self):
 
341
        return iter(self.input_endpoints)
 
342
 
 
343
    def __len__(self):
 
344
        return len(self.input_endpoints)
 
345
 
 
346
    def __getitem__(self, index):
 
347
        return self.input_endpoints[index]
 
348
 
 
349
 
 
350
class InputEndpoint(WindowsAzureData):
 
351
 
 
352
    def __init__(self):
 
353
        self.role_name = u''
 
354
        self.vip = u''
 
355
        self.port = u''
 
356
 
 
357
 
 
358
class RoleList(WindowsAzureData):
 
359
 
 
360
    def __init__(self):
 
361
        self.roles = _list_of(Role)
 
362
 
 
363
    def __iter__(self):
 
364
        return iter(self.roles)
 
365
 
 
366
    def __len__(self):
 
367
        return len(self.roles)
 
368
 
 
369
    def __getitem__(self, index):
 
370
        return self.roles[index]
 
371
 
 
372
 
 
373
class Role(WindowsAzureData):
 
374
 
 
375
    def __init__(self):
 
376
        self.role_name = u''
 
377
        self.role_type = u''
 
378
        self.os_version = u''
 
379
        self.configuration_sets = ConfigurationSets()
 
380
        self.availability_set_name = u''
 
381
        self.data_virtual_hard_disks = DataVirtualHardDisks()
 
382
        self.os_virtual_hard_disk = OSVirtualHardDisk()
 
383
        self.role_size = u''
 
384
        self.default_win_rm_certificate_thumbprint = u''
 
385
 
 
386
 
 
387
class PersistentVMDowntimeInfo(WindowsAzureData):
 
388
 
 
389
    def __init__(self):
 
390
        self.start_time = u''
 
391
        self.end_time = u''
 
392
        self.status = u''
 
393
 
 
394
 
 
395
class Certificates(WindowsAzureData):
 
396
 
 
397
    def __init__(self):
 
398
        self.certificates = _list_of(Certificate)
 
399
 
 
400
    def __iter__(self):
 
401
        return iter(self.certificates)
 
402
 
 
403
    def __len__(self):
 
404
        return len(self.certificates)
 
405
 
 
406
    def __getitem__(self, index):
 
407
        return self.certificates[index]
 
408
 
 
409
 
 
410
class Certificate(WindowsAzureData):
 
411
 
 
412
    def __init__(self):
 
413
        self.certificate_url = u''
 
414
        self.thumbprint = u''
 
415
        self.thumbprint_algorithm = u''
 
416
        self.data = u''
 
417
 
 
418
 
 
419
class OperationError(WindowsAzureData):
 
420
 
 
421
    def __init__(self):
 
422
        self.code = u''
 
423
        self.message = u''
 
424
 
 
425
 
 
426
class Operation(WindowsAzureData):
 
427
 
 
428
    def __init__(self):
 
429
        self.id = u''
 
430
        self.status = u''
 
431
        self.http_status_code = u''
 
432
        self.error = OperationError()
 
433
 
 
434
 
 
435
class OperatingSystem(WindowsAzureData):
 
436
 
 
437
    def __init__(self):
 
438
        self.version = u''
 
439
        self.label = _Base64String()
 
440
        self.is_default = True
 
441
        self.is_active = True
 
442
        self.family = 0
 
443
        self.family_label = _Base64String()
 
444
 
 
445
 
 
446
class OperatingSystems(WindowsAzureData):
 
447
 
 
448
    def __init__(self):
 
449
        self.operating_systems = _list_of(OperatingSystem)
 
450
 
 
451
    def __iter__(self):
 
452
        return iter(self.operating_systems)
 
453
 
 
454
    def __len__(self):
 
455
        return len(self.operating_systems)
 
456
 
 
457
    def __getitem__(self, index):
 
458
        return self.operating_systems[index]
 
459
 
 
460
 
 
461
class OperatingSystemFamily(WindowsAzureData):
 
462
 
 
463
    def __init__(self):
 
464
        self.name = u''
 
465
        self.label = _Base64String()
 
466
        self.operating_systems = OperatingSystems()
 
467
 
 
468
 
 
469
class OperatingSystemFamilies(WindowsAzureData):
 
470
 
 
471
    def __init__(self):
 
472
        self.operating_system_families = _list_of(OperatingSystemFamily)
 
473
 
 
474
    def __iter__(self):
 
475
        return iter(self.operating_system_families)
 
476
 
 
477
    def __len__(self):
 
478
        return len(self.operating_system_families)
 
479
 
 
480
    def __getitem__(self, index):
 
481
        return self.operating_system_families[index]
 
482
 
 
483
 
 
484
class Subscription(WindowsAzureData):
 
485
 
 
486
    def __init__(self):
 
487
        self.subscription_id = u''
 
488
        self.subscription_name = u''
 
489
        self.subscription_status = u''
 
490
        self.account_admin_live_email_id = u''
 
491
        self.service_admin_live_email_id = u''
 
492
        self.max_core_count = 0
 
493
        self.max_storage_accounts = 0
 
494
        self.max_hosted_services = 0
 
495
        self.current_core_count = 0
 
496
        self.current_hosted_services = 0
 
497
        self.current_storage_accounts = 0
 
498
        self.max_virtual_network_sites = 0
 
499
        self.max_local_network_sites = 0
 
500
        self.max_dns_servers = 0
 
501
 
 
502
 
 
503
class AvailabilityResponse(WindowsAzureData):
 
504
 
 
505
    def __init__(self):
 
506
        self.result = False
 
507
 
 
508
 
 
509
class SubscriptionCertificates(WindowsAzureData):
 
510
 
 
511
    def __init__(self):
 
512
        self.subscription_certificates = _list_of(SubscriptionCertificate)
 
513
 
 
514
    def __iter__(self):
 
515
        return iter(self.subscription_certificates)
 
516
 
 
517
    def __len__(self):
 
518
        return len(self.subscription_certificates)
 
519
 
 
520
    def __getitem__(self, index):
 
521
        return self.subscription_certificates[index]
 
522
 
 
523
 
 
524
class SubscriptionCertificate(WindowsAzureData):
 
525
 
 
526
    def __init__(self):
 
527
        self.subscription_certificate_public_key = u''
 
528
        self.subscription_certificate_thumbprint = u''
 
529
        self.subscription_certificate_data = u''
 
530
        self.created = u''
 
531
 
 
532
 
 
533
class Images(WindowsAzureData):
 
534
 
 
535
    def __init__(self):
 
536
        self.images = _list_of(OSImage)
 
537
 
 
538
    def __iter__(self):
 
539
        return iter(self.images)
 
540
 
 
541
    def __len__(self):
 
542
        return len(self.images)
 
543
 
 
544
    def __getitem__(self, index):
 
545
        return self.images[index]
 
546
 
 
547
 
 
548
class OSImage(WindowsAzureData):
 
549
 
 
550
    def __init__(self):
 
551
        self.affinity_group = u''
 
552
        self.category = u''
 
553
        self.location = u''
 
554
        self.logical_size_in_gb = 0
 
555
        self.label = u''
 
556
        self.media_link = u''
 
557
        self.name = u''
 
558
        self.os = u''
 
559
        self.eula = u''
 
560
        self.description = u''
 
561
 
 
562
 
 
563
class Disks(WindowsAzureData):
 
564
 
 
565
    def __init__(self):
 
566
        self.disks = _list_of(Disk)
 
567
 
 
568
    def __iter__(self):
 
569
        return iter(self.disks)
 
570
 
 
571
    def __len__(self):
 
572
        return len(self.disks)
 
573
 
 
574
    def __getitem__(self, index):
 
575
        return self.disks[index]
 
576
 
 
577
 
 
578
class Disk(WindowsAzureData):
 
579
 
 
580
    def __init__(self):
 
581
        self.affinity_group = u''
 
582
        self.attached_to = AttachedTo()
 
583
        self.has_operating_system = u''
 
584
        self.is_corrupted = u''
 
585
        self.location = u''
 
586
        self.logical_disk_size_in_gb = 0
 
587
        self.label = u''
 
588
        self.media_link = u''
 
589
        self.name = u''
 
590
        self.os = u''
 
591
        self.source_image_name = u''
 
592
 
 
593
 
 
594
class AttachedTo(WindowsAzureData):
 
595
 
 
596
    def __init__(self):
 
597
        self.hosted_service_name = u''
 
598
        self.deployment_name = u''
 
599
        self.role_name = u''
 
600
 
 
601
 
 
602
class PersistentVMRole(WindowsAzureData):
 
603
 
 
604
    def __init__(self):
 
605
        self.role_name = u''
 
606
        self.role_type = u''
 
607
        self.os_version = u''  # undocumented
 
608
        self.configuration_sets = ConfigurationSets()
 
609
        self.availability_set_name = u''
 
610
        self.data_virtual_hard_disks = DataVirtualHardDisks()
 
611
        self.os_virtual_hard_disk = OSVirtualHardDisk()
 
612
        self.role_size = u''
 
613
        self.default_win_rm_certificate_thumbprint = u''
 
614
 
 
615
 
 
616
class ConfigurationSets(WindowsAzureData):
 
617
 
 
618
    def __init__(self):
 
619
        self.configuration_sets = _list_of(ConfigurationSet)
 
620
 
 
621
    def __iter__(self):
 
622
        return iter(self.configuration_sets)
 
623
 
 
624
    def __len__(self):
 
625
        return len(self.configuration_sets)
 
626
 
 
627
    def __getitem__(self, index):
 
628
        return self.configuration_sets[index]
 
629
 
 
630
 
 
631
class ConfigurationSet(WindowsAzureData):
 
632
 
 
633
    def __init__(self):
 
634
        self.configuration_set_type = u'NetworkConfiguration'
 
635
        self.role_type = u''
 
636
        self.input_endpoints = ConfigurationSetInputEndpoints()
 
637
        self.subnet_names = _scalar_list_of(str, 'SubnetName')
 
638
 
 
639
 
 
640
class ConfigurationSetInputEndpoints(WindowsAzureData):
 
641
 
 
642
    def __init__(self):
 
643
        self.input_endpoints = _list_of(
 
644
            ConfigurationSetInputEndpoint, 'InputEndpoint')
 
645
 
 
646
    def __iter__(self):
 
647
        return iter(self.input_endpoints)
 
648
 
 
649
    def __len__(self):
 
650
        return len(self.input_endpoints)
 
651
 
 
652
    def __getitem__(self, index):
 
653
        return self.input_endpoints[index]
 
654
 
 
655
 
 
656
class ConfigurationSetInputEndpoint(WindowsAzureData):
 
657
 
 
658
    '''
 
659
    Initializes a network configuration input endpoint.
 
660
 
 
661
    name: Specifies the name for the external endpoint.
 
662
    protocol:
 
663
        Specifies the protocol to use to inspect the virtual machine
 
664
        availability status. Possible values are: HTTP, TCP.
 
665
    port: Specifies the external port to use for the endpoint.
 
666
    local_port:
 
667
        Specifies the internal port on which the virtual machine is listening
 
668
        to serve the endpoint.
 
669
    load_balanced_endpoint_set_name:
 
670
        Specifies a name for a set of load-balanced endpoints. Specifying this
 
671
        element for a given endpoint adds it to the set. If you are setting an
 
672
        endpoint to use to connect to the virtual machine via the Remote
 
673
        Desktop, do not set this property.
 
674
    enable_direct_server_return:
 
675
        Specifies whether direct server return load balancing is enabled.
 
676
    '''
 
677
 
 
678
    def __init__(self, name=u'', protocol=u'', port=u'', local_port=u'',
 
679
                 load_balanced_endpoint_set_name=u'',
 
680
                 enable_direct_server_return=False):
 
681
        self.enable_direct_server_return = enable_direct_server_return
 
682
        self.load_balanced_endpoint_set_name = load_balanced_endpoint_set_name
 
683
        self.local_port = local_port
 
684
        self.name = name
 
685
        self.port = port
 
686
        self.load_balancer_probe = LoadBalancerProbe()
 
687
        self.protocol = protocol
 
688
 
 
689
 
 
690
class WindowsConfigurationSet(WindowsAzureData):
 
691
 
 
692
    def __init__(self, computer_name=None, admin_password=None,
 
693
                 reset_password_on_first_logon=None,
 
694
                 enable_automatic_updates=None, time_zone=None,
 
695
                 admin_username=None):
 
696
        self.configuration_set_type = u'WindowsProvisioningConfiguration'
 
697
        self.computer_name = computer_name
 
698
        self.admin_password = admin_password
 
699
        self.admin_username = admin_username
 
700
        self.reset_password_on_first_logon = reset_password_on_first_logon
 
701
        self.enable_automatic_updates = enable_automatic_updates
 
702
        self.time_zone = time_zone
 
703
        self.domain_join = DomainJoin()
 
704
        self.stored_certificate_settings = StoredCertificateSettings()
 
705
        self.win_rm = WinRM()
 
706
 
 
707
 
 
708
class DomainJoin(WindowsAzureData):
 
709
 
 
710
    def __init__(self):
 
711
        self.credentials = Credentials()
 
712
        self.join_domain = u''
 
713
        self.machine_object_ou = u''
 
714
 
 
715
 
 
716
class Credentials(WindowsAzureData):
 
717
 
 
718
    def __init__(self):
 
719
        self.domain = u''
 
720
        self.username = u''
 
721
        self.password = u''
 
722
 
 
723
 
 
724
class StoredCertificateSettings(WindowsAzureData):
 
725
 
 
726
    def __init__(self):
 
727
        self.stored_certificate_settings = _list_of(CertificateSetting)
 
728
 
 
729
    def __iter__(self):
 
730
        return iter(self.stored_certificate_settings)
 
731
 
 
732
    def __len__(self):
 
733
        return len(self.stored_certificate_settings)
 
734
 
 
735
    def __getitem__(self, index):
 
736
        return self.stored_certificate_settings[index]
 
737
 
 
738
 
 
739
class CertificateSetting(WindowsAzureData):
 
740
 
 
741
    '''
 
742
    Initializes a certificate setting.
 
743
 
 
744
    thumbprint:
 
745
        Specifies the thumbprint of the certificate to be provisioned. The
 
746
        thumbprint must specify an existing service certificate.
 
747
    store_name:
 
748
        Specifies the name of the certificate store from which retrieve
 
749
        certificate.
 
750
    store_location:
 
751
        Specifies the target certificate store location on the virtual machine.
 
752
        The only supported value is LocalMachine.
 
753
    '''
 
754
 
 
755
    def __init__(self, thumbprint=u'', store_name=u'', store_location=u''):
 
756
        self.thumbprint = thumbprint
 
757
        self.store_name = store_name
 
758
        self.store_location = store_location
 
759
 
 
760
 
 
761
class WinRM(WindowsAzureData):
 
762
 
 
763
    '''
 
764
    Contains configuration settings for the Windows Remote Management service on
 
765
    the Virtual Machine.
 
766
    '''
 
767
 
 
768
    def __init__(self):
 
769
        self.listeners = Listeners()
 
770
 
 
771
 
 
772
class Listeners(WindowsAzureData):
 
773
 
 
774
    def __init__(self):
 
775
        self.listeners = _list_of(Listener)
 
776
 
 
777
    def __iter__(self):
 
778
        return iter(self.listeners)
 
779
 
 
780
    def __len__(self):
 
781
        return len(self.listeners)
 
782
 
 
783
    def __getitem__(self, index):
 
784
        return self.listeners[index]
 
785
 
 
786
 
 
787
class Listener(WindowsAzureData):
 
788
 
 
789
    '''
 
790
    Specifies the protocol and certificate information for the listener.
 
791
 
 
792
    protocol:
 
793
        Specifies the protocol of listener.  Possible values are: Http, Https.
 
794
        The value is case sensitive.
 
795
    certificate_thumbprint:
 
796
        Optional. Specifies the certificate thumbprint for the secure
 
797
        connection. If this value is not specified, a self-signed certificate is
 
798
        generated and used for the Virtual Machine.
 
799
    '''
 
800
 
 
801
    def __init__(self, protocol=u'', certificate_thumbprint=u''):
 
802
        self.protocol = protocol
 
803
        self.certificate_thumbprint = certificate_thumbprint
 
804
 
 
805
 
 
806
class LinuxConfigurationSet(WindowsAzureData):
 
807
 
 
808
    def __init__(self, host_name=None, user_name=None, user_password=None,
 
809
                 disable_ssh_password_authentication=None):
 
810
        self.configuration_set_type = u'LinuxProvisioningConfiguration'
 
811
        self.host_name = host_name
 
812
        self.user_name = user_name
 
813
        self.user_password = user_password
 
814
        self.disable_ssh_password_authentication =\
 
815
            disable_ssh_password_authentication
 
816
        self.ssh = SSH()
 
817
 
 
818
 
 
819
class SSH(WindowsAzureData):
 
820
 
 
821
    def __init__(self):
 
822
        self.public_keys = PublicKeys()
 
823
        self.key_pairs = KeyPairs()
 
824
 
 
825
 
 
826
class PublicKeys(WindowsAzureData):
 
827
 
 
828
    def __init__(self):
 
829
        self.public_keys = _list_of(PublicKey)
 
830
 
 
831
    def __iter__(self):
 
832
        return iter(self.public_keys)
 
833
 
 
834
    def __len__(self):
 
835
        return len(self.public_keys)
 
836
 
 
837
    def __getitem__(self, index):
 
838
        return self.public_keys[index]
 
839
 
 
840
 
 
841
class PublicKey(WindowsAzureData):
 
842
 
 
843
    def __init__(self, fingerprint=u'', path=u''):
 
844
        self.fingerprint = fingerprint
 
845
        self.path = path
 
846
 
 
847
 
 
848
class KeyPairs(WindowsAzureData):
 
849
 
 
850
    def __init__(self):
 
851
        self.key_pairs = _list_of(KeyPair)
 
852
 
 
853
    def __iter__(self):
 
854
        return iter(self.key_pairs)
 
855
 
 
856
    def __len__(self):
 
857
        return len(self.key_pairs)
 
858
 
 
859
    def __getitem__(self, index):
 
860
        return self.key_pairs[index]
 
861
 
 
862
 
 
863
class KeyPair(WindowsAzureData):
 
864
 
 
865
    def __init__(self, fingerprint=u'', path=u''):
 
866
        self.fingerprint = fingerprint
 
867
        self.path = path
 
868
 
 
869
 
 
870
class LoadBalancerProbe(WindowsAzureData):
 
871
 
 
872
    def __init__(self):
 
873
        self.path = u''
 
874
        self.port = u''
 
875
        self.protocol = u''
 
876
 
 
877
 
 
878
class DataVirtualHardDisks(WindowsAzureData):
 
879
 
 
880
    def __init__(self):
 
881
        self.data_virtual_hard_disks = _list_of(DataVirtualHardDisk)
 
882
 
 
883
    def __iter__(self):
 
884
        return iter(self.data_virtual_hard_disks)
 
885
 
 
886
    def __len__(self):
 
887
        return len(self.data_virtual_hard_disks)
 
888
 
 
889
    def __getitem__(self, index):
 
890
        return self.data_virtual_hard_disks[index]
 
891
 
 
892
 
 
893
class DataVirtualHardDisk(WindowsAzureData):
 
894
 
 
895
    def __init__(self):
 
896
        self.host_caching = u''
 
897
        self.disk_label = u''
 
898
        self.disk_name = u''
 
899
        self.lun = 0
 
900
        self.logical_disk_size_in_gb = 0
 
901
        self.media_link = u''
 
902
 
 
903
 
 
904
class OSVirtualHardDisk(WindowsAzureData):
 
905
 
 
906
    def __init__(self, source_image_name=None, media_link=None,
 
907
                 host_caching=None, disk_label=None, disk_name=None):
 
908
        self.source_image_name = source_image_name
 
909
        self.media_link = media_link
 
910
        self.host_caching = host_caching
 
911
        self.disk_label = disk_label
 
912
        self.disk_name = disk_name
 
913
        self.os = u''  # undocumented, not used when adding a role
 
914
 
 
915
 
 
916
class AsynchronousOperationResult(WindowsAzureData):
 
917
 
 
918
    def __init__(self, request_id=None):
 
919
        self.request_id = request_id
 
920
 
 
921
 
 
922
class ServiceBusRegion(WindowsAzureData):
 
923
 
 
924
    def __init__(self):
 
925
        self.code = u''
 
926
        self.fullname = u''
 
927
 
 
928
 
 
929
class ServiceBusNamespace(WindowsAzureData):
 
930
 
 
931
    def __init__(self):
 
932
        self.name = u''
 
933
        self.region = u''
 
934
        self.default_key = u''
 
935
        self.status = u''
 
936
        self.created_at = u''
 
937
        self.acs_management_endpoint = u''
 
938
        self.servicebus_endpoint = u''
 
939
        self.connection_string = u''
 
940
        self.subscription_id = u''
 
941
        self.enabled = False
 
942
 
 
943
 
 
944
def _update_management_header(request):
 
945
    ''' Add additional headers for management. '''
 
946
 
 
947
    if request.method in ['PUT', 'POST', 'MERGE', 'DELETE']:
 
948
        request.headers.append(('Content-Length', str(len(request.body))))
 
949
 
 
950
    # append additional headers base on the service
 
951
    request.headers.append(('x-ms-version', X_MS_VERSION))
 
952
 
 
953
    # if it is not GET or HEAD request, must set content-type.
 
954
    if not request.method in ['GET', 'HEAD']:
 
955
        for name, _ in request.headers:
 
956
            if 'content-type' == name.lower():
 
957
                break
 
958
        else:
 
959
            request.headers.append(
 
960
                ('Content-Type',
 
961
                 'application/atom+xml;type=entry;charset=utf-8'))
 
962
 
 
963
    return request.headers
 
964
 
 
965
 
 
966
def _parse_response_for_async_op(response):
 
967
    ''' Extracts request id from response header. '''
 
968
 
 
969
    if response is None:
 
970
        return None
 
971
 
 
972
    result = AsynchronousOperationResult()
 
973
    if response.headers:
 
974
        for name, value in response.headers:
 
975
            if name.lower() == 'x-ms-request-id':
 
976
                result.request_id = value
 
977
 
 
978
    return result
 
979
 
 
980
 
 
981
def _management_error_handler(http_error):
 
982
    ''' Simple error handler for management service. '''
 
983
    return _general_error_handler(http_error)
 
984
 
 
985
 
 
986
def _lower(text):
 
987
    return text.lower()
 
988
 
 
989
 
 
990
class _XmlSerializer(object):
 
991
 
 
992
    @staticmethod
 
993
    def create_storage_service_input_to_xml(service_name, description, label,
 
994
                                            affinity_group, location,
 
995
                                            geo_replication_enabled,
 
996
                                            extended_properties):
 
997
        return _XmlSerializer.doc_from_data(
 
998
            'CreateStorageServiceInput',
 
999
            [('ServiceName', service_name),
 
1000
             ('Description', description),
 
1001
             ('Label', label, _encode_base64),
 
1002
             ('AffinityGroup', affinity_group),
 
1003
             ('Location', location),
 
1004
             ('GeoReplicationEnabled', geo_replication_enabled, _lower)],
 
1005
            extended_properties)
 
1006
 
 
1007
    @staticmethod
 
1008
    def update_storage_service_input_to_xml(description, label,
 
1009
                                            geo_replication_enabled,
 
1010
                                            extended_properties):
 
1011
        return _XmlSerializer.doc_from_data(
 
1012
            'UpdateStorageServiceInput',
 
1013
            [('Description', description),
 
1014
             ('Label', label, _encode_base64),
 
1015
             ('GeoReplicationEnabled', geo_replication_enabled, _lower)],
 
1016
            extended_properties)
 
1017
 
 
1018
    @staticmethod
 
1019
    def regenerate_keys_to_xml(key_type):
 
1020
        return _XmlSerializer.doc_from_data('RegenerateKeys',
 
1021
                                            [('KeyType', key_type)])
 
1022
 
 
1023
    @staticmethod
 
1024
    def update_hosted_service_to_xml(label, description, extended_properties):
 
1025
        return _XmlSerializer.doc_from_data('UpdateHostedService',
 
1026
                                            [('Label', label, _encode_base64),
 
1027
                                             ('Description', description)],
 
1028
                                            extended_properties)
 
1029
 
 
1030
    @staticmethod
 
1031
    def create_hosted_service_to_xml(service_name, label, description,
 
1032
                                     location, affinity_group,
 
1033
                                     extended_properties):
 
1034
        return _XmlSerializer.doc_from_data(
 
1035
            'CreateHostedService',
 
1036
            [('ServiceName', service_name),
 
1037
             ('Label', label, _encode_base64),
 
1038
             ('Description', description),
 
1039
             ('Location', location),
 
1040
             ('AffinityGroup', affinity_group)],
 
1041
            extended_properties)
 
1042
 
 
1043
    @staticmethod
 
1044
    def create_deployment_to_xml(name, package_url, label, configuration,
 
1045
                                 start_deployment, treat_warnings_as_error,
 
1046
                                 extended_properties):
 
1047
        return _XmlSerializer.doc_from_data(
 
1048
            'CreateDeployment',
 
1049
            [('Name', name),
 
1050
             ('PackageUrl', package_url),
 
1051
             ('Label', label, _encode_base64),
 
1052
             ('Configuration', configuration),
 
1053
             ('StartDeployment',
 
1054
             start_deployment, _lower),
 
1055
             ('TreatWarningsAsError', treat_warnings_as_error, _lower)],
 
1056
            extended_properties)
 
1057
 
 
1058
    @staticmethod
 
1059
    def swap_deployment_to_xml(production, source_deployment):
 
1060
        return _XmlSerializer.doc_from_data(
 
1061
            'Swap',
 
1062
            [('Production', production),
 
1063
             ('SourceDeployment', source_deployment)])
 
1064
 
 
1065
    @staticmethod
 
1066
    def update_deployment_status_to_xml(status):
 
1067
        return _XmlSerializer.doc_from_data(
 
1068
            'UpdateDeploymentStatus',
 
1069
            [('Status', status)])
 
1070
 
 
1071
    @staticmethod
 
1072
    def change_deployment_to_xml(configuration, treat_warnings_as_error, mode,
 
1073
                                 extended_properties):
 
1074
        return _XmlSerializer.doc_from_data(
 
1075
            'ChangeConfiguration',
 
1076
            [('Configuration', configuration),
 
1077
             ('TreatWarningsAsError', treat_warnings_as_error, _lower),
 
1078
             ('Mode', mode)],
 
1079
            extended_properties)
 
1080
 
 
1081
    @staticmethod
 
1082
    def upgrade_deployment_to_xml(mode, package_url, configuration, label,
 
1083
                                  role_to_upgrade, force, extended_properties):
 
1084
        return _XmlSerializer.doc_from_data(
 
1085
            'UpgradeDeployment',
 
1086
            [('Mode', mode),
 
1087
             ('PackageUrl', package_url),
 
1088
             ('Configuration', configuration),
 
1089
             ('Label', label, _encode_base64),
 
1090
             ('RoleToUpgrade', role_to_upgrade),
 
1091
             ('Force', force, _lower)],
 
1092
            extended_properties)
 
1093
 
 
1094
    @staticmethod
 
1095
    def rollback_upgrade_to_xml(mode, force):
 
1096
        return _XmlSerializer.doc_from_data(
 
1097
            'RollbackUpdateOrUpgrade',
 
1098
            [('Mode', mode),
 
1099
             ('Force', force, _lower)])
 
1100
 
 
1101
    @staticmethod
 
1102
    def walk_upgrade_domain_to_xml(upgrade_domain):
 
1103
        return _XmlSerializer.doc_from_data(
 
1104
            'WalkUpgradeDomain',
 
1105
            [('UpgradeDomain', upgrade_domain)])
 
1106
 
 
1107
    @staticmethod
 
1108
    def certificate_file_to_xml(data, certificate_format, password):
 
1109
        return _XmlSerializer.doc_from_data(
 
1110
            'CertificateFile',
 
1111
            [('Data', data),
 
1112
             ('CertificateFormat', certificate_format),
 
1113
             ('Password', password)])
 
1114
 
 
1115
    @staticmethod
 
1116
    def create_affinity_group_to_xml(name, label, description, location):
 
1117
        return _XmlSerializer.doc_from_data(
 
1118
            'CreateAffinityGroup',
 
1119
            [('Name', name),
 
1120
             ('Label', label, _encode_base64),
 
1121
             ('Description', description),
 
1122
             ('Location', location)])
 
1123
 
 
1124
    @staticmethod
 
1125
    def update_affinity_group_to_xml(label, description):
 
1126
        return _XmlSerializer.doc_from_data(
 
1127
            'UpdateAffinityGroup',
 
1128
            [('Label', label, _encode_base64),
 
1129
             ('Description', description)])
 
1130
 
 
1131
    @staticmethod
 
1132
    def subscription_certificate_to_xml(public_key, thumbprint, data):
 
1133
        return _XmlSerializer.doc_from_data(
 
1134
            'SubscriptionCertificate',
 
1135
            [('SubscriptionCertificatePublicKey', public_key),
 
1136
             ('SubscriptionCertificateThumbprint', thumbprint),
 
1137
             ('SubscriptionCertificateData', data)])
 
1138
 
 
1139
    @staticmethod
 
1140
    def os_image_to_xml(label, media_link, name, os):
 
1141
        return _XmlSerializer.doc_from_data(
 
1142
            'OSImage',
 
1143
            [('Label', label),
 
1144
             ('MediaLink', media_link),
 
1145
             ('Name', name),
 
1146
             ('OS', os)])
 
1147
 
 
1148
    @staticmethod
 
1149
    def data_virtual_hard_disk_to_xml(host_caching, disk_label, disk_name, lun,
 
1150
                                      logical_disk_size_in_gb, media_link,
 
1151
                                      source_media_link):
 
1152
        return _XmlSerializer.doc_from_data(
 
1153
            'DataVirtualHardDisk',
 
1154
            [('HostCaching', host_caching),
 
1155
             ('DiskLabel', disk_label),
 
1156
             ('DiskName', disk_name),
 
1157
             ('Lun', lun),
 
1158
             ('LogicalDiskSizeInGB', logical_disk_size_in_gb),
 
1159
             ('MediaLink', media_link),
 
1160
             ('SourceMediaLink', source_media_link)])
 
1161
 
 
1162
    @staticmethod
 
1163
    def disk_to_xml(has_operating_system, label, media_link, name, os):
 
1164
        return _XmlSerializer.doc_from_data(
 
1165
            'Disk',
 
1166
            [('HasOperatingSystem', has_operating_system, _lower),
 
1167
             ('Label', label),
 
1168
             ('MediaLink', media_link),
 
1169
             ('Name', name),
 
1170
             ('OS', os)])
 
1171
 
 
1172
    @staticmethod
 
1173
    def restart_role_operation_to_xml():
 
1174
        return _XmlSerializer.doc_from_xml(
 
1175
            'RestartRoleOperation',
 
1176
            '<OperationType>RestartRoleOperation</OperationType>')
 
1177
 
 
1178
    @staticmethod
 
1179
    def shutdown_role_operation_to_xml(post_shutdown_action):
 
1180
        xml = _XmlSerializer.data_to_xml(
 
1181
            [('OperationType', 'ShutdownRoleOperation'),
 
1182
             ('PostShutdownAction', post_shutdown_action)])
 
1183
        return _XmlSerializer.doc_from_xml('ShutdownRoleOperation', xml)
 
1184
 
 
1185
    @staticmethod
 
1186
    def shutdown_roles_operation_to_xml(role_names, post_shutdown_action):
 
1187
        xml = _XmlSerializer.data_to_xml(
 
1188
            [('OperationType', 'ShutdownRolesOperation')])
 
1189
        xml += '<Roles>'
 
1190
        for role_name in role_names:
 
1191
            xml += _XmlSerializer.data_to_xml([('Name', role_name)])
 
1192
        xml += '</Roles>'
 
1193
        xml += _XmlSerializer.data_to_xml(
 
1194
             [('PostShutdownAction', post_shutdown_action)])
 
1195
        return _XmlSerializer.doc_from_xml('ShutdownRolesOperation', xml)
 
1196
 
 
1197
    @staticmethod
 
1198
    def start_role_operation_to_xml():
 
1199
        return _XmlSerializer.doc_from_xml(
 
1200
            'StartRoleOperation',
 
1201
            '<OperationType>StartRoleOperation</OperationType>')
 
1202
 
 
1203
    @staticmethod
 
1204
    def start_roles_operation_to_xml(role_names):
 
1205
        xml = _XmlSerializer.data_to_xml(
 
1206
            [('OperationType', 'StartRolesOperation')])
 
1207
        xml += '<Roles>'
 
1208
        for role_name in role_names:
 
1209
            xml += _XmlSerializer.data_to_xml([('Name', role_name)])
 
1210
        xml += '</Roles>'
 
1211
        return _XmlSerializer.doc_from_xml('StartRolesOperation', xml)
 
1212
 
 
1213
    @staticmethod
 
1214
    def windows_configuration_to_xml(configuration):
 
1215
        xml = _XmlSerializer.data_to_xml(
 
1216
            [('ConfigurationSetType', configuration.configuration_set_type),
 
1217
             ('ComputerName', configuration.computer_name),
 
1218
             ('AdminPassword', configuration.admin_password),
 
1219
             ('ResetPasswordOnFirstLogon',
 
1220
              configuration.reset_password_on_first_logon,
 
1221
              _lower),
 
1222
             ('EnableAutomaticUpdates',
 
1223
              configuration.enable_automatic_updates,
 
1224
              _lower),
 
1225
             ('TimeZone', configuration.time_zone)])
 
1226
 
 
1227
        if configuration.domain_join is not None:
 
1228
            xml += '<DomainJoin>'
 
1229
            xml += '<Credentials>'
 
1230
            xml += _XmlSerializer.data_to_xml(
 
1231
                [('Domain', configuration.domain_join.credentials.domain),
 
1232
                 ('Username', configuration.domain_join.credentials.username),
 
1233
                 ('Password', configuration.domain_join.credentials.password)])
 
1234
            xml += '</Credentials>'
 
1235
            xml += _XmlSerializer.data_to_xml(
 
1236
                [('JoinDomain', configuration.domain_join.join_domain),
 
1237
                 ('MachineObjectOU',
 
1238
                  configuration.domain_join.machine_object_ou)])
 
1239
            xml += '</DomainJoin>'
 
1240
        if configuration.stored_certificate_settings is not None:
 
1241
            xml += '<StoredCertificateSettings>'
 
1242
            for cert in configuration.stored_certificate_settings:
 
1243
                xml += '<CertificateSetting>'
 
1244
                xml += _XmlSerializer.data_to_xml(
 
1245
                    [('StoreLocation', cert.store_location),
 
1246
                     ('StoreName', cert.store_name),
 
1247
                     ('Thumbprint', cert.thumbprint)])
 
1248
                xml += '</CertificateSetting>'
 
1249
            xml += '</StoredCertificateSettings>'
 
1250
        if configuration.win_rm is not None:
 
1251
            xml += '<WinRM><Listeners>'
 
1252
            for listener in configuration.win_rm.listeners:
 
1253
                xml += '<Listener>'
 
1254
                xml += _XmlSerializer.data_to_xml(
 
1255
                    [('Protocol', listener.protocol),
 
1256
                     ('CertificateThumbprint', listener.certificate_thumbprint)])
 
1257
                xml += '</Listener>'
 
1258
            xml += '</Listeners></WinRM>'
 
1259
        xml += _XmlSerializer.data_to_xml(
 
1260
            [('AdminUsername', configuration.admin_username)])
 
1261
        return xml
 
1262
 
 
1263
    @staticmethod
 
1264
    def linux_configuration_to_xml(configuration):
 
1265
        xml = _XmlSerializer.data_to_xml(
 
1266
            [('ConfigurationSetType', configuration.configuration_set_type),
 
1267
             ('HostName', configuration.host_name),
 
1268
             ('UserName', configuration.user_name),
 
1269
             ('UserPassword', configuration.user_password),
 
1270
             ('DisableSshPasswordAuthentication',
 
1271
              configuration.disable_ssh_password_authentication,
 
1272
              _lower)])
 
1273
 
 
1274
        if configuration.ssh is not None:
 
1275
            xml += '<SSH>'
 
1276
            xml += '<PublicKeys>'
 
1277
            for key in configuration.ssh.public_keys:
 
1278
                xml += '<PublicKey>'
 
1279
                xml += _XmlSerializer.data_to_xml(
 
1280
                    [('Fingerprint', key.fingerprint),
 
1281
                     ('Path', key.path)])
 
1282
                xml += '</PublicKey>'
 
1283
            xml += '</PublicKeys>'
 
1284
            xml += '<KeyPairs>'
 
1285
            for key in configuration.ssh.key_pairs:
 
1286
                xml += '<KeyPair>'
 
1287
                xml += _XmlSerializer.data_to_xml(
 
1288
                    [('Fingerprint', key.fingerprint),
 
1289
                     ('Path', key.path)])
 
1290
                xml += '</KeyPair>'
 
1291
            xml += '</KeyPairs>'
 
1292
            xml += '</SSH>'
 
1293
        return xml
 
1294
 
 
1295
    @staticmethod
 
1296
    def network_configuration_to_xml(configuration):
 
1297
        xml = _XmlSerializer.data_to_xml(
 
1298
            [('ConfigurationSetType', configuration.configuration_set_type)])
 
1299
        xml += '<InputEndpoints>'
 
1300
        for endpoint in configuration.input_endpoints:
 
1301
            xml += '<InputEndpoint>'
 
1302
            xml += _XmlSerializer.data_to_xml(
 
1303
                [('LoadBalancedEndpointSetName',
 
1304
                  endpoint.load_balanced_endpoint_set_name),
 
1305
                 ('LocalPort', endpoint.local_port),
 
1306
                 ('Name', endpoint.name),
 
1307
                 ('Port', endpoint.port)])
 
1308
 
 
1309
            if endpoint.load_balancer_probe.path or\
 
1310
                endpoint.load_balancer_probe.port or\
 
1311
                endpoint.load_balancer_probe.protocol:
 
1312
                xml += '<LoadBalancerProbe>'
 
1313
                xml += _XmlSerializer.data_to_xml(
 
1314
                    [('Path', endpoint.load_balancer_probe.path),
 
1315
                     ('Port', endpoint.load_balancer_probe.port),
 
1316
                     ('Protocol', endpoint.load_balancer_probe.protocol)])
 
1317
                xml += '</LoadBalancerProbe>'
 
1318
 
 
1319
            xml += _XmlSerializer.data_to_xml(
 
1320
                [('Protocol', endpoint.protocol),
 
1321
                 ('EnableDirectServerReturn',
 
1322
                  endpoint.enable_direct_server_return,
 
1323
                  _lower)])
 
1324
 
 
1325
            xml += '</InputEndpoint>'
 
1326
        xml += '</InputEndpoints>'
 
1327
        xml += '<SubnetNames>'
 
1328
        for name in configuration.subnet_names:
 
1329
            xml += _XmlSerializer.data_to_xml([('SubnetName', name)])
 
1330
        xml += '</SubnetNames>'
 
1331
        return xml
 
1332
 
 
1333
    @staticmethod
 
1334
    def role_to_xml(availability_set_name, data_virtual_hard_disks,
 
1335
                    network_configuration_set, os_virtual_hard_disk, role_name,
 
1336
                    role_size, role_type, system_configuration_set):
 
1337
        xml = _XmlSerializer.data_to_xml([('RoleName', role_name),
 
1338
                                          ('RoleType', role_type)])
 
1339
 
 
1340
        xml += '<ConfigurationSets>'
 
1341
 
 
1342
        if system_configuration_set is not None:
 
1343
            xml += '<ConfigurationSet>'
 
1344
            if isinstance(system_configuration_set, WindowsConfigurationSet):
 
1345
                xml += _XmlSerializer.windows_configuration_to_xml(
 
1346
                    system_configuration_set)
 
1347
            elif isinstance(system_configuration_set, LinuxConfigurationSet):
 
1348
                xml += _XmlSerializer.linux_configuration_to_xml(
 
1349
                    system_configuration_set)
 
1350
            xml += '</ConfigurationSet>'
 
1351
 
 
1352
        if network_configuration_set is not None:
 
1353
            xml += '<ConfigurationSet>'
 
1354
            xml += _XmlSerializer.network_configuration_to_xml(
 
1355
                network_configuration_set)
 
1356
            xml += '</ConfigurationSet>'
 
1357
 
 
1358
        xml += '</ConfigurationSets>'
 
1359
 
 
1360
        if availability_set_name is not None:
 
1361
            xml += _XmlSerializer.data_to_xml(
 
1362
                [('AvailabilitySetName', availability_set_name)])
 
1363
 
 
1364
        if data_virtual_hard_disks is not None:
 
1365
            xml += '<DataVirtualHardDisks>'
 
1366
            for hd in data_virtual_hard_disks:
 
1367
                xml += '<DataVirtualHardDisk>'
 
1368
                xml += _XmlSerializer.data_to_xml(
 
1369
                    [('HostCaching', hd.host_caching),
 
1370
                     ('DiskLabel', hd.disk_label),
 
1371
                     ('DiskName', hd.disk_name),
 
1372
                     ('Lun', hd.lun),
 
1373
                     ('LogicalDiskSizeInGB', hd.logical_disk_size_in_gb),
 
1374
                     ('MediaLink', hd.media_link)])
 
1375
                xml += '</DataVirtualHardDisk>'
 
1376
            xml += '</DataVirtualHardDisks>'
 
1377
 
 
1378
        if os_virtual_hard_disk is not None:
 
1379
            xml += '<OSVirtualHardDisk>'
 
1380
            xml += _XmlSerializer.data_to_xml(
 
1381
                [('HostCaching', os_virtual_hard_disk.host_caching),
 
1382
                 ('DiskLabel', os_virtual_hard_disk.disk_label),
 
1383
                 ('DiskName', os_virtual_hard_disk.disk_name),
 
1384
                 ('MediaLink', os_virtual_hard_disk.media_link),
 
1385
                 ('SourceImageName', os_virtual_hard_disk.source_image_name)])
 
1386
            xml += '</OSVirtualHardDisk>'
 
1387
 
 
1388
        if role_size is not None:
 
1389
            xml += _XmlSerializer.data_to_xml([('RoleSize', role_size)])
 
1390
 
 
1391
        return xml
 
1392
 
 
1393
    @staticmethod
 
1394
    def add_role_to_xml(role_name, system_configuration_set,
 
1395
                        os_virtual_hard_disk, role_type,
 
1396
                        network_configuration_set, availability_set_name,
 
1397
                        data_virtual_hard_disks, role_size):
 
1398
        xml = _XmlSerializer.role_to_xml(
 
1399
            availability_set_name,
 
1400
            data_virtual_hard_disks,
 
1401
            network_configuration_set,
 
1402
            os_virtual_hard_disk,
 
1403
            role_name,
 
1404
            role_size,
 
1405
            role_type,
 
1406
            system_configuration_set)
 
1407
        return _XmlSerializer.doc_from_xml('PersistentVMRole', xml)
 
1408
 
 
1409
    @staticmethod
 
1410
    def update_role_to_xml(role_name, os_virtual_hard_disk, role_type,
 
1411
                           network_configuration_set, availability_set_name,
 
1412
                           data_virtual_hard_disks, role_size):
 
1413
        xml = _XmlSerializer.role_to_xml(
 
1414
            availability_set_name,
 
1415
            data_virtual_hard_disks,
 
1416
            network_configuration_set,
 
1417
            os_virtual_hard_disk,
 
1418
            role_name,
 
1419
            role_size,
 
1420
            role_type,
 
1421
            None)
 
1422
        return _XmlSerializer.doc_from_xml('PersistentVMRole', xml)
 
1423
 
 
1424
    @staticmethod
 
1425
    def capture_role_to_xml(post_capture_action, target_image_name,
 
1426
                            target_image_label, provisioning_configuration):
 
1427
        xml = _XmlSerializer.data_to_xml(
 
1428
            [('OperationType', 'CaptureRoleOperation'),
 
1429
             ('PostCaptureAction', post_capture_action)])
 
1430
 
 
1431
        if provisioning_configuration is not None:
 
1432
            xml += '<ProvisioningConfiguration>'
 
1433
            if isinstance(provisioning_configuration, WindowsConfigurationSet):
 
1434
                xml += _XmlSerializer.windows_configuration_to_xml(
 
1435
                    provisioning_configuration)
 
1436
            elif isinstance(provisioning_configuration, LinuxConfigurationSet):
 
1437
                xml += _XmlSerializer.linux_configuration_to_xml(
 
1438
                    provisioning_configuration)
 
1439
            xml += '</ProvisioningConfiguration>'
 
1440
 
 
1441
        xml += _XmlSerializer.data_to_xml(
 
1442
            [('TargetImageLabel', target_image_label),
 
1443
             ('TargetImageName', target_image_name)])
 
1444
 
 
1445
        return _XmlSerializer.doc_from_xml('CaptureRoleOperation', xml)
 
1446
 
 
1447
    @staticmethod
 
1448
    def virtual_machine_deployment_to_xml(deployment_name, deployment_slot,
 
1449
                                          label, role_name,
 
1450
                                          system_configuration_set,
 
1451
                                          os_virtual_hard_disk, role_type,
 
1452
                                          network_configuration_set,
 
1453
                                          availability_set_name,
 
1454
                                          data_virtual_hard_disks, role_size,
 
1455
                                          virtual_network_name):
 
1456
        xml = _XmlSerializer.data_to_xml([('Name', deployment_name),
 
1457
                                          ('DeploymentSlot', deployment_slot),
 
1458
                                          ('Label', label)])
 
1459
        xml += '<RoleList>'
 
1460
        xml += '<Role>'
 
1461
        xml += _XmlSerializer.role_to_xml(
 
1462
            availability_set_name,
 
1463
            data_virtual_hard_disks,
 
1464
            network_configuration_set,
 
1465
            os_virtual_hard_disk,
 
1466
            role_name,
 
1467
            role_size,
 
1468
            role_type,
 
1469
            system_configuration_set)
 
1470
        xml += '</Role>'
 
1471
        xml += '</RoleList>'
 
1472
 
 
1473
        if virtual_network_name is not None:
 
1474
            xml += _XmlSerializer.data_to_xml(
 
1475
                [('VirtualNetworkName', virtual_network_name)])
 
1476
 
 
1477
        return _XmlSerializer.doc_from_xml('Deployment', xml)
 
1478
 
 
1479
    @staticmethod
 
1480
    def data_to_xml(data):
 
1481
        '''Creates an xml fragment from the specified data.
 
1482
           data: Array of tuples, where first: xml element name
 
1483
                                        second: xml element text
 
1484
                                        third: conversion function
 
1485
        '''
 
1486
        xml = ''
 
1487
        for element in data:
 
1488
            name = element[0]
 
1489
            val = element[1]
 
1490
            if len(element) > 2:
 
1491
                converter = element[2]
 
1492
            else:
 
1493
                converter = None
 
1494
 
 
1495
            if val is not None:
 
1496
                if converter is not None:
 
1497
                    text = _str(converter(_str(val)))
 
1498
                else:
 
1499
                    text = _str(val)
 
1500
 
 
1501
                xml += ''.join(['<', name, '>', text, '</', name, '>'])
 
1502
        return xml
 
1503
 
 
1504
    @staticmethod
 
1505
    def doc_from_xml(document_element_name, inner_xml):
 
1506
        '''Wraps the specified xml in an xml root element with default azure
 
1507
        namespaces'''
 
1508
        xml = ''.join(['<', document_element_name,
 
1509
                      ' xmlns:i="http://www.w3.org/2001/XMLSchema-instance"',
 
1510
                      ' xmlns="http://schemas.microsoft.com/windowsazure">'])
 
1511
        xml += inner_xml
 
1512
        xml += ''.join(['</', document_element_name, '>'])
 
1513
        return xml
 
1514
 
 
1515
    @staticmethod
 
1516
    def doc_from_data(document_element_name, data, extended_properties=None):
 
1517
        xml = _XmlSerializer.data_to_xml(data)
 
1518
        if extended_properties is not None:
 
1519
            xml += _XmlSerializer.extended_properties_dict_to_xml_fragment(
 
1520
                extended_properties)
 
1521
        return _XmlSerializer.doc_from_xml(document_element_name, xml)
 
1522
 
 
1523
    @staticmethod
 
1524
    def extended_properties_dict_to_xml_fragment(extended_properties):
 
1525
        xml = ''
 
1526
        if extended_properties is not None and len(extended_properties) > 0:
 
1527
            xml += '<ExtendedProperties>'
 
1528
            for key, val in extended_properties.items():
 
1529
                xml += ''.join(['<ExtendedProperty>',
 
1530
                                '<Name>',
 
1531
                                _str(key),
 
1532
                                '</Name>',
 
1533
                               '<Value>',
 
1534
                               _str(val),
 
1535
                               '</Value>',
 
1536
                               '</ExtendedProperty>'])
 
1537
            xml += '</ExtendedProperties>'
 
1538
        return xml
 
1539
 
 
1540
 
 
1541
def _parse_bool(value):
 
1542
    if value.lower() == 'true':
 
1543
        return True
 
1544
    return False
 
1545
 
 
1546
 
 
1547
class _ServiceBusManagementXmlSerializer(object):
 
1548
 
 
1549
    @staticmethod
 
1550
    def namespace_to_xml(region):
 
1551
        '''Converts a service bus namespace description to xml
 
1552
 
 
1553
        The xml format:
 
1554
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
 
1555
<entry xmlns="http://www.w3.org/2005/Atom">
 
1556
    <content type="application/xml">
 
1557
        <NamespaceDescription
 
1558
            xmlns="http://schemas.microsoft.com/netservices/2010/10/servicebus/connect">
 
1559
            <Region>West US</Region>
 
1560
        </NamespaceDescription>
 
1561
    </content>
 
1562
</entry>
 
1563
        '''
 
1564
        body = '<NamespaceDescription xmlns="http://schemas.microsoft.com/netservices/2010/10/servicebus/connect">'
 
1565
        body += ''.join(['<Region>', region, '</Region>'])
 
1566
        body += '</NamespaceDescription>'
 
1567
 
 
1568
        return _create_entry(body)
 
1569
 
 
1570
    @staticmethod
 
1571
    def xml_to_namespace(xmlstr):
 
1572
        '''Converts xml response to service bus namespace
 
1573
 
 
1574
        The xml format for namespace:
 
1575
<entry>
 
1576
<id>uuid:00000000-0000-0000-0000-000000000000;id=0000000</id>
 
1577
<title type="text">myunittests</title>
 
1578
<updated>2012-08-22T16:48:10Z</updated>
 
1579
<content type="application/xml">
 
1580
    <NamespaceDescription
 
1581
        xmlns="http://schemas.microsoft.com/netservices/2010/10/servicebus/connect"
 
1582
        xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
 
1583
    <Name>myunittests</Name>
 
1584
    <Region>West US</Region>
 
1585
    <DefaultKey>0000000000000000000000000000000000000000000=</DefaultKey>
 
1586
    <Status>Active</Status>
 
1587
    <CreatedAt>2012-08-22T16:48:10.217Z</CreatedAt>
 
1588
    <AcsManagementEndpoint>https://myunittests-sb.accesscontrol.windows.net/</AcsManagementEndpoint>
 
1589
    <ServiceBusEndpoint>https://myunittests.servicebus.windows.net/</ServiceBusEndpoint>
 
1590
    <ConnectionString>Endpoint=sb://myunittests.servicebus.windows.net/;SharedSecretIssuer=owner;SharedSecretValue=0000000000000000000000000000000000000000000=</ConnectionString>
 
1591
    <SubscriptionId>00000000000000000000000000000000</SubscriptionId>
 
1592
    <Enabled>true</Enabled>
 
1593
    </NamespaceDescription>
 
1594
</content>
 
1595
</entry>
 
1596
        '''
 
1597
        xmldoc = minidom.parseString(xmlstr)
 
1598
        namespace = ServiceBusNamespace()
 
1599
 
 
1600
        mappings = (
 
1601
            ('Name', 'name', None),
 
1602
            ('Region', 'region', None),
 
1603
            ('DefaultKey', 'default_key', None),
 
1604
            ('Status', 'status', None),
 
1605
            ('CreatedAt', 'created_at', None),
 
1606
            ('AcsManagementEndpoint', 'acs_management_endpoint', None),
 
1607
            ('ServiceBusEndpoint', 'servicebus_endpoint', None),
 
1608
            ('ConnectionString', 'connection_string', None),
 
1609
            ('SubscriptionId', 'subscription_id', None),
 
1610
            ('Enabled', 'enabled', _parse_bool),
 
1611
        )
 
1612
 
 
1613
        for desc in _get_children_from_path(xmldoc,
 
1614
                                            'entry',
 
1615
                                            'content',
 
1616
                                            'NamespaceDescription'):
 
1617
            for xml_name, field_name, conversion_func in mappings:
 
1618
                node_value = _get_first_child_node_value(desc, xml_name)
 
1619
                if node_value is not None:
 
1620
                    if conversion_func is not None:
 
1621
                        node_value = conversion_func(node_value)
 
1622
                    setattr(namespace, field_name, node_value)
 
1623
 
 
1624
        return namespace
 
1625
 
 
1626
    @staticmethod
 
1627
    def xml_to_region(xmlstr):
 
1628
        '''Converts xml response to service bus region
 
1629
 
 
1630
        The xml format for region:
 
1631
<entry>
 
1632
<id>uuid:157c311f-081f-4b4a-a0ba-a8f990ffd2a3;id=1756759</id>
 
1633
<title type="text"></title>
 
1634
<updated>2013-04-10T18:25:29Z</updated>
 
1635
<content type="application/xml">
 
1636
    <RegionCodeDescription
 
1637
        xmlns="http://schemas.microsoft.com/netservices/2010/10/servicebus/connect"
 
1638
        xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
 
1639
    <Code>East Asia</Code>
 
1640
    <FullName>East Asia</FullName>
 
1641
    </RegionCodeDescription>
 
1642
</content>
 
1643
</entry>
 
1644
          '''
 
1645
        xmldoc = minidom.parseString(xmlstr)
 
1646
        region = ServiceBusRegion()
 
1647
 
 
1648
        for desc in _get_children_from_path(xmldoc, 'entry', 'content',
 
1649
                                            'RegionCodeDescription'):
 
1650
            node_value = _get_first_child_node_value(desc, 'Code')
 
1651
            if node_value is not None:
 
1652
                region.code = node_value
 
1653
            node_value = _get_first_child_node_value(desc, 'FullName')
 
1654
            if node_value is not None:
 
1655
                region.fullname = node_value
 
1656
 
 
1657
        return region
 
1658
 
 
1659
    @staticmethod
 
1660
    def xml_to_namespace_availability(xmlstr):
 
1661
        '''Converts xml response to service bus namespace availability
 
1662
 
 
1663
        The xml format:
 
1664
<?xml version="1.0" encoding="utf-8"?>
 
1665
<entry xmlns="http://www.w3.org/2005/Atom">
 
1666
    <id>uuid:9fc7c652-1856-47ab-8d74-cd31502ea8e6;id=3683292</id>
 
1667
    <title type="text"></title>
 
1668
    <updated>2013-04-16T03:03:37Z</updated>
 
1669
    <content type="application/xml">
 
1670
        <NamespaceAvailability
 
1671
            xmlns="http://schemas.microsoft.com/netservices/2010/10/servicebus/connect"
 
1672
            xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
 
1673
            <Result>false</Result>
 
1674
        </NamespaceAvailability>
 
1675
    </content>
 
1676
</entry>
 
1677
        '''
 
1678
        xmldoc = minidom.parseString(xmlstr)
 
1679
        availability = AvailabilityResponse()
 
1680
 
 
1681
        for desc in _get_children_from_path(xmldoc, 'entry', 'content',
 
1682
                                            'NamespaceAvailability'):
 
1683
            node_value = _get_first_child_node_value(desc, 'Result')
 
1684
            if node_value is not None:
 
1685
                availability.result = _parse_bool(node_value)
 
1686
 
 
1687
        return availability
 
1688
 
 
1689
from azure.servicemanagement.servicemanagementservice import (
 
1690
    ServiceManagementService)
 
1691
from azure.servicemanagement.servicebusmanagementservice import (
 
1692
    ServiceBusManagementService)