本文整理汇总了Python中c7n_azure.session.Session类的典型用法代码示例。如果您正苦于以下问题:Python Session类的具体用法?Python Session怎么用?Python Session使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Session类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_add_or_update_single_tag
def test_add_or_update_single_tag(self):
"""Verifies we can add a new tag to a VM and not modify
an existing tag on that resource
"""
p = self.load_policy({
'name': 'test-azure-tag',
'resource': 'azure.vm',
'filters': [
{'type': 'value',
'key': 'name',
'op': 'eq',
'value_type': 'normalize',
'value': 'cctestvm'}
],
'actions': [
{'type': 'tag',
'tag': 'tag1',
'value': 'value1'}
],
})
p.run()
# verify that the a new tag is added without modifying existing tags
s = Session()
client = s.client('azure.mgmt.compute.ComputeManagementClient')
vm = client.virtual_machines.get('test_vm', 'cctestvm')
self.assertEqual(vm.tags, {'tag1': 'value1', 'testtag': 'testvalue'})
示例2: augment
def augment(self, resources):
s = Session(resource='https://graph.windows.net')
graph_client = GraphRbacManagementClient(s.get_credentials(), s.get_tenant_id())
object_ids = list(set(
resource['properties']['principalId'] for resource in resources
if resource['properties']['principalId']))
object_params = GetObjectsParameters(
include_directory_object_references=True,
object_ids=object_ids)
aad_objects = graph_client.objects.get_objects_by_object_ids(object_params)
try:
principal_dics = {aad_object.object_id: aad_object for aad_object in aad_objects}
for resource in resources:
graph_resource = principal_dics[resource['properties']['principalId']]
resource['principalName'] = self.get_principal_name(graph_resource)
resource['displayName'] = graph_resource.display_name
resource['aadType'] = graph_resource.object_type
except CloudError:
log.warning('Credentials not authorized for access to read from Microsoft Graph. \n '
'Can not query on principalName, displayName, or aadType. \n'
)
return resources
示例3: test_get_functions_auth_string_overrides
def test_get_functions_auth_string_overrides(self):
with patch('azure.common.credentials.ServicePrincipalCredentials.__init__',
autospec=True, return_value=None):
with patch.dict(os.environ,
{
constants.ENV_TENANT_ID: 'tenant',
constants.ENV_SUB_ID: 'ea42f556-5106-4743-99b0-c129bfa71a47',
constants.ENV_CLIENT_ID: 'client',
constants.ENV_CLIENT_SECRET: 'secret',
constants.ENV_FUNCTION_TENANT_ID: 'functiontenant',
constants.ENV_FUNCTION_SUB_ID: '000000-5106-4743-99b0-c129bfa71a47',
constants.ENV_FUNCTION_CLIENT_ID: 'functionclient',
constants.ENV_FUNCTION_CLIENT_SECRET: 'functionsecret'
}, clear=True):
s = Session()
auth = s.get_functions_auth_string('000000-5106-4743-99b0-c129bfa71a47')
expected = """{
"credentials": {
"client_id": "functionclient",
"secret": "functionsecret",
"tenant": "functiontenant"
},
"subscription": "000000-5106-4743-99b0-c129bfa71a47"
}"""
self.assertEqual(json.loads(auth), json.loads(expected))
示例4: test_auto_tag_add_creator_tag
def test_auto_tag_add_creator_tag(self, utcnow_mock):
"""Adds CreatorEmail to a resource group
"""
p = self.load_policy({
'name': 'test-azure-tag',
'resource': 'azure.resourcegroup',
'filters': [
{'type': 'value',
'key': 'name',
'op': 'eq',
'value_type': 'normalize',
'value': 'test_vm'}
],
'actions': [
{'type': 'auto-tag-user',
'tag': 'CreatorEmail'},
],
})
p.run()
# verify CreatorEmail tag set
s = Session()
client = s.client('azure.mgmt.resource.ResourceManagementClient')
rg = [rg for rg in client.resource_groups.list() if rg.name == 'test_vm'][0]
self.assertTrue(re.match(self.EMAIL_REGEX, rg.tags['CreatorEmail']))
示例5: test_api_version
def test_api_version(self):
"""Verify we retrieve the correct API version for a resource type"""
s = Session()
client = s.client('azure.mgmt.resource.ResourceManagementClient')
resource = next(client.resources.list())
self.assertTrue(re.match('\\d{4}-\\d{2}-\\d{2}',
s.resource_api_version(resource.id)) is not None)
示例6: test_tag_trim_does_nothing_if_space_available
def test_tag_trim_does_nothing_if_space_available(self):
"""Verifies tag trim returns without trimming tags
if the resource has space equal to or greater than
the space value.
"""
s = Session()
client = s.client('azure.mgmt.compute.ComputeManagementClient')
vm = client.virtual_machines.get('test_vm', 'cctestvm')
start_tags = vm.tags
# verify there is at least 1 space for a tag
self.assertLess(len(start_tags), 15)
# trim for space for 1 tag
p = self.load_policy({
'name': 'test-azure-tag',
'resource': 'azure.vm',
'filters': [
{'type': 'value',
'key': 'name',
'op': 'eq',
'value_type': 'normalize',
'value': 'cctestvm'}
],
'actions': [
{'type': 'tag-trim',
'space': 1}
],
})
p.run()
# verify that tags are unchanged
vm = client.virtual_machines.get('test_vm', 'cctestvm')
self.assertEqual(vm.tags, start_tags)
示例7: setup_account
def setup_account():
# Find actual name of storage account provisioned in our test environment
s = Session()
client = s.client('azure.mgmt.storage.StorageManagementClient')
accounts = list(client.storage_accounts.list())
matching_account = [a for a in accounts if a.name.startswith("cctstorage")]
return matching_account[0]
示例8: test_initialize_session_auth_file
def test_initialize_session_auth_file(self):
with patch('azure.common.credentials.ServicePrincipalCredentials.__init__',
autospec=True, return_value=None):
s = Session(authorization_file=self.authorization_file)
self.assertIs(type(s.get_credentials()), ServicePrincipalCredentials)
self.assertEqual(s.get_subscription_id(), DEFAULT_SUBSCRIPTION_ID)
self.assertEqual(s.get_tenant_id(), 'tenant')
示例9: test_get_client_overrides
def test_get_client_overrides(self, mock):
# Reload the module to re-import patched function
reload(sys.modules['c7n_azure.session'])
s = Session()
client = s.client('azure.mgmt.resource.ResourceManagementClient')
self.assertFalse(client._client.config.retry_policy.policy.respect_retry_after_header)
self.assertIsNotNone(client._client.orig_send)
client._client.send()
self.assertTrue(mock.called)
示例10: test_remove_tags
def test_remove_tags(self):
"""Verifies we can delete multiple tags from a resource
group without modifying existing tags.
"""
p = self.load_policy({
'name': 'test-azure-tag',
'resource': 'azure.resourcegroup',
'filters': [
{'type': 'value',
'key': 'name',
'op': 'eq',
'value_type': 'normalize',
'value': 'test_vm'}
],
'actions': [
{'type': 'tag',
'tags': {'pre-existing-1': 'to-keep', 'pre-existing-2': 'to-keep',
'added-1': 'to-delete', 'added-2': 'to-delete'}},
],
})
p.run()
# verify initial tag set
s = Session()
client = s.client('azure.mgmt.resource.ResourceManagementClient')
rg = [rg for rg in client.resource_groups.list() if rg.name == 'test_vm'][0]
start_tags = rg.tags
self.assertTrue('pre-existing-1' in start_tags)
self.assertTrue('pre-existing-2' in start_tags)
self.assertTrue('added-1' in start_tags)
self.assertTrue('added-2' in start_tags)
p = self.load_policy({
'name': 'test-azure-remove-tag',
'resource': 'azure.resourcegroup',
'filters': [
{'type': 'value',
'key': 'name',
'op': 'eq',
'value_type': 'normalize',
'value': 'test_vm'}
],
'actions': [
{'type': 'untag',
'tags': ['added-1', 'added-2']}
],
})
p.run()
# verify tags removed and pre-existing tags not removed
rg = [rg for rg in client.resource_groups.list() if rg.name == 'test_vm'][0] # NOQA
end_tags = rg.tags
self.assertTrue('pre-existing-1' in end_tags)
self.assertTrue('pre-existing-2' in end_tags)
self.assertTrue('added-1' not in end_tags)
self.assertTrue('added-2' not in end_tags)
示例11: test_tag_trim_space_0_removes_all_tags_but_preserve
def test_tag_trim_space_0_removes_all_tags_but_preserve(self):
"""Verifies tag trim removes all other tags but tags
listed in preserve
"""
# Add tags to trim
p = self.load_policy({
'name': 'test-azure-tag',
'resource': 'azure.vm',
'filters': [
{'type': 'value',
'key': 'name',
'op': 'eq',
'value_type': 'normalize',
'value': 'cctestvm'}
],
'actions': [
{'type': 'tag',
'tags': {'tag-to-trim1': 'value1', 'tag-to-trim2': 'value2',
'tag-to-trim3': 'value3'}}
],
})
p.run()
# verify initial tags contain more than testtag
s = Session()
client = s.client('azure.mgmt.compute.ComputeManagementClient')
vm = client.virtual_machines.get('test_vm', 'cctestvm')
self.assertTrue('tag-to-trim1' in vm.tags)
self.assertTrue('tag-to-trim2' in vm.tags)
self.assertTrue('tag-to-trim3' in vm.tags)
self.assertTrue('testtag' in vm.tags)
p = self.load_policy({
'name': 'test-azure-tag',
'resource': 'azure.vm',
'filters': [
{'type': 'value',
'key': 'name',
'op': 'eq',
'value_type': 'normalize',
'value': 'cctestvm'}
],
'actions': [
{'type': 'tag-trim',
'space': 0,
'preserve': ['testtag']
}
],
})
p.run()
# verify all tags trimmed but testtag
vm = client.virtual_machines.get('test_vm', 'cctestvm')
self.assertEqual(vm.tags, {'testtag': 'testvalue'})
示例12: test_initialize_session_token
def test_initialize_session_token(self):
with patch.dict(os.environ,
{
constants.ENV_ACCESS_TOKEN: 'token',
constants.ENV_SUB_ID: 'ea42f556-5106-4743-99b0-c129bfa71a47'
}, clear=True):
s = Session()
self.assertIs(type(s.get_credentials()), BasicTokenAuthentication)
self.assertEqual(s.get_subscription_id(), 'ea42f556-5106-4743-99b0-c129bfa71a47')
示例13: test_initialize_session_token
def test_initialize_session_token(self):
with patch.dict(os.environ,
{
constants.ENV_ACCESS_TOKEN: 'token',
constants.ENV_SUB_ID: DEFAULT_SUBSCRIPTION_ID
}, clear=True):
s = Session()
self.assertIs(type(s.get_credentials()), BasicTokenAuthentication)
self.assertEqual(s.get_subscription_id(), DEFAULT_SUBSCRIPTION_ID)
示例14: test_tag_trim_removes_tags_for_space
def test_tag_trim_removes_tags_for_space(self):
"""Verifies tag trim removes tags when the space value
and number of tags on the resource are greater than the max
tag value (15)
"""
# Add tags to trim
p = self.load_policy({
'name': 'test-azure-tag',
'resource': 'azure.vm',
'filters': [
{'type': 'value',
'key': 'name',
'op': 'eq',
'value_type': 'normalize',
'value': 'cctestvm'}
],
'actions': [
{'type': 'tag',
'tags': {'tag-to-trim1': 'value1', 'tag-to-trim2': 'value2'}}
],
})
p.run()
# verify more than 1 tag on resource
s = Session()
client = s.client('azure.mgmt.compute.ComputeManagementClient')
vm = client.virtual_machines.get('test_vm', 'cctestvm')
self.assertTrue(len(vm.tags) > 1)
p = self.load_policy({
'name': 'test-azure-tag',
'resource': 'azure.vm',
'filters': [
{'type': 'value',
'key': 'name',
'op': 'eq',
'value_type': 'normalize',
'value': 'cctestvm'}
],
'actions': [
{'type': 'tag-trim',
'space': 14,
'preserve': ['testtag']
}
],
})
p.run()
# verify that tags were trimmed to
# have 14 spaces and 1 preserved
vm = client.virtual_machines.get('test_vm', 'cctestvm')
self.assertEqual(len(vm.tags), 1)
示例15: test_initialize_msi_auth_system
def test_initialize_msi_auth_system(self):
with patch('msrestazure.azure_active_directory.MSIAuthentication.__init__',
autospec=True, return_value=None):
with patch.dict(os.environ,
{
constants.ENV_USE_MSI: 'true',
constants.ENV_SUB_ID: 'ea42f556-5106-4743-99b0-c129bfa71a47'
}, clear=True):
s = Session()
self.assertIs(type(s.get_credentials()), MSIAuthentication)
self.assertEqual(s.get_subscription_id(), 'ea42f556-5106-4743-99b0-c129bfa71a47')