示例#1
0
    def _BuildRequestWithMetadata(self, instance_name, instance_metadata,
                                  disks):
        """Build a request to add the specified instance, given the ssh keys for it.

    Args:
      instance_name: Name of the instance to build a request for.
      instance_metadata: The metadata to be passed to the VM.  This is in the
        form of [{'key': <key>, 'value': <value>}] form, ready to be
        sent to the server.
      disks: Disks to attach to the instance.

    Returns:
      The prepared instance request.
    """
        instance_resource = {
            'kind': self._GetResourceApiKind('instance'),
            'name': self._DenormalizeResourceName(instance_name),
            'description': self._flags.description,
            'networkInterfaces': [],
            'disks': disks,
            'metadata': [],
        }

        if self._flags.image:
            instance_resource['image'] = self.NormalizeResourceName(
                self._project, 'images', self._flags.image)

        if self._flags.machine_type:
            instance_resource['machineType'] = self.NormalizeResourceName(
                self._project, 'machine-types', self._flags.machine_type)

        instance_resource['zone'] = self.NormalizeResourceName(
            self._project, 'zones', self._flags.zone)

        if self._flags.network:
            network_interface = {
                'network':
                self.NormalizeResourceName(self._project, 'networks',
                                           self._flags.network)
            }
            if self._flags.internal_ip_address:
                network_interface[
                    'networkIP'] = self._flags.internal_ip_address
            external_ip_address = self._flags.external_ip_address
            if external_ip_address and external_ip_address.lower() != 'none':
                access_config = {
                    'name': self.DEFAULT_ACCESS_CONFIG_NAME,
                    'type': self.ONE_TO_ONE_NAT_ACCESS_CONFIG_TYPE,
                }
                if external_ip_address.lower(
                ) != self.EPHEMERAL_ACCESS_CONFIG_NAT_IP:
                    access_config['natIP'] = self._flags.external_ip_address

                network_interface['accessConfigs'] = [access_config]

            instance_resource['networkInterfaces'].append(network_interface)

        metadata_subresource = {
            'kind': self._GetResourceApiKind('metadata'),
            'items': []
        }

        metadata_subresource['items'].extend(instance_metadata)
        instance_resource['metadata'] = metadata_subresource

        if self._flags.service_account and (len(
                self._flags.service_account_scopes)):
            instance_resource['serviceAccounts'] = []
            expanded_scopes = scopes.ExpandScopeAliases(
                self._flags.service_account_scopes)
            instance_resource['serviceAccounts'].append({
                'email':
                self._flags.service_account,
                'scopes':
                expanded_scopes
            })

        instance_resource['tags'] = sorted(set(self._flags.tags))

        return self._instances_api.insert(project=self._project,
                                          body=instance_resource)
示例#2
0
 def testExpandMixed(self):
     scopes_in = ['compute-rw', scopes.TASKQUEUE]
     scopes_out = scopes.ExpandScopeAliases(scopes_in)
     self.assertEqual(scopes_out,
                      [scopes.COMPUTE_RW_SCOPE, scopes.TASKQUEUE])
示例#3
0
 def testExpandEmpty(self):
     scopes_in = []
     scopes_out = scopes.ExpandScopeAliases(scopes_in)
     self.assertEqual(scopes_out, scopes_in)
示例#4
0
 def testExpandSingle(self):
     scopes_in = ['compute-rw']
     scopes_out = scopes.ExpandScopeAliases(scopes_in)
     self.assertEqual(scopes_out, [scopes.COMPUTE_RW_SCOPE])
示例#5
0
 def testExpandPassthrough(self):
     scopes_in = ['foo', 'bar', scopes.TASKQUEUE]
     scopes_out = scopes.ExpandScopeAliases(scopes_in)
     self.assertEqual(scopes_out, scopes_in)