def build_ip_configuration(ipc): ''' Attempts to construct a proper IP Configuration from a context object :params `cloudify.context.RelationshipSubjectContext` ipc: IP Configuration context object :returns: IP Configuration dict :rtype: dict or None ''' if not ipc or not ipc.instance.relationships: return None # Find a referenced Subnet subnet = utils.get_rel_id_reference( Subnet, constants.REL_IPC_CONNECTED_TO_SUBNET, _ctx=ipc) # Find a referenced Public IP pubip = utils.get_rel_id_reference( PublicIPAddress, constants.REL_IPC_CONNECTED_TO_PUBIP, _ctx=ipc) ip_configuration = { 'name': utils.get_resource_name(ipc), 'properties': { 'subnet': subnet, 'publicIPAddress': pubip } } ip_configuration['properties'] = utils.dict_update( ip_configuration['properties'], utils.get_resource_config(_ctx=ipc)) return ip_configuration
def build_ip_configuration(ipc): ''' Attempts to construct a proper IP Configuration from a context object :params `cloudify.context.RelationshipSubjectContext` ipc: IP Configuration context object :returns: IP Configuration dict :rtype: dict or None ''' if not ipc or not ipc.instance.relationships: return None # Find a referenced Subnet subnet = utils.get_rel_id_reference(Subnet, constants.REL_IPC_CONNECTED_TO_SUBNET, _ctx=ipc) # Find a referenced Public IP pubip = utils.get_rel_id_reference(PublicIPAddress, constants.REL_IPC_CONNECTED_TO_PUBIP, _ctx=ipc) # Build a partial config and update it with properties config return utils.dict_update( { 'name': utils.get_resource_name(ipc), 'properties': { 'subnet': subnet, 'publicIPAddress': pubip } }, utils.get_resource_config(_ctx=ipc))
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 create(**_): '''Uses an existing, or creates a new, Virtual Machine''' # Generate a resource name (if needed) utils.generate_resource_name( VirtualMachine(), generator=vm_name_generator) res_cfg = utils.get_resource_config() or dict() # Build storage profile osdisk = build_osdisk_profile( res_cfg.get('storageProfile', dict()).get('osDisk', dict())) datadisks = build_datadisks_profile( res_cfg.get('storageProfile', dict()).get('dataDisks', list())) storage_profile = { 'osDisk': osdisk, 'dataDisks': datadisks } # Build the network profile network_profile = build_network_profile() # Build the OS profile os_family = ctx.node.properties.get('os_family', '').lower() os_profile = dict() # Set defaults for Windows installs to enable WinRM listener if os_family == 'windows' and \ not res_cfg.get('osProfile', dict()).get('windowsConfiguration'): os_profile = { 'windowsConfiguration': { # This is required for extension scripts to work 'provisionVMAgent': True, 'winRM': { 'listeners': [{ 'protocol': 'Http', 'certificateUrl': None }] } }, 'linuxConfiguration': None } elif not res_cfg.get('osProfile', dict()).get('linuxConfiguration'): os_profile = { 'linuxConfiguration': { 'disablePasswordAuthentication': False }, 'windowsConfiguration': None } # Set the computerName if it's not set already os_profile['computerName'] = \ res_cfg.get( 'osProfile', dict() ).get('computerName', utils.get_resource_name()) # Create a resource (if necessary) utils.task_resource_create( VirtualMachine(), { 'location': ctx.node.properties.get('location'), 'tags': ctx.node.properties.get('tags'), 'plan': ctx.node.properties.get('plan'), 'properties': utils.dict_update( utils.get_resource_config(), { 'availabilitySet': utils.get_rel_id_reference( AvailabilitySet, constants.REL_CONNECTED_TO_AS), 'networkProfile': network_profile, 'storageProfile': storage_profile, 'osProfile': os_profile } ) })
def create(args=None, **_): '''Uses an existing, or creates a new, Virtual Machine''' # Generate a resource name (if needed) utils.generate_resource_name( VirtualMachine(api_version=ctx.node.properties.get( 'api_version', constants.API_VER_COMPUTE)), generator=vm_name_generator) res_cfg = utils.get_resource_config(args=args) or dict() # Build storage profile osdisk = build_osdisk_profile( res_cfg.get('storageProfile', dict()).get('osDisk', dict())) datadisks = build_datadisks_profile( res_cfg.get('storageProfile', dict()).get('dataDisks', list())) storage_profile = {'osDisk': osdisk, 'dataDisks': datadisks} # Build the network profile network_profile = build_network_profile() # Build the OS profile os_family = ctx.node.properties.get('os_family', '').lower() os_profile = dict() # Set defaults for Windows installs to enable WinRM listener if os_family == 'windows' and \ not res_cfg.get('osProfile', dict()).get('windowsConfiguration'): os_profile = { 'windowsConfiguration': { # This is required for extension scripts to work 'provisionVMAgent': True, 'winRM': { 'listeners': [{ 'protocol': 'Http', 'certificateUrl': None }] } }, 'linuxConfiguration': None } elif not res_cfg.get('osProfile', dict()).get('linuxConfiguration'): os_profile = { 'linuxConfiguration': { 'disablePasswordAuthentication': False }, 'windowsConfiguration': None } # Set the computerName if it's not set already os_profile['computerName'] = \ res_cfg.get( 'osProfile', dict() ).get('computerName', utils.get_resource_name()) resource_create_payload = \ { 'location': ctx.node.properties.get('location'), 'tags': ctx.node.properties.get('tags'), 'plan': ctx.node.properties.get('plan'), 'properties': utils.dict_update( utils.get_resource_config(args=args), { 'availabilitySet': utils.get_rel_id_reference( AvailabilitySet, constants.REL_CONNECTED_TO_AS), 'networkProfile': network_profile, 'storageProfile': storage_profile, 'osProfile': os_profile } ) } # support userdata from args. os_profile = resource_create_payload['properties']['osProfile'] userdata = _handle_userdata(os_profile.get('customData')) if userdata: ctx.logger.warn('Azure customData implementation is dependent on ' 'Virtual Machine image support.') os_profile['customData'] = base64.b64encode(userdata.encode()) # Remove customData from osProfile if empty to avoid 400 Error. elif 'customData' in resource_create_payload['properties']['osProfile']: del resource_create_payload['properties']['osProfile']['customData'] # Create a resource (if necessary) utils.task_resource_create( VirtualMachine(api_version=ctx.node.properties.get( 'api_version', constants.API_VER_COMPUTE)), resource_create_payload)
def create(args=None, **_): '''Uses an existing, or creates a new, Virtual Machine''' # Generate a resource name (if needed) utils.generate_resource_name( VirtualMachine(api_version=ctx.node.properties.get( 'api_version', constants.API_VER_COMPUTE)), generator=vm_name_generator) res_cfg = utils.get_resource_config(args=args) or dict() # Build storage profile osdisk = build_osdisk_profile( res_cfg.get('storageProfile', dict()).get('osDisk', dict())) datadisks = build_datadisks_profile( res_cfg.get('storageProfile', dict()).get('dataDisks', list())) storage_profile = { 'osDisk': osdisk, 'dataDisks': datadisks } # Build the network profile network_profile = build_network_profile() # Build the OS profile os_family = ctx.node.properties.get('os_family', '').lower() os_profile = dict() # Set defaults for Windows installs to enable WinRM listener if os_family == 'windows' and \ not res_cfg.get('osProfile', dict()).get('windowsConfiguration'): os_profile = { 'windowsConfiguration': { # This is required for extension scripts to work 'provisionVMAgent': True, 'winRM': { 'listeners': [{ 'protocol': 'Http', 'certificateUrl': None }] } }, 'linuxConfiguration': None } elif not res_cfg.get('osProfile', dict()).get('linuxConfiguration'): os_profile = { 'linuxConfiguration': { 'disablePasswordAuthentication': False }, 'windowsConfiguration': None } # Set the computerName if it's not set already os_profile['computerName'] = \ res_cfg.get( 'osProfile', dict() ).get('computerName', utils.get_resource_name()) resource_create_payload = \ { 'location': ctx.node.properties.get('location'), 'tags': ctx.node.properties.get('tags'), 'plan': ctx.node.properties.get('plan'), 'properties': utils.dict_update( utils.get_resource_config(args=args), { 'availabilitySet': utils.get_rel_id_reference( AvailabilitySet, constants.REL_CONNECTED_TO_AS), 'networkProfile': network_profile, 'storageProfile': storage_profile, 'osProfile': os_profile } ) } # support userdata from args. os_profile = resource_create_payload['properties']['osProfile'] userdata = _handle_userdata(os_profile.get('customData')) if userdata: ctx.logger.warn( 'Azure customData implementation is dependent on ' 'Virtual Machine image support.') os_profile['customData'] = base64.b64encode(userdata.encode()) # Remove customData from osProfile if empty to avoid 400 Error. elif 'customData' in resource_create_payload['properties']['osProfile']: del resource_create_payload['properties']['osProfile']['customData'] # Create a resource (if necessary) utils.task_resource_create(VirtualMachine( api_version=ctx.node.properties.get( 'api_version', constants.API_VER_COMPUTE)), resource_create_payload)