def create_probe(ctx, **_): """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 cfy_exc.NonRecoverableError( '"use_external_resource" specified without a resource "name"') # Generate a name if it doesn't exist azure_config = ctx.node.properties.get('azure_config') if not azure_config.get("subscription_id"): azure_config = ctx.node.properties.get('client_config') else: ctx.logger.warn("azure_config is deprecated please use client_config, " "in later version it will be removed") resource_group_name = utils.get_resource_group(ctx) load_balancer_name = ctx.node.properties.get('load_balancer_name') or \ utils.get_resource_name_ref(constants.REL_CONTAINED_IN_LB, 'load_balancer_name', _ctx=ctx) load_balancer = LoadBalancer(azure_config, ctx.logger) probe_name = ctx.node.properties.get('name') probe_name = \ get_unique_lb_prop_name(load_balancer, resource_group_name, load_balancer_name, "probes", probe_name) ctx.instance.runtime_properties['name'] = probe_name # 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) # Get the existing probes lb_data = load_balancer.get(resource_group_name, lb_name) lb_probes = lb_data.get('probes', list()) lb_probes.append({ 'name': probe_name, }) lb_probes = \ utils.handle_resource_config_params(lb_probes, ctx.node.properties.get( 'resource_config', {})) # Update the Load Balancer with the new probe lb_params = { 'probes': lb_probes } # clean empty values from params lb_params = \ utils.cleanup_empty_params(lb_params) try: result = load_balancer.create_or_update(resource_group_name, lb_name, lb_params) for item in result.get("probes"): if item.get("name") == probe_name: ctx.instance.runtime_properties['resource_id'] = item.get("id") ctx.instance.runtime_properties['resource'] = item except CloudError as cr: raise cfy_exc.NonRecoverableError( "create probe '{0}' " "failed with this error : {1}".format(probe_name, cr.message) )
def delete(ctx, **_): """Deletes a Load Balancer""" # Delete the resource azure_config = ctx.node.properties.get('azure_config') if not azure_config.get("subscription_id"): azure_config = ctx.node.properties.get('client_config') else: ctx.logger.warn("azure_config is deprecated please use client_config, " "in later version it will be removed") resource_group_name = utils.get_resource_group(ctx) name = ctx.instance.runtime_properties.get('name') api_version = \ ctx.node.properties.get('api_version', constants.API_VER_NETWORK) load_balancer = LoadBalancer(azure_config, ctx.logger, api_version) try: load_balancer.get(resource_group_name, name) except CloudError: ctx.logger.info("Resource with name {0} doesn't exist".format(name)) return try: load_balancer.delete(resource_group_name, name) utils.runtime_properties_cleanup(ctx) except CloudError as cr: raise cfy_exc.NonRecoverableError( "delete load_balancer '{0}' " "failed with this error : {1}".format(name, cr.message) )
def delete_rule(ctx, **_): """ 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 azure_config = ctx.node.properties.get('azure_config') if not azure_config.get("subscription_id"): azure_config = ctx.node.properties.get('client_config') else: ctx.logger.warn("azure_config is deprecated please use client_config, " "in later version it will be removed") resource_group_name = utils.get_resource_group(ctx) lb_rel = utils.get_relationship_by_type( ctx.instance.relationships, constants.REL_CONTAINED_IN_LB) lb_name = utils.get_resource_name(lb_rel.target) load_balancer = LoadBalancer(azure_config, ctx.logger) name = ctx.instance.runtime_properties.get('name') # Get the existing rules lb_data = load_balancer.get(resource_group_name, lb_name) lb_rules = lb_data.get('load_balancing_rules', list()) for idx, rule in enumerate(lb_rules): if rule.get('name') == name: del lb_rules[idx] # Update the Load Balancer with the new rules list lb_params = { 'load_balancing_rules': lb_rules } try: load_balancer.create_or_update(resource_group_name, lb_name, lb_params) except CloudError as cr: raise cfy_exc.NonRecoverableError( "delete load_balancing_rules '{0}' " "failed with this error : {1}".format(name, cr.message) )
def attach_ip_configuration(ctx, **_): """Generates a usable UUID for the NIC's IP Configuration""" # Generate the IPConfiguration's name azure_config = ctx.source.node.properties.get('azure_config') if not azure_config.get("subscription_id"): azure_config = ctx.source.node.properties.get('client_config') else: ctx.logger.warn("azure_config is deprecated please use client_config, " "in later version it will be removed") resource_group_name = \ ctx.source.instance.runtime_properties.get('resource_group') load_balancer_name = ctx.source.instance.runtime_properties.get('name') load_balancer = LoadBalancer(azure_config, ctx.logger) ip_configuration_name = ctx.target.node.properties['name'] ip_configuration_name = \ get_unique_lb_prop_name(load_balancer, resource_group_name, load_balancer_name, "frontend_ip_configurations", ip_configuration_name) ctx.target.instance.runtime_properties['name'] = ip_configuration_name
def configure(ctx, **_): """Uses an existing, or creates a new, Load Balancer""" # Get the Frontend IP Configuration fe_ip_cfg = get_ip_configurations(rel=constants.REL_LB_CONNECTED_TO_IPC) ctx.logger.debug('fe_ip_cfg: {0}'.format(fe_ip_cfg)) if not len(fe_ip_cfg): raise cfy_exc.NonRecoverableError( 'At least 1 Frontend IP Configuration must be ' 'associated with the Load Balancer') # Remove the subnet if there's a public IP present for ip_cfg in fe_ip_cfg: if ip_cfg.get('public_ip_address'): if ip_cfg.get('subnet'): del ip_cfg['subnet'] # Create a resource (if necessary) azure_config = ctx.node.properties.get('azure_config') if not azure_config.get("subscription_id"): azure_config = ctx.node.properties.get('client_config') else: ctx.logger.warn("azure_config is deprecated please use client_config, " "in later version it will be removed") name = ctx.instance.runtime_properties.get('name') resource_group_name = utils.get_resource_group(ctx) api_version = \ ctx.node.properties.get('api_version', constants.API_VER_NETWORK) load_balancer = LoadBalancer(azure_config, ctx.logger, api_version) lb_params = { 'location': ctx.node.properties.get('location'), 'tags': ctx.node.properties.get('tags'), } lb_params = \ utils.handle_resource_config_params(lb_params, ctx.node.properties.get( 'resource_config', {})) lb_params = utils.dict_update( lb_params, { 'frontend_ip_configurations': fe_ip_cfg } ) # clean empty values from params lb_params = \ utils.cleanup_empty_params(lb_params) try: result = \ load_balancer.create_or_update(resource_group_name, name, lb_params) except CloudError as cr: raise cfy_exc.NonRecoverableError( "create load_balancer '{0}' " "failed with this error : {1}".format(name, cr.message) ) ctx.instance.runtime_properties['resource_group'] = resource_group_name ctx.instance.runtime_properties['resource'] = result ctx.instance.runtime_properties['resource_id'] = result.get("id", "") ctx.instance.runtime_properties['name'] = name for fe_ipc_data in result.get('frontend_ip_configurations', list()): ctx.instance.runtime_properties['ip'] = \ fe_ipc_data.get('private_ip_address') public_ip = \ fe_ipc_data.get('public_ip_address', {}).get('ip_address', None) if not public_ip: pip = PublicIPAddress(azure_config, ctx.logger) pip_name = \ ip_cfg.get('public_ip_address').get('id').rsplit('/', 1)[1] public_ip_data = pip.get(resource_group_name, pip_name) public_ip = public_ip_data.get("ip_address") ctx.instance.runtime_properties['public_ip'] = public_ip
def create_rule(ctx, **_): """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 cfy_exc.NonRecoverableError( '"use_external_resource" specified without a resource "name"') # Generate a name if it doesn't exist azure_config = ctx.node.properties.get('azure_config') if not azure_config.get("subscription_id"): azure_config = ctx.node.properties.get('client_config') else: ctx.logger.warn("azure_config is deprecated please use client_config, " "in later version it will be removed") resource_group_name = utils.get_resource_group(ctx) load_balancer_name = ctx.node.properties.get('load_balancer_name') or \ utils.get_resource_name_ref(constants.REL_CONTAINED_IN_LB, 'load_balancer_name', _ctx=ctx) load_balancer = LoadBalancer(azure_config, ctx.logger) lb_rule_name = ctx.node.properties.get('name') lb_rule_name = \ get_unique_lb_prop_name(load_balancer, resource_group_name, load_balancer_name, "load_balancing_rules", lb_rule_name) ctx.instance.runtime_properties['name'] = lb_rule_name # 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) load_balancer = LoadBalancer(azure_config, ctx.logger) lb_data = load_balancer.get(resource_group_name, lb_name) # Get the Load Balancer Backend Pool/ Probe/ Frontend IP Configuration lb_be_pool_id = "" lb_probe_id = "" lb_fe_ipc_id = "" rel_pool_type = constants.REL_CONNECTED_TO_LB_BE_POOL rel_probe_type = constants.REL_CONNECTED_TO_LB_PROBE rel_fe_type = constants.REL_CONNECTED_TO_IPC for rel in ctx.instance.relationships: if isinstance(rel_pool_type, tuple): if any(x in rel.type_hierarchy for x in rel_pool_type): lb_be_pool_id = \ rel.target.instance.runtime_properties.get('resource_id') else: if rel_pool_type in rel.type_hierarchy: lb_be_pool_id = \ rel.target.instance.runtime_properties.get('resource_id') if isinstance(rel_probe_type, tuple): if any(x in rel.type_hierarchy for x in rel_probe_type): lb_probe_id = \ rel.target.instance.runtime_properties.get('resource_id') else: if constants.REL_CONNECTED_TO_LB_PROBE in rel.type_hierarchy: lb_probe_id = \ rel.target.instance.runtime_properties.get('resource_id') if isinstance(rel_fe_type, tuple): if any(x in rel.type_hierarchy for x in rel_fe_type): lb_fe_ipc_id = \ rel.target.instance.runtime_properties.get('resource_id') else: if constants.REL_CONNECTED_TO_IPC in rel.type_hierarchy: lb_fe_ipc_id = \ rel.target.instance.runtime_properties.get('resource_id') # Get the existing Load Balancer Rules lb_rules = lb_data.get('load_balancing_rules', list()) lb_rules.append({ 'name': lb_rule_name, 'frontend_ip_configuration': {'id': lb_fe_ipc_id}, 'backend_address_pool': {'id': lb_be_pool_id}, 'probe': {'id': lb_probe_id} }) # Update the Load Balancer with the new rule lb_params = { 'load_balancing_rules': lb_rules } # clean empty values from params lb_params = \ utils.cleanup_empty_params(lb_params) try: result = load_balancer.create_or_update(resource_group_name, lb_name, lb_params) for item in result.get("load_balancing_rules"): if item.get("name") == lb_rule_name: ctx.instance.runtime_properties['resource_id'] = item.get("id") ctx.instance.runtime_properties['resource'] = item except CloudError as cr: raise cfy_exc.NonRecoverableError( "create load_balancing_rules '{0}' " "failed with this error : {1}".format(lb_rule_name, cr.message) )