def remove_instance_tag(instance_name, tag, **kwargs):
    gcp_config = utils.get_gcp_config()
    if not instance_name:
        return
    gcp_config['network'] = utils.get_gcp_resource_name(gcp_config['network'])
    instance = Instance(gcp_config,
                        ctx.logger,
                        name=instance_name)
    instance.remove_tags([utils.get_gcp_resource_name(t) for t in tag])
def add_instance_tag(instance_name, tag, **kwargs):
    if not tag:
        return
    gcp_config = utils.get_gcp_config()
    gcp_config['network'] = utils.get_gcp_resource_name(gcp_config['network'])
    instance = Instance(gcp_config,
                        ctx.logger,
                        name=instance_name)
    instance.set_tags([utils.get_gcp_resource_name(t) for t in tag])
def remove_external_ip(instance_name, **kwargs):
    if not instance_name:
        return
    gcp_config = utils.get_gcp_config()
    gcp_config['network'] = utils.get_gcp_resource_name(gcp_config['network'])
    instance = Instance(gcp_config,
                        ctx.logger,
                        name=instance_name)
    instance.delete_access_config()
    def test_get_resource_name(self):
        resource_name = 'test_resource_name1'
        resource_name_correct = 'test-resource-name1'
        result = utils.get_gcp_resource_name(resource_name)
        self.assertEqual(result, resource_name_correct)

        resource_name_too_long = 'a' * 70
        resource_name_too_long_correct = 'a' * 63
        result = utils.get_gcp_resource_name(resource_name_too_long)
        self.assertEqual(result, resource_name_too_long_correct)

        resource_name_nonalpha = 'test_345%$*^&()+_sd^*()'
        resource_name_nonalpha_correct = 'test-345-sd'
        result = utils.get_gcp_resource_name(resource_name_nonalpha)
        self.assertEqual(result, resource_name_nonalpha_correct)

        resource_name_too_long2 = 'a' * 70 + '_123ab'
        resource_name_too_long2_correct = 'a' * 57 + '-123ab'
        result = utils.get_gcp_resource_name(resource_name_too_long2)
        self.assertEqual(result, resource_name_too_long2_correct)
Beispiel #5
0
def create(firewall_rule, **kwargs):
    gcp_config = utils.get_gcp_config()
    network_name = utils.get_gcp_resource_name(gcp_config['network'])
    set_firewall_rule_name(firewall_rule, network_name)
    firewall = FirewallRule(gcp_config,
                            ctx.logger,
                            firewall=firewall_rule,
                            network=network_name)

    utils.create(firewall)
    ctx.instance.runtime_properties[constants.NAME] = firewall.name
    def test_get_resource_name(self):
        resource_name = 'test_resource_name1'
        resource_name_correct = 'test-resource-name1'
        result = utils.get_gcp_resource_name(resource_name)
        self.assertEqual(result, resource_name_correct)

        resource_name_too_long = 'a' * 70
        resource_name_too_long_correct = 'a' * 63
        result = utils.get_gcp_resource_name(resource_name_too_long)
        self.assertEqual(result, resource_name_too_long_correct)

        resource_name_nonalpha = 'test_345%$*^&()+_sd^*()'
        resource_name_nonalpha_correct = 'test-345-sd'
        result = utils.get_gcp_resource_name(resource_name_nonalpha)
        self.assertEqual(result, resource_name_nonalpha_correct)

        resource_name_too_long2 = 'a' * 70 + '_123ab'
        resource_name_too_long2_correct = 'a' * 57 + '-123ab'
        result = utils.get_gcp_resource_name(resource_name_too_long2)
        self.assertEqual(result, resource_name_too_long2_correct)
def create(firewall_rule, **kwargs):
    gcp_config = utils.get_gcp_config()
    network_name = utils.get_gcp_resource_name(gcp_config['network'])
    set_firewall_rule_name(firewall_rule, network_name)
    firewall = FirewallRule(gcp_config,
                            ctx.logger,
                            firewall=firewall_rule,
                            network=network_name)

    utils.create(firewall)
    ctx.instance.runtime_properties[constants.NAME] = firewall.name
Beispiel #8
0
def delete(**kwargs):
    gcp_config = utils.get_gcp_config()
    firewall_name = ctx.instance.runtime_properties.get(constants.NAME, None)
    if not firewall_name:
        return
    network_name = utils.get_gcp_resource_name(gcp_config['network'])
    firewall = FirewallRule(gcp_config,
                            ctx.logger,
                            firewall={'name': firewall_name},
                            network=network_name)
    utils.delete_if_not_external(firewall)
    ctx.instance.runtime_properties.pop(constants.NAME, None)
def delete(**kwargs):
    gcp_config = utils.get_gcp_config()
    firewall_name = ctx.instance.runtime_properties.get(constants.NAME, None)
    if not firewall_name:
        return
    network_name = utils.get_gcp_resource_name(gcp_config['network'])
    firewall = FirewallRule(gcp_config,
                            ctx.logger,
                            firewall={'name': firewall_name},
                            network=network_name)
    utils.delete_if_not_external(firewall)
    ctx.instance.runtime_properties.pop(constants.NAME, None)
Beispiel #10
0
    def __init__(self, config, logger, network):
        """
        Create Network object

        :param config: gcp auth file
        :param logger: logger object
        :param network: network dictionary having at least 'name' key

        """
        super(Network,
              self).__init__(config, logger,
                             utils.get_gcp_resource_name(network['name']))
        self.network = network
        self.iprange = None
def add_external_ip(instance_name, **kwargs):
    gcp_config = utils.get_gcp_config()
    # check if the instance has no external ips, only one is supported so far
    gcp_config['network'] = utils.get_gcp_resource_name(gcp_config['network'])
    ip_node = ctx.target.node
    instance = Instance(gcp_config,
                        ctx.logger,
                        name=instance_name)
    if ip_node.properties[constants.USE_EXTERNAL_RESOURCE]:
        ip_address = ip_node.properties['ip_address']
        if not ip_address:
            raise GCPError('{} is set, but ip_address is not set'
                           .format(constants.USE_EXTERNAL_RESOURCE))
        instance.add_access_config(ip_address)
    else:
        instance.add_access_config()
    set_ip(instance, relationship=True)
    def __init__(self,
                 config,
                 logger,
                 network):
        """
        Create Network object

        :param config: gcp auth file
        :param logger: logger object
        :param network: network dictionary having at least 'name' key

        """
        super(Network, self).__init__(
            config,
            logger,
            utils.get_gcp_resource_name(network['name']))
        self.network = network
        self.iprange = None
def create(instance_type,
           image_id,
           name,
           zone,
           external_ip,
           startup_script,
           scopes,
           user_data,
           **kwargs):
    if zone:
        ctx.instance.runtime_properties[constants.GCP_ZONE] = zone
    gcp_config = utils.get_gcp_config()
    gcp_config['network'] = utils.get_gcp_resource_name(gcp_config['network'])
    script = ''
    if not startup_script:
        startup_script = ctx.instance.runtime_properties.get('startup_script')
    #TODO: make it pythonistic

    ctx.logger.info('The script is {0}'.format(str(startup_script)))
    if startup_script and startup_script.get('type') == 'file':
        script = ctx.get_resource(startup_script.get('script'))
    elif startup_script and startup_script.get('type') == 'string':
        script = startup_script.get('script')

    instance_name = utils.get_final_resource_name(name)
    instance = Instance(gcp_config,
                        ctx.logger,
                        name=instance_name,
                        image=image_id,
                        machine_type=instance_type,
                        external_ip=external_ip,
                        startup_script=script,
                        scopes=scopes,
                        user_data=user_data)
    ctx.instance.runtime_properties[constants.NAME] = instance.name
    if ctx.node.properties['install_agent']:
        add_to_security_groups(instance)
    disk = ctx.instance.runtime_properties.get(constants.DISK)
    if disk:
        instance.disks = [disk]
    utils.create(instance)
    set_ip(instance)
    def __init__(self,
                 config,
                 logger,
                 name,
                 image=None,
                 machine_type=None,
                 startup_script=None,
                 external_ip=False,
                 tags=None,
                 scopes=None,
                 user_data=None):
        """
        Create Instance object

        :param config: gcp auth file
        :param logger: logger object
        :param name: name of the instance
        :param image: image id in Google Cloud Platform
        :param machine_type: machine type on GCP, default None
        :param startup_script: shell script text to be run on instance startup,
        default None
        :param external_ip: boolean external ip indicator, default False
        :param tags: tags for the instance, default []
        """
        super(Instance, self).__init__(
            config,
            logger,
            utils.get_gcp_resource_name(name))
        self.image = image
        self.machine_type = machine_type
        self.network = config['network']
        self.startup_script = startup_script
        self.tags = tags.append(self.name) if tags else [self.name]
        self.externalIP = external_ip
        self.disks = []
        self.scopes = scopes or self.DEFAULT_SCOPES
        self.user_data = user_data