def detach_nic_from_backend_pool(**_): ''' Detaches a Network Interface Card's IPConfigurations from a Load Balancer Backend Pool ''' # Get the ID of the Backend Pool be_pool_id = utils.get_full_id_reference(BackendAddressPool, _ctx=ctx.target) # Get an interface to the Network Interface Card nic_iface = NetworkInterfaceCard(_ctx=ctx.source) # Get the existing NIC IPConfigurations nic_data = nic_iface.get(utils.get_resource_name(ctx.source)) nic_ip_cfgs = nic_data.get('properties', dict()).get('ipConfigurations', list()) # Remove the Backend Pool from the NIC IPConfigurations for ip_idx, _ in enumerate(nic_ip_cfgs): nic_pools = nic_ip_cfgs[ip_idx].get('properties', dict()).get( LB_ADDRPOOLS_KEY, list()) for pool_idx, nic_pool in enumerate(nic_pools): if nic_pool != be_pool_id: continue del nic_pools[pool_idx] nic_ip_cfgs[ip_idx]['properties'][LB_ADDRPOOLS_KEY] = nic_pools # Update the NIC IPConfigurations utils.task_resource_update( nic_iface, {'properties': { 'ipConfigurations': nic_ip_cfgs }}, _ctx=ctx.source)
def detach_data_disk(**_): '''Detaches a data disk''' vm_iface = VirtualMachine(_ctx=ctx.source) vm_state = vm_iface.get(name=utils.get_resource_name(_ctx=ctx.source)) data_disks = [ x for x in vm_state.get( 'properties', dict()).get( 'storageProfile', dict()).get( 'dataDisks', list()) if x.get('vhd', dict()).get('uri') != ctx.target.instance.runtime_properties['uri'] ] ctx.logger.info('async_op: {0}'.format( ctx.source.instance.runtime_properties.get('async_op'))) # Update the VM utils.task_resource_update( VirtualMachine(_ctx=ctx.source), { 'location': ctx.source.node.properties.get('location'), 'properties': { 'storageProfile': { 'dataDisks': data_disks } } }, force=True, _ctx=ctx.source )
def delete_backend_pool(**_): '''Deletes a Load Balancer Backend Pool''' if ctx.node.properties.get('use_external_resource', False): return # Get an interface to the Load Balancer lb_rel = utils.get_relationship_by_type( ctx.instance.relationships, constants.REL_CONTAINED_IN_LB) lb_name = utils.get_resource_name(lb_rel.target) lb_iface = LoadBalancer( _ctx=lb_rel.target, api_version=ctx.node.properties.get('api_version', constants.API_VER_NETWORK)) # Get the existing pools lb_data = lb_iface.get(lb_name) lb_pools = lb_data.get('properties', dict()).get( 'backendAddressPools', list()) for idx, pool in enumerate(lb_pools): if pool.get('name') == utils.get_resource_name(): del lb_pools[idx] # Update the Load Balancer with the new pool list utils.task_resource_update( lb_iface, { 'properties': { 'backendAddressPools': lb_pools } }, name=lb_name)
def delete_rule(**_): ''' Deletes a Load Balancer Rule TODO: Rewrite this to occur inside of a Relationship Operation ''' if ctx.node.properties.get('use_external_resource', False): return # Get an interface to the Load Balancer lb_rel = utils.get_relationship_by_type(ctx.instance.relationships, constants.REL_CONTAINED_IN_LB) lb_name = utils.get_resource_name(lb_rel.target) lb_ctx = utils.get_relationship_subject_ctx(ctx, lb_rel.target) lb_iface = LoadBalancer(_ctx=lb_ctx, api_version=ctx.node.properties.get( 'api_version', constants.API_VER_NETWORK)) # Get the existing rules lb_data = lb_iface.get(lb_name) lb_rules = lb_data.get('properties', dict()).get('loadBalancingRules', list()) for idx, rule in enumerate(lb_rules): if rule.get('name') == utils.get_resource_name(): del lb_rules[idx] # Update the Load Balancer with the new rules list utils.task_resource_update( lb_iface, {'properties': { 'loadBalancingRules': lb_rules }}, name=lb_name)
def create_probe(**_): '''Uses an existing, or creates a new, Load Balancer Probe''' # Check if invalid external resource if ctx.node.properties.get('use_external_resource', False) and \ not ctx.node.properties.get('name'): raise NonRecoverableError( '"use_external_resource" specified without a resource "name"') # Generate a name if it doesn't exist utils.generate_resource_name(Probe(api_version=ctx.node.properties.get( 'api_version', constants.API_VER_NETWORK))) # Get an interface to the Load Balancer lb_rel = utils.get_relationship_by_type( ctx.instance.relationships, constants.REL_CONTAINED_IN_LB) lb_name = utils.get_resource_name(lb_rel.target) lb_iface = LoadBalancer(api_version=ctx.node.properties.get( 'api_version', constants.API_VER_NETWORK)) # Get the existing probes lb_data = lb_iface.get(lb_name) lb_probes = lb_data.get('properties', dict()).get( 'probes', list()) lb_probes.append({ 'name': utils.get_resource_name(), 'properties': utils.get_resource_config() }) # Update the Load Balancer with the new probe utils.task_resource_update( lb_iface, { 'properties': { 'probes': lb_probes } }, name=lb_name)
def create_probe(**_): '''Uses an existing, or creates a new, Load Balancer Probe''' # Check if invalid external resource if ctx.node.properties.get('use_external_resource', False) and \ not ctx.node.properties.get('name'): raise NonRecoverableError( '"use_external_resource" specified without a resource "name"') # Generate a name if it doesn't exist utils.generate_resource_name( Probe(api_version=ctx.node.properties.get('api_version', constants.API_VER_NETWORK))) # Get an interface to the Load Balancer lb_rel = utils.get_relationship_by_type(ctx.instance.relationships, constants.REL_CONTAINED_IN_LB) lb_name = utils.get_resource_name(lb_rel.target) lb_iface = LoadBalancer(api_version=ctx.node.properties.get( 'api_version', constants.API_VER_NETWORK)) # Get the existing probes lb_data = lb_iface.get(lb_name) lb_probes = lb_data.get('properties', dict()).get('probes', list()) lb_probes.append({ 'name': utils.get_resource_name(), 'properties': utils.get_resource_config() }) # Update the Load Balancer with the new probe utils.task_resource_update(lb_iface, {'properties': { 'probes': lb_probes }}, name=lb_name)
def delete_incoming_nat_rule(**_): '''Deletes a Load Balancer Incoming NAT Rule''' if ctx.node.properties.get('use_external_resource', False): return # Get an interface to the Load Balancer lb_rel = utils.get_relationship_by_type( ctx.instance.relationships, constants.REL_CONTAINED_IN_LB) lb_name = utils.get_resource_name(lb_rel.target) lb_iface = LoadBalancer( _ctx=lb_rel.target, api_version=ctx.node.properties.get('api_version', constants.API_VER_NETWORK)) # Get the existing probes lb_data = lb_iface.get(lb_name) lb_rules = lb_data.get('properties', dict()).get( 'inboundNatRules', list()) for idx, rule in enumerate(lb_rules): if rule.get('name') == utils.get_resource_name(): del lb_rules[idx] # Update the Load Balancer with the new NAT rule list utils.task_resource_update( lb_iface, { 'properties': { 'inboundNatRules': lb_rules } }, name=lb_name)
def delete_incoming_nat_rule(**_): '''Deletes a Load Balancer Incoming NAT Rule''' if ctx.node.properties.get('use_external_resource', False): return # Get an interface to the Load Balancer lb_rel = utils.get_relationship_by_type( ctx.instance.relationships, constants.REL_CONTAINED_IN_LB) lb_name = utils.get_resource_name(lb_rel.target) lb_iface = LoadBalancer(_ctx=lb_rel.target) # Get the existing probes lb_data = lb_iface.get(lb_name) lb_rules = lb_data.get('properties', dict()).get( 'inboundNatRules', list()) for idx, rule in enumerate(lb_rules): if rule.get('name') == utils.get_resource_name(): del lb_rules[idx] # Update the Load Balancer with the new NAT rule list utils.task_resource_update( lb_iface, { 'properties': { 'inboundNatRules': lb_rules } }, name=lb_name)
def delete_backend_pool(**_): '''Deletes a Load Balancer Backend Pool''' if ctx.node.properties.get('use_external_resource', False): return # Get an interface to the Load Balancer lb_rel = utils.get_relationship_by_type( ctx.instance.relationships, constants.REL_CONTAINED_IN_LB) lb_name = utils.get_resource_name(lb_rel.target) lb_iface = LoadBalancer(_ctx=lb_rel.target) # Get the existing pools lb_data = lb_iface.get(lb_name) lb_pools = lb_data.get('properties', dict()).get( 'backendAddressPools', list()) for idx, pool in enumerate(lb_pools): if pool.get('name') == utils.get_resource_name(): del lb_pools[idx] # Update the Load Balancer with the new pool list utils.task_resource_update( lb_iface, { 'properties': { 'backendAddressPools': lb_pools } }, name=lb_name)
def delete_probe(**_): '''Deletes a Load Balancer Probe''' if ctx.node.properties.get('use_external_resource', False): return # Get an interface to the Load Balancer lb_rel = utils.get_relationship_by_type( ctx.instance.relationships, constants.REL_CONTAINED_IN_LB) lb_name = utils.get_resource_name(lb_rel.target) lb_iface = LoadBalancer(_ctx=lb_rel.target) # Get the existing probes lb_data = lb_iface.get(lb_name) lb_probes = lb_data.get('properties', dict()).get( 'probes', list()) for idx, probe in enumerate(lb_probes): if probe.get('name') == utils.get_resource_name(): del lb_probes[idx] # Update the Load Balancer with the new probes list utils.task_resource_update( lb_iface, { 'properties': { 'probes': lb_probes } }, name=lb_name)
def delete_rule(**_): ''' Deletes a Load Balancer Rule TODO: Rewrite this to occur inside of a Relationship Operation ''' if ctx.node.properties.get('use_external_resource', False): return # Get an interface to the Load Balancer lb_rel = utils.get_relationship_by_type( ctx.instance.relationships, constants.REL_CONTAINED_IN_LB) lb_name = utils.get_resource_name(lb_rel.target) lb_ctx = utils.get_relationship_subject_ctx(ctx, lb_rel.target) lb_iface = LoadBalancer(_ctx=lb_ctx, api_version=ctx.node.properties.get( 'api_version', constants.API_VER_NETWORK)) # Get the existing rules lb_data = lb_iface.get(lb_name) lb_rules = lb_data.get('properties', dict()).get( 'loadBalancingRules', list()) for idx, rule in enumerate(lb_rules): if rule.get('name') == utils.get_resource_name(): del lb_rules[idx] # Update the Load Balancer with the new rules list utils.task_resource_update( lb_iface, { 'properties': { 'loadBalancingRules': lb_rules } }, name=lb_name)
def create_backend_pool(**_): '''Uses an existing, or creates a new, Load Balancer Backend Pool''' # Check if invalid external resource if ctx.node.properties.get('use_external_resource', False) and \ not ctx.node.properties.get('name'): raise NonRecoverableError( '"use_external_resource" specified without a resource "name"') # Generate a name if it doesn't exist utils.generate_resource_name(BackendAddressPool()) # Get an interface to the Load Balancer lb_rel = utils.get_relationship_by_type( ctx.instance.relationships, constants.REL_CONTAINED_IN_LB) lb_name = utils.get_resource_name(lb_rel.target) lb_iface = LoadBalancer() # Get the existing pools lb_data = lb_iface.get(lb_name) lb_pools = lb_data.get('properties', dict()).get( 'backendAddressPools', list()) lb_pools.append({'name': utils.get_resource_name()}) # Update the Load Balancer with the new pool utils.task_resource_update( lb_iface, { 'properties': { 'backendAddressPools': lb_pools } }, name=lb_name)
def detach_nic_from_backend_pool(**_): ''' Detaches a Network Interface Card's IPConfigurations from a Load Balancer Backend Pool ''' # Get the ID of the Backend Pool be_pool_id = utils.get_full_id_reference( BackendAddressPool, _ctx=ctx.target) # Get an interface to the Network Interface Card nic_iface = NetworkInterfaceCard(_ctx=ctx.source) # Get the existing NIC IPConfigurations nic_data = nic_iface.get(utils.get_resource_name(ctx.source)) nic_ip_cfgs = nic_data.get('properties', dict()).get( 'ipConfigurations', list()) # Remove the Backend Pool from the NIC IPConfigurations for ip_idx, _ in enumerate(nic_ip_cfgs): nic_pools = nic_ip_cfgs[ip_idx].get( 'properties', dict()).get( LB_ADDRPOOLS_KEY, list()) for pool_idx, nic_pool in enumerate(nic_pools): if nic_pool != be_pool_id: continue del nic_pools[pool_idx] nic_ip_cfgs[ip_idx]['properties'][LB_ADDRPOOLS_KEY] = nic_pools # Update the NIC IPConfigurations utils.task_resource_update( nic_iface, { 'properties': { 'ipConfigurations': nic_ip_cfgs } }, _ctx=ctx.source)
def detach_data_disk(**_): '''Detaches a data disk''' vm_iface = VirtualMachine(_ctx=ctx.source, api_version=ctx.source.node.properties.get( 'api_version', constants.API_VER_COMPUTE)) vm_state = vm_iface.get(name=utils.get_resource_name(_ctx=ctx.source)) data_disks = [ x for x in vm_state.get('properties', dict()).get( 'storageProfile', dict()).get('dataDisks', list()) if x.get('vhd', dict()).get('uri') != ctx.target.instance.runtime_properties['uri'] ] ctx.logger.info('async_op: {0}'.format( ctx.source.instance.runtime_properties.get('async_op'))) # Update the VM utils.task_resource_update( VirtualMachine(_ctx=ctx.source, api_version=ctx.source.node.properties.get( 'api_version', constants.API_VER_COMPUTE)), { 'location': ctx.source.node.properties.get('location'), 'properties': { 'storageProfile': { 'dataDisks': data_disks } } }, force=True, _ctx=ctx.source)
def detach_route_table(**_): '''Detaches a Route Table to the Subnet''' # Detach utils.task_resource_update(Subnet(_ctx=ctx.target), {'properties': { 'routeTable': None }}, _ctx=ctx.target)
def detach_network_security_group(**_): '''Detaches a Network Security Group to the Subnet''' # Detach utils.task_resource_update(Subnet(_ctx=ctx.target), {'properties': { 'networkSecurityGroup': None }}, _ctx=ctx.target)
def detach_route_table(**_): '''Detaches a Route Table to the Subnet''' # Detach utils.task_resource_update( Subnet(_ctx=ctx.target), { 'properties': { 'routeTable': None } }, _ctx=ctx.target)
def detach_network_security_group(**_): '''Detaches a Network Security Group to the Subnet''' # Detach utils.task_resource_update( Subnet(_ctx=ctx.target), { 'properties': { 'networkSecurityGroup': None } }, _ctx=ctx.target)
def detach_network_security_group(**_): '''Detaches a Network Security Group to the Subnet''' # Detach utils.task_resource_update(Subnet( _ctx=ctx.target, api_version=ctx.source.node.properties.get('api_version', constants.API_VER_NETWORK)), {'properties': { 'networkSecurityGroup': None }}, _ctx=ctx.target)
def detach_route_table(**_): '''Detaches a Route Table to the Subnet''' # Detach utils.task_resource_update( Subnet(_ctx=ctx.target, api_version=ctx.source.node.properties.get( 'api_version', constants.API_VER_NETWORK) ), { 'properties': { 'routeTable': None } }, _ctx=ctx.target)
def detach_route_table(**_): '''Detaches a Route Table to the Subnet''' # Detach utils.task_resource_update(Subnet( _ctx=ctx.target, api_version=ctx.source.node.properties.get('api_version', constants.API_VER_NETWORK)), {'properties': { 'routeTable': None }}, _ctx=ctx.target)
def detach_network_security_group(**_): '''Detaches a Network Security Group to the Subnet''' # Detach utils.task_resource_update( Subnet(_ctx=ctx.target, api_version=ctx.source.node.properties.get( 'api_version', constants.API_VER_NETWORK) ), { 'properties': { 'networkSecurityGroup': None } }, _ctx=ctx.target)
def create_rule(**_): '''Uses an existing, or creates a new, Load Balancer Rule''' # Check if invalid external resource if ctx.node.properties.get('use_external_resource', False) and \ not ctx.node.properties.get('name'): raise NonRecoverableError( '"use_external_resource" specified without a resource "name"') # Generate a name if it doesn't exist utils.generate_resource_name(LoadBalancerRule( api_version=ctx.node.properties.get('api_version', constants.API_VER_NETWORK))) # Get the resource config res_cfg = utils.get_resource_config() # Get an interface to the Load Balancer lb_rel = utils.get_relationship_by_type( ctx.instance.relationships, constants.REL_CONTAINED_IN_LB) lb_name = utils.get_resource_name(lb_rel.target) lb_iface = LoadBalancer(api_version=ctx.node.properties.get( 'api_version', constants.API_VER_NETWORK)) lb_data = lb_iface.get(lb_name) # Get the Load Balancer Backend Pool lb_be_pool_id = utils.get_rel_id_reference( BackendAddressPool, constants.REL_CONNECTED_TO_LB_BE_POOL) # Get the Load Balancer Probe lb_probe_id = utils.get_rel_id_reference( Probe, constants.REL_CONNECTED_TO_LB_PROBE) # Get the Load Balancer Frontend IP Configuration lb_fe_ipc_name = utils.get_rel_node_name(constants.REL_CONNECTED_TO_IPC) lb_fe_ipc_id = utils.get_full_resource_id( FrontendIPConfiguration(api_version=ctx.node.properties.get( 'api_version', constants.API_VER_NETWORK) ), lb_fe_ipc_name) # Get the existing Load Balancer Rules lb_rules = lb_data.get('properties', dict()).get( 'loadBalancingRules', list()) # Update the resource config res_cfg['backendAddressPool'] = lb_be_pool_id res_cfg['frontendIPConfiguration'] = lb_fe_ipc_id res_cfg['probe'] = lb_probe_id lb_rules.append({ 'name': utils.get_resource_name(), 'properties': res_cfg }) # Update the Load Balancer with the new rule utils.task_resource_update( lb_iface, { 'properties': { 'loadBalancingRules': lb_rules } }, name=lb_name)
def create_rule(**_): '''Uses an existing, or creates a new, Load Balancer Rule''' # Check if invalid external resource if ctx.node.properties.get('use_external_resource', False) and \ not ctx.node.properties.get('name'): raise NonRecoverableError( '"use_external_resource" specified without a resource "name"') # Generate a name if it doesn't exist utils.generate_resource_name(LoadBalancerRule()) # Get the resource config res_cfg = utils.get_resource_config() # Get an interface to the Load Balancer lb_rel = utils.get_relationship_by_type( ctx.instance.relationships, constants.REL_CONTAINED_IN_LB) lb_name = utils.get_resource_name(lb_rel.target) lb_iface = LoadBalancer() lb_data = lb_iface.get(lb_name) # Get the Load Balancer Backend Pool lb_be_pool_id = utils.get_rel_id_reference( BackendAddressPool, constants.REL_CONNECTED_TO_LB_BE_POOL) # Get the Load Balancer Probe lb_probe_id = utils.get_rel_id_reference( Probe, constants.REL_CONNECTED_TO_LB_PROBE) # Get the Load Balancer Frontend IP Configuration lb_fe_ipc_name = utils.get_rel_node_name(constants.REL_CONNECTED_TO_IPC) lb_fe_ipc_id = utils.get_full_resource_id( FrontendIPConfiguration(), lb_fe_ipc_name) # Get the existing Load Balancer Rules lb_rules = lb_data.get('properties', dict()).get( 'loadBalancingRules', list()) # Update the resource config res_cfg['backendAddressPool'] = lb_be_pool_id res_cfg['frontendIPConfiguration'] = lb_fe_ipc_id res_cfg['probe'] = lb_probe_id lb_rules.append({ 'name': utils.get_resource_name(), 'properties': res_cfg }) # Update the Load Balancer with the new rule utils.task_resource_update( lb_iface, { 'properties': { 'loadBalancingRules': lb_rules } }, name=lb_name)
def attach_network_security_group(**_): '''Attaches a Network Security Group (source) to the Subnet (target)''' nsg = NetworkSecurityGroup(_ctx=ctx.source) nsg_name = utils.get_resource_name(ctx.source) # Attach utils.task_resource_update( Subnet(_ctx=ctx.target), { 'properties': { 'networkSecurityGroup': { 'id': '/subscriptions/{0}{1}/{2}'.format( utils.get_subscription_id(_ctx=ctx.source), nsg.endpoint, nsg_name) } } }, _ctx=ctx.target)
def attach_network_security_group(**_): '''Attaches a Network Security Group (source) to the Subnet (target)''' nsg = NetworkSecurityGroup(_ctx=ctx.source) nsg_name = utils.get_resource_name(ctx.source) # Attach utils.task_resource_update(Subnet(_ctx=ctx.target), { 'properties': { 'networkSecurityGroup': { 'id': '/subscriptions/{0}{1}/{2}'.format( utils.get_subscription_id(_ctx=ctx.source), nsg.endpoint, nsg_name) } } }, _ctx=ctx.target)
def attach_route_table(**_): '''Attaches a Route Table (source) to the Subnet (target)''' rtbl = RouteTable(_ctx=ctx.source) rtbl_name = utils.get_resource_name(ctx.source) # Attach utils.task_resource_update(Subnet(_ctx=ctx.target), { 'properties': { 'routeTable': { 'id': '/subscriptions/{0}{1}/{2}'.format( utils.get_subscription_id(_ctx=ctx.source), rtbl.endpoint, rtbl_name) } } }, _ctx=ctx.target)
def attach_route_table(**_): '''Attaches a Route Table (source) to the Subnet (target)''' rtbl = RouteTable(_ctx=ctx.source) rtbl_name = utils.get_resource_name(ctx.source) # Attach utils.task_resource_update( Subnet(_ctx=ctx.target), { 'properties': { 'routeTable': { 'id': '/subscriptions/{0}{1}/{2}'.format( utils.get_subscription_id(_ctx=ctx.source), rtbl.endpoint, rtbl_name) } } }, _ctx=ctx.target)
def attach_data_disk(lun, **_): '''Attaches a data disk''' vm_iface = VirtualMachine(_ctx=ctx.source, api_version=ctx.source.node.properties.get( 'api_version', constants.API_VER_COMPUTE)) vm_state = vm_iface.get(name=utils.get_resource_name(_ctx=ctx.source)) data_disks = vm_state.get('properties', dict()).get('storageProfile', dict()).get('dataDisks', list()) # Get the createOption create_opt = 'Empty' if ctx.target.node.properties.get('use_external_resource', False): create_opt = 'Attach' # Add the disk to the list data_disks.append({ 'name': utils.get_resource_name(_ctx=ctx.target), 'lun': lun, 'diskSizeGB': ctx.target.instance.runtime_properties['diskSizeGB'], 'vhd': { 'uri': ctx.target.instance.runtime_properties['uri'] }, 'createOption': create_opt, 'caching': 'None' }) ctx.logger.info('async_op: {0}'.format( ctx.source.instance.runtime_properties.get('async_op'))) # Update the VM utils.task_resource_update( VirtualMachine(_ctx=ctx.source, api_version=ctx.source.node.properties.get( 'api_version', constants.API_VER_COMPUTE)), { 'location': ctx.source.node.properties.get('location'), 'properties': { 'storageProfile': { 'dataDisks': data_disks } } }, force=True, _ctx=ctx.source)
def attach_data_disk(lun, **_): '''Attaches a data disk''' vm_iface = VirtualMachine(_ctx=ctx.source, api_version=ctx.source.node.properties.get( 'api_version', constants.API_VER_COMPUTE)) vm_state = vm_iface.get(name=utils.get_resource_name(_ctx=ctx.source)) data_disks = vm_state.get( 'properties', dict()).get( 'storageProfile', dict()).get( 'dataDisks', list()) # Get the createOption create_opt = 'Empty' if ctx.target.node.properties.get('use_external_resource', False): create_opt = 'Attach' # Add the disk to the list data_disks.append({ 'name': utils.get_resource_name(_ctx=ctx.target), 'lun': lun, 'diskSizeGB': ctx.target.instance.runtime_properties['diskSizeGB'], 'vhd': { 'uri': ctx.target.instance.runtime_properties['uri'] }, 'createOption': create_opt, 'caching': 'None' }) ctx.logger.info('async_op: {0}'.format( ctx.source.instance.runtime_properties.get('async_op'))) # Update the VM utils.task_resource_update( VirtualMachine(_ctx=ctx.source, api_version=ctx.source.node.properties.get( 'api_version', constants.API_VER_COMPUTE)), { 'location': ctx.source.node.properties.get('location'), 'properties': { 'storageProfile': { 'dataDisks': data_disks } } }, force=True, _ctx=ctx.source )
def create_incoming_nat_rule(**_): '''Uses an existing, or creates a new, Load Balancer Incoming NAT Rule''' # Check if invalid external resource if ctx.node.properties.get('use_external_resource', False) and \ not ctx.node.properties.get('name'): raise NonRecoverableError( '"use_external_resource" specified without a resource "name"') # Generate a name if it doesn't exist utils.generate_resource_name(InboundNATRule()) # Get an interface to the Load Balancer lb_rel = utils.get_relationship_by_type( ctx.instance.relationships, constants.REL_CONTAINED_IN_LB) lb_name = utils.get_resource_name(lb_rel.target) lb_iface = LoadBalancer() # Get the resource config res_cfg = utils.get_resource_config() # Get the existing rules lb_data = lb_iface.get(lb_name) lb_rules = lb_data.get('properties', dict()).get( 'inboundNatRules', list()) # Get the Load Balancer Frontend IP Configuration lb_fe_ipc_name = utils.get_rel_node_name(constants.REL_CONNECTED_TO_IPC) lb_fe_ipc_id = utils.get_full_resource_id( FrontendIPConfiguration(), lb_fe_ipc_name) # Update the resource config res_cfg['frontendIPConfiguration'] = lb_fe_ipc_id lb_rules.append({ 'name': utils.get_resource_name(), 'properties': res_cfg }) # Update the Load Balancer with the new NAT rule utils.task_resource_update( lb_iface, { 'properties': { 'inboundNatRules': lb_rules } }, name=lb_name)
def delete_probe(**_): '''Deletes a Load Balancer Probe''' if ctx.node.properties.get('use_external_resource', False): return # Get an interface to the Load Balancer lb_rel = utils.get_relationship_by_type(ctx.instance.relationships, constants.REL_CONTAINED_IN_LB) lb_name = utils.get_resource_name(lb_rel.target) lb_iface = LoadBalancer(_ctx=lb_rel.target, api_version=ctx.node.properties.get( 'api_version', constants.API_VER_NETWORK)) # Get the existing probes lb_data = lb_iface.get(lb_name) lb_probes = lb_data.get('properties', dict()).get('probes', list()) for idx, probe in enumerate(lb_probes): if probe.get('name') == utils.get_resource_name(): del lb_probes[idx] # Update the Load Balancer with the new probes list utils.task_resource_update(lb_iface, {'properties': { 'probes': lb_probes }}, name=lb_name)
def configure(command_to_execute, file_uris, type_handler_version='v2.0', **_): '''Configures the resource''' os_family = ctx.node.properties.get('os_family', '').lower() if os_family == 'windows': utils.task_resource_create( VirtualMachineExtension(virtual_machine=utils.get_resource_name(), api_version=ctx.node.properties.get( 'api_version', constants.API_VER_COMPUTE)), { 'location': ctx.node.properties.get('location'), 'tags': ctx.node.properties.get('tags'), 'properties': { 'publisher': 'Microsoft.Compute', 'type': 'CustomScriptExtension', 'typeHandlerVersion': type_handler_version, 'settings': { 'fileUris': file_uris, 'commandToExecute': command_to_execute } } }) virtual_machine_name = ctx.instance.runtime_properties.get('name') virtual_machine_iface = \ VirtualMachine( api_version=ctx.node.properties.get( 'api_version', constants.API_VER_COMPUTE)).get( name=virtual_machine_name) # Write the IP address to runtime properties for the agent # Get a reference to the NIC rel_nics = utils.get_relationships_by_type(ctx.instance.relationships, constants.REL_CONNECTED_TO_NIC) # No NIC? Exit and hope the user doesn't plan to install an agent if not rel_nics: return for rel_nic in rel_nics: # Get the NIC data from the API directly (because of IPConfiguration) nic_iface = NetworkInterfaceCard( _ctx=rel_nic.target, api_version=rel_nic.target.node.properties.get( 'api_version', constants.API_VER_NETWORK)) nic_name = utils.get_resource_name(rel_nic.target) nic_data = nic_iface.get(nic_name) nic_virtual_machine_id = nic_data.get('properties', dict()).get( 'virtualMachine', dict()).get('id') if virtual_machine_name not in nic_virtual_machine_id: nic_data['properties'] = \ utils.dict_update( nic_data.get('properties', {}), { 'virtualMachine': { 'id': virtual_machine_iface.get('id') } } ) utils.task_resource_update(nic_iface, nic_data, _ctx=rel_nic.target) nic_data = nic_iface.get(nic_name) if virtual_machine_name not in nic_data.get( 'properties', dict()).get('virtualMachine', dict()).get('id', str()): return ctx.operation.retry(message='Waiting for NIC {0} to ' 'attach to VM {1}..'.format( nic_name, virtual_machine_name), retry_after=10) # Iterate over each IPConfiguration entry creds = utils.get_credentials(_ctx=ctx) for ip_cfg in nic_data.get('properties', dict()).get('ipConfigurations', list()): # Get the Private IP Address endpoint ctx.instance.runtime_properties['ip'] = \ ip_cfg.get('properties', dict()).get('privateIPAddress') # Get the Public IP Address endpoint pubip_id = ip_cfg.get('properties', dict()).get('publicIPAddress', dict()).get('id') if isinstance(pubip_id, basestring): # use the ID to get the data on the public ip pubip = PublicIPAddress( _ctx=rel_nic.target, api_version=rel_nic.target.node.properties.get( 'api_version', constants.API_VER_NETWORK)) pubip.endpoint = '{0}{1}'.format( creds.endpoints_resource_manager, pubip_id) pubip_data = pubip.get() if isinstance(pubip_data, dict): public_ip = \ pubip_data.get('properties', dict()).get('ipAddress') # Maintained for backwards compatibility. ctx.instance.runtime_properties['public_ip'] = \ public_ip # For consistency with other plugins. ctx.instance.runtime_properties[PUBLIC_IP_PROPERTY] = \ public_ip # We should also consider that maybe there will be many # public ip addresses. public_ip_addresses = \ ctx.instance.runtime_properties.get( PUBLIC_IP_PROPERTY, []) if public_ip not in public_ip_addresses: public_ip_addresses.append(public_ip) ctx.instance.runtime_properties['public_ip_addresses'] = \ public_ip_addresses # See if the user wants to use the public IP as primary IP if ctx.node.properties.get('use_public_ip') and \ ctx.instance.runtime_properties.get('public_ip'): ctx.instance.runtime_properties['ip'] = \ ctx.instance.runtime_properties.get('public_ip') ctx.logger.info('OUTPUT {0}.{1} = "{2}"'.format( ctx.instance.id, 'ip', ctx.instance.runtime_properties.get('ip'))) ctx.logger.info('OUTPUT {0}.{1} = "{2}"'.format( ctx.instance.id, 'public_ip', ctx.instance.runtime_properties.get('public_ip')))
def configure(command_to_execute, file_uris, type_handler_version='v2.0', **_): '''Configures the resource''' os_family = ctx.node.properties.get('os_family', '').lower() if os_family == 'windows': utils.task_resource_create( VirtualMachineExtension( virtual_machine=utils.get_resource_name(), api_version=ctx.node.properties.get('api_version', constants.API_VER_COMPUTE) ), { 'location': ctx.node.properties.get('location'), 'tags': ctx.node.properties.get('tags'), 'properties': { 'publisher': 'Microsoft.Compute', 'type': 'CustomScriptExtension', 'typeHandlerVersion': type_handler_version, 'settings': { 'fileUris': file_uris, 'commandToExecute': command_to_execute } } }) virtual_machine_name = ctx.instance.runtime_properties.get('name') virtual_machine_iface = \ VirtualMachine( api_version=ctx.node.properties.get( 'api_version', constants.API_VER_COMPUTE)).get( name=virtual_machine_name) # Write the IP address to runtime properties for the agent # Get a reference to the NIC rel_nics = utils.get_relationships_by_type( ctx.instance.relationships, constants.REL_CONNECTED_TO_NIC) # No NIC? Exit and hope the user doesn't plan to install an agent if not rel_nics: return for rel_nic in rel_nics: # Get the NIC data from the API directly (because of IPConfiguration) nic_iface = NetworkInterfaceCard( _ctx=rel_nic.target, api_version=rel_nic.target.node.properties.get( 'api_version', constants.API_VER_NETWORK)) nic_name = utils.get_resource_name(rel_nic.target) nic_data = nic_iface.get(nic_name) nic_virtual_machine_id = nic_data.get( 'properties', dict()).get( 'virtualMachine', dict()).get('id') if virtual_machine_name not in nic_virtual_machine_id: nic_data['properties'] = \ utils.dict_update( nic_data.get('properties', {}), { 'virtualMachine': { 'id': virtual_machine_iface.get('id') } } ) utils.task_resource_update( nic_iface, nic_data, _ctx=rel_nic.target) nic_data = nic_iface.get(nic_name) if virtual_machine_name not in nic_data.get( 'properties', dict()).get( 'virtualMachine', dict()).get('id', str()): return ctx.operation.retry( message='Waiting for NIC {0} to ' 'attach to VM {1}..' .format(nic_name, virtual_machine_name), retry_after=10) # Iterate over each IPConfiguration entry creds = utils.get_credentials(_ctx=ctx) for ip_cfg in nic_data.get( 'properties', dict()).get( 'ipConfigurations', list()): # Get the Private IP Address endpoint ctx.instance.runtime_properties['ip'] = \ ip_cfg.get('properties', dict()).get('privateIPAddress') # Get the Public IP Address endpoint pubip_id = ip_cfg.get( 'properties', dict()).get( 'publicIPAddress', dict()).get('id') if isinstance(pubip_id, basestring): # use the ID to get the data on the public ip pubip = PublicIPAddress( _ctx=rel_nic.target, api_version=rel_nic.target.node.properties.get( 'api_version', constants.API_VER_NETWORK)) pubip.endpoint = '{0}{1}'.format( creds.endpoints_resource_manager, pubip_id) pubip_data = pubip.get() if isinstance(pubip_data, dict): public_ip = \ pubip_data.get('properties', dict()).get('ipAddress') # Maintained for backwards compatibility. ctx.instance.runtime_properties['public_ip'] = \ public_ip # For consistency with other plugins. ctx.instance.runtime_properties[PUBLIC_IP_PROPERTY] = \ public_ip # We should also consider that maybe there will be many # public ip addresses. public_ip_addresses = \ ctx.instance.runtime_properties.get( PUBLIC_IP_PROPERTY, []) if public_ip not in public_ip_addresses: public_ip_addresses.append(public_ip) ctx.instance.runtime_properties['public_ip_addresses'] = \ public_ip_addresses # See if the user wants to use the public IP as primary IP if ctx.node.properties.get('use_public_ip') and \ ctx.instance.runtime_properties.get('public_ip'): ctx.instance.runtime_properties['ip'] = \ ctx.instance.runtime_properties.get('public_ip') ctx.logger.info('OUTPUT {0}.{1} = "{2}"'.format( ctx.instance.id, 'ip', ctx.instance.runtime_properties.get('ip'))) ctx.logger.info('OUTPUT {0}.{1} = "{2}"'.format( ctx.instance.id, 'public_ip', ctx.instance.runtime_properties.get('public_ip')))