def resolve_storage_source(self, source): blob_uri = None disk = None snapshot = None # blob URI can only be given by str if isinstance(source, str) and source.lower().endswith('.vhd'): blob_uri = source return (blob_uri, disk, snapshot) tokenize = dict() if isinstance(source, dict): tokenize = source elif isinstance(source, str): tokenize = parse_resource_id(source) else: self.fail( "source parameter should be in type string or dictionary") if tokenize.get('type') == 'disks': disk = format_resource_id( tokenize['name'], tokenize.get('subscription_id') or self.subscription_id, 'Microsoft.Compute', 'disks', tokenize.get('resource_group') or self.resource_group) return (blob_uri, disk, snapshot) if tokenize.get('type') == 'snapshots': snapshot = format_resource_id( tokenize['name'], tokenize.get('subscription_id') or self.subscription_id, 'Microsoft.Compute', 'snapshots', tokenize.get('resource_group') or self.resource_group) return (blob_uri, disk, snapshot) # not a disk or snapshots if 'type' in tokenize: return (blob_uri, disk, snapshot) # source can be name of snapshot or disk snapshot_instance = self.get_snapshot( tokenize.get('resource_group') or self.resource_group, tokenize['name']) if snapshot_instance: snapshot = snapshot_instance.id return (blob_uri, disk, snapshot) disk_instance = self.get_disk( tokenize.get('resource_group') or self.resource_group, tokenize['name']) if disk_instance: disk = disk_instance.id return (blob_uri, disk, snapshot)
def create_or_update_vnet_peering(self): ''' Creates or Update Azure Virtual Network Peering. :return: deserialized Azure Virtual Network Peering instance state dictionary ''' self.log("Creating or Updating the Azure Virtual Network Peering {0}". format(self.name)) vnet_id = format_resource_id(self.virtual_network['name'], self.subscription_id, 'Microsoft.Network', 'virtualNetworks', self.virtual_network['resource_group']) peering = self.network_models.VirtualNetworkPeering( id=vnet_id, name=self.name, remote_virtual_network=self.network_models.SubResource( id=self.remote_virtual_network), allow_virtual_network_access=self.allow_virtual_network_access, allow_gateway_transit=self.allow_gateway_transit, allow_forwarded_traffic=self.allow_forwarded_traffic, use_remote_gateways=self.use_remote_gateways) try: response = self.network_client.virtual_network_peerings.create_or_update( self.resource_group, self.virtual_network['name'], self.name, peering) if isinstance(response, LROPoller): response = self.get_poller_result(response) return vnetpeering_to_dict(response) except CloudError as exc: self.fail( "Error creating Azure Virtual Network Peering: {0}.".format( exc.message))
def get_public_ip_address_instance(self, id): """Get a reference to the public ip address resource""" self.log('Fetching public ip address {0}'.format(id)) resource_id = format_resource_id(id, self.subscription_id, 'Microsoft.Network', 'publicIPAddresses', self.resource_group) return self.network_models.PublicIPAddress(id=resource_id)
def parse_vn_id(self, vn): vn_dict = self.parse_resource_to_dict(vn) if not isinstance( vn, dict) else vn return format_resource_id(val=vn_dict['name'], subscription_id=vn_dict.get('subscription') or self.subscription_id, namespace='Microsoft.Network', types='virtualNetworks', resource_group=vn_dict.get('resource_group') or self.resource_group)
def format_vnet_id(self, vnet): if not vnet: return vnet if isinstance( vnet, dict) and vnet.get('name') and vnet.get('resource_group'): remote_vnet_id = format_resource_id(vnet['name'], self.subscription_id, 'Microsoft.Network', 'virtualNetworks', vnet['resource_group']) elif isinstance(vnet, str): if is_valid_resource_id(vnet): remote_vnet_id = vnet else: remote_vnet_id = format_resource_id(vnet, self.subscription_id, 'Microsoft.Network', 'virtualNetworks', self.resource_group) else: self.fail( "remote_virtual_network could be a valid resource id, dict of name and resource_group, name of virtual network in same resource group." ) return remote_vnet_id
def get_source_vm(self): # self.resource can be a vm (id/name/dict), or not a vm. return the vm iff it is an existing vm. resource = dict() if isinstance(self.source, dict): if self.source.get('type') != 'virtual_machines': return None resource = dict(type='virtualMachines', name=self.source['name'], resource_group=self.source.get('resource_group') or self.resource_group) elif isinstance(self.source, str): vm_resource_id = format_resource_id(self.source, self.subscription_id, 'Microsoft.Compute', 'virtualMachines', self.resource_group) resource = parse_resource_id(vm_resource_id) else: self.fail( "Unsupported type of source parameter, please give string or dictionary" ) return self.get_vm(resource['resource_group'], resource['name'] ) if resource['type'] == 'virtualMachines' else None
def exec_module(self, **kwargs): for key in list(self.module_arg_spec.keys()) + ['tags']: setattr(self, key, kwargs[key]) results = None changed = False self.log('Fetching auto scale settings {0}'.format(self.name)) results = self.get_auto_scale() if results and self.state == 'absent': # delete changed = True if not self.check_mode: self.delete_auto_scale() elif self.state == 'present': if not self.location: # Set default location resource_group = self.get_resource_group(self.resource_group) self.location = resource_group.location resource_id = self.target if isinstance(self.target, dict): resource_id = format_resource_id( val=self.target['name'], subscription_id=self.target.get('subscription_id') or self.subscription_id, namespace=self.target['namespace'], types=self.target['types'], resource_group=self.target.get('resource_group') or self.resource_group) self.target = resource_id resource_name = self.name def create_rule_instance(params): rule = params.copy() rule['metric_resource_uri'] = rule.get('metric_resource_uri', self.target) rule['time_grain'] = timedelta( minutes=rule.get('time_grain', 0)) rule['time_window'] = timedelta( minutes=rule.get('time_window', 0)) rule['cooldown'] = timedelta(minutes=rule.get('cooldown', 0)) return ScaleRule(metric_trigger=MetricTrigger(**rule), scale_action=ScaleAction(**rule)) profiles = [ AutoscaleProfile( name=p.get('name'), capacity=ScaleCapacity(minimum=p.get('min_count'), maximum=p.get('max_count'), default=p.get('count')), rules=[ create_rule_instance(r) for r in p.get('rules') or [] ], fixed_date=TimeWindow( time_zone=p.get('fixed_date_timezone'), start=p.get('fixed_date_start'), end=p.get('fixed_date_end')) if p.get('fixed_date_timezone') else None, recurrence=Recurrence( frequency=p.get('recurrence_frequency'), schedule=(RecurrentSchedule( time_zone=p.get('recurrence_timezone'), days=p.get('recurrence_days'), hours=p.get('recurrence_hours'), minutes=p.get('recurrence_mins')))) if p.get('recurrence_frequency') and p['recurrence_frequency'] != 'None' else None) for p in self.profiles or [] ] notifications = [ AutoscaleNotification(email=EmailNotification(**n), webhooks=[ WebhookNotification(service_uri=w) for w in n.get('webhooks') or [] ]) for n in self.notifications or [] ] if not results: # create new changed = True else: # check changed resource_name = results.autoscale_setting_resource_name or self.name update_tags, tags = self.update_tags(results.tags) if update_tags: changed = True self.tags = tags if self.target != results.target_resource_uri: changed = True if self.enabled != results.enabled: changed = True profile_result_set = set( [str(profile_to_dict(p)) for p in results.profiles or []]) if profile_result_set != set( [str(profile_to_dict(p)) for p in profiles]): changed = True notification_result_set = set([ str(notification_to_dict(n)) for n in results.notifications or [] ]) if notification_result_set != set( [str(notification_to_dict(n)) for n in notifications]): changed = True if changed: # construct the instance will be send to create_or_update api results = AutoscaleSettingResource( location=self.location, tags=self.tags, profiles=profiles, notifications=notifications, enabled=self.enabled, autoscale_setting_resource_name=resource_name, target_resource_uri=self.target) if not self.check_mode: results = self.create_or_update_auto_scale(results) # results should be the dict of the instance self.results = auto_scale_to_dict(results) self.results['changed'] = changed return self.results