Exemplo n.º 1
0
def create(key):
  """Creates an instance template from the given InstanceTemplateRevision.

  Args:
    key: ndb.Key for a models.InstanceTemplateRevision entity.

  Raises:
    net.Error: HTTP status code is not 200 (created) or 409 (already created).
  """
  entity = key.get()
  if not entity:
    logging.warning('InstanceTemplateRevision does not exist: %s', key)
    return

  if not entity.project:
    logging.warning('InstanceTemplateRevision project unspecified: %s', key)
    return

  if entity.url:
    logging.info(
        'Instance template for InstanceTemplateRevision already exists: %s',
        key,
    )
    return

  if entity.metadata:
    metadata = [{'key': key, 'value': value}
                for key, value in entity.metadata.iteritems()]
  else:
    metadata = []

  service_accounts = [{
      'email': service_account.name, 'scopes': service_account.scopes
  }
  for service_account in entity.service_accounts]

  api = gce.Project(entity.project)
  try:
    result = api.create_instance_template(
        get_name(entity),
        entity.disk_size_gb,
        gce.get_image_url(api.project_id, entity.image_name),
        entity.machine_type,
        gce.get_network_url(api.project_id, 'default'),
        tags=entity.tags,
        metadata=metadata,
        service_accounts=service_accounts,
    )
  except net.Error as e:
    if e.status_code == 409:
      # If the instance template already exists, just record the URL.
      result = api.get_instance_template(get_name(entity))
      update_url(entity.key, result['selfLink'])
      return
    else:
      raise

  update_url(entity.key, result['targetLink'])
Exemplo n.º 2
0
 def test_get_network_interfaces(self):
   expected_interfaces_ext  = [{'network': 'global/networks/default',
                                'accessConfigs': [{'type': 'ONE_TO_ONE_NAT'}]}]
   expected_interfaces_int  = [{'network': 'global/networks/default'}]
   self.assertEqual(expected_interfaces_int,
                    gce.get_network_interfaces('project',
                                               'global/networks/default',
                                               False))
   self.assertEqual(expected_interfaces_ext,
                    gce.get_network_interfaces('project',
                                               'global/networks/default',
                                               True))
   expected_interfaces_int  = [{'network':
                                gce.get_network_url('project', 'default')}]
   self.assertEqual(expected_interfaces_int,
                    gce.get_network_interfaces('project', '', False))
Exemplo n.º 3
0
def create(key):
    """Creates an instance template from the given InstanceTemplateRevision.

  Args:
    key: ndb.Key for a models.InstanceTemplateRevision entity.

  Raises:
    net.Error: HTTP status code is not 200 (created) or 409 (already created).
  """
    entity = key.get()
    if not entity:
        logging.warning('InstanceTemplateRevision does not exist: %s', key)
        return

    if not entity.project:
        logging.warning('InstanceTemplateRevision project unspecified: %s',
                        key)
        return

    if entity.url:
        logging.info(
            'Instance template for InstanceTemplateRevision already exists: %s',
            key,
        )
        return

    if entity.metadata:
        metadata = [{
            'key': key,
            'value': value
        } for key, value in entity.metadata.iteritems()]
    else:
        metadata = []

    service_accounts = [{
        'email': service_account.name,
        'scopes': service_account.scopes
    } for service_account in entity.service_accounts]

    api = gce.Project(entity.project)
    try:
        result = api.create_instance_template(
            get_name(entity),
            entity.disk_size_gb,
            gce.get_image_url(api.project_id, entity.image_name),
            entity.machine_type,
            gce.get_network_url(api.project_id, 'default'),
            tags=entity.tags,
            metadata=metadata,
            service_accounts=service_accounts,
        )
    except net.Error as e:
        if e.status_code == 409:
            # If the instance template already exists, just record the URL.
            result = api.get_instance_template(get_name(entity))
            update_url(entity.key, result['selfLink'])
            return
        else:
            raise

    update_url(entity.key, result['targetLink'])