1
|
|
|
from st2actions.runners.pythonrunner import Action |
2
|
|
|
|
3
|
|
|
from libcloud.compute.providers import Provider as ComputeProvider |
4
|
|
|
from libcloud.compute.providers import get_driver as get_compute_driver |
5
|
|
|
from libcloud.storage.providers import Provider as StorageProvider |
6
|
|
|
from libcloud.storage.providers import get_driver as get_storage_driver |
7
|
|
|
|
8
|
|
|
from msrestazure.azure_active_directory import ServicePrincipalCredentials |
9
|
|
|
|
10
|
|
|
__all__ = [ |
11
|
|
|
'AzureBaseComputeAction', |
12
|
|
|
'AzureBaseStorageAction', |
13
|
|
|
'AzureBaseResourceManagerAction' |
14
|
|
|
] |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
class AzureBaseComputeAction(Action): |
18
|
|
|
def __init__(self, config): |
19
|
|
|
super(AzureBaseComputeAction, self).__init__(config=config) |
20
|
|
|
|
21
|
|
|
config = self.config['compute'] |
22
|
|
|
subscription_id = config['subscription_id'] |
23
|
|
|
key_file = config['cert_file'] |
24
|
|
|
self._driver = self._get_driver(subscription_id=subscription_id, |
25
|
|
|
key_file=key_file) |
26
|
|
|
|
27
|
|
|
def _get_driver(self, subscription_id, key_file): |
28
|
|
|
cls = get_compute_driver(ComputeProvider.AZURE) |
29
|
|
|
driver = cls(subscription_id=subscription_id, key_file=key_file) |
30
|
|
|
return driver |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
class AzureBaseStorageAction(Action): |
34
|
|
|
def __init__(self, config): |
35
|
|
|
super(AzureBaseStorageAction, self).__init__(config=config) |
36
|
|
|
|
37
|
|
|
config = self.config['storage'] |
38
|
|
|
name = config['name'] |
39
|
|
|
access_key = config['access_key'] |
40
|
|
|
self._driver = self._get_driver(name=name, access_key=access_key) |
41
|
|
|
|
42
|
|
|
def _get_driver(self, name, access_key): |
43
|
|
|
cls = get_storage_driver(StorageProvider.AZURE_BLOBS) |
44
|
|
|
driver = cls(key=name, secret=access_key) |
45
|
|
|
return driver |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
class AzureBaseResourceManagerAction(Action): |
49
|
|
|
def __init__(self, config): |
50
|
|
|
super(AzureBaseResourceManagerAction, self).__init__(config=config) |
51
|
|
|
|
52
|
|
|
resource_config = self.config['resource_manager'] |
53
|
|
|
self.credentials = ServicePrincipalCredentials( |
54
|
|
|
client_id=resource_config['client_id'], |
55
|
|
|
secret=resource_config['secret'], |
56
|
|
|
tenant=resource_config['tenant'] |
57
|
|
|
) |
58
|
|
|
|